Skip to content

Commit e979c58

Browse files
authored
Merge pull request #4609 from willmmiles/usermod-cfg-live
Use live cfg json instead of file for usermod settings page
2 parents b0b3196 + 36cb1ca commit e979c58

File tree

11 files changed

+55
-54
lines changed

11 files changed

+55
-54
lines changed

wled00/cfg.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ void deserializeConfigFromFS() {
671671
// call readFromConfig() with an empty object so that usermods can initialize to defaults prior to saving
672672
JsonObject empty = JsonObject();
673673
UsermodManager::readFromConfig(empty);
674-
serializeConfig();
674+
serializeConfigToFS();
675675
// init Ethernet (in case default type is set at compile time)
676676
#if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_ETHERNET)
677677
initEthernet();
@@ -685,10 +685,10 @@ void deserializeConfigFromFS() {
685685
bool needsSave = deserializeConfig(root, true);
686686
releaseJSONBufferLock();
687687

688-
if (needsSave) serializeConfig(); // usermods required new parameters
688+
if (needsSave) serializeConfigToFS(); // usermods required new parameters
689689
}
690690

691-
void serializeConfig() {
691+
void serializeConfigToFS() {
692692
serializeConfigSec();
693693

694694
DEBUG_PRINTLN(F("Writing settings to /cfg.json..."));
@@ -697,6 +697,17 @@ void serializeConfig() {
697697

698698
JsonObject root = pDoc->to<JsonObject>();
699699

700+
serializeConfig(root);
701+
702+
File f = WLED_FS.open(FPSTR(s_cfg_json), "w");
703+
if (f) serializeJson(root, f);
704+
f.close();
705+
releaseJSONBufferLock();
706+
707+
configNeedsWrite = false;
708+
}
709+
710+
void serializeConfig(JsonObject root) {
700711
JsonArray rev = root.createNestedArray("rev");
701712
rev.add(1); //major settings revision
702713
rev.add(0); //minor settings revision
@@ -1111,13 +1122,6 @@ void serializeConfig() {
11111122

11121123
JsonObject usermods_settings = root.createNestedObject("um");
11131124
UsermodManager::addToConfig(usermods_settings);
1114-
1115-
File f = WLED_FS.open(FPSTR(s_cfg_json), "w");
1116-
if (f) serializeJson(root, f);
1117-
f.close();
1118-
releaseJSONBufferLock();
1119-
1120-
doSerializeConfig = false;
11211125
}
11221126

11231127

wled00/data/settings_um.htm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
function S() {
1414
getLoc();
1515
// load settings and insert values into DOM
16-
fetch(getURL('/cfg.json'), {
16+
fetch(getURL('/json/cfg'), {
1717
method: 'get'
1818
})
1919
.then(res => {

wled00/dmx_input.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void rdmPersonalityChangedCb(dmx_port_t dmxPort, const rdm_header_t *header,
2222
if (header->cc == RDM_CC_SET_COMMAND_RESPONSE) {
2323
const uint8_t personality = dmx_get_current_personality(dmx->inputPortNum);
2424
DMXMode = std::min(DMX_MODE_PRESET, std::max(DMX_MODE_SINGLE_RGB, int(personality)));
25-
doSerializeConfig = true;
25+
configNeedsWrite = true;
2626
DEBUG_PRINTF("DMX personality changed to to: %d\n", DMXMode);
2727
}
2828
}
@@ -40,7 +40,7 @@ void rdmAddressChangedCb(dmx_port_t dmxPort, const rdm_header_t *header,
4040
if (header->cc == RDM_CC_SET_COMMAND_RESPONSE) {
4141
const uint16_t addr = dmx_get_start_address(dmx->inputPortNum);
4242
DMXAddress = std::min(512, int(addr));
43-
doSerializeConfig = true;
43+
configNeedsWrite = true;
4444
DEBUG_PRINTF("DMX start addr changed to: %d\n", DMXAddress);
4545
}
4646
}

wled00/fcn_declare.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ void IRAM_ATTR touchButtonISR();
2727
bool deserializeConfig(JsonObject doc, bool fromFS = false);
2828
void deserializeConfigFromFS();
2929
bool deserializeConfigSec();
30-
void serializeConfig();
30+
void serializeConfig(JsonObject doc);
31+
void serializeConfigToFS();
3132
void serializeConfigSec();
3233

3334
template<typename DestType>

wled00/improv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,5 +272,5 @@ void parseWiFiCommand(char* rpcData) {
272272
improvActive = 2;
273273

274274
forceReconnect = true;
275-
serializeConfig();
275+
serializeConfigToFS();
276276
}

wled00/json.cpp

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
#include "palettes.h"
44

5-
#define JSON_PATH_STATE 1
6-
#define JSON_PATH_INFO 2
7-
#define JSON_PATH_STATE_INFO 3
8-
#define JSON_PATH_NODES 4
9-
#define JSON_PATH_PALETTES 5
10-
#define JSON_PATH_FXDATA 6
11-
#define JSON_PATH_NETWORKS 7
12-
#define JSON_PATH_EFFECTS 8
13-
145
/*
156
* JSON API (De)serialization
167
*/
@@ -1036,16 +1027,21 @@ class LockedJsonResponse: public AsyncJsonResponse {
10361027

10371028
void serveJson(AsyncWebServerRequest* request)
10381029
{
1039-
byte subJson = 0;
1030+
enum class json_target {
1031+
all, state, info, state_info, nodes, effects, palettes, fxdata, networks, config
1032+
};
1033+
json_target subJson = json_target::all;
1034+
10401035
const String& url = request->url();
1041-
if (url.indexOf("state") > 0) subJson = JSON_PATH_STATE;
1042-
else if (url.indexOf("info") > 0) subJson = JSON_PATH_INFO;
1043-
else if (url.indexOf("si") > 0) subJson = JSON_PATH_STATE_INFO;
1044-
else if (url.indexOf(F("nodes")) > 0) subJson = JSON_PATH_NODES;
1045-
else if (url.indexOf(F("eff")) > 0) subJson = JSON_PATH_EFFECTS;
1046-
else if (url.indexOf(F("palx")) > 0) subJson = JSON_PATH_PALETTES;
1047-
else if (url.indexOf(F("fxda")) > 0) subJson = JSON_PATH_FXDATA;
1048-
else if (url.indexOf(F("net")) > 0) subJson = JSON_PATH_NETWORKS;
1036+
if (url.indexOf("state") > 0) subJson = json_target::state;
1037+
else if (url.indexOf("info") > 0) subJson = json_target::info;
1038+
else if (url.indexOf("si") > 0) subJson = json_target::state_info;
1039+
else if (url.indexOf(F("nodes")) > 0) subJson = json_target::nodes;
1040+
else if (url.indexOf(F("eff")) > 0) subJson = json_target::effects;
1041+
else if (url.indexOf(F("palx")) > 0) subJson = json_target::palettes;
1042+
else if (url.indexOf(F("fxda")) > 0) subJson = json_target::fxdata;
1043+
else if (url.indexOf(F("net")) > 0) subJson = json_target::networks;
1044+
else if (url.indexOf(F("cfg")) > 0) subJson = json_target::config;
10491045
#ifdef WLED_ENABLE_JSONLIVE
10501046
else if (url.indexOf("live") > 0) {
10511047
serveLiveLeds(request);
@@ -1056,9 +1052,6 @@ void serveJson(AsyncWebServerRequest* request)
10561052
request->send_P(200, FPSTR(CONTENT_TYPE_JSON), JSON_palette_names);
10571053
return;
10581054
}
1059-
else if (url.indexOf(F("cfg")) > 0 && handleFileRead(request, F("/cfg.json"))) {
1060-
return;
1061-
}
10621055
else if (url.length() > 6) { //not just /json
10631056
serveJsonError(request, 501, ERR_NOT_IMPL);
10641057
return;
@@ -1070,32 +1063,35 @@ void serveJson(AsyncWebServerRequest* request)
10701063
}
10711064
// releaseJSONBufferLock() will be called when "response" is destroyed (from AsyncWebServer)
10721065
// make sure you delete "response" if no "request->send(response);" is made
1073-
LockedJsonResponse *response = new LockedJsonResponse(pDoc, subJson==JSON_PATH_FXDATA || subJson==JSON_PATH_EFFECTS); // will clear and convert JsonDocument into JsonArray if necessary
1066+
LockedJsonResponse *response = new LockedJsonResponse(pDoc, subJson==json_target::fxdata || subJson==json_target::effects); // will clear and convert JsonDocument into JsonArray if necessary
10741067

10751068
JsonVariant lDoc = response->getRoot();
10761069

10771070
switch (subJson)
10781071
{
1079-
case JSON_PATH_STATE:
1072+
case json_target::state:
10801073
serializeState(lDoc); break;
1081-
case JSON_PATH_INFO:
1074+
case json_target::info:
10821075
serializeInfo(lDoc); break;
1083-
case JSON_PATH_NODES:
1076+
case json_target::nodes:
10841077
serializeNodes(lDoc); break;
1085-
case JSON_PATH_PALETTES:
1078+
case json_target::palettes:
10861079
serializePalettes(lDoc, request->hasParam(F("page")) ? request->getParam(F("page"))->value().toInt() : 0); break;
1087-
case JSON_PATH_EFFECTS:
1080+
case json_target::effects:
10881081
serializeModeNames(lDoc); break;
1089-
case JSON_PATH_FXDATA:
1082+
case json_target::fxdata:
10901083
serializeModeData(lDoc); break;
1091-
case JSON_PATH_NETWORKS:
1084+
case json_target::networks:
10921085
serializeNetworks(lDoc); break;
1093-
default: //all
1086+
case json_target::config:
1087+
serializeConfig(lDoc); break;
1088+
case json_target::state_info:
1089+
case json_target::all:
10941090
JsonObject state = lDoc.createNestedObject("state");
10951091
serializeState(state);
10961092
JsonObject info = lDoc.createNestedObject("info");
10971093
serializeInfo(info);
1098-
if (subJson != JSON_PATH_STATE_INFO)
1094+
if (subJson == json_target::all)
10991095
{
11001096
JsonArray effects = lDoc.createNestedArray(F("effects"));
11011097
serializeModeNames(effects); // remove WLED-SR extensions from effect names

wled00/presets.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
242242
if (!sObj[FPSTR(bootPS)].isNull()) {
243243
bootPreset = sObj[FPSTR(bootPS)] | bootPreset;
244244
sObj.remove(FPSTR(bootPS));
245-
doSerializeConfig = true;
245+
configNeedsWrite = true;
246246
}
247247

248248
if (sObj.size()==0 || sObj["o"].isNull()) { // no "o" means not a playlist or custom API call, saving of state is async (not immediately)

wled00/set.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,8 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
805805

806806
lastEditTime = millis();
807807
// do not save if factory reset or LED settings (which are saved after LED re-init)
808-
doSerializeConfig = subPage != SUBPAGE_LEDS && !(subPage == SUBPAGE_SEC && doReboot);
809-
if (subPage == SUBPAGE_UM) doReboot = request->hasArg(F("RBT")); // prevent race condition on dual core system (set reboot here, after doSerializeConfig has been set)
808+
configNeedsWrite = subPage != SUBPAGE_LEDS && !(subPage == SUBPAGE_SEC && doReboot);
809+
if (subPage == SUBPAGE_UM) doReboot = request->hasArg(F("RBT")); // prevent race condition on dual core system (set reboot here, after configNeedsWrite has been set)
810810
#ifndef WLED_DISABLE_ALEXA
811811
if (subPage == SUBPAGE_SYNC) alexaInit();
812812
#endif

wled00/wled.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ void WLED::loop()
193193
if (aligned) strip.makeAutoSegments();
194194
else strip.fixInvalidSegments();
195195
BusManager::setBrightness(bri); // fix re-initialised bus' brightness
196-
doSerializeConfig = true;
196+
configNeedsWrite = true;
197197
}
198198
if (loadLedmap >= 0) {
199199
strip.deserializeMap(loadLedmap);
200200
loadLedmap = -1;
201201
}
202202
yield();
203-
if (doSerializeConfig) serializeConfig();
203+
if (configNeedsWrite) serializeConfigToFS();
204204

205205
yield();
206206
handleWs();
@@ -223,7 +223,7 @@ void WLED::loop()
223223
}
224224
#endif
225225

226-
if (doReboot && (!doInitBusses || !doSerializeConfig)) // if busses have to be inited & saved, wait until next iteration
226+
if (doReboot && (!doInitBusses || !configNeedsWrite)) // if busses have to be inited & saved, wait until next iteration
227227
reset();
228228

229229
// DEBUG serial logging (every 30s)

wled00/wled.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ WLED_GLOBAL byte errorFlag _INIT(0);
877877
WLED_GLOBAL String messageHead, messageSub;
878878
WLED_GLOBAL byte optionType;
879879

880-
WLED_GLOBAL bool doSerializeConfig _INIT(false); // flag to initiate saving of config
880+
WLED_GLOBAL bool configNeedsWrite _INIT(false); // flag to initiate saving of config
881881
WLED_GLOBAL bool doReboot _INIT(false); // flag to initiate reboot from async handlers
882882

883883
WLED_GLOBAL bool psramSafe _INIT(true); // is it safe to use PSRAM (on ESP32 rev.1; compiler fix used "-mfix-esp32-psram-cache-issue")

0 commit comments

Comments
 (0)