Add config class
This commit is contained in:
69
include/Config.hpp
Normal file
69
include/Config.hpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
#include "logger.cpp"
|
||||||
|
|
||||||
|
class Config
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
static bool Read()
|
||||||
|
{
|
||||||
|
// Lee la configuración
|
||||||
|
StaticJsonDocument<JSON_CONFIG> jsonConfig;
|
||||||
|
File file = SPIFFS.open("/Config.json", "r");
|
||||||
|
if (deserializeJson(jsonConfig, file))
|
||||||
|
{
|
||||||
|
// Si falla la lectura inicia valores por defecto
|
||||||
|
Logger::Log(Logger::ERROR, "Fallo al leer settings. Restaurando valores de fábrica");
|
||||||
|
Reset();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* ------------------- CLIENTE -------------------- */
|
||||||
|
strlcpy(id, jsonConfig["id"] | "", sizeof(id));
|
||||||
|
strlcpy(ssid, jsonConfig["ssid"] | "", sizeof(ssid));
|
||||||
|
strlcpy(pw, jsonConfig["pw"] | "", sizeof(pw));
|
||||||
|
/* ------------------- AP -------------------- */
|
||||||
|
strlcpy(nameap, jsonConfig["nameap"] | "", sizeof(nameap));
|
||||||
|
strlcpy(passwordap, jsonConfig["passwordap"] | "", sizeof(passwordap));
|
||||||
|
file.close();
|
||||||
|
Logger::Log("Se han leído los settings");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static bool Save()
|
||||||
|
{
|
||||||
|
// Graba configuración
|
||||||
|
StaticJsonDocument<JSON_CONFIG> jsonConfig;
|
||||||
|
File file = SPIFFS.open("/Config.json", "w+");
|
||||||
|
if (file)
|
||||||
|
{
|
||||||
|
/* ------------------- CLIENTE -------------------- */
|
||||||
|
jsonConfig["id"] = id;
|
||||||
|
jsonConfig["ssid"] = ssid;
|
||||||
|
jsonConfig["pw"] = pw;
|
||||||
|
/* ------------------- AP -------------------- */
|
||||||
|
jsonConfig["nameap"] = nameap;
|
||||||
|
jsonConfig["passwordap"] = passwordap;
|
||||||
|
serializeJsonPretty(jsonConfig, file);
|
||||||
|
file.close();
|
||||||
|
Logger::Log("Settings restaurados");
|
||||||
|
// serializeJsonPretty(jsonConfig, Serial);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Logger::Log(Logger::ERROR, "Fallo al guardar");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static void Reset()
|
||||||
|
{
|
||||||
|
/* ------------------- CLIENTE -------------------- */
|
||||||
|
strlcpy(id, "LASER-CONTROLLER", sizeof(id));
|
||||||
|
strlcpy(ssid, "SSID", sizeof(ssid));
|
||||||
|
strlcpy(pw, "P4SSW0RD", sizeof(pw));
|
||||||
|
/* ------------------- AP -------------------- */
|
||||||
|
strlcpy(nameap, "EUSS Chrono Setup", sizeof(nameap));
|
||||||
|
strlcpy(passwordap, "EUSS2022", sizeof(passwordap));
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user