Merge pull request #1912 from MichaelDvP/dev

scheduler: change to std::string
This commit is contained in:
Proddy
2024-08-02 08:30:30 +02:00
committed by GitHub
4 changed files with 27 additions and 26 deletions

View File

@@ -39,7 +39,7 @@ unbuild_flags =
[espressi32_base] [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 ; 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 framework = arduino
board_build.filesystem = littlefs board_build.filesystem = littlefs
build_flags = build_flags =

View File

@@ -541,14 +541,14 @@ static void setup_commands(std::shared_ptr<Commands> & commands) {
cmd = Command::parse_command_string(cmd, id); // extract hc or dhw cmd = Command::parse_command_string(cmd, id); // extract hc or dhw
} }
if (cmd == nullptr) { if (cmd == nullptr) {
cmd = device_type == EMSdevice::DeviceType::SYSTEM ? F_(info) : F_(values); cmd = F_(values);
} }
if (arguments.size() == 2) { if (arguments.size() == 2) {
// no value specified, just the cmd // no value specified, just the cmd
return_code = Command::call(device_type, cmd, nullptr, true, id, json); return_code = Command::call(device_type, cmd, nullptr, true, id, json);
} else if (arguments.size() == 3) { } 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 // info has a id but no value
return_code = Command::call(device_type, cmd, nullptr, true, atoi(arguments.back().c_str()), json); return_code = Command::call(device_type, cmd, nullptr, true, atoi(arguments.back().c_str()), json);
} else if (arguments[2] == "?") { } else if (arguments[2] == "?") {

View File

@@ -334,31 +334,32 @@ bool WebSchedulerService::command(const char * name, const std::string & command
// parse json // parse json
JsonDocument doc; JsonDocument doc;
if (deserializeJson(doc, cmd) == DeserializationError::Ok) { if (deserializeJson(doc, cmd) == DeserializationError::Ok) {
HTTPClient http; HTTPClient http;
int httpResult = 0; int httpResult = 0;
String url = doc["url"] | ""; std::string url = doc["url"] | "";
// for a GET with parameters replace commands with values // for a GET with parameters replace commands with values
auto q = url.indexOf('?'); // don't search the complete url, it may contain a devicename in path
if (q != -1) { auto q = url.find_first_of('?');
auto s = url.substring(q + 1); if (q != std::string::npos) {
std::string v = s.c_str(); auto s = url.substr(q + 1); // copy only parameters
commands(v, false); auto l = s.length();
url.replace(s, v.c_str()); 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 // add any given headers
for (JsonPair p : doc["header"].as<JsonObject>()) { for (JsonPair p : doc["header"].as<JsonObject>()) {
http.addHeader(p.key().c_str(), p.value().as<String>().c_str()); http.addHeader(p.key().c_str(), p.value().as<String>().c_str());
} }
String value = doc["value"] | data.c_str(); // extract value if its in the command, or take the data std::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 method = doc["method"] | "GET"; // default GET
// if there is data, force a POST // if there is data, force a POST
if (value.length() || method == "post") { // we have all lowercase 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 http.addHeader("Content-Type", "application/json"); // auto-set to JSON
} }
httpResult = http.POST(value); httpResult = http.POST(value.c_str());
} else { } else {
httpResult = http.GET(); // normal GET httpResult = http.GET(); // normal GET
} }

View File

@@ -612,29 +612,29 @@ std::string compute(const std::string & expr) {
std::string cmd = expr_new.substr(f, e - f).c_str(); std::string cmd = expr_new.substr(f, e - f).c_str();
JsonDocument doc; JsonDocument doc;
if (DeserializationError::Ok == deserializeJson(doc, cmd)) { if (DeserializationError::Ok == deserializeJson(doc, cmd)) {
HTTPClient http; HTTPClient http;
String url = doc["url"] | ""; std::string url = doc["url"] | "";
if (url.startsWith("http") && http.begin(url)) { if (!url.find("http") && http.begin(url.c_str())) {
int httpResult = 0; int httpResult = 0;
for (JsonPair p : doc["header"].as<JsonObject>()) { for (JsonPair p : doc["header"].as<JsonObject>()) {
http.addHeader(p.key().c_str(), p.value().as<std::string>().c_str()); http.addHeader(p.key().c_str(), p.value().as<std::string>().c_str());
} }
String value = doc["value"] | ""; std::string value = doc["value"] | "";
String method = doc["method"] | "GET"; // default GET std::string method = doc["method"] | "GET"; // default GET
// if there is data, force a POST // if there is data, force a POST
if (value.length() || method == "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 http.addHeader("Content-Type", "application/json"); // auto-set to JSON
} }
httpResult = http.POST(value); httpResult = http.POST(value.c_str());
} else { } else {
httpResult = http.GET(); // normal GET httpResult = http.GET(); // normal GET
} }
if (httpResult > 0) { if (httpResult > 0) {
std::string result = emsesp::Helpers::toLower(http.getString().c_str()); std::string result = emsesp::Helpers::toLower(http.getString().c_str());
String key = doc["key"] | ""; std::string key = doc["key"] | "";
doc.clear(); doc.clear();
if (key.length() && DeserializationError::Ok == deserializeJson(doc, result)) { if (key.length() && DeserializationError::Ok == deserializeJson(doc, result)) {
result = doc[key.c_str()].as<std::string>(); result = doc[key.c_str()].as<std::string>();
@@ -644,7 +644,7 @@ std::string compute(const std::string & expr) {
http.end(); http.end();
} }
} }
f = expr_new.find_first_of("{", e); f = expr_new.find_first_of('{', e);
} }
// positions: q-questionmark, c-colon // positions: q-questionmark, c-colon