mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
some minor refactor
This commit is contained in:
@@ -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
|
// 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) {
|
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 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) {
|
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
|
cmdfunctions_.emplace_back(device_type, flags, cmd, cb, nullptr, description); // callback for json is nullptr
|
||||||
|
|
||||||
// see if we need to subscribe
|
Mqtt::sub_command(device_type, cmd, cb, flags);
|
||||||
if (Mqtt::enabled()) {
|
|
||||||
Mqtt::register_command(device_type, cmd, cb, flags);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add a command to the list, which does return a json object as output
|
// 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,
|
void Command::add_json(const uint8_t device_type,
|
||||||
const __FlashStringHelper * cmd,
|
const __FlashStringHelper * cmd,
|
||||||
const cmd_json_function_p cb,
|
const cmd_json_function_p cb,
|
||||||
|
|||||||
@@ -36,23 +36,23 @@ namespace emsesp {
|
|||||||
|
|
||||||
// mqtt flags for command subscriptions
|
// mqtt flags for command subscriptions
|
||||||
enum CommandFlag : uint8_t {
|
enum CommandFlag : uint8_t {
|
||||||
MQTT_SUB_FLAG_NORMAL = 0, // 0
|
MQTT_SUB_FLAG_DEFAULT = 0, // 0 no flags set, always subscribe to MQTT
|
||||||
MQTT_SUB_FLAG_HC = (1 << 0), // 1
|
MQTT_SUB_FLAG_HC = (1 << 0), // 1 TAG_HC1 - TAG_HC4
|
||||||
MQTT_SUB_FLAG_WWC = (1 << 1), // 2
|
MQTT_SUB_FLAG_WWC = (1 << 1), // 2 TAG_WWC1 - TAG_WWC4
|
||||||
MQTT_SUB_FLAG_NOSUB = (1 << 2), // 4
|
MQTT_SUB_FLAG_NOSUB = (1 << 2), // 4
|
||||||
HIDDEN = (1 << 3), // 8
|
MQTT_SUB_FLAG_WW = (1 << 3), // 8 TAG_DEVICE_DATA_WW
|
||||||
ADMIN_ONLY = (1 << 4), // 16
|
HIDDEN = (1 << 4), // 16 do not show in API or Web
|
||||||
MQTT_SUB_FLAG_WW = (1 << 5) // 32
|
ADMIN_ONLY = (1 << 5) // 32 requires authentication
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// return status after calling a Command
|
// return status after calling a Command
|
||||||
enum CommandRet : uint8_t {
|
enum CommandRet : uint8_t {
|
||||||
ERRORED = 0,
|
ERRORED = 0, // 0 or FALSE
|
||||||
OK, // 1 or TRUE
|
OK, // 1 or TRUE
|
||||||
NOT_FOUND, // 2
|
NOT_FOUND, // 2
|
||||||
ERROR, // 3
|
ERROR, // 3
|
||||||
NOT_ALLOWED // needs authentication
|
NOT_ALLOWED // needs authentication
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -108,13 +108,13 @@ class Command {
|
|||||||
const __FlashStringHelper * cmd,
|
const __FlashStringHelper * cmd,
|
||||||
const cmd_function_p cb,
|
const cmd_function_p cb,
|
||||||
const __FlashStringHelper * description,
|
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,
|
static void add_json(const uint8_t device_type,
|
||||||
const __FlashStringHelper * cmd,
|
const __FlashStringHelper * cmd,
|
||||||
const cmd_json_function_p cb,
|
const cmd_json_function_p cb,
|
||||||
const __FlashStringHelper * description,
|
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 void show_all(uuid::console::Shell & shell);
|
||||||
static Command::CmdFunction * find_command(const uint8_t device_type, const char * cmd);
|
static Command::CmdFunction * find_command(const uint8_t device_type, const char * cmd);
|
||||||
|
|||||||
@@ -537,7 +537,7 @@ void EMSdevice::register_device_value(uint8_t tag,
|
|||||||
} else if (tag == TAG_DEVICE_DATA_WW) {
|
} else if (tag == TAG_DEVICE_DATA_WW) {
|
||||||
Command::add(device_type_, name[0], f, name[1], CommandFlag::MQTT_SUB_FLAG_WW | CommandFlag::ADMIN_ONLY);
|
Command::add(device_type_, name[0], f, name[1], CommandFlag::MQTT_SUB_FLAG_WW | CommandFlag::ADMIN_ONLY);
|
||||||
} else {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ class Mqtt {
|
|||||||
const __FlashStringHelper * entity,
|
const __FlashStringHelper * entity,
|
||||||
const uint8_t uom,
|
const uint8_t uom,
|
||||||
const bool has_cmd = false);
|
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_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type);
|
||||||
static void show_mqtt(uuid::console::Shell & shell);
|
static void show_mqtt(uuid::console::Shell & shell);
|
||||||
|
|||||||
@@ -232,6 +232,10 @@ void System::syslog_start() {
|
|||||||
syslog_.mark_interval(syslog_mark_interval_);
|
syslog_.mark_interval(syslog_mark_interval_);
|
||||||
syslog_.destination(syslog_host_.c_str(), syslog_port_);
|
syslog_.destination(syslog_host_.c_str(), syslog_port_);
|
||||||
syslog_.hostname(hostname().c_str());
|
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) {
|
} else if (was_enabled) {
|
||||||
// in case service is still running, this flushes the queue
|
// in case service is still running, this flushes the queue
|
||||||
// https://github.com/emsesp/EMS-ESP/issues/496
|
// https://github.com/emsesp/EMS-ESP/issues/496
|
||||||
@@ -287,8 +291,10 @@ void System::wifi_tweak() {
|
|||||||
bool s1 = WiFi.getSleep();
|
bool s1 = WiFi.getSleep();
|
||||||
WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE
|
WiFi.setSleep(false); // turn off sleep - WIFI_PS_NONE
|
||||||
bool s2 = WiFi.getSleep();
|
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);
|
LOG_DEBUG(F("[DEBUG] Adjusting WiFi - Tx power %d->%d, Sleep %d->%d"), p1, p2, s1, s2);
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for valid ESP32 pins. This is very dependent on which ESP32 board is being used.
|
// 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:""}
|
// 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
|
// no individual subscribe for pin command because id is needed
|
||||||
void System::commands_init() {
|
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_(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_(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_(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_(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_(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_(settings), System::command_settings, F("list system settings"));
|
||||||
Command::add_json(EMSdevice::DeviceType::SYSTEM, F_(commands), System::command_commands, F("list system commands"));
|
Command::add_json(EMSdevice::DeviceType::SYSTEM, F_(commands), System::command_commands, F("list system commands"));
|
||||||
|
|||||||
Reference in New Issue
Block a user