mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
fix show commands/api/<device>/commands, rename CommandFlags
This commit is contained in:
@@ -187,7 +187,7 @@ uint8_t Command::process(const char * path, const bool is_admin, const JsonObjec
|
|||||||
return_code = Command::call(device_type, command_p, data.as<const char *>(), is_admin, id_n, output);
|
return_code = Command::call(device_type, command_p, data.as<const char *>(), is_admin, id_n, output);
|
||||||
} else if (data.is<int>()) {
|
} else if (data.is<int>()) {
|
||||||
char data_str[10];
|
char data_str[10];
|
||||||
return_code = Command::call(device_type, command_p, Helpers::itoa((int16_t)data.as<int>(), data_str), is_admin, id_n, output);
|
return_code = Command::call(device_type, command_p, Helpers::itoa(data.as<int32_t>(), data_str), is_admin, id_n, output);
|
||||||
} else if (data.is<float>()) {
|
} else if (data.is<float>()) {
|
||||||
char data_str[10];
|
char data_str[10];
|
||||||
return_code = Command::call(device_type, command_p, Helpers::render_value(data_str, data.as<float>(), 2), is_admin, id_n, output);
|
return_code = Command::call(device_type, command_p, Helpers::render_value(data_str, data.as<float>(), 2), is_admin, id_n, output);
|
||||||
@@ -302,16 +302,16 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
|||||||
auto dname = EMSdevice::device_type_2_device_name(device_type);
|
auto dname = EMSdevice::device_type_2_device_name(device_type);
|
||||||
uint8_t device_id = EMSESP::device_id_from_cmd(device_type, cmd, id);
|
uint8_t device_id = EMSESP::device_id_from_cmd(device_type, cmd, id);
|
||||||
|
|
||||||
uint8_t flag = 0;
|
uint8_t flag = CommandFlag::CMD_FLAG_DEFAULT;
|
||||||
uint8_t tag = id - 1 + DeviceValueTAG::TAG_HC1;
|
uint8_t tag = id - 1 + DeviceValueTAG::TAG_HC1;
|
||||||
if (tag >= DeviceValueTAG::TAG_HC1 && tag <= DeviceValueTAG::TAG_HC8) {
|
if (tag >= DeviceValueTAG::TAG_HC1 && tag <= DeviceValueTAG::TAG_HC8) {
|
||||||
flag = CommandFlag::MQTT_SUB_FLAG_HC;
|
flag = CommandFlag::CMD_FLAG_HC;
|
||||||
} else if (tag >= DeviceValueTAG::TAG_DHW1 && tag <= DeviceValueTAG::TAG_DHW10) {
|
} else if (tag >= DeviceValueTAG::TAG_DHW1 && tag <= DeviceValueTAG::TAG_DHW10) {
|
||||||
flag = CommandFlag::MQTT_SUB_FLAG_DHW;
|
flag = CommandFlag::CMD_FLAG_DHW;
|
||||||
} else if (tag >= DeviceValueTAG::TAG_HS1 && tag <= DeviceValueTAG::TAG_HS16) {
|
} else if (tag >= DeviceValueTAG::TAG_HS1 && tag <= DeviceValueTAG::TAG_HS16) {
|
||||||
flag = CommandFlag::MQTT_SUB_FLAG_HS;
|
flag = CommandFlag::CMD_FLAG_HS;
|
||||||
} else if (tag >= DeviceValueTAG::TAG_AHS1 && tag <= DeviceValueTAG::TAG_AHS1) {
|
} else if (tag >= DeviceValueTAG::TAG_AHS1 && tag <= DeviceValueTAG::TAG_AHS1) {
|
||||||
flag = CommandFlag::MQTT_SUB_FLAG_AHS;
|
flag = CommandFlag::CMD_FLAG_AHS;
|
||||||
}
|
}
|
||||||
// see if there is a command registered
|
// see if there is a command registered
|
||||||
auto cf = find_command(device_type, device_id, cmd, flag);
|
auto cf = find_command(device_type, device_id, cmd, flag);
|
||||||
@@ -381,9 +381,9 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
|
|||||||
// report back. If not OK show output from error, other return the HTTP code
|
// report back. If not OK show output from error, other return the HTTP code
|
||||||
if (return_code != CommandRet::OK) {
|
if (return_code != CommandRet::OK) {
|
||||||
if ((value == nullptr) || (strlen(value) == 0)) {
|
if ((value == nullptr) || (strlen(value) == 0)) {
|
||||||
LOG_ERROR("Command '%s' failed with error code %d", cmd, FL_(cmdRet)[return_code]);
|
LOG_ERROR("Command '%s' failed with error '%s'", cmd, FL_(cmdRet)[return_code]);
|
||||||
} else {
|
} else {
|
||||||
LOG_ERROR("Command '%s/%s' failed with error code %c", cmd, value, return_code);
|
LOG_ERROR("Command '%s/%s' failed with error '%s'", cmd, value, FL_(cmdRet)[return_code]);
|
||||||
}
|
}
|
||||||
return message(return_code, "callback function failed", output);
|
return message(return_code, "callback function failed", output);
|
||||||
}
|
}
|
||||||
@@ -430,7 +430,7 @@ Command::CmdFunction * Command::find_command(const uint8_t device_type, const ui
|
|||||||
|
|
||||||
for (auto & cf : cmdfunctions_) {
|
for (auto & cf : cmdfunctions_) {
|
||||||
if (Helpers::toLower(cmd) == Helpers::toLower(cf.cmd_) && (cf.device_type_ == device_type) && (!device_id || cf.device_id_ == device_id)
|
if (Helpers::toLower(cmd) == Helpers::toLower(cf.cmd_) && (cf.device_type_ == device_type) && (!device_id || cf.device_id_ == device_id)
|
||||||
&& (!(flag & 0x3F) || (flag & 0x3F) == (cf.flags_ & 0x3F))) {
|
&& (flag & 0x3F) == (cf.flags_ & 0x3F)) {
|
||||||
return &cf;
|
return &cf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -438,13 +438,13 @@ Command::CmdFunction * Command::find_command(const uint8_t device_type, const ui
|
|||||||
return nullptr; // command not found
|
return nullptr; // command not found
|
||||||
}
|
}
|
||||||
|
|
||||||
void Command::erase_command(const uint8_t device_type, const char * cmd) {
|
void Command::erase_command(const uint8_t device_type, const char * cmd, uint8_t flag) {
|
||||||
if ((cmd == nullptr) || (strlen(cmd) == 0) || (cmdfunctions_.empty())) {
|
if ((cmd == nullptr) || (strlen(cmd) == 0) || (cmdfunctions_.empty())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto it = cmdfunctions_.begin();
|
auto it = cmdfunctions_.begin();
|
||||||
for (auto & cf : cmdfunctions_) {
|
for (auto & cf : cmdfunctions_) {
|
||||||
if (Helpers::toLower(cmd) == Helpers::toLower(cf.cmd_) && (cf.device_type_ == device_type)) {
|
if (Helpers::toLower(cmd) == Helpers::toLower(cf.cmd_) && (cf.device_type_ == device_type) && ((flag & 0x3F) == (cf.flags_ & 0x3F))) {
|
||||||
cmdfunctions_.erase(it);
|
cmdfunctions_.erase(it);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -452,6 +452,22 @@ void Command::erase_command(const uint8_t device_type, const char * cmd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get the tagged command
|
||||||
|
std::string Command::tagged_cmd(std::string cmd, const uint8_t flag) {
|
||||||
|
switch (flag & 0x3F) {
|
||||||
|
case CommandFlag::CMD_FLAG_HC:
|
||||||
|
return "[hc<n>.]" + cmd;
|
||||||
|
case CommandFlag::CMD_FLAG_DHW:
|
||||||
|
return "dhw[n]." + cmd;
|
||||||
|
case CommandFlag::CMD_FLAG_HS:
|
||||||
|
return "hs<n>." + cmd;
|
||||||
|
case CommandFlag::CMD_FLAG_AHS:
|
||||||
|
return "ahs<n>." + cmd;
|
||||||
|
default:
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// list all commands for a specific device, output as json
|
// list all commands for a specific device, output as json
|
||||||
bool Command::list(const uint8_t device_type, JsonObject output) {
|
bool Command::list(const uint8_t device_type, JsonObject output) {
|
||||||
// force add info and commands for those non-EMS devices
|
// force add info and commands for those non-EMS devices
|
||||||
@@ -467,40 +483,28 @@ bool Command::list(const uint8_t device_type, JsonObject output) {
|
|||||||
std::list<std::string> sorted_cmds;
|
std::list<std::string> sorted_cmds;
|
||||||
for (const auto & cf : cmdfunctions_) {
|
for (const auto & cf : cmdfunctions_) {
|
||||||
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN)) {
|
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN)) {
|
||||||
sorted_cmds.push_back((cf.cmd_));
|
sorted_cmds.push_back(tagged_cmd(cf.cmd_, cf.flags_));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sorted_cmds.sort();
|
sorted_cmds.sort();
|
||||||
|
|
||||||
for (const auto & cl : sorted_cmds) {
|
for (const auto & cl : sorted_cmds) {
|
||||||
for (const auto & cf : cmdfunctions_) {
|
for (const auto & cf : cmdfunctions_) {
|
||||||
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN) && cf.description_ && (cl == std::string(cf.cmd_))) {
|
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN) && cf.description_ && (cl == tagged_cmd(cf.cmd_, cf.flags_))) {
|
||||||
if (cf.has_flags(CommandFlag::MQTT_SUB_FLAG_DHW)) {
|
output[cl] = Helpers::translated_word(cf.description_);
|
||||||
char s[100];
|
|
||||||
snprintf(s, sizeof(s), "%s %s", EMSdevice::tag_to_string(DeviceValueTAG::TAG_DHW1), Helpers::translated_word(cf.description_));
|
|
||||||
output[cl] = s;
|
|
||||||
} else {
|
|
||||||
output[cl] = Helpers::translated_word(cf.description_);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// output list of all commands to console for a specific DeviceType
|
// output list of all commands to console for a specific DeviceType
|
||||||
void Command::show(uuid::console::Shell & shell, uint8_t device_type, bool verbose) {
|
void Command::show(uuid::console::Shell & shell, uint8_t device_type, bool verbose) {
|
||||||
if (cmdfunctions_.empty()) {
|
|
||||||
shell.println("No commands available");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// create list of commands we have registered
|
// create list of commands we have registered
|
||||||
std::list<std::string> sorted_cmds;
|
std::list<std::string> sorted_cmds;
|
||||||
for (const auto & cf : cmdfunctions_) {
|
for (const auto & cf : cmdfunctions_) {
|
||||||
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN)) {
|
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN)) {
|
||||||
sorted_cmds.push_back((cf.cmd_));
|
sorted_cmds.push_back(tagged_cmd(cf.cmd_, cf.flags_));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,22 +543,9 @@ void Command::show(uuid::console::Shell & shell, uint8_t device_type, bool verbo
|
|||||||
for (const auto & cl : sorted_cmds) {
|
for (const auto & cl : sorted_cmds) {
|
||||||
// find and print the description
|
// find and print the description
|
||||||
for (const auto & cf : cmdfunctions_) {
|
for (const auto & cf : cmdfunctions_) {
|
||||||
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN) && cf.description_ && (cl == std::string(cf.cmd_))) {
|
if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN) && cf.description_ && (cl == tagged_cmd(cf.cmd_, cf.flags_))) {
|
||||||
uint8_t i = cl.length();
|
uint8_t i = cl.length();
|
||||||
shell.print(" ");
|
shell.print(" ");
|
||||||
if (cf.has_flags(MQTT_SUB_FLAG_HC)) {
|
|
||||||
shell.print("[hc<n>.]");
|
|
||||||
i += 8;
|
|
||||||
} else if (cf.has_flags(MQTT_SUB_FLAG_DHW)) {
|
|
||||||
shell.print("[dhw<n>.]");
|
|
||||||
i += 9;
|
|
||||||
} else if (cf.has_flags(MQTT_SUB_FLAG_AHS)) {
|
|
||||||
shell.print("[ahs<n>.]");
|
|
||||||
i += 9;
|
|
||||||
} else if (cf.has_flags(MQTT_SUB_FLAG_HS)) {
|
|
||||||
shell.print("[hs<n>.]");
|
|
||||||
i += 8;
|
|
||||||
}
|
|
||||||
shell.print(cl);
|
shell.print(cl);
|
||||||
// pad with spaces
|
// pad with spaces
|
||||||
while (i++ < 30) {
|
while (i++ < 30) {
|
||||||
|
|||||||
@@ -31,13 +31,13 @@ namespace emsesp {
|
|||||||
|
|
||||||
// mqtt flags for command subscriptions
|
// mqtt flags for command subscriptions
|
||||||
enum CommandFlag : uint8_t {
|
enum CommandFlag : uint8_t {
|
||||||
MQTT_SUB_FLAG_DEFAULT = 0, // 0 no flags set, always subscribe to MQTT
|
CMD_FLAG_DEFAULT = 0, // 0 no flags set, always subscribe to MQTT
|
||||||
MQTT_SUB_FLAG_HC = (1 << 0), // 1 TAG_HC1 - TAG_HC8
|
CMD_FLAG_HC = (1 << 0), // 1 TAG_HC1 - TAG_HC8
|
||||||
MQTT_SUB_FLAG_DHW = (1 << 1), // 2 TAG_DHW1 - TAG_DHW4
|
CMD_FLAG_DHW = (1 << 1), // 2 TAG_DHW1 - TAG_DHW4
|
||||||
MQTT_SUB_FLAG_AHS = (1 << 2), // 4 TAG_DEVICE_DATA_AHS
|
CMD_FLAG_AHS = (1 << 2), // 4 TAG_AHS1
|
||||||
MQTT_SUB_FLAG_HS = (1 << 3), // 8 TAG_DEVICE_DATA_HS
|
CMD_FLAG_HS = (1 << 3), // 8 TAG_HS1 - TAG_HS16
|
||||||
HIDDEN = (1 << 6), // 64 do not show in API or Web
|
HIDDEN = (1 << 6), // 64 do not show in API or Web
|
||||||
ADMIN_ONLY = (1 << 7) // 128 requires authentication
|
ADMIN_ONLY = (1 << 7) // 128 requires authentication
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -110,26 +110,23 @@ class Command {
|
|||||||
const char * cmd,
|
const char * cmd,
|
||||||
const cmd_function_p cb,
|
const cmd_function_p cb,
|
||||||
const char * const * description,
|
const char * const * description,
|
||||||
uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT);
|
uint8_t flags = CommandFlag::CMD_FLAG_DEFAULT);
|
||||||
|
|
||||||
// same for system/temperature/analog devices
|
// same for system/temperature/analog devices
|
||||||
static void add(const uint8_t device_type,
|
static void
|
||||||
const char * cmd,
|
add(const uint8_t device_type, const char * cmd, const cmd_function_p cb, const char * const * description, uint8_t flags = CommandFlag::CMD_FLAG_DEFAULT);
|
||||||
const cmd_function_p cb,
|
|
||||||
const char * const * description,
|
|
||||||
uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT);
|
|
||||||
|
|
||||||
// callback function taking value, id and a json object for its output
|
// callback function taking value, id and a json object for its output
|
||||||
static void add(const uint8_t device_type,
|
static void add(const uint8_t device_type,
|
||||||
const char * cmd,
|
const char * cmd,
|
||||||
const cmd_json_function_p cb,
|
const cmd_json_function_p cb,
|
||||||
const char * const * description,
|
const char * const * description,
|
||||||
uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT);
|
uint8_t flags = CommandFlag::CMD_FLAG_DEFAULT);
|
||||||
|
|
||||||
static void show_all(uuid::console::Shell & shell);
|
static void show_all(uuid::console::Shell & shell);
|
||||||
static Command::CmdFunction * find_command(const uint8_t device_type, const uint8_t device_id, const char * cmd, const uint8_t flag);
|
static Command::CmdFunction * find_command(const uint8_t device_type, const uint8_t device_id, const char * cmd, const uint8_t flag);
|
||||||
|
static std::string tagged_cmd(std::string cmd, const uint8_t flag);
|
||||||
|
|
||||||
static void erase_command(const uint8_t device_type, const char * cmd);
|
static void erase_command(const uint8_t device_type, const char * cmd, uint8_t flag = CommandFlag::CMD_FLAG_DEFAULT);
|
||||||
static void show(uuid::console::Shell & shell, uint8_t device_type, bool verbose);
|
static void show(uuid::console::Shell & shell, uint8_t device_type, bool verbose);
|
||||||
static void show_devices(uuid::console::Shell & shell);
|
static void show_devices(uuid::console::Shell & shell);
|
||||||
static bool device_has_commands(const uint8_t device_type);
|
static bool device_has_commands(const uint8_t device_type);
|
||||||
|
|||||||
@@ -597,13 +597,13 @@ void EMSdevice::add_device_value(uint8_t tag, // to b
|
|||||||
uint8_t flags = CommandFlag::ADMIN_ONLY; // executing commands require admin privileges
|
uint8_t flags = CommandFlag::ADMIN_ONLY; // executing commands require admin privileges
|
||||||
|
|
||||||
if (tag >= DeviceValueTAG::TAG_HC1 && tag <= DeviceValueTAG::TAG_HC8) {
|
if (tag >= DeviceValueTAG::TAG_HC1 && tag <= DeviceValueTAG::TAG_HC8) {
|
||||||
flags |= CommandFlag::MQTT_SUB_FLAG_HC;
|
flags |= CommandFlag::CMD_FLAG_HC;
|
||||||
} else if (tag >= DeviceValueTAG::TAG_DHW1 && tag <= DeviceValueTAG::TAG_DHW10) {
|
} else if (tag >= DeviceValueTAG::TAG_DHW1 && tag <= DeviceValueTAG::TAG_DHW10) {
|
||||||
flags |= CommandFlag::MQTT_SUB_FLAG_DHW;
|
flags |= CommandFlag::CMD_FLAG_DHW;
|
||||||
} else if (tag >= DeviceValueTAG::TAG_HS1 && tag <= DeviceValueTAG::TAG_HS16) {
|
} else if (tag >= DeviceValueTAG::TAG_HS1 && tag <= DeviceValueTAG::TAG_HS16) {
|
||||||
flags |= CommandFlag::MQTT_SUB_FLAG_HS;
|
flags |= CommandFlag::CMD_FLAG_HS;
|
||||||
} else if (tag >= DeviceValueTAG::TAG_AHS1 && tag <= DeviceValueTAG::TAG_AHS1) {
|
} else if (tag >= DeviceValueTAG::TAG_AHS1 && tag <= DeviceValueTAG::TAG_AHS1) {
|
||||||
flags |= CommandFlag::MQTT_SUB_FLAG_AHS;
|
flags |= CommandFlag::CMD_FLAG_AHS;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the command to our library
|
// add the command to our library
|
||||||
|
|||||||
Reference in New Issue
Block a user