This commit is contained in:
MichaelDvP
2025-03-15 13:31:48 +01:00
16 changed files with 169 additions and 189 deletions

View File

@@ -184,7 +184,8 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
}
// call the command based on the type
uint8_t return_code = CommandRet::ERROR;
uint8_t return_code = CommandRet::OK;
if (data.is<const char *>()) {
return_code = Command::call(device_type, command_p, data.as<const char *>(), is_admin, id_n, output);
} else if (data.is<int>()) {
@@ -312,11 +313,17 @@ bool Command::set_attribute(JsonObject output, const char * cmd, const char * at
return true;
}
// not found
output.clear();
// attribute isn't found
// it could be a value command, but the value doesn't exist?
if (strcmp(attribute, "value") == 0) {
LOG_DEBUG("%s has no value set", cmd);
return false; // fail
}
char error[100];
snprintf(error, sizeof(error), "no %s in %s", attribute, cmd);
snprintf(error, sizeof(error), "no attribute '%s' in %s", attribute, cmd);
output["message"] = error;
return false;
}
@@ -356,7 +363,7 @@ uint8_t Command::call(const uint8_t device_type, const char * command, const cha
return CommandRet::OK;
}
} else if (device_type == EMSdevice::DeviceType::SYSTEM && strchr(cmd, '/')) {
// check service commands, if not found continue with commandsfunctions
// check service commands, if not found continue with command functions
if (EMSESP::system_.command_service(cmd, value)) {
return CommandRet::OK;
}
@@ -376,14 +383,23 @@ uint8_t Command::call(const uint8_t device_type, const char * command, const cha
flag = CommandFlag::CMD_FLAG_AHS;
}
// see if there is a command registered and it's valid
// see if there is a command registered for this EMS device
auto cf = find_command(device_type, device_id, cmd, flag);
if (!cf) {
// if we don't already have a message set, set it to invalid command
if (output["message"]) {
LOG_WARNING("Command failed: %s", output["message"].as<const char *>());
return CommandRet::ERROR;
} else {
std::string err = "no " + std::string(cmd) + " in " + dname;
// not an error, no test if we're fetching a value, but the value is not set
auto attribute_s = Command::get_attribute(cmd);
if (attribute_s) {
if (strcmp(attribute_s, "value") == 0) {
return CommandRet::NO_VALUE;
}
}
std::string err = "no entity '" + std::string(cmd) + "' in " + dname;
output["message"] = err;
LOG_WARNING("Command failed: %s", err.c_str());
}
@@ -496,7 +512,7 @@ Command::CmdFunction * Command::find_command(const uint8_t device_type, const ui
}
}
return nullptr; // command not found
return nullptr; // command not found, could be an attribute?
}
void Command::erase_device_commands(const uint8_t device_type) {

View File

@@ -48,7 +48,8 @@ enum CommandRet : uint8_t {
NOT_FOUND, // 2
ERROR, // 3
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)>;

View File

@@ -1528,6 +1528,7 @@ bool EMSdevice::get_value_info(JsonObject output, const char * cmd, const int8_t
if (cmd_s == Helpers::toLower(dv.short_name) && (tag <= 0 || tag == dv.tag)) {
get_value_json(output, dv);
// if we're filtering on an attribute, go find it
// if we can't find it, maybe it exists but doesn't not have a value assigned yet
return Command::set_attribute(output, cmd_s, attribute_s);
}
}

View File

@@ -780,6 +780,7 @@ bool EMSESP::get_device_value_info(JsonObject root, const char * cmd, const int8
}
}
}
// if the EMS device was valid, but the cmd not found exit. it will be handled upstream.
if (found_device) {
return false;

View File

@@ -380,7 +380,13 @@ std::string commands(std::string & expr, bool quotes = true) {
JsonObject output = doc_out.to<JsonObject>();
JsonObject input = doc_in.to<JsonObject>();
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::OK && return_code != emsesp::CommandRet::NO_VALUE) {
return expr = "";
}
std::string data = output["api_data"] | "";
if (!isnum(data) && quotes) {
data.insert(data.begin(), '"');