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

@@ -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<String>();
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
}

View File

@@ -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

View File

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

View File

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

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];
@@ -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());