Usermod class vars pt3:

Moved initDone, addToConfig and readFromConfig to Usermod superclass

Updated cleanup procedure:

Part 1
- remove bool enabled = false/true (now default false)
- remove static const char _name[] and _enabled[]
- add constructor which calls superclass (temp?): XXXUsermod(const char *name, bool enabled):Usermod(name, enabled) {} 
- replace _enabled with "enabled"
- remove const char PROGMEM init for  _name[] and _enabled[]
Part 2
- Remove bool initDone = false;
- addToConfig: replace createNestedObject with Usermod::addToConfig(root); JsonObject top = root[FPSTR(_name)];
- readFromConfig: replace !top.isNull and enabled with bool configComplete = Usermod::readFromConfig(root);JsonObject top = root[FPSTR(_name)];

See Temperature, MPU6050 and weather as examples (rest to be done)
This commit is contained in:
Ewoud
2023-03-16 15:41:23 +01:00
parent e3c359a4a4
commit c928df9d70
7 changed files with 34 additions and 32 deletions

View File

@@ -21,7 +21,6 @@ class UsermodTemperature : public Usermod {
private:
bool initDone = false;
OneWire *oneWire;
// GPIO pin used for sensor (with a default compile-time fallback)
int8_t temperaturePin = TEMPERATURE_PIN;
@@ -322,9 +321,10 @@ class UsermodTemperature : public Usermod {
* addToConfig() (called from set.cpp) stores persistent properties to cfg.json
*/
void addToConfig(JsonObject &root) {
Usermod::addToConfig(root);
JsonObject top = root[FPSTR(_name)];
// we add JSON object: {"Temperature": {"pin": 0, "degC": true}}
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
top[FPSTR("enabled")] = enabled;
top["pin"] = temperaturePin; // usermodparam
top["degC"] = degC; // usermodparam
top[FPSTR(_readInterval)] = readingInterval / 1000;
@@ -339,17 +339,18 @@ class UsermodTemperature : public Usermod {
* The function should return true if configuration was successfully loaded or false if there was no configuration.
*/
bool readFromConfig(JsonObject &root) {
bool configComplete = Usermod::readFromConfig(root);
JsonObject top = root[FPSTR(_name)];
// we look for JSON object: {"Temperature": {"pin": 0, "degC": true}}
int8_t newTemperaturePin = temperaturePin;
DEBUG_PRINT(FPSTR(_name));
JsonObject top = root[FPSTR(_name)];
if (top.isNull()) {
DEBUG_PRINTLN(F(": No config found. (Using defaults.)"));
return false;
}
enabled = top[FPSTR("enabled")] | enabled;
newTemperaturePin = top["pin"] | newTemperaturePin;
degC = top["degC"] | degC;
readingInterval = top[FPSTR(_readInterval)] | readingInterval/1000;