fixes for device_info

This commit is contained in:
proddy
2024-07-11 16:29:37 +02:00
parent 76675c79fb
commit d7ba360483
12 changed files with 59 additions and 55 deletions

View File

@@ -705,18 +705,14 @@ bool AnalogSensor::get_value_info(JsonObject output, const char * cmd, const int
output["api_data"] = data; output["api_data"] = data;
return true; return true;
} else { } else {
char error[100]; return EMSESP::return_not_found(output, "attribute", sensor_name); // not found
snprintf(error, sizeof(error), "cannot find attribute %s in entity %s", attribute_s, sensor_name);
output.clear();
output["message"] = error;
return false;
} }
} }
return true; // found a match, exit 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) { void AnalogSensor::addSensorJson(JsonObject output, const Sensor & sensor) {

View File

@@ -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); uint8_t device_type = EMSdevice::device_name_2_device_type(device_s);
if (!device_has_commands(device_type)) { if (!device_has_commands(device_type)) {
LOG_DEBUG("Command failed: unknown device '%s'", device_s); 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 // 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) { if (data_p == nullptr) {
return CommandRet::INVALID; return CommandRet::INVALID;
} }
char data_s[COMMAND_MAX_LENGTH]; char data_s[COMMAND_MAX_LENGTH];
strlcpy(data_s, Helpers::toLower(data_p).c_str(), 30); strlcpy(data_s, Helpers::toLower(data_p).c_str(), 30);
if (strstr(data_s, "/value") == nullptr) { if (strstr(data_s, "/value") == nullptr) {
strcat(data_s, "/value"); strcat(data_s, "/value");
} }
uint8_t device_type = EMSdevice::device_name_2_device_type(device_p); 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)) { if (CommandRet::OK != Command::call(device_type, data_s, "", true, id_d, output)) {
return CommandRet::INVALID; return CommandRet::INVALID;
} }
// TODO refactor containsKey - not recommended to use
if (!output.containsKey("api_data")) { if (!output.containsKey("api_data")) {
return CommandRet::INVALID; return CommandRet::INVALID;
} }
String dat = output["api_data"].as<String>(); String dat = output["api_data"].as<String>();
output.clear(); output.clear();
return Command::call(device_type, command_p, dat.c_str(), is_admin, id_n, output); return Command::call(device_type, command_p, dat.c_str(), is_admin, id_n, output);

View File

@@ -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 we're filtering on an attribute, go find it
if (attribute_s) { if (attribute_s) {
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)
EMSESP::logger().debug("Attribute '%s'", attribute_s); EMSESP::logger().debug("[DEBUG] single attribute '%s'", attribute_s);
#endif #endif
if (json.containsKey(attribute_s)) { if (json.containsKey(attribute_s)) {
String data = json[attribute_s].as<String>(); String data = json[attribute_s].as<String>();
@@ -1598,11 +1598,7 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t
output["api_data"] = data; output["api_data"] = data;
return true; return true;
} else { } else {
char error[100]; return EMSESP::return_not_found(output, "attribute", command_s); // not found
snprintf(error, sizeof(error), "cannot find attribute %s in entity %s", attribute_s, command_s);
output.clear();
output["message"] = error;
return false;
} }
} }
@@ -1610,10 +1606,7 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t
} }
} }
char error[100]; return EMSESP::return_not_found(output, "entity data", cmd); // not found
snprintf(error, sizeof(error), "cannot find values for entity '%s'", cmd);
json["message"] = error;
return false;
} }
// mqtt publish all single values from one device (used for time schedule) // mqtt publish all single values from one device (used for time schedule)

View File

@@ -782,6 +782,15 @@ bool EMSESP::get_device_value_info(JsonObject root, const char * cmd, const int8
return false; // not found 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 // search for recognized device_ids : Me, All, otherwise print hex value
std::string EMSESP::device_tostring(const uint8_t device_id) { std::string EMSESP::device_tostring(const uint8_t device_id) {
if ((device_id & 0x7F) == EMSbus::ems_bus_id()) { if ((device_id & 0x7F) == EMSbus::ems_bus_id()) {

View File

@@ -213,6 +213,8 @@ class EMSESP {
static void scan_devices(); static void scan_devices();
static void clear_all_devices(); static void clear_all_devices();
static bool return_not_found(JsonObject output, const char * msg, const char * cmd);
static std::vector<std::unique_ptr<EMSdevice>> emsdevices; static std::vector<std::unique_ptr<EMSdevice>> emsdevices;
// services // services

View File

@@ -1348,9 +1348,7 @@ bool System::get_value_info(JsonObject root, const char * command) {
} }
} }
root.clear(); return EMSESP::return_not_found(root, "data", command); // not found
return false; // not found
} }
// export status information including the device information // export status information including the device information

View File

@@ -343,7 +343,7 @@ bool TemperatureSensor::updated_values() {
// called from emsesp.cpp for commands // called from emsesp.cpp for commands
bool TemperatureSensor::get_value_info(JsonObject output, const char * cmd, const int8_t id) { 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)) { if (Helpers::toLower(cmd) == F_(commands)) {
return Command::list(EMSdevice::DeviceType::TEMPERATURESENSOR, output); 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; output["api_data"] = data;
return true; return true;
} else { } else {
char error[100]; return EMSESP::return_not_found(output, "attribute", sensor_name); // not found
snprintf(error, sizeof(error), "cannot find attribute %s in entity %s", attribute_s, sensor_name);
output.clear();
output["message"] = error;
return false;
} }
} }
return true; // found a match, exit 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) { void TemperatureSensor::addSensorJson(JsonObject output, const Sensor & sensor) {

View File

@@ -128,9 +128,9 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
if (return_code != CommandRet::OK) { if (return_code != CommandRet::OK) {
char error[100]; char error[100];
if (output.size()) { 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 { } 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); emsesp::EMSESP::logger().err(error);
api_fails_++; api_fails_++;
@@ -138,17 +138,29 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
// if we're returning single values, just sent as plain text // if we're returning single values, just sent as plain text
// https://github.com/emsesp/EMS-ESP32/issues/462#issuecomment-1093877210 // https://github.com/emsesp/EMS-ESP32/issues/462#issuecomment-1093877210
if (output.containsKey("api_data")) { const char * api_data = output["api_data"];
String data = output["api_data"].as<String>(); if (api_data) {
request->send(200, "text/plain; charset=utf-8", 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_++; api_count_++;
delete response; delete response;
return; return;
} }
// send the json that came back from the command call // 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) // sequence is FAIL, OK, NOT_FOUND, ERROR, NOT_ALLOWED, INVALID
int ret_codes[6] = {400, 200, 400, 400, 401, 400}; // 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->setCode(ret_codes[return_code]);
response->setLength(); response->setLength();
response->setContentType("application/json; charset=utf-8"); response->setContentType("application/json; charset=utf-8");
@@ -157,14 +169,14 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
#if defined(EMSESP_STANDALONE) #if defined(EMSESP_STANDALONE)
Serial.print(COLOR_YELLOW); Serial.print(COLOR_YELLOW);
Serial.print("data: "); Serial.print("web output: ");
if (output.size()) { if (output.size()) {
serializeJson(output, Serial); serializeJson(output, Serial);
} }
Serial.print(" (response code "); Serial.print(" (response code ");
Serial.print(ret_codes[return_code]); Serial.print(ret_codes[return_code]);
Serial.println(")"); Serial.print(")");
Serial.print(COLOR_RESET); Serial.println(COLOR_RESET);
#endif #endif
} }

View File

@@ -272,6 +272,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
// if no entries, return empty json // if no entries, return empty json
// https://github.com/emsesp/EMS-ESP32/issues/1297 // https://github.com/emsesp/EMS-ESP32/issues/1297
if (customEntityItems_->size() == 0) { if (customEntityItems_->size() == 0) {
// TODO or should this report back the message[] ?
return true; return true;
} }
@@ -324,11 +325,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
output["api_data"] = data; output["api_data"] = data;
return true; return true;
} else { } else {
char error[100]; return EMSESP::return_not_found(output, "attribute", command_s); // not found
snprintf(error, sizeof(error), "cannot find attribute %s in entity %s", attribute_s, command_s);
output.clear();
output["message"] = error;
return false;
} }
} }
} }
@@ -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 // publish single value

View File

@@ -158,6 +158,7 @@ void WebCustomizationService::reset_customization(AsyncWebServerRequest * reques
EMSESP::system_.restart_requested(true); EMSESP::system_.restart_requested(true);
return; return;
} }
// failed // failed
AsyncWebServerResponse * response = request->beginResponse(400); // bad request AsyncWebServerResponse * response = request->beginResponse(400); // bad request
request->send(response); request->send(response);

View File

@@ -212,7 +212,7 @@ void WebDataService::device_data(AsyncWebServerRequest * request) {
} }
// invalid // invalid
AsyncWebServerResponse * response = request->beginResponse(400); AsyncWebServerResponse * response = request->beginResponse(400); // bad request
request->send(response); request->send(response);
} }

View File

@@ -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]; char command_s[COMMAND_MAX_LENGTH];
@@ -205,7 +209,7 @@ bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) {
return true; return true;
} }
return false; // not found return EMSESP::return_not_found(output, "scheduler", cmd); // not found
} }
// publish single value // publish single value
@@ -499,15 +503,6 @@ void WebSchedulerService::test() {
std::string test_cmd = "system/message"; std::string test_cmd = "system/message";
std::string test_value; 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' // should output 'locale is en'
test_value = "\"locale is \"system/settings/locale"; test_value = "\"locale is \"system/settings/locale";
command(test_cmd.c_str(), compute(test_value).c_str()); command(test_cmd.c_str(), compute(test_value).c_str());