V2 of usermod configuration settings

- added parsing of POST fields to use proper type
- updated readFromConfig() to reflect parsing
- added a possibility to use nested object in UM settings
- internal changes and fixes
This commit is contained in:
Blaz Kristan
2021-06-27 12:15:35 +02:00
parent 81182bb125
commit f7ce83ea34
13 changed files with 204 additions and 291 deletions

View File

@@ -319,6 +319,8 @@ public:
/**
* restore the changeable values
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
*
* The function should return true if configuration was successfully loaded or false if there was no configuration.
*/
bool readFromConfig(JsonObject &root)
{
@@ -326,57 +328,32 @@ public:
int8_t oldPin = PIRsensorPin;
JsonObject top = root[FPSTR(_name)];
if (top.isNull()) return false;
if (top["pin"] != nullptr) {
PIRsensorPin = min(39,max(-1,top["pin"].as<int>())); // check bounds
if (top.isNull()) {
DEBUG_PRINT(FPSTR(_name));
DEBUG_PRINTLN(F(": No config found. (Using defaults.)"));
return false;
}
if (top[FPSTR(_enabled)] != nullptr) {
if (top[FPSTR(_enabled)].is<bool>()) {
enabled = top[FPSTR(_enabled)].as<bool>(); // reading from cfg.json
} else {
// change from settings page
String str = top[FPSTR(_enabled)]; // checkbox -> off or on
enabled = (bool)(str!="off"); // off is guaranteed to be present
}
}
PIRsensorPin = top["pin"] | PIRsensorPin;
// PIRsensorPin = min(39,max(-1,(int)PIRsensorPin)); // check bounds
if (top[FPSTR(_switchOffDelay)] != nullptr) {
m_switchOffDelay = (top[FPSTR(_switchOffDelay)].as<int>() * 1000);
}
enabled = top[FPSTR(_enabled)] | enabled;
if (top[FPSTR(_onPreset)] != nullptr) {
m_onPreset = max(0,min(250,top[FPSTR(_onPreset)].as<int>()));
}
m_switchOffDelay = (top[FPSTR(_switchOffDelay)] | m_switchOffDelay/1000) * 1000;
if (top[FPSTR(_offPreset)] != nullptr) {
m_offPreset = max(0,min(250,top[FPSTR(_offPreset)].as<int>()));
}
m_onPreset = top[FPSTR(_onPreset)] | m_onPreset;
m_onPreset = max(0,min(250,(int)m_onPreset));
if (top[FPSTR(_nightTime)] != nullptr) {
if (top[FPSTR(_nightTime)].is<bool>()) {
m_nightTimeOnly = top[FPSTR(_nightTime)].as<bool>(); // reading from cfg.json
} else {
// change from settings page
String str = top[FPSTR(_nightTime)]; // checkbox -> off or on
m_nightTimeOnly = (bool)(str!="off"); // off is guaranteed to be present
}
}
m_offPreset = top[FPSTR(_offPreset)] | m_offPreset;
m_offPreset = max(0,min(250,(int)m_offPreset));
if (top[FPSTR(_mqttOnly)] != nullptr) {
if (top[FPSTR(_mqttOnly)].is<bool>()) {
m_mqttOnly = top[FPSTR(_mqttOnly)].as<bool>(); // reading from cfg.json
} else {
// change from settings page
String str = top[FPSTR(_mqttOnly)]; // checkbox -> off or on
m_mqttOnly = (bool)(str!="off"); // off is guaranteed to be present
}
}
m_nightTimeOnly = top[FPSTR(_nightTime)] | m_nightTimeOnly;
m_mqttOnly = top[FPSTR(_mqttOnly)] | m_mqttOnly;
DEBUG_PRINT(FPSTR(_name));
if (!initDone) {
// reading config prior to setup()
DEBUG_PRINTLN(F("PIR config loaded."));
DEBUG_PRINTLN(F(" config loaded."));
} else {
if (oldPin != PIRsensorPin || oldEnabled != enabled) {
// check if pin is OK
@@ -395,10 +372,10 @@ public:
if (enabled) {
sensorPinState = digitalRead(PIRsensorPin);
}
DEBUG_PRINTLN(F("PIR config (re)loaded."));
}
DEBUG_PRINTLN(F(" config (re)loaded."));
}
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features
return true;
}