diff --git a/src/app/App.cpp b/src/app/App.cpp index f7ecb7a..5934a93 100644 --- a/src/app/App.cpp +++ b/src/app/App.cpp @@ -20,7 +20,7 @@ void appStart(const String& name) { error("No such app: \"%s\"\n", name.c_str()); return; } - info("App instantiated: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent()); + info("App loaded: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent()); app->start(); info("App started: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent()); diff --git a/src/app/App.h b/src/app/App.h index d8323a5..4edb077 100644 --- a/src/app/App.h +++ b/src/app/App.h @@ -11,17 +11,16 @@ class App { const String path; - JsonDocument configJson; + JsonDocument configJsonDoc; - JsonObject config = configJson.to(); + JsonObject config = configJsonDoc.to(); bool dirty = true; public: - explicit App(const char *name) : - name(name), - path(String("/") + name + ".json") { + explicit App(const char *name) : name(name), + path(String("/") + name + ".json") { // } @@ -32,7 +31,7 @@ public: } void start() { - configLoad(); + configRead(); _start(); markDirty(); } @@ -88,6 +87,7 @@ protected: if (config[key].is()) { return config[key].as(); } + warn("config key \"%s\" not found!", key); return fallback; } @@ -116,51 +116,50 @@ protected: dirty = true; } - void configLoad() { - configJson.clear(); + bool deserializeConfig(File file) { + if (deserializeJson(configJsonDoc, file) != DeserializationError::Ok) { + error("failed to deserialize config: %s", file.path()); + return false; + } + if (!configJsonDoc.is()) { + error("not a json-object: %s", file.path()); + return false; + } + char buffer[256]; + serializeJson(configJsonDoc, buffer, sizeof buffer); + info("config loaded: %s => %s", path.c_str(), buffer); + config = configJsonDoc.as(); + return true; + } + + void configRead() { auto file = LittleFS.open(path, "r"); if (!file) { error("failed to open file for config read: %s", path.c_str()); return; } - - if (deserializeJson(configJson, file)) { - info("config loaded: %s", path.c_str()); - } else { - error("failed to load config: %s", path.c_str()); + if (!deserializeConfig(file)) { + configJsonDoc.clear(); + config = configJsonDoc.to(); } file.close(); - - config = configJson.to(); - - char buffer[256]; - serializeJsonPretty(configJson, buffer, sizeof buffer); - Serial.println(buffer); } - void configSave() { - auto file = LittleFS.open(path, "w"); - if (!file) { + void configWrite() const { + auto write = LittleFS.open(path, "w"); + if (!write) { error("failed to open file for config write: %s", path.c_str()); return; } - - if (serializeJson(configJson, file)) { - info("config written: %s", path.c_str()); + const auto size = measureJson(configJsonDoc); + if (serializeJson(configJsonDoc, write) == size) { + char buffer[256]; + serializeJson(configJsonDoc, buffer, sizeof buffer); + info("config written: %s => %s", path.c_str(), buffer); } else { error("failed to write config: %s", path.c_str()); } - file.close(); - - auto file2 = LittleFS.open(path, "r"); - if (file2) { - while (file2.available()) { - Serial.write(file2.read()); - } - file2.close(); - } else { - Serial.println("Failed to open file2 for reading"); - } + write.close(); } }; diff --git a/src/app/AppMatch.h b/src/app/AppMatch.h index fc83c77..ccc02cf 100644 --- a/src/app/AppMatch.h +++ b/src/app/AppMatch.h @@ -43,7 +43,7 @@ class AppMatch final : public App { public: explicit AppMatch() - : App(APP_MATCH_NAME) { + : App(APP_MATCH_NAME) { // } @@ -54,7 +54,7 @@ public: if (seconds > 0 && configMillis != newMillis) { configMillis = newMillis; configSet(CONFIG_SECONDS_KEY, seconds); - configSave(); + configWrite(); return true; } } @@ -64,7 +64,11 @@ public: protected: void _start() override { - configMillis = configGet(CONFIG_SECONDS_KEY, CONFIG_SECONDS_DEFAULT) * 1000; + const auto seconds = configGet(CONFIG_SECONDS_KEY, CONFIG_SECONDS_DEFAULT); + info("config:"); + info(" seconds = %ld", seconds); + + configMillis = seconds * 1000; totalMillis = configMillis; setState(PAUSE); }