From c387f65b4a520761c8410b6d64f592ebae8e2ec0 Mon Sep 17 00:00:00 2001 From: proddy Date: Thu, 21 Oct 2021 22:56:56 +0200 Subject: [PATCH] some minor refactor --- src/command.cpp | 8 ++------ src/command.h | 28 ++++++++++++++-------------- src/emsdevice.cpp | 2 +- src/mqtt.h | 2 +- src/system.cpp | 16 +++++++++++++--- 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index 2e955d447..36ec82cd8 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -166,7 +166,6 @@ Command::CmdFunction * Command::find_command(const uint8_t device_type, char * c } // add a command to the list, which does not return json -// these commands are not callable directly via MQTT subscriptions either void Command::add(const uint8_t device_type, const __FlashStringHelper * cmd, const cmd_function_p cb, const __FlashStringHelper * description, uint8_t flags) { // 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) { @@ -180,14 +179,11 @@ void Command::add(const uint8_t device_type, const __FlashStringHelper * cmd, co cmdfunctions_.emplace_back(device_type, flags, cmd, cb, nullptr, description); // callback for json is nullptr - // see if we need to subscribe - if (Mqtt::enabled()) { - Mqtt::register_command(device_type, cmd, cb, flags); - } + Mqtt::sub_command(device_type, cmd, cb, flags); } // add a command to the list, which does return a json object as output -// flag is fixed to MqttSubFlag::FLAG_NOSUB +// flag is fixed to MqttSubFlag::MQTT_SUB_FLAG_NOSUB so there will be no topic subscribed to this void Command::add_json(const uint8_t device_type, const __FlashStringHelper * cmd, const cmd_json_function_p cb, diff --git a/src/command.h b/src/command.h index 1feee8c48..f64a2a49e 100644 --- a/src/command.h +++ b/src/command.h @@ -36,23 +36,23 @@ namespace emsesp { // mqtt flags for command subscriptions enum CommandFlag : uint8_t { - MQTT_SUB_FLAG_NORMAL = 0, // 0 - MQTT_SUB_FLAG_HC = (1 << 0), // 1 - MQTT_SUB_FLAG_WWC = (1 << 1), // 2 - MQTT_SUB_FLAG_NOSUB = (1 << 2), // 4 - HIDDEN = (1 << 3), // 8 - ADMIN_ONLY = (1 << 4), // 16 - MQTT_SUB_FLAG_WW = (1 << 5) // 32 + MQTT_SUB_FLAG_DEFAULT = 0, // 0 no flags set, always subscribe to MQTT + MQTT_SUB_FLAG_HC = (1 << 0), // 1 TAG_HC1 - TAG_HC4 + MQTT_SUB_FLAG_WWC = (1 << 1), // 2 TAG_WWC1 - TAG_WWC4 + MQTT_SUB_FLAG_NOSUB = (1 << 2), // 4 + MQTT_SUB_FLAG_WW = (1 << 3), // 8 TAG_DEVICE_DATA_WW + HIDDEN = (1 << 4), // 16 do not show in API or Web + ADMIN_ONLY = (1 << 5) // 32 requires authentication }; // return status after calling a Command enum CommandRet : uint8_t { - ERRORED = 0, - OK, // 1 or TRUE - NOT_FOUND, // 2 - ERROR, // 3 - NOT_ALLOWED // needs authentication + ERRORED = 0, // 0 or FALSE + OK, // 1 or TRUE + NOT_FOUND, // 2 + ERROR, // 3 + NOT_ALLOWED // needs authentication }; @@ -108,13 +108,13 @@ class Command { const __FlashStringHelper * cmd, const cmd_function_p cb, const __FlashStringHelper * description, - uint8_t flags = CommandFlag::MQTT_SUB_FLAG_NORMAL); + uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT); static void add_json(const uint8_t device_type, const __FlashStringHelper * cmd, const cmd_json_function_p cb, const __FlashStringHelper * description, - uint8_t flags = CommandFlag::MQTT_SUB_FLAG_NORMAL); + uint8_t flags = CommandFlag::MQTT_SUB_FLAG_DEFAULT); static void show_all(uuid::console::Shell & shell); static Command::CmdFunction * find_command(const uint8_t device_type, const char * cmd); diff --git a/src/emsdevice.cpp b/src/emsdevice.cpp index dc213825b..a282a1bb4 100644 --- a/src/emsdevice.cpp +++ b/src/emsdevice.cpp @@ -537,7 +537,7 @@ void EMSdevice::register_device_value(uint8_t tag, } else if (tag == TAG_DEVICE_DATA_WW) { Command::add(device_type_, name[0], f, name[1], CommandFlag::MQTT_SUB_FLAG_WW | CommandFlag::ADMIN_ONLY); } else { - Command::add(device_type_, name[0], f, name[1], CommandFlag::MQTT_SUB_FLAG_NORMAL | CommandFlag::ADMIN_ONLY); + Command::add(device_type_, name[0], f, name[1], CommandFlag::ADMIN_ONLY); } } diff --git a/src/mqtt.h b/src/mqtt.h index 2f759c8fe..372c07633 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -127,7 +127,7 @@ class Mqtt { const __FlashStringHelper * entity, const uint8_t uom, const bool has_cmd = false); - static void register_command(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_p cb, uint8_t flags = 0); + static void sub_command(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_p cb, uint8_t flags = 0); static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type); static void show_mqtt(uuid::console::Shell & shell); diff --git a/src/system.cpp b/src/system.cpp index ace9e2720..28923754f 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -232,6 +232,10 @@ void System::syslog_start() { syslog_.mark_interval(syslog_mark_interval_); syslog_.destination(syslog_host_.c_str(), syslog_port_); syslog_.hostname(hostname().c_str()); + + // register the command + Command::add(EMSdevice::DeviceType::SYSTEM, F_(syslog_level), System::command_syslog_level, F("change syslog level"), CommandFlag::ADMIN_ONLY); + } else if (was_enabled) { // in case service is still running, this flushes the queue // https://github.com/emsesp/EMS-ESP/issues/496 @@ -287,8 +291,10 @@ void System::wifi_tweak() { bool s1 = WiFi.getSleep(); WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE bool s2 = WiFi.getSleep(); +#if defined(EMSESP_DEBUG) LOG_DEBUG(F("[DEBUG] Adjusting WiFi - Tx power %d->%d, Sleep %d->%d"), p1, p2, s1, s2); #endif +#endif } // check for valid ESP32 pins. This is very dependent on which ESP32 board is being used. @@ -669,14 +675,18 @@ void System::system_check() { // these commands respond to the topic "system" and take a payload like {cmd:"", data:"", id:""} // no individual subscribe for pin command because id is needed void System::commands_init() { - Command::add(EMSdevice::DeviceType::SYSTEM, F_(pin), System::command_pin, F("set GPIO"), CommandFlag::MQTT_SUB_FLAG_NOSUB | CommandFlag::ADMIN_ONLY); + Command::add(EMSdevice::DeviceType::SYSTEM, + F_(pin), + System::command_pin, + F("set GPIO"), + CommandFlag::MQTT_SUB_FLAG_NOSUB | CommandFlag::ADMIN_ONLY); // dont create a MQTT topic for this + Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, F("send a telegram"), CommandFlag::ADMIN_ONLY); - Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, F("force a MQTT publish"), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(fetch), System::command_fetch, F("refresh all EMS values"), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(restart), System::command_restart, F("restarts EMS-ESP"), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(watch), System::command_watch, F("watch incoming telegrams"), CommandFlag::ADMIN_ONLY); - Command::add(EMSdevice::DeviceType::SYSTEM, F_(syslog_level), System::command_syslog_level, F("set syslog level"), CommandFlag::ADMIN_ONLY); + // these commands will return data in JSON format Command::add_json(EMSdevice::DeviceType::SYSTEM, F_(info), System::command_info, F("system status")); Command::add_json(EMSdevice::DeviceType::SYSTEM, F_(settings), System::command_settings, F("list system settings")); Command::add_json(EMSdevice::DeviceType::SYSTEM, F_(commands), System::command_commands, F("list system commands"));