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