Config: has, getAutoWriteInMillis, getAutoWriteAtEpoch, json public

This commit is contained in:
Patrick Haßel 2025-01-27 14:46:48 +01:00
parent 80df3ec9cc
commit 1a75387aea

View File

@ -15,14 +15,14 @@ class Config final {
const String path; const String path;
JsonDocument config;
unsigned long dirty = 0; unsigned long dirty = 0;
bool doForceNextHexBuffer = true; bool doForceNextHexBuffer = true;
public: public:
JsonDocument json;
explicit Config(String path) : path(std::move(path)) { explicit Config(String path) : path(std::move(path)) {
// //
} }
@ -37,8 +37,8 @@ public:
return; return;
} }
if (!deserialize(file)) { if (!deserialize(file)) {
config.clear(); json.clear();
config = config.to<JsonObject>(); json = json.to<JsonObject>();
} }
file.close(); file.close();
} }
@ -50,10 +50,10 @@ public:
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(config); const auto size = measureJson(json);
if (serializeJson(config, write) == size) { if (serializeJson(json, write) == size) {
char buffer[256]; char buffer[256];
serializeJson(config, buffer, sizeof buffer); serializeJson(json, buffer, sizeof buffer);
info("Config written: %s => %s", path.c_str(), 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());
@ -68,19 +68,24 @@ public:
} }
} }
template<typename T>
bool has(const char *key) {
return json[key].is<T>();
}
template<typename T> template<typename T>
T get(const char *key, T fallback) { T get(const char *key, T fallback) {
if (config[key].is<T>()) { if (json[key].is<T>()) {
return config[key].as<T>(); return json[key].as<T>();
} }
warn("Config key \"%s\" not found!", key); warn("Config key \"%s\" not found!", key);
return fallback; return fallback;
} }
bool setIfNot(const char *key, const int value) { bool setIfNot(const char *key, const int value) {
auto changed = !config[key].is<int>(); auto changed = !json[key].is<int>();
if (!changed) { if (!changed) {
changed = config[key].as<int>() != value; changed = json[key].as<int>() != value;
} }
if (changed) { if (changed) {
set(key, value); set(key, value);
@ -90,9 +95,9 @@ public:
} }
bool setIfNot(const char *key, const double value) { bool setIfNot(const char *key, const double value) {
auto changed = !config[key].is<double>(); auto changed = !json[key].is<double>();
if (!changed) { if (!changed) {
changed = config[key].as<double>() != value; changed = json[key].as<double>() != value;
} }
if (changed) { if (changed) {
set(key, value); set(key, value);
@ -102,26 +107,44 @@ public:
} }
template<typename T> template<typename T>
void set(const char *key, T value) { void set(const String& key, T value) {
config[key] = value; json[key] = value;
dirty = max(1UL, millis()); // avoid special value zero (=> not dirty) dirty = millis();
if (dirty == 0) {
// avoid special value zero (=> not dirty)
dirty--;
}
}
unsigned long getAutoWriteInMillis() const {
if (dirty == 0) {
return 0;
}
return CONFIG_WRITE_DELAY_MILLIS - (millis() - dirty);
}
time_t getAutoWriteAtEpoch() const {
if (dirty == 0) {
return 0;
}
return time(nullptr) + static_cast<long>(getAutoWriteInMillis() / 1000);
} }
private: private:
bool deserialize(File file) { bool deserialize(File file) {
if (deserializeJson(config, file) != DeserializationError::Ok) { if (deserializeJson(json, file) != DeserializationError::Ok) {
error("Failed to deserialize config: %s", file.path()); error("Failed to deserialize config: %s", file.path());
return false; return false;
} }
if (!config.is<JsonObject>()) { if (!json.is<JsonObject>()) {
error("Config not a json-object: %s", file.path()); error("Config not a json-object: %s", file.path());
return false; return false;
} }
char buffer[256]; char buffer[256];
serializeJson(config, buffer, sizeof buffer); serializeJson(json, buffer, sizeof buffer);
info("Config loaded: %s => %s", path.c_str(), buffer); info("Config loaded: %s => %s", path.c_str(), buffer);
config = config.as<JsonObject>(); json = json.as<JsonObject>();
return true; return true;
} }