Button added to Info panel. Logic needs work.

This commit is contained in:
Troy
2024-03-30 12:30:32 -04:00
parent 04a8681f71
commit c36adbdad0

View File

@@ -5,7 +5,8 @@
class AutoPlaylistUsermod : public Usermod { class AutoPlaylistUsermod : public Usermod {
private: private:
bool initDone = false;
bool silenceDetected = true; bool silenceDetected = true;
uint32_t lastSoundTime = 0; uint32_t lastSoundTime = 0;
byte ambientPlaylist = 1; byte ambientPlaylist = 1;
@@ -44,7 +45,8 @@ class AutoPlaylistUsermod : public Usermod {
std::vector<int> autoChangeIds; std::vector<int> autoChangeIds;
static const char _enabled[]; static const char _name[];
static const char _autoPlaylistEnabled[];
static const char _ambientPlaylist[]; static const char _ambientPlaylist[];
static const char _musicPlaylist[]; static const char _musicPlaylist[];
static const char _timeout[]; static const char _timeout[];
@@ -63,6 +65,7 @@ class AutoPlaylistUsermod : public Usermod {
// network here // network here
void setup() { void setup() {
USER_PRINTLN("AutoPlaylistUsermod"); USER_PRINTLN("AutoPlaylistUsermod");
initDone = true;
} }
// gets called every time WiFi is (re-)connected. Initialize own network // gets called every time WiFi is (re-)connected. Initialize own network
@@ -264,48 +267,46 @@ class AutoPlaylistUsermod : public Usermod {
} }
/* /*
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API. * addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI. * Values in the state object may be modified by connected clients
* Below it is shown how this could be used for e.g. a light sensor
*/ */
void addToJsonInfo(JsonObject& root) { void addToJsonInfo(JsonObject& root) {
JsonObject user = root["u"]; JsonObject user = root["u"];
if (user.isNull()) { if (user.isNull()) {
user = root.createNestedObject("u"); user = root.createNestedObject("u");
} }
JsonArray infoArr = user.createNestedArray(FPSTR(_name)); // name JsonArray infoArr = user.createNestedArray(FPSTR(_name)); // name
infoArr = user.createNestedArray(F("")); String uiDomString = F("<button class=\"btn btn-xs\" onclick=\"requestJson({");
if(!enabled) { uiDomString += FPSTR(_name);
infoArr.add(""); uiDomString += F(":{");
} uiDomString += FPSTR(_autoPlaylistEnabled);
else { uiDomString += enabled ? F(":false}});\">") : F(":true}});\">");
infoArr.add("Active"); 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).
* Values in the state object may be modified by connected clients
*/
//void addToJsonState(JsonObject& root) {
//}
/* /*
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object). * 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 * Values in the state object may be modified by connected clients
*/ */
void readFromJsonState(JsonObject& root) { void readFromJsonState(JsonObject& root) {
if (!initDone) return; // prevent crash on boot applyPreset() if (!initDone) return; // prevent crash on boot applyPreset()
bool en = enabled;
JsonObject um = root[FPSTR(_name)]; JsonObject um = root[FPSTR(_name)];
if (!um.isNull()) { if (!um.isNull()) {
if (um[FPSTR(_enabled)].is<bool>()) { if (um[FPSTR(_autoPlaylistEnabled)].is<bool>()) {
enabled = um[FPSTR(_enabled)].as<bool>(); en = um[FPSTR(_autoPlaylistEnabled)].as<bool>();
} else {
String str = um[FPSTR(_autoPlaylistEnabled)]; // checkbox -> off or on
en = (bool)(str!="off"); // off is guaranteed to be present
} }
if (en != enabled) enabled = en;
USER_PRINT("AutoPlaylist enabled = ");
USER_PRINTLN(en);
} }
} }
@@ -331,18 +332,18 @@ class AutoPlaylistUsermod : public Usermod {
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
top[FPSTR(_enabled)] = enabled; top[FPSTR(_autoPlaylistEnabled)] = enabled;
top[FPSTR(_timeout)] = timeout; top[FPSTR(_timeout)] = timeout;
top[FPSTR(_ambientPlaylist)] = ambientPlaylist; // usermodparam top[FPSTR(_ambientPlaylist)] = ambientPlaylist; // usermodparam
top[FPSTR(_musicPlaylist)] = musicPlaylist; // usermodparam top[FPSTR(_musicPlaylist)] = musicPlaylist; // usermodparam
top[FPSTR(_autoChange)] = autoChange; top[FPSTR(_autoChange)] = autoChange;
top[FPSTR(_change_lockout)] = change_lockout; top[FPSTR(_change_lockout)] = change_lockout;
top[FPSTR(_ideal_change_min)] = ideal_change_min; top[FPSTR(_ideal_change_min)] = ideal_change_min;
top[FPSTR(_ideal_change_max)] = ideal_change_max; top[FPSTR(_ideal_change_max)] = ideal_change_max;
lastAutoPlaylist = 0; lastAutoPlaylist = 0;
DEBUG_PRINTLN(F("AutoPlaylist config saved.")); USER_PRINTLN(F("AutoPlaylist config saved."));
} }
@@ -361,22 +362,22 @@ class AutoPlaylistUsermod : public Usermod {
JsonObject top = root[FPSTR(_name)]; JsonObject top = root[FPSTR(_name)];
if (top.isNull()) { if (top.isNull()) {
DEBUG_PRINT(FPSTR(_name)); USER_PRINT(FPSTR(_name));
DEBUG_PRINTLN(F(": No config found. (Using defaults.)")); USER_PRINTLN(F(": No config found. (Using defaults.)"));
return false; return false;
} }
DEBUG_PRINT(FPSTR(_name)); enabled = top[FPSTR(_autoPlaylistEnabled)] | enabled;
getJsonValue(top[_enabled], enabled); timeout = top[FPSTR(_timeout)] | timeout;
getJsonValue(top[_timeout], timeout); ambientPlaylist = top[FPSTR(_ambientPlaylist)] | ambientPlaylist;
getJsonValue(top[_ambientPlaylist], ambientPlaylist); musicPlaylist = top[FPSTR(_musicPlaylist)] | musicPlaylist;
getJsonValue(top[_musicPlaylist], musicPlaylist); autoChange = top[FPSTR(_autoChange)] | autoChange;
getJsonValue(top[_autoChange], autoChange); change_lockout = top[FPSTR(_change_lockout)] | change_lockout;
getJsonValue(top[_change_lockout], change_lockout); ideal_change_min = top[FPSTR(_ideal_change_min)] | ideal_change_min;
getJsonValue(top[_ideal_change_min], ideal_change_min); ideal_change_max = top[FPSTR(_ideal_change_max)] | ideal_change_max;
getJsonValue(top[_ideal_change_max], ideal_change_max);
DEBUG_PRINTLN(F(" config (re)loaded.")); USER_PRINT(FPSTR(_name));
USER_PRINTLN(F(" config (re)loaded."));
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features // use "return !top["newestParameter"].isNull();" when updating Usermod with new features
return true; return true;
@@ -403,11 +404,12 @@ class AutoPlaylistUsermod : public Usermod {
}; };
const char AutoPlaylistUsermod::_enabled[] PROGMEM = "enabled"; const char AutoPlaylistUsermod::_name[] PROGMEM = "AutoPlaylist";
const char AutoPlaylistUsermod::_ambientPlaylist[] PROGMEM = "ambientPlaylist"; const char AutoPlaylistUsermod::_autoPlaylistEnabled[] PROGMEM = "enabled";
const char AutoPlaylistUsermod::_musicPlaylist[] PROGMEM = "musicPlaylist"; const char AutoPlaylistUsermod::_ambientPlaylist[] PROGMEM = "ambientPlaylist";
const char AutoPlaylistUsermod::_timeout[] PROGMEM = "timeout"; const char AutoPlaylistUsermod::_musicPlaylist[] PROGMEM = "musicPlaylist";
const char AutoPlaylistUsermod::_autoChange[] PROGMEM = "autoChange"; const char AutoPlaylistUsermod::_timeout[] PROGMEM = "timeout";
const char AutoPlaylistUsermod::_change_lockout[] PROGMEM = "change_lockout"; const char AutoPlaylistUsermod::_autoChange[] PROGMEM = "autoChange";
const char AutoPlaylistUsermod::_ideal_change_min[] PROGMEM = "ideal_change_min"; const char AutoPlaylistUsermod::_change_lockout[] PROGMEM = "change_lockout";
const char AutoPlaylistUsermod::_ideal_change_max[] PROGMEM = "ideal_change_max"; const char AutoPlaylistUsermod::_ideal_change_min[] PROGMEM = "ideal_change_min";
const char AutoPlaylistUsermod::_ideal_change_max[] PROGMEM = "ideal_change_max";