From f417cb991dcbd45b40622e1504da71a274a4219c Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 2 Aug 2024 08:07:55 +0200 Subject: [PATCH 1/4] all devices api default to /values --- src/console.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index 33113a2a1..3d4dfb0c6 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -541,14 +541,14 @@ static void setup_commands(std::shared_ptr & commands) { cmd = Command::parse_command_string(cmd, id); // extract hc or dhw } if (cmd == nullptr) { - cmd = device_type == EMSdevice::DeviceType::SYSTEM ? F_(info) : F_(values); + cmd = F_(values); } if (arguments.size() == 2) { // no value specified, just the cmd return_code = Command::call(device_type, cmd, nullptr, true, id, json); } else if (arguments.size() == 3) { - if ((strncmp(cmd, F_(info), 4) == 0) || strncmp(cmd, F_(values), 6) == 0) { + if (!strcmp(cmd, F_(info)) || !strcmp(cmd, F_(values))) { // info has a id but no value return_code = Command::call(device_type, cmd, nullptr, true, atoi(arguments.back().c_str()), json); } else if (arguments[2] == "?") { From fb04bdf54c861af875f3c8f9f991783a8eac1622 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 2 Aug 2024 08:08:11 +0200 Subject: [PATCH 2/4] platform 6.8.1 --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 533cd25e0..a742abab0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -39,7 +39,7 @@ unbuild_flags = [espressi32_base] ; 6.7.0 = Arduino v2.0.16 (based on IDF v4.4.7). See https://github.com/platformio/platform-espressif32/releases/tag/v6.7.0 -platform = espressif32@6.8.0 +platform = espressif32@6.8.1 framework = arduino board_build.filesystem = littlefs build_flags = From 70e5b19223972bf095679340a21545b90b4fd12e Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 2 Aug 2024 08:20:10 +0200 Subject: [PATCH 3/4] scheduler: change to std::string --- src/web/WebSchedulerService.cpp | 29 +++++++++++++++-------------- src/web/shuntingYard.hpp | 18 +++++++++--------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index 48d17f1e2..de72da031 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -334,31 +334,32 @@ bool WebSchedulerService::command(const char * name, const std::string & command // parse json JsonDocument doc; if (deserializeJson(doc, cmd) == DeserializationError::Ok) { - HTTPClient http; - int httpResult = 0; - String url = doc["url"] | ""; + HTTPClient http; + int httpResult = 0; + std::string url = doc["url"] | ""; // for a GET with parameters replace commands with values - auto q = url.indexOf('?'); - if (q != -1) { - auto s = url.substring(q + 1); - std::string v = s.c_str(); - commands(v, false); - url.replace(s, v.c_str()); + // don't search the complete url, it may contain a devicename in path + auto q = url.find_first_of('?'); + if (q != std::string::npos) { + auto s = url.substr(q + 1); // copy only parameters + auto l = s.length(); + commands(s, false); + url.replace(q + 1, l, s); } - if (url.startsWith("http") && http.begin(url)) { + if (!url.find("http") && http.begin(url.c_str())) { // add any given headers for (JsonPair p : doc["header"].as()) { http.addHeader(p.key().c_str(), p.value().as().c_str()); } - String value = doc["value"] | data.c_str(); // extract value if its in the command, or take the data - String method = doc["method"] | "GET"; // default GET + std::string value = doc["value"] | data.c_str(); // extract value if its in the command, or take the data + std::string method = doc["method"] | "GET"; // default GET // if there is data, force a POST if (value.length() || method == "post") { // we have all lowercase - if (value.startsWith("{")) { + if (value.find_first_of('{') != std::string::npos) { http.addHeader("Content-Type", "application/json"); // auto-set to JSON } - httpResult = http.POST(value); + httpResult = http.POST(value.c_str()); } else { httpResult = http.GET(); // normal GET } diff --git a/src/web/shuntingYard.hpp b/src/web/shuntingYard.hpp index aa19ec813..46f5b2174 100644 --- a/src/web/shuntingYard.hpp +++ b/src/web/shuntingYard.hpp @@ -612,29 +612,29 @@ std::string compute(const std::string & expr) { std::string cmd = expr_new.substr(f, e - f).c_str(); JsonDocument doc; if (DeserializationError::Ok == deserializeJson(doc, cmd)) { - HTTPClient http; - String url = doc["url"] | ""; - if (url.startsWith("http") && http.begin(url)) { + HTTPClient http; + std::string url = doc["url"] | ""; + if (!url.find("http") && http.begin(url.c_str())) { int httpResult = 0; for (JsonPair p : doc["header"].as()) { http.addHeader(p.key().c_str(), p.value().as().c_str()); } - String value = doc["value"] | ""; - String method = doc["method"] | "GET"; // default GET + std::string value = doc["value"] | ""; + std::string method = doc["method"] | "GET"; // default GET // if there is data, force a POST if (value.length() || method == "post") { - if (value.startsWith("{")) { + if (value.find_first_of('{') != std::string::npos) { http.addHeader("Content-Type", "application/json"); // auto-set to JSON } - httpResult = http.POST(value); + httpResult = http.POST(value.c_str()); } else { httpResult = http.GET(); // normal GET } if (httpResult > 0) { std::string result = emsesp::Helpers::toLower(http.getString().c_str()); - String key = doc["key"] | ""; + std::string key = doc["key"] | ""; doc.clear(); if (key.length() && DeserializationError::Ok == deserializeJson(doc, result)) { result = doc[key.c_str()].as(); @@ -644,7 +644,7 @@ std::string compute(const std::string & expr) { http.end(); } } - f = expr_new.find_first_of("{", e); + f = expr_new.find_first_of('{', e); } // positions: q-questionmark, c-colon From cd992ff45744f6fddd97c93864cbfa0856841313 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Fri, 2 Aug 2024 08:42:32 +0200 Subject: [PATCH 4/4] fix sonar complains --- src/command.cpp | 2 +- src/system.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index b41cfb3c6..f0642d3de 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -286,7 +286,7 @@ const char * Command::parse_command_string(const char * command, int8_t & id) { // check if command contains an attribute const char * Command::get_attribute(const char * cmd) { - char * breakp = strchr(cmd, '/'); + char * breakp = (char *)strchr(cmd, '/'); if (breakp) { *breakp = '\0'; return breakp + 1; diff --git a/src/system.cpp b/src/system.cpp index 49f48d883..f6349cd80 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1315,7 +1315,7 @@ bool System::get_value_info(JsonObject output, const char * cmd) { if (p.value().is()) { // String prefix = p.key().c_str(); for (JsonPair p1 : p.value().as()) { - JsonObject entity = output[String(p.key().c_str()) + '.' + p1.key().c_str()].to(); + JsonObject entity = output[std::string(p.key().c_str()) + "." + p1.key().c_str()].to(); get_value_json(entity, p.key().c_str(), p1.key().c_str(), p1.value()); } } // else { // we don't have pairs in json root object @@ -1325,12 +1325,12 @@ bool System::get_value_info(JsonObject output, const char * cmd) { return true; } - char * val = strstr(cmd, "/value"); + char * val = (char *)strstr(cmd, "/value"); if (val) { *val = '\0'; } - char * slash = strchr(cmd, '/'); + char * slash = (char *)strchr(cmd, '/'); if (slash) { *slash = '\0'; slash++;