Autosave enable/disable UI button

This commit is contained in:
Blaz Kristan 2022-11-20 15:50:42 +01:00
parent 43582b6319
commit caef289b9b

View File

@ -91,6 +91,10 @@ class AutoSaveUsermod : public Usermod {
#endif
}
void enable(bool enable) {
enabled = enable;
}
public:
// gets called once at boot. Do all initialization that doesn't depend on
@ -155,12 +159,24 @@ class AutoSaveUsermod : public Usermod {
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
* Below it is shown how this could be used for e.g. a light sensor
*/
//void addToJsonInfo(JsonObject& root) {
//JsonObject user = root["u"];
//if (user.isNull()) user = root.createNestedObject("u");
//JsonArray data = user.createNestedArray(F("Autosave"));
//data.add(F("Loaded."));
//}
void addToJsonInfo(JsonObject& root) {
JsonObject user = root["u"];
if (user.isNull()) {
user = root.createNestedObject("u");
}
JsonArray infoArr = user.createNestedArray(FPSTR(_name)); // name
String uiDomString = F("<button class=\"btn btn-xs\" onclick=\"requestJson({");
uiDomString += FPSTR(_name);
uiDomString += F(":{");
uiDomString += FPSTR(_autoSaveEnabled);
uiDomString += enabled ? F(":false}});\">") : F(":true}});\">");
uiDomString += F("<i class=\"icons ");
uiDomString += enabled ? "on" : "off";
uiDomString += F("\">&#xe08f;</i></button>");
infoArr.add(uiDomString);
}
/*
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
@ -173,9 +189,20 @@ class AutoSaveUsermod : public Usermod {
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
* Values in the state object may be modified by connected clients
*/
//void readFromJsonState(JsonObject& root) {
// if (!initDone) return; // prevent crash on boot applyPreset()
//}
void readFromJsonState(JsonObject& root) {
if (!initDone) return; // prevent crash on boot applyPreset()
bool en = enabled;
JsonObject um = root[FPSTR(_name)];
if (!um.isNull()) {
if (um[FPSTR(_autoSaveEnabled)].is<bool>()) {
en = um[FPSTR(_autoSaveEnabled)].as<bool>();
} else {
String str = um[FPSTR(_autoSaveEnabled)]; // checkbox -> off or on
en = (bool)(str!="off"); // off is guaranteed to be present
}
if (en != enabled) enable(en);
}
}
/*
* addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object.