add new return code NO_VALUE

This commit is contained in:
proddy
2025-03-13 22:46:04 +01:00
parent ed7a9f43de
commit b1eedcb1d8
3 changed files with 12 additions and 4 deletions

View File

@@ -48,7 +48,8 @@ enum CommandRet : uint8_t {
NOT_FOUND, // 2 NOT_FOUND, // 2
ERROR, // 3 ERROR, // 3
NOT_ALLOWED, // 4 - needs authentication NOT_ALLOWED, // 4 - needs authentication
INVALID // 5 - invalid (tag) INVALID, // 5 - invalid (tag)
NO_VALUE // 6 - no value
}; };
using cmd_function_p = std::function<bool(const char * data, const int8_t id)>; using cmd_function_p = std::function<bool(const char * data, const int8_t id)>;

View File

@@ -380,7 +380,13 @@ std::string commands(std::string & expr, bool quotes = true) {
JsonObject output = doc_out.to<JsonObject>(); JsonObject output = doc_out.to<JsonObject>();
JsonObject input = doc_in.to<JsonObject>(); JsonObject input = doc_in.to<JsonObject>();
std::string cmd_s = "api/" + std::string(cmd); std::string cmd_s = "api/" + std::string(cmd);
emsesp::Command::process(cmd_s.c_str(), true, input, output);
auto return_code = emsesp::Command::process(cmd_s.c_str(), true, input, output);
// check for no value (entity is valid but has no value set)
if (return_code == emsesp::CommandRet::NO_VALUE) {
return expr = ""; // just ignore for now
}
if (output["api_data"].is<std::string>()) { if (output["api_data"].is<std::string>()) {
std::string data = output["api_data"]; std::string data = output["api_data"];
if (!isnum(data) && quotes) { if (!isnum(data) && quotes) {

View File

@@ -134,12 +134,13 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
} }
// send the json that came back from the command call // send the json that came back from the command call
// sequence is FAIL, OK, NOT_FOUND, ERROR, NOT_ALLOWED, INVALID // sequence matches CommandRet in command.h (FAIL, OK, NOT_FOUND, ERROR, NOT_ALLOWED, INVALID, NO_VALUE)
// 400 (bad request) // 400 (bad request)
// 200 (OK) // 200 (OK)
// 404 (not found) // 404 (not found)
// 401 (unauthorized) // 401 (unauthorized)
int ret_codes[6] = {400, 200, 404, 400, 401, 400}; // 400 (invalid)
int ret_codes[7] = {400, 200, 404, 400, 401, 400, 404};
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");