readFromConfig() update
This commit is contained in:
parent
660de0b4e5
commit
1e5420e6a7
@ -2,6 +2,12 @@
|
||||
|
||||
### Builds after release 0.12.0
|
||||
|
||||
#### Build 2106241
|
||||
|
||||
- BREAKING: Added ability for usermods to force a config save if config incomplete. `readFromConfig()` needs to return a `bool` to indicate if the config is complete
|
||||
- Updated usermods implementing `readFromConfig()`
|
||||
- Auto-create segments based on configured busses
|
||||
|
||||
#### Build 2106200
|
||||
|
||||
- Added 2 Ethernet boards and split Ethernet configs into separate file
|
||||
|
@ -427,7 +427,7 @@ class Animated_Staircase : public Usermod {
|
||||
/*
|
||||
* Reads the configuration to internal flash memory before setup() is called.
|
||||
*/
|
||||
void readFromConfig(JsonObject& root) {
|
||||
bool readFromConfig(JsonObject& root) {
|
||||
bool oldUseUSSensorTop = useUSSensorTop;
|
||||
bool oldUseUSSensorBottom = useUSSensorBottom;
|
||||
int8_t oldTopAPin = topPIRorTriggerPin;
|
||||
@ -435,6 +435,8 @@ class Animated_Staircase : public Usermod {
|
||||
int8_t oldBottomAPin = bottomPIRorTriggerPin;
|
||||
int8_t oldBottomBPin = bottomEchoPin;
|
||||
|
||||
bool configComplete = true;
|
||||
|
||||
JsonObject staircase = root[FPSTR(_name)];
|
||||
if (!staircase.isNull()) {
|
||||
if (staircase[FPSTR(_enabled)].is<bool>()) {
|
||||
@ -468,6 +470,7 @@ class Animated_Staircase : public Usermod {
|
||||
bottomMaxDist = min(150,max(30,staircase[FPSTR(_bottomEchoCm)].as<int>())); // max distance ~1.5m (a lag of 9ms may be expected)
|
||||
} else {
|
||||
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
|
||||
configComplete = false;
|
||||
}
|
||||
if (!initDone) {
|
||||
// first run: reading from cfg.json
|
||||
@ -490,6 +493,7 @@ class Animated_Staircase : public Usermod {
|
||||
}
|
||||
if (changed) setup();
|
||||
}
|
||||
return configComplete;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -133,13 +133,28 @@ class MyExampleUsermod : public Usermod {
|
||||
* readFromConfig() is called BEFORE setup(). This means you can use your persistent values in setup() (e.g. pin assignments, buffer sizes),
|
||||
* but also that if you want to write persistent values to a dynamic buffer, you'd need to allocate it here instead of in setup.
|
||||
* If you don't know what that is, don't fret. It most likely doesn't affect your use case :)
|
||||
*
|
||||
* Return true in case your config was complete, or false if you'd like WLED to save your defaults to disk
|
||||
*
|
||||
* This function is guaranteed to be called on boot, but could also be called every time settings are updated
|
||||
*/
|
||||
void readFromConfig(JsonObject& root)
|
||||
bool readFromConfig(JsonObject& root)
|
||||
{
|
||||
JsonObject top = root["top"];
|
||||
userVar0 = 42; //set your variables to their boot default value (this can also be done when declaring the variable)
|
||||
|
||||
JsonObject top = root["exampleUsermod"];
|
||||
if (!top.isNull()) {
|
||||
userVar0 = top["great"] | 42; //The value right of the pipe "|" is the default value in case your setting was not present in cfg.json (e.g. first boot)
|
||||
bool configComplete = true;
|
||||
|
||||
//check if value is there
|
||||
if (top.containsKey("great")) {
|
||||
//convert value to the correct type
|
||||
userVar0 = top["great"].as<int>();
|
||||
} else configComplete = false;
|
||||
|
||||
if (configComplete) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
@ -149,11 +149,13 @@ Delay <input type=\"number\" min=\"5\" max=\"300\" value=\"";
|
||||
/**
|
||||
* restore the changeable values
|
||||
*/
|
||||
void readFromConfig(JsonObject &root)
|
||||
bool readFromConfig(JsonObject &root)
|
||||
{
|
||||
JsonObject top = root["FixUnreachableNetServices"];
|
||||
if (top.isNull()) return false;
|
||||
m_pingDelayMs = top["PingDelayMs"] | m_pingDelayMs;
|
||||
m_pingDelayMs = max(5000UL, min(18000000UL, m_pingDelayMs));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -321,13 +321,13 @@ public:
|
||||
* restore the changeable values
|
||||
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
|
||||
*/
|
||||
void readFromConfig(JsonObject &root)
|
||||
bool readFromConfig(JsonObject &root)
|
||||
{
|
||||
bool oldEnabled = enabled;
|
||||
int8_t oldPin = PIRsensorPin;
|
||||
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
if (top.isNull()) return;
|
||||
if (top.isNull()) return false;
|
||||
|
||||
if (top["pin"] != nullptr) {
|
||||
PIRsensorPin = min(39,max(-1,top["pin"].as<int>())); // check bounds
|
||||
@ -399,6 +399,8 @@ public:
|
||||
DEBUG_PRINTLN(F("PIR config (re)loaded."));
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -169,7 +169,7 @@ public:
|
||||
/**
|
||||
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
|
||||
*/
|
||||
void readFromConfig(JsonObject &root)
|
||||
bool readFromConfig(JsonObject &root)
|
||||
{
|
||||
// we look for JSON object.
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
@ -196,7 +196,9 @@ public:
|
||||
else
|
||||
{
|
||||
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -249,11 +249,13 @@ class UsermodTemperature : public Usermod {
|
||||
/**
|
||||
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
|
||||
*/
|
||||
void readFromConfig(JsonObject &root) {
|
||||
bool readFromConfig(JsonObject &root) {
|
||||
// we look for JSON object: {"Temperature": {"pin": 0, "degC": true}}
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
int8_t newTemperaturePin = temperaturePin;
|
||||
|
||||
bool configComplete = true;
|
||||
|
||||
if (!top.isNull() && top["pin"] != nullptr) {
|
||||
if (top[FPSTR(_enabled)].is<bool>()) {
|
||||
disabled = !top[FPSTR(_enabled)].as<bool>();
|
||||
@ -274,13 +276,14 @@ class UsermodTemperature : public Usermod {
|
||||
DEBUG_PRINTLN(F("Temperature config (re)loaded."));
|
||||
} else {
|
||||
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
|
||||
configComplete = false;
|
||||
}
|
||||
|
||||
if (!initDone) {
|
||||
// first run: reading from cfg.json
|
||||
temperaturePin = newTemperaturePin;
|
||||
} else {
|
||||
// changing paramters from settings page
|
||||
// changing parameters from settings page
|
||||
if (newTemperaturePin != temperaturePin) {
|
||||
// deallocate pin and release memory
|
||||
delete oneWire;
|
||||
@ -290,6 +293,7 @@ class UsermodTemperature : public Usermod {
|
||||
setup();
|
||||
}
|
||||
}
|
||||
return configComplete;
|
||||
}
|
||||
|
||||
uint16_t getId()
|
||||
|
@ -348,11 +348,11 @@ class MultiRelay : public Usermod {
|
||||
* restore the changeable values
|
||||
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
|
||||
*/
|
||||
void readFromConfig(JsonObject &root) {
|
||||
bool readFromConfig(JsonObject &root) {
|
||||
int8_t oldPin[MULTI_RELAY_MAX_RELAYS];
|
||||
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
if (top.isNull()) return;
|
||||
if (top.isNull()) return false;
|
||||
|
||||
if (top[FPSTR(_enabled)] != nullptr) {
|
||||
if (top[FPSTR(_enabled)].is<bool>()) {
|
||||
@ -413,6 +413,7 @@ class MultiRelay : public Usermod {
|
||||
}
|
||||
DEBUG_PRINTLN(F("MultiRelay config (re)loaded."));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,18 +82,6 @@ class StairwayWipeUsermod : public Usermod {
|
||||
//if (root["bri"] == 255) Serial.println(F("Don't burn down your garage!"));
|
||||
}
|
||||
|
||||
void addToConfig(JsonObject& root)
|
||||
{
|
||||
JsonObject top = root.createNestedObject("exampleUsermod");
|
||||
top["great"] = userVar0; //save this var persistently whenever settings are saved
|
||||
}
|
||||
|
||||
void readFromConfig(JsonObject& root)
|
||||
{
|
||||
JsonObject top = root["top"];
|
||||
userVar0 = top["great"] | 42; //The value right of the pipe "|" is the default value in case your setting was not present in cfg.json (e.g. first boot)
|
||||
}
|
||||
|
||||
uint16_t getId()
|
||||
{
|
||||
return USERMOD_ID_EXAMPLE;
|
||||
|
@ -198,12 +198,12 @@ class AutoSaveUsermod : public Usermod {
|
||||
* but also that if you want to write persistent values to a dynamic buffer, you'd need to allocate it here instead of in setup.
|
||||
* If you don't know what that is, don't fret. It most likely doesn't affect your use case :)
|
||||
*/
|
||||
void readFromConfig(JsonObject& root) {
|
||||
bool readFromConfig(JsonObject& root) {
|
||||
// we look for JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}}
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
if (top.isNull()) {
|
||||
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (top[FPSTR(_autoSaveEnabled)].is<bool>()) {
|
||||
@ -225,6 +225,7 @@ class AutoSaveUsermod : public Usermod {
|
||||
applyAutoSaveOnBoot = (bool)(str!="off"); // off is guaranteed to be present
|
||||
}
|
||||
DEBUG_PRINTLN(F("Autosave config (re)loaded."));
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -615,12 +615,14 @@ class FourLineDisplayUsermod : public Usermod {
|
||||
* but also that if you want to write persistent values to a dynamic buffer, you'd need to allocate it here instead of in setup.
|
||||
* If you don't know what that is, don't fret. It most likely doesn't affect your use case :)
|
||||
*/
|
||||
void readFromConfig(JsonObject& root) {
|
||||
bool readFromConfig(JsonObject& root) {
|
||||
bool needsRedraw = false;
|
||||
DisplayType newType = type;
|
||||
int8_t newScl = sclPin;
|
||||
int8_t newSda = sdaPin;
|
||||
|
||||
bool configComplete = true;
|
||||
|
||||
JsonObject top = root[FPSTR(_name)];
|
||||
if (!top.isNull() && top["pin"] != nullptr) {
|
||||
newScl = top["pin"][0];
|
||||
@ -653,6 +655,7 @@ class FourLineDisplayUsermod : public Usermod {
|
||||
DEBUG_PRINTLN(F("4 Line Display config (re)loaded."));
|
||||
} else {
|
||||
DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
|
||||
configComplete = false;
|
||||
}
|
||||
|
||||
if (!initDone) {
|
||||
@ -684,6 +687,8 @@ class FourLineDisplayUsermod : public Usermod {
|
||||
setFlipMode(flip);
|
||||
if (needsRedraw && !wakeDisplay()) redraw(true);
|
||||
}
|
||||
|
||||
return configComplete;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -410,7 +410,10 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
|
||||
|
||||
DEBUG_PRINTLN(F("Starting usermod config."));
|
||||
JsonObject usermods_settings = doc["um"];
|
||||
if (!usermods_settings.isNull()) usermods.readFromConfig(usermods_settings);
|
||||
if (!usermods_settings.isNull()) {
|
||||
bool allComplete = usermods.readFromConfig(usermods_settings);
|
||||
if (!allComplete && fromFS) serializeConfig();
|
||||
}
|
||||
|
||||
if (fromFS) return false;
|
||||
doReboot = doc[F("rb")] | doReboot;
|
||||
@ -418,11 +421,10 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
|
||||
}
|
||||
|
||||
void deserializeConfigFromFS() {
|
||||
bool fromeep = false;
|
||||
bool success = deserializeConfigSec();
|
||||
if (!success) { //if file does not exist, try reading from EEPROM
|
||||
deEEPSettings();
|
||||
fromeep = true;
|
||||
return;
|
||||
}
|
||||
|
||||
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
||||
@ -431,7 +433,7 @@ void deserializeConfigFromFS() {
|
||||
|
||||
success = readObjectFromFile("/cfg.json", nullptr, &doc);
|
||||
if (!success) { //if file does not exist, try reading from EEPROM
|
||||
if (!fromeep) deEEPSettings();
|
||||
deEEPSettings();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,6 @@
|
||||
color: white;
|
||||
border: 0px solid white;
|
||||
border-radius: 25px;
|
||||
filter: drop-shadow(0px 0px 1px #000);
|
||||
}
|
||||
|
||||
img {
|
||||
|
@ -189,7 +189,7 @@ class Usermod {
|
||||
virtual void addToJsonInfo(JsonObject& obj) {}
|
||||
virtual void readFromJsonState(JsonObject& obj) {}
|
||||
virtual void addToConfig(JsonObject& obj) {}
|
||||
virtual void readFromConfig(JsonObject& obj) {}
|
||||
virtual bool readFromConfig(JsonObject& obj) { return true; } //Heads up! readFromConfig() now needs to return a bool
|
||||
virtual void onMqttConnect(bool sessionPresent) {}
|
||||
virtual bool onMqttMessage(char* topic, char* payload) { return false; }
|
||||
virtual uint16_t getId() {return USERMOD_ID_UNSPECIFIED;}
|
||||
@ -211,7 +211,7 @@ class UsermodManager {
|
||||
void readFromJsonState(JsonObject& obj);
|
||||
|
||||
void addToConfig(JsonObject& obj);
|
||||
void readFromConfig(JsonObject& obj);
|
||||
bool readFromConfig(JsonObject& obj);
|
||||
void onMqttConnect(bool sessionPresent);
|
||||
bool onMqttMessage(char* topic, char* payload);
|
||||
bool add(Usermod* um);
|
||||
|
@ -55,7 +55,7 @@ Please do not close or refresh the page :)</div></body></html>)=====";
|
||||
const char PAGE_welcome[] PROGMEM = R"=====(<!DOCTYPE html><html><head><meta charset="utf-8"><meta
|
||||
content="width=device-width" name="viewport"><meta name="theme-color"
|
||||
content="#222222"><title>Welcome!</title><style>
|
||||
body{font-family:Verdana,Helvetica,sans-serif;text-align:center;background-color:#222;margin:0;color:#fff}button{outline:0;cursor:pointer;padding:8px;margin:10px;width:230px;text-transform:uppercase;font-family:helvetica;font-size:19px;background-color:#333;color:#fff;border:0 solid #fff;border-radius:25px;filter:drop-shadow(0 0 1px #000)}img{width:950px;max-width:82%;image-rendering:pixelated;image-rendering:crisp-edges;margin:4vh 0 0 0;animation:fi 1s}@keyframes fi{from{opacity:0}to{opacity:1}}.main{animation:fi 1.5s .7s both}
|
||||
body{font-family:Verdana,Helvetica,sans-serif;text-align:center;background-color:#222;margin:0;color:#fff}button{outline:0;cursor:pointer;padding:8px;margin:10px;width:230px;text-transform:uppercase;font-family:helvetica;font-size:19px;background-color:#333;color:#fff;border:0 solid #fff;border-radius:25px}img{width:950px;max-width:82%;image-rendering:pixelated;image-rendering:crisp-edges;margin:4vh 0 0 0;animation:fi 1s}@keyframes fi{from{opacity:0}to{opacity:1}}.main{animation:fi 1.5s .7s both}
|
||||
</style></head><body><img alt=""
|
||||
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG0AAAAfCAMAAADazLOuAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAABLUExURQAAAAB81gCU/zKq///mo7sWMN8bO+ZIYtZaAP9rAP+HMsCiG+TAIOnMS0KqNU7KPnLUZOrq6v///4CAgGhoaL+/v6CgoExMTAAAAAlm4O8AAAAZdFJOU////////////////////////////////wABNAq3AAAACXBIWXMAAA7DAAAOwwHHb6hkAAACN0lEQVRIS73VjVLCMBAEYIr8CYKkrdj3f1J37zaXFCpTO+piaDgbPq9px9VQ0qyrvKj4q6m0Zr1h+M7xF1zRmnWzqV9/0d2jttGotO1uv9dUObwej5oqp7fzWVPl8n69aprzoOUUbbvdIbV3OLwitXc6vSG1d7m8I3feSEN0j2CeNbOY4MxigjOLCc4sZsTV2l1cCyy4wIILLLjAxtykltq2rbTU+qi01N5rXNO2leaFORoija2l5MM5a02ac9Ya16Sk5tgaPrUpjZub0BL6YqSxKwbH77XUUmSkJXSl8QtaMuyJhq5maL5nTKVpZC13VmtMpTFT2g4vJjTuGfMzzXftiUZnhdtgb1xofvypRon5TjNnxYN9zJo6K5ruSIzQtGuVZn0x91rKvdHBvm39E7SyZ4y06Gz8BDBFKzsXmhcwyfsGZ9VpbhoiCinaxPNmGWmWWrNU2jB0q6HvOhN1JUtCixQtp2g51ZVUXIPS2RMAD++T2nY/DrDjOMDO4wC7jmNYj3d73nrXug8Yt9uNB8xNU1cKNXWlUFNXCjV1pZhGTE83m2vWfYf/NGj4Bg1zu5JD3/MnH5ZWfLOksbmGWGjgXMN5/C2GXYGFFW9Nmtle6Xut0Gm+JsayCj8z0nhjGvYJzVf4aSzmNYsr+u7Q2JIdoX3YOQjOslmsW1jJ3120nE9gfo79hTaNdcsqVR610lvO47pllae9ReZ805zKo2a3iaY5c75pTmVCA6dJ5H7N0sr/asPwBehb7ifEhusRAAAAAElFTkSuQmCC">
|
||||
<div class="main"><h1>Welcome to WLED!</h1><h3>
|
||||
|
@ -13,7 +13,13 @@ void UsermodManager::addToJsonState(JsonObject& obj) { for (byte i = 0; i < n
|
||||
void UsermodManager::addToJsonInfo(JsonObject& obj) { for (byte i = 0; i < numMods; i++) ums[i]->addToJsonInfo(obj); }
|
||||
void UsermodManager::readFromJsonState(JsonObject& obj) { for (byte i = 0; i < numMods; i++) ums[i]->readFromJsonState(obj); }
|
||||
void UsermodManager::addToConfig(JsonObject& obj) { for (byte i = 0; i < numMods; i++) ums[i]->addToConfig(obj); }
|
||||
void UsermodManager::readFromConfig(JsonObject& obj) { for (byte i = 0; i < numMods; i++) ums[i]->readFromConfig(obj); }
|
||||
bool UsermodManager::readFromConfig(JsonObject& obj) {
|
||||
bool allComplete = true;
|
||||
for (byte i = 0; i < numMods; i++) {
|
||||
if (!ums[i]->readFromConfig(obj)) allComplete = false;
|
||||
}
|
||||
return allComplete;
|
||||
}
|
||||
void UsermodManager::onMqttConnect(bool sessionPresent) { for (byte i = 0; i < numMods; i++) ums[i]->onMqttConnect(sessionPresent); }
|
||||
bool UsermodManager::onMqttMessage(char* topic, char* payload) {
|
||||
for (byte i = 0; i < numMods; i++) if (ums[i]->onMqttMessage(topic, payload)) return true;
|
||||
|
@ -9,7 +9,8 @@
|
||||
* || || ||
|
||||
* \/ \/ \/
|
||||
*/
|
||||
//#include "usermod_v2_example.h"
|
||||
#include "../usermods/EXAMPLE_v2/usermod_v2_example.h"
|
||||
|
||||
#ifdef USERMOD_DALLASTEMPERATURE
|
||||
#include "../usermods/Temperature/usermod_temperature.h"
|
||||
#endif
|
||||
@ -80,7 +81,7 @@ void registerUsermods()
|
||||
* || || ||
|
||||
* \/ \/ \/
|
||||
*/
|
||||
//usermods.add(new MyExampleUsermod());
|
||||
usermods.add(new MyExampleUsermod());
|
||||
|
||||
#ifdef USERMOD_DALLASTEMPERATURE
|
||||
usermods.add(new UsermodTemperature());
|
||||
|
@ -479,5 +479,9 @@ void deEEPSettings() {
|
||||
loadSettingsFromEEPROM();
|
||||
EEPROM.end();
|
||||
|
||||
//call readFromConfig() with an empty object so that usermods can initialize to defaults prior to saving
|
||||
JsonObject empty = JsonObject();
|
||||
usermods.readFromConfig(empty);
|
||||
|
||||
serializeConfig();
|
||||
}
|
Loading…
Reference in New Issue
Block a user