config FIX
This commit is contained in:
parent
542c90dc92
commit
0a8de2905f
@ -20,7 +20,7 @@ void appStart(const String& name) {
|
|||||||
error("No such app: \"%s\"\n", name.c_str());
|
error("No such app: \"%s\"\n", name.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
info("App instantiated: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent());
|
info("App loaded: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent());
|
||||||
|
|
||||||
app->start();
|
app->start();
|
||||||
info("App started: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent());
|
info("App started: \"%s\" (heap usage: %d%%)", app->getName(), getHeapUsagePercent());
|
||||||
|
|||||||
@ -11,17 +11,16 @@ class App {
|
|||||||
|
|
||||||
const String path;
|
const String path;
|
||||||
|
|
||||||
JsonDocument configJson;
|
JsonDocument configJsonDoc;
|
||||||
|
|
||||||
JsonObject config = configJson.to<JsonObject>();
|
JsonObject config = configJsonDoc.to<JsonObject>();
|
||||||
|
|
||||||
bool dirty = true;
|
bool dirty = true;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit App(const char *name) :
|
explicit App(const char *name) : name(name),
|
||||||
name(name),
|
path(String("/") + name + ".json") {
|
||||||
path(String("/") + name + ".json") {
|
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +31,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void start() {
|
void start() {
|
||||||
configLoad();
|
configRead();
|
||||||
_start();
|
_start();
|
||||||
markDirty();
|
markDirty();
|
||||||
}
|
}
|
||||||
@ -88,6 +87,7 @@ protected:
|
|||||||
if (config[key].is<T>()) {
|
if (config[key].is<T>()) {
|
||||||
return config[key].as<T>();
|
return config[key].as<T>();
|
||||||
}
|
}
|
||||||
|
warn("config key \"%s\" not found!", key);
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,51 +116,50 @@ protected:
|
|||||||
dirty = true;
|
dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void configLoad() {
|
bool deserializeConfig(File file) {
|
||||||
configJson.clear();
|
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");
|
auto file = LittleFS.open(path, "r");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
error("failed to open file for config read: %s", path.c_str());
|
error("failed to open file for config read: %s", path.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!deserializeConfig(file)) {
|
||||||
if (deserializeJson(configJson, file)) {
|
configJsonDoc.clear();
|
||||||
info("config loaded: %s", path.c_str());
|
config = configJsonDoc.to<JsonObject>();
|
||||||
} else {
|
|
||||||
error("failed to load config: %s", path.c_str());
|
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
config = configJson.to<JsonObject>();
|
|
||||||
|
|
||||||
char buffer[256];
|
|
||||||
serializeJsonPretty(configJson, buffer, sizeof buffer);
|
|
||||||
Serial.println(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void configSave() {
|
void configWrite() const {
|
||||||
auto file = LittleFS.open(path, "w");
|
auto write = LittleFS.open(path, "w");
|
||||||
if (!file) {
|
if (!write) {
|
||||||
error("failed to open file for config write: %s", path.c_str());
|
error("failed to open file for config write: %s", path.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const auto size = measureJson(configJsonDoc);
|
||||||
if (serializeJson(configJson, file)) {
|
if (serializeJson(configJsonDoc, write) == size) {
|
||||||
info("config written: %s", path.c_str());
|
char buffer[256];
|
||||||
|
serializeJson(configJsonDoc, buffer, sizeof buffer);
|
||||||
|
info("config written: %s => %s", path.c_str(), buffer);
|
||||||
} else {
|
} else {
|
||||||
error("failed to write config: %s", path.c_str());
|
error("failed to write config: %s", path.c_str());
|
||||||
}
|
}
|
||||||
file.close();
|
write.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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -43,7 +43,7 @@ class AppMatch final : public App {
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
explicit AppMatch()
|
explicit AppMatch()
|
||||||
: App(APP_MATCH_NAME) {
|
: App(APP_MATCH_NAME) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ public:
|
|||||||
if (seconds > 0 && configMillis != newMillis) {
|
if (seconds > 0 && configMillis != newMillis) {
|
||||||
configMillis = newMillis;
|
configMillis = newMillis;
|
||||||
configSet(CONFIG_SECONDS_KEY, seconds);
|
configSet(CONFIG_SECONDS_KEY, seconds);
|
||||||
configSave();
|
configWrite();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,7 +64,11 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
|
|
||||||
void _start() override {
|
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;
|
totalMillis = configMillis;
|
||||||
setState(PAUSE);
|
setState(PAUSE);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user