From cab2f91bc6d068ad950ae8174827c2541f629e5d Mon Sep 17 00:00:00 2001 From: Will Miles Date: Sun, 10 Nov 2024 18:36:56 -0500 Subject: [PATCH] handleHttpApi: Move web response to web context No need to even consider this for non-web requests. Move the request special case to that context as well. --- wled00/fcn_declare.h | 2 +- wled00/ir.cpp | 2 +- wled00/json.cpp | 2 +- wled00/mqtt.cpp | 2 +- wled00/presets.cpp | 2 +- wled00/remote.cpp | 2 +- wled00/set.cpp | 14 ++------------ wled00/udp.cpp | 2 +- wled00/wled_server.cpp | 10 +++++++++- 9 files changed, 18 insertions(+), 20 deletions(-) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 272228bfe..fbd0c3dde 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -235,7 +235,7 @@ void handleRemote(uint8_t *data, size_t len); //set.cpp bool isAsterisksOnly(const char* str, byte maxLen); void handleSettingsSet(AsyncWebServerRequest *request, byte subPage); -bool handleHttpApi(AsyncWebServerRequest *request, const String& req, bool apply=true); +bool handleHttpApi(const String& req, bool apply=true); //udp.cpp void notify(byte callMode, bool followUp=false); diff --git a/wled00/ir.cpp b/wled00/ir.cpp index 3a4957c41..e93f93b0c 100644 --- a/wled00/ir.cpp +++ b/wled00/ir.cpp @@ -606,7 +606,7 @@ static void decodeIRJson(uint32_t code) cmdStr += tmp; } fdo.clear(); // clear JSON buffer (it is no longer needed) - handleHttpApi(nullptr, cmdStr, false); // no stateUpdated() call here + handleHttpApi(cmdStr, false); // no stateUpdated() call here } } else { // command is JSON object (TODO: currently will not handle irApplyToAllSelected correctly) diff --git a/wled00/json.cpp b/wled00/json.cpp index 38ee231ec..57afcd65c 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -449,7 +449,7 @@ bool deserializeState(JsonObject root, byte callMode, byte presetId) // HTTP API commands (must be handled before "ps") const char* httpwin = root["win"]; if (httpwin) { - handleHttpApi(nullptr, httpwin, false); // may set stateChanged + handleHttpApi(httpwin, false); // may set stateChanged } // Applying preset from JSON API has 2 cases: a) "pd" AKA "preset direct" and b) "ps" AKA "preset select" diff --git a/wled00/mqtt.cpp b/wled00/mqtt.cpp index 01fc038fa..c8eee5160 100644 --- a/wled00/mqtt.cpp +++ b/wled00/mqtt.cpp @@ -107,7 +107,7 @@ static void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProp deserializeJson(*pDoc, payloadStr); deserializeState(pDoc->as()); } else { //HTTP API - handleHttpApi(nullptr, payloadStr); + handleHttpApi(payloadStr); } releaseJSONBufferLock(); } diff --git a/wled00/presets.cpp b/wled00/presets.cpp index a35a54513..acfb58144 100644 --- a/wled00/presets.cpp +++ b/wled00/presets.cpp @@ -180,7 +180,7 @@ void handlePresets() //HTTP API commands const char* httpwin = fdo["win"]; if (httpwin) { - handleHttpApi(nullptr, httpwin, false); // may call applyPreset() via PL= + handleHttpApi(httpwin, false); // may call applyPreset() via PL= setValuesFromFirstSelectedSeg(); // fills legacy values changePreset = true; } else { diff --git a/wled00/remote.cpp b/wled00/remote.cpp index e87166a0e..5c2fa4a37 100644 --- a/wled00/remote.cpp +++ b/wled00/remote.cpp @@ -160,7 +160,7 @@ static bool remoteJson(int button) cmdStr += tmp; } fdo.clear(); // clear JSON buffer (it is no longer needed) - handleHttpApi(nullptr, cmdStr, false); // no stateUpdated() call here + handleHttpApi(cmdStr, false); // no stateUpdated() call here stateUpdated(CALL_MODE_BUTTON); parsed = true; } diff --git a/wled00/set.cpp b/wled00/set.cpp index 23155c34d..27160b253 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -803,7 +803,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) //HTTP API request parser -bool handleHttpApi(AsyncWebServerRequest *request, const String& req, bool apply) +bool handleHttpApi(const String& req, bool apply) { int pos = 0; DEBUG_PRINTF_P(PSTR("API req: %s\n"), req.c_str()); @@ -1030,10 +1030,7 @@ bool handleHttpApi(AsyncWebServerRequest *request, const String& req, bool apply bool fxModeChanged = false, speedChanged = false, intensityChanged = false, paletteChanged = false; bool custom1Changed = false, custom2Changed = false, custom3Changed = false, check1Changed = false, check2Changed = false, check3Changed = false; // set effect parameters - if (updateVal(req.c_str(), "FX=", &effectIn, 0, strip.getModeCount()-1)) { - if (request != nullptr) unloadPlaylist(); // unload playlist if changing FX using web request - fxModeChanged = true; - } + fxModeChanged = updateVal(req.c_str(), "FX=", &effectIn, 0, strip.getModeCount()-1); speedChanged = updateVal(req.c_str(), "SX=", &speedIn); intensityChanged = updateVal(req.c_str(), "IX=", &intensityIn); paletteChanged = updateVal(req.c_str(), "FP=", &paletteIn, 0, strip.getPaletteCount()-1); @@ -1187,12 +1184,5 @@ bool handleHttpApi(AsyncWebServerRequest *request, const String& req, bool apply pos = req.indexOf(F("&NN")); //do not send UDP notifications this time stateUpdated((pos >= 0) ? CALL_MODE_NO_NOTIFY : CALL_MODE_DIRECT_CHANGE); - // internal call, does not send XML response - if (request != nullptr) { - auto response = request->beginResponseStream("text/xml"); - XML_response(*response); - request->send(response); - } - return true; } diff --git a/wled00/udp.cpp b/wled00/udp.cpp index 089cd82ce..edc97ef63 100644 --- a/wled00/udp.cpp +++ b/wled00/udp.cpp @@ -681,7 +681,7 @@ void handleNotifications() if (requestJSONBufferLock(18)) { if (udpIn[0] >= 'A' && udpIn[0] <= 'Z') { //HTTP API - handleHttpApi(nullptr, (char*)udpIn); + handleHttpApi((char*)udpIn); } else if (udpIn[0] == '{') { //JSON API DeserializationError error = deserializeJson(*pDoc, udpIn); JsonObject root = pDoc->as(); diff --git a/wled00/wled_server.cpp b/wled00/wled_server.cpp index 29ed6e205..ff5d895da 100644 --- a/wled00/wled_server.cpp +++ b/wled00/wled_server.cpp @@ -483,7 +483,15 @@ void initServer() } if (request->url().indexOf("win") == 0) { - if(handleHttpApi(request, request->url().substring(3))) return; + if (handleHttpApi(request->url().substring(3))) { + if (request->url().indexOf(F("&FX=")) > 0) { + unloadPlaylist(); // Setting an FX via HTTP disables any active playlist. + } + auto response = request->beginResponseStream(CONTENT_TYPE_XML); + XML_response(*response); + request->send(response); + return; + } } #ifndef WLED_DISABLE_ALEXA if(espalexa.handleAlexaApiCall(request)) return;