From 230af488992b978fb96b28ca3f61bea5791b2fbb Mon Sep 17 00:00:00 2001 From: Broersma Date: Tue, 28 Nov 2023 12:04:41 +0100 Subject: [PATCH] Fixing parsing only VALID JSON, in the past, when invalid JSON, the WLED runtime would crash --- .../usermod_v2_HttpPullLightControl.cpp | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/usermods/usermod_v2_HttpPullLightControl/usermod_v2_HttpPullLightControl.cpp b/usermods/usermod_v2_HttpPullLightControl/usermod_v2_HttpPullLightControl.cpp index 5cd89e77c..99962611a 100644 --- a/usermods/usermod_v2_HttpPullLightControl/usermod_v2_HttpPullLightControl.cpp +++ b/usermods/usermod_v2_HttpPullLightControl/usermod_v2_HttpPullLightControl.cpp @@ -133,6 +133,9 @@ void HttpPullLightControl::addToConfig(JsonObject& root) { // Write the configuration parameters to the nested object top[FPSTR(_enabled)] = enabled; + if (enabled==false) + // To make it a bit more user-friendly, we unfreeze the main segment after disabling the module. Because individual light control (like for a christmas card) might have been done. + strip.getMainSegment().freeze=false; top["checkInterval"] = checkInterval; #ifndef HTTP_PULL_LIGHT_CONTROL_HIDE_URL top["url"] = url; @@ -281,25 +284,26 @@ void HttpPullLightControl::handleResponse(String& responseStr) { DEBUG_PRINTLN("Response: "); DEBUG_PRINTLN(jsonStr); - // Attempt to deserialize the JSON response - DeserializationError error = deserializeJson(doc, jsonStr); - if (error) { - // If there is an error in deserialization, exit the function - DEBUG_PRINT(F("DeserializationError: ")); - DEBUG_PRINTLN(error.c_str()); - return; + // Check for valid JSON, otherwise we brick the program runtime + if (jsonStr[0] == '{' || jsonStr[0] == '[') { + // Attempt to deserialize the JSON response + DeserializationError error = deserializeJson(doc, jsonStr); + if (error == DeserializationError::Ok) { + // Get JSON object from th doc + JsonObject obj = doc.as(); + // Parse the object throuhg deserializeState (use CALL_MODE_NO_NOTIFY or OR CALL_MODE_DIRECT_CHANGE) + deserializeState(obj, CALL_MODE_NO_NOTIFY); + } else { + // If there is an error in deserialization, exit the function + DEBUG_PRINT(F("DeserializationError: ")); + DEBUG_PRINTLN(error.c_str()); + } + } else { + DEBUG_PRINTLN(F("Invalid JSON response")); } } else { DEBUG_PRINTLN(F("No body found in the response")); - return; } - - // Get JSON object from th doc - JsonObject obj = doc.as(); - - // Parse the object throuhg deserializeState (use CALL_MODE_NO_NOTIFY or OR CALL_MODE_DIRECT_CHANGE) - deserializeState(obj, CALL_MODE_NO_NOTIFY); - // Release the BufferLock again releaseJSONBufferLock(); } \ No newline at end of file