From 961f61d48dfcf06014ae85243f3bb1f1d82a20f0 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Thu, 2 May 2024 08:57:55 +0200 Subject: [PATCH] fix `show commands`/`api//commands`, rename CommandFlags --- src/command.cpp | 75 +++++++++++++++++++++-------------------------- src/command.h | 29 ++++++++---------- src/emsdevice.cpp | 8 ++--- 3 files changed, 50 insertions(+), 62 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index 4fe8496ed..0d36270b4 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -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(), is_admin, id_n, output); } else if (data.is()) { char data_str[10]; - return_code = Command::call(device_type, command_p, Helpers::itoa((int16_t)data.as(), data_str), is_admin, id_n, output); + return_code = Command::call(device_type, command_p, Helpers::itoa(data.as(), data_str), is_admin, id_n, output); } else if (data.is()) { char data_str[10]; return_code = Command::call(device_type, command_p, Helpers::render_value(data_str, data.as(), 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); uint8_t device_id = EMSESP::device_id_from_cmd(device_type, cmd, id); - uint8_t flag = 0; - uint8_t tag = id - 1 + DeviceValueTAG::TAG_HC1; + uint8_t flag = CommandFlag::CMD_FLAG_DEFAULT; + uint8_t tag = id - 1 + DeviceValueTAG::TAG_HC1; 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) { - flag = CommandFlag::MQTT_SUB_FLAG_DHW; + flag = CommandFlag::CMD_FLAG_DHW; } 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) { - flag = CommandFlag::MQTT_SUB_FLAG_AHS; + flag = CommandFlag::CMD_FLAG_AHS; } // see if there is a command registered 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 if (return_code != CommandRet::OK) { 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 { - 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); } @@ -430,7 +430,7 @@ Command::CmdFunction * Command::find_command(const uint8_t device_type, const ui for (auto & cf : cmdfunctions_) { 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; } } @@ -438,13 +438,13 @@ Command::CmdFunction * Command::find_command(const uint8_t device_type, const ui 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())) { return; } auto it = cmdfunctions_.begin(); 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); 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.]" + cmd; + case CommandFlag::CMD_FLAG_DHW: + return "dhw[n]." + cmd; + case CommandFlag::CMD_FLAG_HS: + return "hs." + cmd; + case CommandFlag::CMD_FLAG_AHS: + return "ahs." + cmd; + default: + return cmd; + } +} + // list all commands for a specific device, output as json bool Command::list(const uint8_t device_type, JsonObject output) { // 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 sorted_cmds; for (const auto & cf : cmdfunctions_) { 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(); for (const auto & cl : sorted_cmds) { 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.has_flags(CommandFlag::MQTT_SUB_FLAG_DHW)) { - 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_); - } + if ((cf.device_type_ == device_type) && !cf.has_flags(CommandFlag::HIDDEN) && cf.description_ && (cl == tagged_cmd(cf.cmd_, cf.flags_))) { + output[cl] = Helpers::translated_word(cf.description_); } } } - return true; } // output list of all commands to console for a specific DeviceType 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 std::list sorted_cmds; for (const auto & cf : cmdfunctions_) { 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) { // find and print the description 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(); shell.print(" "); - if (cf.has_flags(MQTT_SUB_FLAG_HC)) { - shell.print("[hc.]"); - i += 8; - } else if (cf.has_flags(MQTT_SUB_FLAG_DHW)) { - shell.print("[dhw.]"); - i += 9; - } else if (cf.has_flags(MQTT_SUB_FLAG_AHS)) { - shell.print("[ahs.]"); - i += 9; - } else if (cf.has_flags(MQTT_SUB_FLAG_HS)) { - shell.print("[hs.]"); - i += 8; - } shell.print(cl); // pad with spaces while (i++ < 30) { diff --git a/src/command.h b/src/command.h index 7f6191ff2..2abb27460 100644 --- a/src/command.h +++ b/src/command.h @@ -31,13 +31,13 @@ namespace emsesp { // mqtt flags for command subscriptions enum CommandFlag : uint8_t { - MQTT_SUB_FLAG_DEFAULT = 0, // 0 no flags set, always subscribe to MQTT - MQTT_SUB_FLAG_HC = (1 << 0), // 1 TAG_HC1 - TAG_HC8 - MQTT_SUB_FLAG_DHW = (1 << 1), // 2 TAG_DHW1 - TAG_DHW4 - MQTT_SUB_FLAG_AHS = (1 << 2), // 4 TAG_DEVICE_DATA_AHS - MQTT_SUB_FLAG_HS = (1 << 3), // 8 TAG_DEVICE_DATA_HS - HIDDEN = (1 << 6), // 64 do not show in API or Web - ADMIN_ONLY = (1 << 7) // 128 requires authentication + CMD_FLAG_DEFAULT = 0, // 0 no flags set, always subscribe to MQTT + CMD_FLAG_HC = (1 << 0), // 1 TAG_HC1 - TAG_HC8 + CMD_FLAG_DHW = (1 << 1), // 2 TAG_DHW1 - TAG_DHW4 + CMD_FLAG_AHS = (1 << 2), // 4 TAG_AHS1 + CMD_FLAG_HS = (1 << 3), // 8 TAG_HS1 - TAG_HS16 + HIDDEN = (1 << 6), // 64 do not show in API or Web + ADMIN_ONLY = (1 << 7) // 128 requires authentication }; @@ -110,26 +110,23 @@ class Command { const char * cmd, const cmd_function_p cb, 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 - static void add(const uint8_t device_type, - const char * cmd, - const cmd_function_p cb, - const char * const * description, - uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT); - + static void + 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); // callback function taking value, id and a json object for its output static void add(const uint8_t device_type, const char * cmd, const cmd_json_function_p cb, 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 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_devices(uuid::console::Shell & shell); static bool device_has_commands(const uint8_t device_type); diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index ac57eaa62..653b826d0 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -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 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) { - flags |= CommandFlag::MQTT_SUB_FLAG_DHW; + flags |= CommandFlag::CMD_FLAG_DHW; } 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) { - flags |= CommandFlag::MQTT_SUB_FLAG_AHS; + flags |= CommandFlag::CMD_FLAG_AHS; } // add the command to our library