From 1f04c1fc2c54337954df28444e257fe0fbdec197 Mon Sep 17 00:00:00 2001 From: proddy Date: Sat, 16 Nov 2024 14:47:07 +0100 Subject: [PATCH] fix customSupport for ArduinoJson 7.2.1 --- interface/src/app/main/Help.tsx | 2 +- src/test/test.cpp | 19 +++++++++---------- src/web/WebStatusService.cpp | 29 ++++++++++++++++++++++------- src/web/WebStatusService.h | 2 +- test/test_data/custom_support.json | 18 ++++++++++++++++++ 5 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 test/test_data/custom_support.json diff --git a/interface/src/app/main/Help.tsx b/interface/src/app/main/Help.tsx index 593f79d08..932e4de38 100644 --- a/interface/src/app/main/Help.tsx +++ b/interface/src/app/main/Help.tsx @@ -39,7 +39,7 @@ const Help = () => { const [customSupportHTML, setCustomSupportHTML] = useState(null); const [notFound, setNotFound] = useState(false); - useRequest(() => callAction({ action: 'customSupport' })).onSuccess((event) => { + useRequest(() => callAction({ action: 'getCustomSupport' })).onSuccess((event) => { if (event && event.data && Object.keys(event.data).length !== 0) { const data = event.data.Support; if (data.img_url) { diff --git a/src/test/test.cpp b/src/test/test.cpp index 3c9bf294e..8c5eae5e9 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -1014,10 +1014,10 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const // request.url("/api"); // EMSESP::webAPIService.webAPIService(&request, doc.as()); - // char data2[] = "{\"action\":\"customSupport\", \"param\":\"hello\"}"; - // deserializeJson(doc, data2); - // request.url("/rest/action"); - // EMSESP::webStatusService.action(&request, doc.as()); + char data2[] = "{\"action\":\"getCustomSupport\", \"param\":\"hello\"}"; + deserializeJson(doc, data2); + request.url("/rest/action"); + EMSESP::webStatusService.action(&request, doc.as()); // char data3[] = "{\"action\":\"export\", \"param\":\"schedule\"}"; // deserializeJson(doc, data3); @@ -1031,11 +1031,11 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const // test version checks // test with "current_version_s = "3.7.1-dev.8" in WebStatusService::checkUpgrade() - request.url("/rest/action"); - deserializeJson(doc, "{\"action\":\"checkUpgrade\", \"param\":\"3.7.1-dev.9,3.7.0\"}"); // is upgradable - EMSESP::webStatusService.action(&request, doc.as()); - deserializeJson(doc, "{\"action\":\"checkUpgrade\", \"param\":\"3.7.1-dev.7,3.7.0\"}"); // is not upgradable - EMSESP::webStatusService.action(&request, doc.as()); + // request.url("/rest/action"); + // deserializeJson(doc, "{\"action\":\"checkUpgrade\", \"param\":\"3.7.1-dev.9,3.7.0\"}"); // is upgradable + // EMSESP::webStatusService.action(&request, doc.as()); + // deserializeJson(doc, "{\"action\":\"checkUpgrade\", \"param\":\"3.7.1-dev.7,3.7.0\"}"); // is not upgradable + // EMSESP::webStatusService.action(&request, doc.as()); // test with "current_version_s = "3.6.5" in WebStatusService::checkUpgrade() // request.url("/rest/action"); @@ -1044,7 +1044,6 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const // deserializeJson(doc, "{\"action\":\"checkUpgrade\", \"param\":\"3.7.1-dev.7,3.7.0\"}"); // is upgradable // EMSESP::webStatusService.action(&request, doc.as()); - // char data6[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"8 2 27 1\"}"; // deserializeJson(doc, data6); // json = doc.as(); diff --git a/src/web/WebStatusService.cpp b/src/web/WebStatusService.cpp index e9e4a3b4c..6b6b84ac2 100644 --- a/src/web/WebStatusService.cpp +++ b/src/web/WebStatusService.cpp @@ -179,8 +179,8 @@ void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json) if (has_param) { ok = exportData(root, param); } - } else if (action == "customSupport") { - ok = customSupport(root); + } else if (action == "getCustomSupport") { + ok = getCustomSupport(root); } else if (action == "uploadURL" && is_admin) { ok = uploadURL(param.c_str()); } @@ -301,19 +301,27 @@ bool WebStatusService::exportData(JsonObject root, std::string & type) { return true; } -// action = customSupport +// action = getCustomSupport // reads any upload customSupport.json file and sends to to Help page to be shown as Guest -bool WebStatusService::customSupport(JsonObject root) { -#ifndef EMSESP_STANDALONE +bool WebStatusService::getCustomSupport(JsonObject root) { + JsonDocument doc; + +#if defined(EMSESP_STANDALONE) + // dummy test data for "test api3" + deserializeJson(doc, "{\"type\":\"customSupport\",\"Support\":{\"html\":[\"html code\",\"here\"], \"img_url\": \"https://docs.emsesp.org/_media/images/designer.png\"}"); +#else // check if we have custom support file uploaded File file = LittleFS.open(EMSESP_CUSTOMSUPPORT_FILE, "r"); if (!file) { // there is no custom file, return empty object +#if defined(EMSESP_DEBUG) + emsesp::EMSESP::logger().debug("No custom support file found"); +#endif return true; } - // read the contents of the file into the root output json object - DeserializationError error = deserializeJson(root, file); + // read the contents of the file into a json doc. We can't do this direct to object since 7.2.1 + DeserializationError error = deserializeJson(doc, file); if (error) { emsesp::EMSESP::logger().err("Failed to read custom support file"); return false; @@ -321,6 +329,13 @@ bool WebStatusService::customSupport(JsonObject root) { file.close(); #endif + +#if defined(EMSESP_DEBUG) + emsesp::EMSESP::logger().debug("Showing custom support page"); +#endif + + root.set(doc.as()); // add to web response root object + return true; } diff --git a/src/web/WebStatusService.h b/src/web/WebStatusService.h index 703464589..6c3c0c6c2 100644 --- a/src/web/WebStatusService.h +++ b/src/web/WebStatusService.h @@ -25,7 +25,7 @@ class WebStatusService { // actions bool checkUpgrade(JsonObject root, std::string & latest_version); bool exportData(JsonObject root, std::string & type); - bool customSupport(JsonObject root); + bool getCustomSupport(JsonObject root); bool uploadURL(const char * url); void allvalues(JsonObject output); diff --git a/test/test_data/custom_support.json b/test/test_data/custom_support.json new file mode 100644 index 000000000..882851445 --- /dev/null +++ b/test/test_data/custom_support.json @@ -0,0 +1,18 @@ +{ + "type": "customSupport", + "Support": { + "html": [ + "This product is installed and managed by:", + "", + "Bosch Installer Example", + "", + "Nefit Road 12", + "1234 AB Amsterdam", + "Phone: +31 123 456 789", + "email: support@boschinstaller.nl", + "", + "For help and questions please contact your installer." + ], + "img_url": "https://docs.emsesp.org/_media/images/designer.png" + } +} \ No newline at end of file