From 4a269fd5087313c80238c476b6c35f59e50cf263 Mon Sep 17 00:00:00 2001 From: proddy Date: Sat, 8 May 2021 18:41:46 +0200 Subject: [PATCH] added hidden commands, sort commands --- src/command.cpp | 20 +++++++++++++++----- src/command.h | 13 ++++++++++--- src/emsesp.h | 2 +- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index ca0c376d4..80c266397 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -140,13 +140,14 @@ void Command::add(const uint8_t device_type, const __FlashStringHelper * cmd, cm } // add a command to the list, which does return json object as output -void Command::add_with_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb) { +// optional parameter hidden for commands that will not show up on the Console +void Command::add_with_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb, bool hidden) { // if the command already exists for that device type don't add it if (find_command(device_type, uuid::read_flash_string(cmd).c_str()) != nullptr) { return; } - cmdfunctions_.emplace_back(device_type, MqttSubFlag::FLAG_NOSUB, cmd, nullptr, cb); // add command + cmdfunctions_.emplace_back(device_type, MqttSubFlag::FLAG_NOSUB, cmd, nullptr, cb, hidden); // add command } // see if a command exists for that device type @@ -175,14 +176,23 @@ Command::CmdFunction * Command::find_command(const uint8_t device_type, const ch // output list of all commands to console for a specific DeviceType void Command::show(uuid::console::Shell & shell, uint8_t device_type) { if (cmdfunctions_.empty()) { - shell.println(F("No commands")); + shell.println(F("No commands available")); } + // create a list of commands, sort them, print them + std::list sorted_cmds; for (const auto & cf : cmdfunctions_) { - if (cf.device_type_ == device_type) { - shell.printf("%s ", uuid::read_flash_string(cf.cmd_).c_str()); + if ((cf.device_type_ == device_type) && !cf.hidden_) { + sorted_cmds.push_back(uuid::read_flash_string(cf.cmd_)); } } + + sorted_cmds.sort(); + for (auto & cl : sorted_cmds) { + shell.print(cl); + shell.print(" "); + } + shell.println(); } diff --git a/src/command.h b/src/command.h index c6e7da5d6..9c3e9ec0c 100644 --- a/src/command.h +++ b/src/command.h @@ -45,13 +45,20 @@ class Command { const __FlashStringHelper * cmd_; cmdfunction_p cmdfunction_; cmdfunction_json_p cmdfunction_json_; + bool hidden_; // if its command not to be shown on the Console - CmdFunction(const uint8_t device_type, const uint8_t flag, const __FlashStringHelper * cmd, cmdfunction_p cmdfunction, cmdfunction_json_p cmdfunction_json) + CmdFunction(const uint8_t device_type, + const uint8_t flag, + const __FlashStringHelper * cmd, + cmdfunction_p cmdfunction, + cmdfunction_json_p cmdfunction_json, + bool hidden = false) : device_type_(device_type) , flag_(flag) , cmd_(cmd) , cmdfunction_(cmdfunction) - , cmdfunction_json_(cmdfunction_json) { + , cmdfunction_json_(cmdfunction_json) + , hidden_(hidden) { } }; @@ -62,7 +69,7 @@ class Command { static bool call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id, JsonObject & json); static bool call(const uint8_t device_type, const char * cmd, const char * value, const int8_t id = 0); static void add(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_p cb, uint8_t flag = 0); - static void add_with_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb); + static void add_with_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb, bool hidden = false); static void show_all(uuid::console::Shell & shell); static Command::CmdFunction * find_command(const uint8_t device_type, const char * cmd); static char * check_command(char * out, const char * cmd, int8_t & id); diff --git a/src/emsesp.h b/src/emsesp.h index 40dc7aa13..54fcbab0e 100644 --- a/src/emsesp.h +++ b/src/emsesp.h @@ -215,7 +215,7 @@ class EMSESP { static void process_version(std::shared_ptr telegram); static void publish_response(std::shared_ptr telegram); static void publish_all_loop(); - static bool command_info(uint8_t device_type, JsonObject & json, const int8_t id); + static bool command_info(uint8_t device_type, JsonObject & json, const int8_t id, bool verbose = true); static constexpr uint32_t EMS_FETCH_FREQUENCY = 60000; // check every minute static uint32_t last_fetch_;