add test for URI in scheduler

This commit is contained in:
proddy
2024-07-20 16:31:40 +02:00
parent 87548f9322
commit 74c4940971
3 changed files with 43 additions and 19 deletions

View File

@@ -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");

View File

@@ -335,7 +335,8 @@ bool WebSchedulerService::command(const char * name, const char * cmd, const cha
// tasmota(get): http://<tasmotsIP>/cm?cmnd=power%20ON
// shelly(get): http://<shellyIP>/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

View File

@@ -618,12 +618,25 @@ std::string compute(const std::string & expr) {
for (JsonPair p : doc["header"].as<JsonObject>()) {
http.addHeader(p.key().c_str(), p.value().as<std::string>().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);