config FIX

This commit is contained in:
Patrick Haßel 2025-01-10 12:09:45 +01:00
parent 542c90dc92
commit 0a8de2905f
3 changed files with 43 additions and 40 deletions

View File

@ -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());

View File

@ -11,17 +11,16 @@ class App {
const String path;
JsonDocument configJson;
JsonDocument configJsonDoc;
JsonObject config = configJson.to<JsonObject>();
JsonObject config = configJsonDoc.to<JsonObject>();
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<T>()) {
return config[key].as<T>();
}
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<JsonObject>()) {
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<JsonObject>();
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<JsonObject>();
}
file.close();
config = configJson.to<JsonObject>();
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();
}
};

View File

@ -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<unsigned long>(CONFIG_SECONDS_KEY, CONFIG_SECONDS_DEFAULT) * 1000;
const auto seconds = configGet<unsigned long>(CONFIG_SECONDS_KEY, CONFIG_SECONDS_DEFAULT);
info("config:");
info(" seconds = %ld", seconds);
configMillis = seconds * 1000;
totalMillis = configMillis;
setState(PAUSE);
}