diff --git a/include/Config.hpp b/include/Config.hpp new file mode 100644 index 0000000..44563fe --- /dev/null +++ b/include/Config.hpp @@ -0,0 +1,69 @@ +#include "logger.cpp" + +class Config +{ + +public: + static bool Read() + { + // Lee la configuración + StaticJsonDocument 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 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)); + } +}; \ No newline at end of file