mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
@@ -320,9 +320,10 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
|
|||||||
// add devices
|
// add devices
|
||||||
test("general");
|
test("general");
|
||||||
|
|
||||||
EMSESP::temperaturesensor_.test(); // add temperature sensors
|
EMSESP::webCustomEntityService.test(); // add custom entities
|
||||||
EMSESP::webSchedulerService.test(); // add scheduler items
|
EMSESP::webCustomizationService.test(); // set customizations - this will overwrite any settings in the FS
|
||||||
EMSESP::webCustomEntityService.test(); // add custom entities
|
EMSESP::temperaturesensor_.test(); // add temperature sensors
|
||||||
|
EMSESP::webSchedulerService.test(); // add scheduler items
|
||||||
|
|
||||||
// shell.invoke_command("show devices");
|
// shell.invoke_command("show devices");
|
||||||
// shell.invoke_command("show values");
|
// shell.invoke_command("show values");
|
||||||
|
|||||||
@@ -335,7 +335,8 @@ bool WebSchedulerService::command(const char * name, const char * cmd, const cha
|
|||||||
// tasmota(get): http://<tasmotsIP>/cm?cmnd=power%20ON
|
// tasmota(get): http://<tasmotsIP>/cm?cmnd=power%20ON
|
||||||
// shelly(get): http://<shellyIP>/relais/0?turn=on
|
// shelly(get): http://<shellyIP>/relais/0?turn=on
|
||||||
const char * c = strchr(cmd, '{');
|
const char * c = strchr(cmd, '{');
|
||||||
if (c) { // parse json
|
if (c) {
|
||||||
|
// parse json
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
int httpResult = 0;
|
int httpResult = 0;
|
||||||
if (DeserializationError::Ok == deserializeJson(doc, c)) {
|
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);
|
emsesp::EMSESP::logger().warning(error);
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -554,38 +560,43 @@ void WebSchedulerService::test() {
|
|||||||
// test with negative value
|
// test with negative value
|
||||||
// should output 'rssi is -23'
|
// should output 'rssi is -23'
|
||||||
test_value = "\"rssi is \"0+system/network/rssi";
|
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'
|
// should output 'rssi is -23 dbm'
|
||||||
test_value = "\"rssi is \"(system/network/rssi)\" 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)";
|
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)";
|
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)";
|
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)";
|
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";
|
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)";
|
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)";
|
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
|
// (14 - 40) * 2.8 + 5 = -67.8
|
||||||
test_value = "(custom/seltemp - boiler/flowtempoffset) * 2.8 + 5";
|
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
|
// 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
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -618,12 +618,25 @@ std::string compute(const std::string & expr) {
|
|||||||
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 data = doc["value"] | "";
|
String value = doc["value"] | "";
|
||||||
if (data.length()) {
|
String method = doc["method"] | "GET"; // default GET
|
||||||
httpResult = http.POST(data);
|
|
||||||
|
// 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 {
|
} 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) {
|
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"] | "";
|
String key = doc["key"] | "";
|
||||||
@@ -633,7 +646,6 @@ std::string compute(const std::string & expr) {
|
|||||||
}
|
}
|
||||||
expr_new.replace(f, e - f, result.c_str());
|
expr_new.replace(f, e - f, result.c_str());
|
||||||
}
|
}
|
||||||
http.end();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f = expr_new.find_first_of("{", e);
|
f = expr_new.find_first_of("{", e);
|
||||||
|
|||||||
Reference in New Issue
Block a user