diff --git a/src/test/test.cpp b/src/test/test.cpp index d36b89b45..b19f732d2 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -320,9 +320,10 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const // add devices test("general"); - EMSESP::temperaturesensor_.test(); // add temperature sensors - EMSESP::webSchedulerService.test(); // add scheduler items - EMSESP::webCustomEntityService.test(); // add custom entities + EMSESP::webCustomEntityService.test(); // add custom entities + EMSESP::webCustomizationService.test(); // set customizations - this will overwrite any settings in the FS + EMSESP::temperaturesensor_.test(); // add temperature sensors + EMSESP::webSchedulerService.test(); // add scheduler items // shell.invoke_command("show devices"); // shell.invoke_command("show values"); diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index 2093d37e1..2be883a8a 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -335,7 +335,8 @@ bool WebSchedulerService::command(const char * name, const char * cmd, const cha // tasmota(get): http:///cm?cmnd=power%20ON // shelly(get): http:///relais/0?turn=on const char * c = strchr(cmd, '{'); - if (c) { // parse json + if (c) { + // parse json JsonDocument doc; int httpResult = 0; if (DeserializationError::Ok == deserializeJson(doc, c)) { @@ -375,6 +376,11 @@ bool WebSchedulerService::command(const char * name, const char * cmd, const cha emsesp::EMSESP::logger().warning(error); return false; } +#if defined(EMSESP_DEBUG) + char msg[100]; + snprintf(msg, sizeof(msg), "Schedule %s: URL command successful with http code %d", name, httpResult); + emsesp::EMSESP::logger().debug(msg); +#endif return true; } } @@ -554,38 +560,43 @@ void WebSchedulerService::test() { // test with negative value // should output 'rssi is -23' test_value = "\"rssi is \"0+system/network/rssi"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test1", test_cmd.c_str(), compute(test_value).c_str()); // should output 'rssi is -23 dbm' test_value = "\"rssi is \"(system/network/rssi)\" dBm\""; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test2", test_cmd.c_str(), compute(test_value).c_str()); test_value = "(custom/seltemp/value)"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test3", test_cmd.c_str(), compute(test_value).c_str()); test_value = "\"seltemp=\"(custom/seltemp/value)"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test4", test_cmd.c_str(), compute(test_value).c_str()); test_value = "(custom/seltemp)"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test5", test_cmd.c_str(), compute(test_value).c_str()); + // this will fail unless test("boiler") is loaded test_value = "(boiler/outdoortemp)"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test6", test_cmd.c_str(), compute(test_value).c_str()); test_value = "boiler/flowtempoffset"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test7", test_cmd.c_str(), compute(test_value).c_str()); test_value = "(boiler/flowtempoffset/value)"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test8", test_cmd.c_str(), compute(test_value).c_str()); test_value = "(boiler/storagetemp1/value)"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test9", test_cmd.c_str(), compute(test_value).c_str()); // (14 - 40) * 2.8 + 5 = -67.8 test_value = "(custom/seltemp - boiler/flowtempoffset) * 2.8 + 5"; - command("test", test_cmd.c_str(), compute(test_value).c_str()); + command("test10", test_cmd.c_str(), compute(test_value).c_str()); // TODO add some HTTP/URI tests + test_cmd = "{\"method\":\"POST\",\"url\":\"http://192.168.1.42:8123/api/services/script/test_notify2\", \"header\":{\"authorization\":\"Bearer " + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhMmNlYWI5NDgzMmI0ODE2YWQ2NzU4MjkzZDE2YWMxZSIsImlhdCI6MTcyMTM5MTI0NCwiZXhwIjoyMDM2NzUxMjQ0fQ." + "S5sago1tEI6lNhrDCO0dM_WsVQHkD_laAjcks8tWAqo\"}}"; + command("test11", test_cmd.c_str(), ""); } #endif diff --git a/src/web/shuntingYard.hpp b/src/web/shuntingYard.hpp index ab394426d..f0dd629b9 100644 --- a/src/web/shuntingYard.hpp +++ b/src/web/shuntingYard.hpp @@ -618,12 +618,25 @@ std::string compute(const std::string & expr) { for (JsonPair p : doc["header"].as()) { http.addHeader(p.key().c_str(), p.value().as().c_str()); } - String data = doc["value"] | ""; - if (data.length()) { - httpResult = http.POST(data); + String value = doc["value"] | ""; + String method = doc["method"] | "GET"; // default GET + + // if there is data, force a POST + if (value.length()) { + if (value.startsWith("{")) { + http.addHeader("Content-Type", "application/json"); // auto-set to JSON + } + httpResult = http.POST(value); } else { - httpResult = http.GET(); + // no value, but check if it still a POST request + if (method == "POST") { + httpResult = http.POST(value); + } else { + httpResult = http.GET(); // normal GET + } } + http.end(); + if (httpResult > 0) { std::string result = emsesp::Helpers::toLower(http.getString().c_str()); String key = doc["key"] | ""; @@ -633,7 +646,6 @@ std::string compute(const std::string & expr) { } expr_new.replace(f, e - f, result.c_str()); } - http.end(); } } f = expr_new.find_first_of("{", e);