diff --git a/src/Relay.h b/src/Relay.h index ca357f2..e65a458 100644 --- a/src/Relay.h +++ b/src/Relay.h @@ -18,31 +18,39 @@ public: void setup() override { Output::setup(); - Output::setName(configRead(String("relayName") + index, nameFallback)); - Output::setInitial(configRead(String("relayInitial") + index, INITIAL_OFF)); - Output::setOnMillis(configRead(String("relayOnMillis") + index, 0L)); - Output::setOffMillis(configRead(String("relayOffMillis") + index, 0L)); + 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)); _applyInitial(); } void setName(const String &value) override { Output::setName(value); - configWrite(String("relayName") + index, nameFallback, value); + configWrite(path("name"), nameFallback, value, false); } void setInitial(const Initial value) override { Output::setInitial(value); - configWrite(String("relayInitial") + index, INITIAL_OFF, value); + configWrite(path("initial"), INITIAL_OFF, value); } void setOnMillis(const unsigned long value) override { Output::setOnMillis(value); - configWrite(String("relayOnMillis") + index, 0L, value); + configWrite(path("onMillis"), 0L, value); } void setOffMillis(const unsigned long value) override { Output::setOffMillis(value); - configWrite(String("relayOffMillis") + index, 0L, value); + configWrite(path("offMillis"), 0L, value); + } + +private: + + String path(const char *name) const { + char path[64]; + snprintf(path, sizeof(path), "/relay%d/%s", index, name); + return String(path); } }; diff --git a/src/config.cpp b/src/config.cpp index 9ca318c..7e392c7 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,83 +1,141 @@ #include "config.h" #include -#include + +enum CONFIG_LOG { + CONFIG_LOG_FALLBACK, + CONFIG_LOG_READ, + CONFIG_LOG_UNCHANGED, + CONFIG_LOG_WRITE, +}; + +void listDir(const String &path, const String &indent) { + auto dir = LittleFS.open(path, "r"); + if (!dir || !dir.isDirectory()) { + return; + } + while (true) { + File child = dir.openNextFile(); + if (!child) { + break; + } + if (child.isDirectory()) { + Serial.printf("%s[D] [ ] %s\n", indent.c_str(), child.name()); + listDir(child.path(), indent + " "); + } + child.close(); + } + dir.rewindDirectory(); + while (true) { + File child = dir.openNextFile(); + if (!child) { + break; + } + if (!child.isDirectory()) { + Serial.printf("%s[F] [%4d] %s\n", indent.c_str(), child.size(), child.name()); + } + child.close(); + } + dir.close(); +} void configSetup() { - LittleFS.begin(); + LittleFS.begin(true); + Serial.println("Filesystem-content:"); + listDir("/", ""); } -File configOpen(const String &name, const char *mode) { - char path[64]; - snprintf(path, sizeof(path), "/%s", name.c_str()); - return LittleFS.open(path, mode); +File configOpen(const String &path, const bool write) { + if (!write && !LittleFS.exists(path)) { + return {}; + } + return LittleFS.open(path, write ? "w" : "r", write); } -long configRead(const String &name, const long fallback) { - if (auto file = configOpen(name, "r")) { +void doLog(const String &path, const String &value, const bool isPassword, const CONFIG_LOG type, const bool enable) { + if (!enable) { + return; + } + Serial.printf( + "[CONFIG] %-20s = %-30s [%s]\n", + path.c_str(), + isPassword ? "*" : value.c_str(), + type == CONFIG_LOG_FALLBACK ? "FALLBACK" : type == CONFIG_LOG_READ ? "READ" : type == CONFIG_LOG_UNCHANGED ? "UNCHANGED" : type == CONFIG_LOG_WRITE ? "WRITE" : "" + ); +} + +long configRead(const String &path, const long fallback, const bool log) { + if (auto file = configOpen(path, false)) { const auto content = file.readString(); file.close(); - return content.toInt(); + const auto value = content.toInt(); + doLog(path, String(value), false, CONFIG_LOG_READ, log); + return value; } + doLog(path, String(fallback), false, CONFIG_LOG_FALLBACK, log); return fallback; } -bool configWrite(const String &name, const long fallback, const long value) { - if (configRead(name, fallback) == value) { +bool configWrite(const String &path, const long fallback, const long value) { + if (configRead(path, fallback, false) == value) { + doLog(path, String(value), false, CONFIG_LOG_UNCHANGED, true); return false; } - Serial.printf("[CONFIG] \"%s\" = \"%ld\"\n", name.c_str(), value); - if (auto file = configOpen(name, "w")) { + if (auto file = configOpen(path, true)) { const auto content = String(value); file.write(reinterpret_cast(content.c_str()), content.length()); file.close(); + doLog(path, String(value), false, CONFIG_LOG_WRITE, true); return true; } return false; } -bool configRead(const String &name, const bool fallback) { - return configRead(name, fallback ? 1L : 0L) > 0; +bool configRead(const String &path, const bool fallback) { + return configRead(path, fallback ? 1L : 0L) > 0; } -bool configWrite(const String &name, const bool fallback, const bool value) { - return configWrite(name, fallback ? 1L : 0L, value ? 1L : 0L); +bool configWrite(const String &path, const bool fallback, const bool value) { + return configWrite(path, fallback ? 1L : 0L, value ? 1L : 0L); } -String configRead(const String &name, const char *fallback) { - return configRead(name, String(fallback)); +String configRead(const String &path, const char *fallback) { + return configRead(path, String(fallback)); } -bool configWrite(const String &name, const char *fallback, const char *value) { - return configWrite(name, String(fallback), String(value)); +bool configWrite(const String &path, const char *fallback, const char *value) { + return configWrite(path, String(fallback), String(value), false); } -String configRead(const String &name, const String &fallback) { - if (auto file = configOpen(name, "r")) { - const auto content = file.readString(); +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(); - return content; + doLog(path, value.c_str(), isPassword, CONFIG_LOG_READ, log); + return value; } + doLog(path, fallback.c_str(), isPassword, CONFIG_LOG_FALLBACK, log); return fallback; } -bool configWrite(const String &name, const String &fallback, const String &value) { - if (configRead(name, fallback) == value) { +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); return false; } - Serial.printf("[CONFIG] \"%s\" = \"%s\"\n", name.c_str(), value.c_str()); - if (auto file = configOpen(name, "w")) { + 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); return true; } return false; } -Initial configRead(const String &name, const Initial &fallback) { - return stringToInitial(configRead(name, initialToString(fallback))); +Initial configRead(const String &path, const Initial &fallback) { + return stringToInitial(configRead(path, initialToString(fallback))); } -bool configWrite(const String &name, const Initial &fallback, const Initial &value) { - return configWrite(name, initialToString(fallback), initialToString(value)); +bool configWrite(const String &path, const Initial &fallback, const Initial &value) { + return configWrite(path, initialToString(fallback), initialToString(value)); } diff --git a/src/config.h b/src/config.h index 268f50b..1894a18 100644 --- a/src/config.h +++ b/src/config.h @@ -7,24 +7,24 @@ void configSetup(); -long configRead(const String &name, long fallback); +long configRead(const String &path, long fallback, bool log = true); -bool configWrite(const String &name, long fallback, long value); +bool configWrite(const String &path, long fallback, long value); -bool configRead(const String &name, bool fallback); +bool configRead(const String &path, bool fallback); -bool configWrite(const String &name, bool fallback, bool value); +bool configWrite(const String &path, bool fallback, bool value); -String configRead(const String &name, const char *fallback); +String configRead(const String &path, const char *fallback); -bool configWrite(const String &name, const char *fallback, const char *value); +bool configWrite(const String &path, const char *fallback, const char *value); -String configRead(const String &name, const String &fallback); +String configRead(const String &path, const String &fallback, bool log = true, bool isPassword = false); -bool configWrite(const String &name, const String &fallback, const String &value); +bool configWrite(const String &path, const String &fallback, const String &value, bool isPassword); -Initial configRead(const String &name, const Initial &fallback); +Initial configRead(const String &path, const Initial &fallback); -bool configWrite(const String &name, const Initial &fallback, const Initial &value); +bool configWrite(const String &path, const Initial &fallback, const Initial &value); #endif diff --git a/src/wifi.cpp b/src/wifi.cpp index 706dfe1..f76b0f3 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -3,9 +3,9 @@ #include "http.h" #include "io.h" -#define CONFIG_HOSTNAME "hostname" -#define CONFIG_WIFI_SSID "wifiSSID" -#define CONFIG_WIFI_PASSWORD "wifiPassword" +#define CONFIG_HOSTNAME "/wifi/hostname" +#define CONFIG_WIFI_SSID "/wifi/ssid" +#define CONFIG_WIFI_PASSWORD "/wifi/password" #define DEFAULT_HOSTNAME "PatrixSonoff4ChPro" #define DEFAULT_WIFI_SSID "HappyNet" @@ -28,7 +28,7 @@ void wifiConnect() { const auto hostname = configRead(CONFIG_HOSTNAME, DEFAULT_HOSTNAME); const auto wifiSSID = configRead(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID); - const auto wifiPass = configRead(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD); + const auto wifiPass = configRead(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, true, true); WiFi.hostname(hostname); WiFi.begin(wifiSSID.c_str(), wifiPass.c_str()); @@ -66,19 +66,15 @@ void wifiLoop() { void wifiChangeHostname(const char *hostname) { if (configWrite(CONFIG_HOSTNAME, DEFAULT_HOSTNAME, hostname)) { WiFi.setHostname(hostname); - Serial.printf("[WiFi] hostname: %s\n", hostname); } } void wifiChangeSSID(const char *ssid) { - if (configWrite(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID, ssid)) { - Serial.printf("[WiFi] SSID: %s\n", ssid); - } + configWrite(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID, ssid); } void wifiChangePassword(const char *password) { - configWrite(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, password); - Serial.printf("[WiFi] password: ***"); + configWrite(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, password, true); } void wifiSetup() {