diff --git a/src/Relay.h b/src/Relay.h index 520af0e..fe8debd 100644 --- a/src/Relay.h +++ b/src/Relay.h @@ -38,20 +38,20 @@ public: void setup() override { Output::setup(); - Output::setName(configRead(path("name"), nameFallback)); - Output::setInitial(configRead(path("initial"), INITIAL_OFF)); - Output::setOnMillis(configRead(path("onMillis"), 0L)); - Output::setOffMillis(configRead(path("offMillis"), 0L)); + Output::setName(loadString(path("name"), nameFallback)); + Output::setInitial(loadInitial(path("initial"), INITIAL_OFF)); + Output::setOnMillis(loadLong(path("onMillis"), 0L)); + Output::setOffMillis(loadLong(path("offMillis"), 0L)); - topic = configRead(path("topic"), topicFallback); + topic = loadString(path("topic"), topicFallback); - gridPowerDeltaOnEnabled = configRead(path("gridPowerDeltaOnEnabled"), false); - gridPowerDeltaOnThreshold = configRead(path("gridPowerDeltaOnThreshold"), 0L); - gridPowerDeltaOnDelay = configRead(path("gridPowerDeltaOnDelay"), 0L); + gridPowerDeltaOnEnabled = loadBool(path("gridPowerDeltaOnEnabled"), false); + gridPowerDeltaOnThreshold = loadLong(path("gridPowerDeltaOnThreshold"), 0L); + gridPowerDeltaOnDelay = loadLong(path("gridPowerDeltaOnDelay"), 0L); - gridPowerDeltaOffEnabled = configRead(path("gridPowerDeltaOffEnabled"), false); - gridPowerDeltaOffThreshold = configRead(path("gridPowerDeltaOffThreshold"), 0L); - gridPowerDeltaOffDelay = configRead(path("gridPowerDeltaOffDelay"), 0L); + gridPowerDeltaOffEnabled = loadBool(path("gridPowerDeltaOffEnabled"), false); + gridPowerDeltaOffThreshold = loadLong(path("gridPowerDeltaOffThreshold"), 0L); + gridPowerDeltaOffDelay = loadLong(path("gridPowerDeltaOffDelay"), 0L); _applyInitial(); } @@ -63,57 +63,57 @@ public: void setName(const String &value) override { Output::setName(value); - configWrite(path("name"), nameFallback, value); + storeString(path("name"), nameFallback, value); } void setInitial(const Initial value) override { Output::setInitial(value); - configWrite(path("initial"), INITIAL_OFF, value); + storeInitial(path("initial"), INITIAL_OFF, value); } void setOnMillis(const unsigned long value) override { Output::setOnMillis(value); - configWrite(path("onMillis"), 0L, value); + storeLong(path("onMillis"), 0L, value); } void setOffMillis(const unsigned long value) override { Output::setOffMillis(value); - configWrite(path("offMillis"), 0L, value); + storeLong(path("offMillis"), 0L, value); } void setTopic(const String &value) { topic = value; - configWrite(path("topic"), topicFallback, value); + storeString(path("topic"), topicFallback, value); } void setGridPowerDeltaOnEnabled(const bool value) { gridPowerDeltaOnEnabled = value; - configWrite(path("gridPowerDeltaOnEnabled"), false, value); + storeBool(path("gridPowerDeltaOnEnabled"), false, value); } void setGridPowerDeltaOnThreshold(const long value) { gridPowerDeltaOnThreshold = value; - configWrite(path("gridPowerDeltaOnThreshold"), 0L, value); + storeLong(path("gridPowerDeltaOnThreshold"), 0L, value); } void setGridPowerDeltaOnDelay(const long value) { gridPowerDeltaOnDelay = value; - configWrite(path("gridPowerDeltaOnDelay"), 0L, value); + storeLong(path("gridPowerDeltaOnDelay"), 0L, value); } void setGridPowerDeltaOffEnabled(const bool value) { gridPowerDeltaOffEnabled = value; - configWrite(path("gridPowerDeltaOffEnabled"), false, value); + storeBool(path("gridPowerDeltaOffEnabled"), false, value); } void setGridPowerDeltaOffThreshold(const long value) { gridPowerDeltaOffThreshold = value; - configWrite(path("gridPowerDeltaOffThreshold"), 0L, value); + storeLong(path("gridPowerDeltaOffThreshold"), 0L, value); } void setGridPowerDeltaOffDelay(const long value) { gridPowerDeltaOffDelay = value; - configWrite(path("gridPowerDeltaOffDelay"), 0L, value); + storeLong(path("gridPowerDeltaOffDelay"), 0L, value); } void json(const JsonObject json) const { diff --git a/src/config.cpp b/src/config.cpp index b8ba4d5..84c62da 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -82,7 +82,7 @@ void doLog(const String &path, const String &value, const bool isPassword, const ); } -long configRead(const String &path, const long fallback, const bool log) { +long loadLong(const String &path, const long fallback, const bool log) { if (auto file = configOpen(path, false)) { const auto content = file.readString(); file.close(); @@ -94,8 +94,8 @@ long configRead(const String &path, const long fallback, const bool log) { return fallback; } -bool configWrite(const String &path, const long fallback, const long value) { - if (configRead(path, fallback, false) == value) { +bool storeLong(const String &path, const long fallback, const long value) { + if (loadLong(path, fallback, false) == value) { doLog(path, String(value), false, CONFIG_LOG_UNCHANGED, true); return false; } @@ -109,47 +109,67 @@ bool configWrite(const String &path, const long fallback, const long value) { return false; } -bool configRead(const String &path, const bool fallback) { - return configRead(path, fallback ? 1L : 0L) > 0; -} - -bool configWrite(const String &path, const bool fallback, const bool value) { - return configWrite(path, fallback ? 1L : 0L, value ? 1L : 0L); -} - -String configRead(const String &path, const char *fallback) { - return configRead(path, String(fallback)); -} - -String configRead(const String &path, const String &fallback, const bool log, const bool isPassword) { - if (auto file = configOpen(path, false)) { - const auto value = file.readString(); - file.close(); - doLog(path, value.c_str(), isPassword, CONFIG_LOG_READ, log); - return value; +bool loadBool(const String &path, const bool fallback, const bool log) { + const auto value = loadString(path, fallback ? "true" : "false", log); + if (value == "true") { + return true; } - doLog(path, fallback.c_str(), isPassword, CONFIG_LOG_FALLBACK, log); + if (value == "false") { + return false; + } + Serial.printf("[CONFIG] Not a boolean: path=%s, value=%s\n", path.c_str(), value.c_str()); return fallback; } -bool configWrite(const String &path, const String &fallback, const String &value, const bool isPassword) { - if (configRead(path, fallback, false) == value) { - doLog(path, value.c_str(), isPassword, CONFIG_LOG_UNCHANGED, true); +bool storeBool(const String &path, const bool fallback, const bool value) { + return storeString(path, fallback ? "true" : "false", value ? "true" : "false"); +} + +String _loadString(const String &path, const String &fallback, const bool password, const bool log) { + if (auto file = configOpen(path, false)) { + const auto value = file.readString(); + file.close(); + doLog(path, value.c_str(), password, CONFIG_LOG_READ, log); + return value; + } + doLog(path, fallback.c_str(), password, CONFIG_LOG_FALLBACK, log); + return fallback; +} + +bool _storeString(const String &path, const String &fallback, const bool password, const String &value) { + if (_loadString(path, fallback, password, false) == value) { + doLog(path, value.c_str(), password, CONFIG_LOG_UNCHANGED, true); return false; } if (auto file = configOpen(path, true)) { file.write(reinterpret_cast(value.c_str()), value.length()); file.close(); - doLog(path, value.c_str(), isPassword, CONFIG_LOG_WRITE, true); + doLog(path, value.c_str(), password, CONFIG_LOG_WRITE, true); return true; } return false; } -Initial configRead(const String &path, const Initial &fallback) { - return stringToInitial(configRead(path, initialToString(fallback))); +String loadString(const String &path, const String &fallback, const bool log) { + return _loadString(path, fallback, false, log); } -bool configWrite(const String &path, const Initial &fallback, const Initial &value) { - return configWrite(path, initialToString(fallback), initialToString(value)); +bool storeString(const String &path, const String &fallback, const String &value) { + return _storeString(path, fallback, false, value); +} + +String loadPassword(const String &path, const String &fallback, const bool log) { + return _loadString(path, fallback, true, log); +} + +bool storePassword(const String &path, const String &fallback, const String &value) { + return _storeString(path, fallback, true, value); +} + +Initial loadInitial(const String &path, const Initial &fallback, const bool log) { + return stringToInitial(loadString(path, initialToString(fallback), log)); +} + +bool storeInitial(const String &path, const Initial &fallback, const Initial &value) { + return storeString(path, initialToString(fallback), initialToString(value)); } diff --git a/src/config.h b/src/config.h index 8745243..a64924c 100644 --- a/src/config.h +++ b/src/config.h @@ -7,22 +7,24 @@ void configSetup(); -long configRead(const String &path, long fallback, bool log = true); +long loadLong(const String &path, long fallback, bool log = true); -bool configWrite(const String &path, long fallback, long value); +bool storeLong(const String &path, long fallback, long value); -bool configRead(const String &path, bool fallback); +bool loadBool(const String &path, bool fallback, bool log = true); -bool configWrite(const String &path, bool fallback, bool value); +bool storeBool(const String &path, bool fallback, bool value); -String configRead(const String &path, const char *fallback); +String loadString(const String &path, const String &fallback, bool log = true); -String configRead(const String &path, const String &fallback, bool log = true, bool isPassword = false); +bool storeString(const String &path, const String &fallback, const String &value); -bool configWrite(const String &path, const String &fallback, const String &value, bool isPassword = false); +String loadPassword(const String &path, const String &fallback, bool log = true); -Initial configRead(const String &path, const Initial &fallback); +bool storePassword(const String &path, const String &fallback, const String &value); -bool configWrite(const String &path, const Initial &fallback, const Initial &value); +Initial loadInitial(const String &path, const Initial &fallback, bool log = true); + +bool storeInitial(const String &path, const Initial &fallback, const Initial &value); #endif diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 30e862f..4a5b37c 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -75,10 +75,10 @@ void mqttLoop() { Serial.println("[MQTT] Stopped."); } } else if (mqttShouldConnect) { - mqttHost = configRead(MQTT_HOST_KEY, MQTT_HOST_FALLBACK, false); - mqttPort = configRead(MQTT_PORT_KEY, MQTT_PORT_FALLBACK, true); - mqttUser = configRead(MQTT_USER_KEY, MQTT_USER_FALLBACK); - const auto mqttPass = configRead(MQTT_PASSWORD_KEY, MQTT_PASSWORD_FALLBACK, true, true); + mqttHost = loadString(MQTT_HOST_KEY, MQTT_HOST_FALLBACK); + mqttPort = loadLong(MQTT_PORT_KEY, MQTT_PORT_FALLBACK); + mqttUser = loadString(MQTT_USER_KEY, MQTT_USER_FALLBACK); + const auto mqttPass = loadPassword(MQTT_PASSWORD_KEY, MQTT_PASSWORD_FALLBACK); if (mqttHost == "") { return; } @@ -125,19 +125,19 @@ long getMqttPort() { void mqttSetHost(const String &value) { mqttHost = value; - configWrite(MQTT_HOST_KEY, String(MQTT_HOST_FALLBACK), value); + storeString(MQTT_HOST_KEY, String(MQTT_HOST_FALLBACK), value); } void mqttSetPort(const long &value) { mqttPort = value; - configWrite(MQTT_PORT_KEY,MQTT_HOST_FALLBACK, value); + storeLong(MQTT_PORT_KEY,MQTT_PORT_FALLBACK, value); } void mqttSetUser(const String &value) { mqttUser = value; - configWrite(MQTT_USER_KEY, String(MQTT_HOST_FALLBACK), value); + storeString(MQTT_USER_KEY, String(MQTT_USER_FALLBACK), value); } void mqttSetPassword(const String &value) { - configWrite(MQTT_PASSWORD_KEY,MQTT_HOST_FALLBACK, value, true); + storePassword(MQTT_PASSWORD_KEY,MQTT_PASSWORD_FALLBACK, value); } diff --git a/src/wifi.cpp b/src/wifi.cpp index 4471357..f91b38f 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -26,9 +26,9 @@ void wifiConnect() { status.cycle(500, 500); - const auto hostname = configRead(WIFI_HOSTNAME_KEY, WIFI_HOSTNAME_FALLBACK); - const auto wifiSSID = configRead(WIFI_SSID_KEY, WIFI_SSID_FALLBACK); - const auto wifiPass = configRead(WIFI_PASSWORD_KEY, WIFI_PASSWORD_FALLBACK, true, true); + const auto hostname = loadString(WIFI_HOSTNAME_KEY, WIFI_HOSTNAME_FALLBACK); + const auto wifiSSID = loadString(WIFI_SSID_KEY, WIFI_SSID_FALLBACK); + const auto wifiPass = loadPassword(WIFI_PASSWORD_KEY, WIFI_PASSWORD_FALLBACK); Serial.printf("[WiFi] Connecting: \"%s\"\n", wifiSSID.c_str()); WiFi.hostname(hostname); @@ -90,15 +90,15 @@ void wifiSetup() { } void wifiSetHostname(const String &hostname) { - if (configWrite(WIFI_HOSTNAME_KEY, WIFI_HOSTNAME_FALLBACK, hostname.c_str())) { + if (storeString(WIFI_HOSTNAME_KEY, WIFI_HOSTNAME_FALLBACK, hostname)) { WiFi.setHostname(hostname.c_str()); } } void wifiSetSSID(const String &ssid) { - configWrite(WIFI_SSID_KEY, WIFI_SSID_FALLBACK, ssid.c_str()); + storeString(WIFI_SSID_KEY, WIFI_SSID_FALLBACK, ssid); } void wifiSetPassword(const String &password) { - configWrite(WIFI_PASSWORD_KEY, WIFI_PASSWORD_FALLBACK, password, true); + storePassword(WIFI_PASSWORD_KEY, WIFI_PASSWORD_FALLBACK, password); }