From d7ba3604835bf8a87cc3e54337741751ba3ef2c1 Mon Sep 17 00:00:00 2001 From: proddy Date: Thu, 11 Jul 2024 16:29:37 +0200 Subject: [PATCH] fixes for device_info --- src/analogsensor.cpp | 8 ++------ src/command.cpp | 7 ++++++- src/emsdevice.cpp | 13 +++--------- src/emsesp.cpp | 9 ++++++++ src/emsesp.h | 2 ++ src/system.cpp | 4 +--- src/temperaturesensor.cpp | 10 +++------ src/web/WebAPIService.cpp | 32 ++++++++++++++++++++--------- src/web/WebCustomEntityService.cpp | 9 +++----- src/web/WebCustomizationService.cpp | 1 + src/web/WebDataService.cpp | 2 +- src/web/WebSchedulerService.cpp | 17 ++++++--------- 12 files changed, 59 insertions(+), 55 deletions(-) diff --git a/src/analogsensor.cpp b/src/analogsensor.cpp index 440b0f43b..0298dc7a5 100644 --- a/src/analogsensor.cpp +++ b/src/analogsensor.cpp @@ -705,18 +705,14 @@ bool AnalogSensor::get_value_info(JsonObject output, const char * cmd, const int output["api_data"] = data; return true; } else { - char error[100]; - snprintf(error, sizeof(error), "cannot find attribute %s in entity %s", attribute_s, sensor_name); - output.clear(); - output["message"] = error; - return false; + return EMSESP::return_not_found(output, "attribute", sensor_name); // not found } } return true; // found a match, exit } } - return false; // not found + return EMSESP::return_not_found(output, "analog sensor", cmd); // not found } void AnalogSensor::addSensorJson(JsonObject output, const Sensor & sensor) { diff --git a/src/command.cpp b/src/command.cpp index df2f1f1a0..70226ad70 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -79,7 +79,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec uint8_t device_type = EMSdevice::device_name_2_device_type(device_s); if (!device_has_commands(device_type)) { LOG_DEBUG("Command failed: unknown device '%s'", device_s); - return message(CommandRet::ERROR, "unknown device", output); + return message(CommandRet::NOT_FOUND, "unknown device", output); } // the next value on the path should be the command or entity name @@ -161,18 +161,23 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec if (data_p == nullptr) { return CommandRet::INVALID; } + char data_s[COMMAND_MAX_LENGTH]; strlcpy(data_s, Helpers::toLower(data_p).c_str(), 30); if (strstr(data_s, "/value") == nullptr) { strcat(data_s, "/value"); } + uint8_t device_type = EMSdevice::device_name_2_device_type(device_p); if (CommandRet::OK != Command::call(device_type, data_s, "", true, id_d, output)) { return CommandRet::INVALID; } + + // TODO refactor containsKey - not recommended to use if (!output.containsKey("api_data")) { return CommandRet::INVALID; } + String dat = output["api_data"].as(); output.clear(); return Command::call(device_type, command_p, dat.c_str(), is_admin, id_n, output); diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index e2c209fa1..74580dd32 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -1590,7 +1590,7 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t // if we're filtering on an attribute, go find it if (attribute_s) { #if defined(EMSESP_DEBUG) - EMSESP::logger().debug("Attribute '%s'", attribute_s); + EMSESP::logger().debug("[DEBUG] single attribute '%s'", attribute_s); #endif if (json.containsKey(attribute_s)) { String data = json[attribute_s].as(); @@ -1598,11 +1598,7 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t output["api_data"] = data; return true; } else { - char error[100]; - snprintf(error, sizeof(error), "cannot find attribute %s in entity %s", attribute_s, command_s); - output.clear(); - output["message"] = error; - return false; + return EMSESP::return_not_found(output, "attribute", command_s); // not found } } @@ -1610,10 +1606,7 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t } } - char error[100]; - snprintf(error, sizeof(error), "cannot find values for entity '%s'", cmd); - json["message"] = error; - return false; + return EMSESP::return_not_found(output, "entity data", cmd); // not found } // mqtt publish all single values from one device (used for time schedule) diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 7ae8a3cbb..ba3bc3547 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -782,6 +782,15 @@ bool EMSESP::get_device_value_info(JsonObject root, const char * cmd, const int8 return false; // not found } +// sends JSON error message, used with API calls +bool EMSESP::return_not_found(JsonObject output, const char * msg, const char * cmd) { + output.clear(); + char error[100]; + snprintf(error, sizeof(error), "cannot find %s for '%s'", msg, cmd); + output["message"] = error; + return false; // always return false +} + // search for recognized device_ids : Me, All, otherwise print hex value std::string EMSESP::device_tostring(const uint8_t device_id) { if ((device_id & 0x7F) == EMSbus::ems_bus_id()) { diff --git a/src/emsesp.h b/src/emsesp.h index f28d4dba9..77c0246ce 100644 --- a/src/emsesp.h +++ b/src/emsesp.h @@ -213,6 +213,8 @@ class EMSESP { static void scan_devices(); static void clear_all_devices(); + static bool return_not_found(JsonObject output, const char * msg, const char * cmd); + static std::vector> emsdevices; // services diff --git a/src/system.cpp b/src/system.cpp index a93c370cd..7b012d735 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1348,9 +1348,7 @@ bool System::get_value_info(JsonObject root, const char * command) { } } - root.clear(); - - return false; // not found + return EMSESP::return_not_found(root, "data", command); // not found } // export status information including the device information diff --git a/src/temperaturesensor.cpp b/src/temperaturesensor.cpp index c5ae4ddfd..7d79a6bfe 100644 --- a/src/temperaturesensor.cpp +++ b/src/temperaturesensor.cpp @@ -343,7 +343,7 @@ bool TemperatureSensor::updated_values() { // called from emsesp.cpp for commands bool TemperatureSensor::get_value_info(JsonObject output, const char * cmd, const int8_t id) { - // check of it a 'commmands' command + // check of it a 'commands' command if (Helpers::toLower(cmd) == F_(commands)) { return Command::list(EMSdevice::DeviceType::TEMPERATURESENSOR, output); } @@ -401,18 +401,14 @@ bool TemperatureSensor::get_value_info(JsonObject output, const char * cmd, cons output["api_data"] = data; return true; } else { - char error[100]; - snprintf(error, sizeof(error), "cannot find attribute %s in entity %s", attribute_s, sensor_name); - output.clear(); - output["message"] = error; - return false; + return EMSESP::return_not_found(output, "attribute", sensor_name); // not found } } return true; // found a match, exit } } - return false; // not found + return EMSESP::return_not_found(output, "temperature sensor", cmd); // not found } void TemperatureSensor::addSensorJson(JsonObject output, const Sensor & sensor) { diff --git a/src/web/WebAPIService.cpp b/src/web/WebAPIService.cpp index a7e2e83eb..7a5633e3f 100644 --- a/src/web/WebAPIService.cpp +++ b/src/web/WebAPIService.cpp @@ -128,9 +128,9 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) { if (return_code != CommandRet::OK) { char error[100]; if (output.size()) { - snprintf(error, sizeof(error), "API failed with error %s (%s)", (const char *)output["message"], Command::return_code_string(return_code).c_str()); + snprintf(error, sizeof(error), "API call failed. %s (%s)", (const char *)output["message"], Command::return_code_string(return_code).c_str()); } else { - snprintf(error, sizeof(error), "API failed with error %s", Command::return_code_string(return_code).c_str()); + snprintf(error, sizeof(error), "API call failed (%s)", Command::return_code_string(return_code).c_str()); } emsesp::EMSESP::logger().err(error); api_fails_++; @@ -138,17 +138,29 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) { // if we're returning single values, just sent as plain text // https://github.com/emsesp/EMS-ESP32/issues/462#issuecomment-1093877210 - if (output.containsKey("api_data")) { - String data = output["api_data"].as(); - request->send(200, "text/plain; charset=utf-8", data); + const char * api_data = output["api_data"]; + if (api_data) { + request->send(200, "text/plain; charset=utf-8", api_data); +#if defined(EMSESP_STANDALONE) + Serial.print(COLOR_YELLOW); + Serial.print("web output: "); + if (output.size()) { + serializeJson(output, Serial); + } + Serial.println(COLOR_RESET); +#endif api_count_++; delete response; return; } // send the json that came back from the command call - // FAIL, OK, NOT_FOUND, ERROR, NOT_ALLOWED = 400 (bad request), 200 (OK), 400 (not found), 400 (bad request), 401 (unauthorized) - int ret_codes[6] = {400, 200, 400, 400, 401, 400}; + // sequence is FAIL, OK, NOT_FOUND, ERROR, NOT_ALLOWED, INVALID + // 400 (bad request) + // 200 (OK) + // 404 (not found) + // 401 (unauthorized) + int ret_codes[6] = {400, 200, 404, 400, 401, 400}; response->setCode(ret_codes[return_code]); response->setLength(); response->setContentType("application/json; charset=utf-8"); @@ -157,14 +169,14 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) { #if defined(EMSESP_STANDALONE) Serial.print(COLOR_YELLOW); - Serial.print("data: "); + Serial.print("web output: "); if (output.size()) { serializeJson(output, Serial); } Serial.print(" (response code "); Serial.print(ret_codes[return_code]); - Serial.println(")"); - Serial.print(COLOR_RESET); + Serial.print(")"); + Serial.println(COLOR_RESET); #endif } diff --git a/src/web/WebCustomEntityService.cpp b/src/web/WebCustomEntityService.cpp index 233fe84a2..433ec9112 100644 --- a/src/web/WebCustomEntityService.cpp +++ b/src/web/WebCustomEntityService.cpp @@ -272,6 +272,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd) // if no entries, return empty json // https://github.com/emsesp/EMS-ESP32/issues/1297 if (customEntityItems_->size() == 0) { + // TODO or should this report back the message[] ? return true; } @@ -324,11 +325,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd) output["api_data"] = data; return true; } else { - char error[100]; - snprintf(error, sizeof(error), "cannot find attribute %s in entity %s", attribute_s, command_s); - output.clear(); - output["message"] = error; - return false; + return EMSESP::return_not_found(output, "attribute", command_s); // not found } } } @@ -338,7 +335,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd) } } - return false; // not found + return EMSESP::return_not_found(output, "custom entity", cmd); // not found } // publish single value diff --git a/src/web/WebCustomizationService.cpp b/src/web/WebCustomizationService.cpp index d7eb6e8dc..f016a0f5e 100644 --- a/src/web/WebCustomizationService.cpp +++ b/src/web/WebCustomizationService.cpp @@ -158,6 +158,7 @@ void WebCustomizationService::reset_customization(AsyncWebServerRequest * reques EMSESP::system_.restart_requested(true); return; } + // failed AsyncWebServerResponse * response = request->beginResponse(400); // bad request request->send(response); diff --git a/src/web/WebDataService.cpp b/src/web/WebDataService.cpp index 622c93748..073812506 100644 --- a/src/web/WebDataService.cpp +++ b/src/web/WebDataService.cpp @@ -212,7 +212,7 @@ void WebDataService::device_data(AsyncWebServerRequest * request) { } // invalid - AsyncWebServerResponse * response = request->beginResponse(400); + AsyncWebServerResponse * response = request->beginResponse(400); // bad request request->send(response); } diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index 413a4763b..cfede1b53 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -161,7 +161,11 @@ bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) { } } - return (output.size() > 0); + if (output.size()) { + return true; + } + + return EMSESP::return_not_found(output, "scheduler", cmd); // not found } char command_s[COMMAND_MAX_LENGTH]; @@ -205,7 +209,7 @@ bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) { return true; } - return false; // not found + return EMSESP::return_not_found(output, "scheduler", cmd); // not found } // publish single value @@ -499,15 +503,6 @@ void WebSchedulerService::test() { std::string test_cmd = "system/message"; std::string test_value; - test_value = "1+2*3"; - EMSESP::logger().warning("Shunting yard test 1: %s = %s", test_value.c_str(), compute(test_value).c_str()); - - test_value = "system/settings/locale"; - EMSESP::logger().warning("Shunting yard test 2: %s = %s", test_value.c_str(), compute(test_value).c_str()); - - test_value = "hello"; - EMSESP::logger().warning("Shunting yard test 3: %s = %s", test_value.c_str(), compute(test_value).c_str()); - // should output 'locale is en' test_value = "\"locale is \"system/settings/locale"; command(test_cmd.c_str(), compute(test_value).c_str());