mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
minor changes, make cppcheck happy
This commit is contained in:
@@ -167,14 +167,13 @@ 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
|
// these commands are not callable directly via MQTT subscriptions either
|
||||||
void Command::add(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_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) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the description is empty, it's hidden which means it will not show up in Web API or Console as an available command
|
// if the description is empty, it's hidden which means it will not show up in Web API or Console as an available command
|
||||||
// TODO check whether we still need this piece of code
|
|
||||||
if (description == nullptr) {
|
if (description == nullptr) {
|
||||||
flags |= CommandFlag::HIDDEN;
|
flags |= CommandFlag::HIDDEN;
|
||||||
}
|
}
|
||||||
@@ -189,7 +188,11 @@ void Command::add(const uint8_t device_type, const __FlashStringHelper * cmd, cm
|
|||||||
|
|
||||||
// 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::FLAG_NOSUB
|
||||||
void Command::add_json(const uint8_t device_type, const __FlashStringHelper * cmd, cmdfunction_json_p cb, const __FlashStringHelper * description, uint8_t flags) {
|
void Command::add_json(const uint8_t device_type,
|
||||||
|
const __FlashStringHelper * cmd,
|
||||||
|
const cmd_json_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) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ enum CommandRet : uint8_t {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using cmdfunction_p = std::function<bool(const char * data, const int8_t id)>;
|
using cmd_function_p = std::function<bool(const char * data, const int8_t id)>;
|
||||||
using cmdfunction_json_p = std::function<bool(const char * data, const int8_t id, JsonObject & json)>;
|
using cmd_json_function_p = std::function<bool(const char * data, const int8_t id, JsonObject & json)>;
|
||||||
|
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
@@ -64,15 +64,15 @@ class Command {
|
|||||||
uint8_t device_type_; // DeviceType::
|
uint8_t device_type_; // DeviceType::
|
||||||
uint8_t flags_; // mqtt flags for command subscriptions
|
uint8_t flags_; // mqtt flags for command subscriptions
|
||||||
const __FlashStringHelper * cmd_;
|
const __FlashStringHelper * cmd_;
|
||||||
cmdfunction_p cmdfunction_;
|
const cmd_function_p cmdfunction_;
|
||||||
cmdfunction_json_p cmdfunction_json_;
|
const cmd_json_function_p cmdfunction_json_;
|
||||||
const __FlashStringHelper * description_;
|
const __FlashStringHelper * description_;
|
||||||
|
|
||||||
CmdFunction(const uint8_t device_type,
|
CmdFunction(const uint8_t device_type,
|
||||||
const uint8_t flags,
|
const uint8_t flags,
|
||||||
const __FlashStringHelper * cmd,
|
const __FlashStringHelper * cmd,
|
||||||
cmdfunction_p cmdfunction,
|
const cmd_function_p cmdfunction,
|
||||||
cmdfunction_json_p cmdfunction_json,
|
const cmd_json_function_p cmdfunction_json,
|
||||||
const __FlashStringHelper * description)
|
const __FlashStringHelper * description)
|
||||||
: device_type_(device_type)
|
: device_type_(device_type)
|
||||||
, flags_(flags)
|
, flags_(flags)
|
||||||
@@ -105,13 +105,13 @@ class Command {
|
|||||||
|
|
||||||
static void add(const uint8_t device_type,
|
static void add(const uint8_t device_type,
|
||||||
const __FlashStringHelper * cmd,
|
const __FlashStringHelper * cmd,
|
||||||
cmdfunction_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_NORMAL);
|
||||||
|
|
||||||
static void add_json(const uint8_t device_type,
|
static void add_json(const uint8_t device_type,
|
||||||
const __FlashStringHelper * cmd,
|
const __FlashStringHelper * cmd,
|
||||||
cmdfunction_json_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_NORMAL);
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ enum DeviceValueType : uint8_t {
|
|||||||
TIME, // same as ULONG (32 bits)
|
TIME, // same as ULONG (32 bits)
|
||||||
ENUM,
|
ENUM,
|
||||||
TEXT,
|
TEXT,
|
||||||
CMD
|
CMD // special for commands only
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -239,7 +239,7 @@ class EMSdevice {
|
|||||||
|
|
||||||
using process_function_p = std::function<void(std::shared_ptr<const Telegram>)>;
|
using process_function_p = std::function<void(std::shared_ptr<const Telegram>)>;
|
||||||
|
|
||||||
void register_telegram_type(const uint16_t telegram_type_id, const __FlashStringHelper * telegram_type_name, bool fetch, process_function_p cb);
|
void register_telegram_type(const uint16_t telegram_type_id, const __FlashStringHelper * telegram_type_name, bool fetch, const process_function_p cb);
|
||||||
bool handle_telegram(std::shared_ptr<const Telegram> telegram);
|
bool handle_telegram(std::shared_ptr<const Telegram> telegram);
|
||||||
|
|
||||||
std::string get_value_uom(const char * key);
|
std::string get_value_uom(const char * key);
|
||||||
@@ -263,7 +263,7 @@ class EMSdevice {
|
|||||||
const __FlashStringHelper * const * options,
|
const __FlashStringHelper * const * options,
|
||||||
const __FlashStringHelper * const * name,
|
const __FlashStringHelper * const * name,
|
||||||
uint8_t uom,
|
uint8_t uom,
|
||||||
cmdfunction_p f,
|
const cmd_function_p f,
|
||||||
int32_t min,
|
int32_t min,
|
||||||
uint32_t max);
|
uint32_t max);
|
||||||
void register_device_value(uint8_t tag,
|
void register_device_value(uint8_t tag,
|
||||||
@@ -272,7 +272,7 @@ class EMSdevice {
|
|||||||
const __FlashStringHelper * const * options,
|
const __FlashStringHelper * const * options,
|
||||||
const __FlashStringHelper * const * name,
|
const __FlashStringHelper * const * name,
|
||||||
uint8_t uom,
|
uint8_t uom,
|
||||||
cmdfunction_p f);
|
const cmd_function_p f);
|
||||||
void register_device_value(uint8_t tag,
|
void register_device_value(uint8_t tag,
|
||||||
void * value_p,
|
void * value_p,
|
||||||
uint8_t type,
|
uint8_t type,
|
||||||
@@ -286,7 +286,7 @@ class EMSdevice {
|
|||||||
|
|
||||||
void read_command(const uint16_t type_id, uint8_t offset = 0, uint8_t length = 0);
|
void read_command(const uint16_t type_id, uint8_t offset = 0, uint8_t length = 0);
|
||||||
|
|
||||||
void register_mqtt_topic(const std::string & topic, mqtt_subfunction_p f);
|
void register_mqtt_topic(const std::string & topic, const mqtt_sub_function_p f);
|
||||||
|
|
||||||
void publish_mqtt_ha_sensor();
|
void publish_mqtt_ha_sensor();
|
||||||
|
|
||||||
@@ -402,7 +402,7 @@ class EMSdevice {
|
|||||||
bool fetch_; // if this type_id be queried automatically
|
bool fetch_; // if this type_id be queried automatically
|
||||||
process_function_p process_function_;
|
process_function_p process_function_;
|
||||||
|
|
||||||
TelegramFunction(uint16_t telegram_type_id, const __FlashStringHelper * telegram_type_name, bool fetch, process_function_p process_function)
|
TelegramFunction(uint16_t telegram_type_id, const __FlashStringHelper * telegram_type_name, bool fetch, const process_function_p process_function)
|
||||||
: telegram_type_id_(telegram_type_id)
|
: telegram_type_id_(telegram_type_id)
|
||||||
, telegram_type_name_(telegram_type_name)
|
, telegram_type_name_(telegram_type_name)
|
||||||
, fetch_(fetch)
|
, fetch_(fetch)
|
||||||
|
|||||||
@@ -66,7 +66,7 @@
|
|||||||
|
|
||||||
// helpers for callback functions
|
// helpers for callback functions
|
||||||
#define MAKE_PF_CB(__f) [&](std::shared_ptr<const Telegram> t) { __f(t); } // for Process Function callbacks to EMSDevice::process_function_p
|
#define MAKE_PF_CB(__f) [&](std::shared_ptr<const Telegram> t) { __f(t); } // for Process Function callbacks to EMSDevice::process_function_p
|
||||||
#define MAKE_CF_CB(__f) [&](const char * value, const int8_t id) { return __f(value, id); } // for Command Function callbacks Command::cmdfunction_p
|
#define MAKE_CF_CB(__f) [&](const char * value, const int8_t id) { return __f(value, id); } // for Command Function callbacks Command::cmd_function_p
|
||||||
|
|
||||||
namespace emsesp {
|
namespace emsesp {
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ uuid::log::Logger Mqtt::logger_{F_(mqtt), uuid::log::Facility::DAEMON};
|
|||||||
|
|
||||||
// subscribe to an MQTT topic, and store the associated callback function
|
// subscribe to an MQTT topic, and store the associated callback function
|
||||||
// only if it already hasn't been added
|
// only if it already hasn't been added
|
||||||
void Mqtt::subscribe(const uint8_t device_type, const std::string & topic, mqtt_subfunction_p cb) {
|
void Mqtt::subscribe(const uint8_t device_type, const std::string & topic, mqtt_sub_function_p cb) {
|
||||||
// check if we already have the topic subscribed, if so don't add it again
|
// check if we already have the topic subscribed, if so don't add it again
|
||||||
if (!mqtt_subfunctions_.empty()) {
|
if (!mqtt_subfunctions_.empty()) {
|
||||||
for (auto & mqtt_subfunction : mqtt_subfunctions_) {
|
for (auto & mqtt_subfunction : mqtt_subfunctions_) {
|
||||||
@@ -126,7 +126,7 @@ void Mqtt::register_command(const uint8_t device_type, const __FlashStringHelper
|
|||||||
|
|
||||||
// subscribe to an MQTT topic, and store the associated callback function
|
// subscribe to an MQTT topic, and store the associated callback function
|
||||||
// For generic functions not tied to a specific device
|
// For generic functions not tied to a specific device
|
||||||
void Mqtt::subscribe(const std::string & topic, mqtt_subfunction_p cb) {
|
void Mqtt::subscribe(const std::string & topic, mqtt_sub_function_p cb) {
|
||||||
subscribe(0, topic, cb); // no device_id needed if generic to EMS-ESP
|
subscribe(0, topic, cb); // no device_id needed if generic to EMS-ESP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
src/mqtt.h
10
src/mqtt.h
@@ -46,7 +46,7 @@ using uuid::console::Shell;
|
|||||||
|
|
||||||
namespace emsesp {
|
namespace emsesp {
|
||||||
|
|
||||||
using mqtt_subfunction_p = std::function<bool(const char * message)>;
|
using mqtt_sub_function_p = std::function<bool(const char * message)>;
|
||||||
using cmdfunction_p = std::function<bool(const char * data, const int8_t id)>;
|
using cmdfunction_p = std::function<bool(const char * data, const int8_t id)>;
|
||||||
|
|
||||||
struct MqttMessage {
|
struct MqttMessage {
|
||||||
@@ -100,8 +100,8 @@ class Mqtt {
|
|||||||
|
|
||||||
static void on_connect();
|
static void on_connect();
|
||||||
|
|
||||||
static void subscribe(const uint8_t device_type, const std::string & topic, mqtt_subfunction_p cb);
|
static void subscribe(const uint8_t device_type, const std::string & topic, mqtt_sub_function_p cb);
|
||||||
static void subscribe(const std::string & topic, mqtt_subfunction_p cb);
|
static void subscribe(const std::string & topic, mqtt_sub_function_p cb);
|
||||||
static void resubscribe();
|
static void resubscribe();
|
||||||
|
|
||||||
static void publish(const std::string & topic, const std::string & payload);
|
static void publish(const std::string & topic, const std::string & payload);
|
||||||
@@ -241,9 +241,9 @@ class Mqtt {
|
|||||||
struct MQTTSubFunction {
|
struct MQTTSubFunction {
|
||||||
uint8_t device_type_; // which device type, from DeviceType::
|
uint8_t device_type_; // which device type, from DeviceType::
|
||||||
const std::string topic_; // short topic name
|
const std::string topic_; // short topic name
|
||||||
mqtt_subfunction_p mqtt_subfunction_; // can be empty
|
mqtt_sub_function_p mqtt_subfunction_; // can be empty
|
||||||
|
|
||||||
MQTTSubFunction(uint8_t device_type, const std::string && topic, mqtt_subfunction_p mqtt_subfunction)
|
MQTTSubFunction(uint8_t device_type, const std::string && topic, mqtt_sub_function_p mqtt_subfunction)
|
||||||
: device_type_(device_type)
|
: device_type_(device_type)
|
||||||
, topic_(topic)
|
, topic_(topic)
|
||||||
, mqtt_subfunction_(mqtt_subfunction) {
|
, mqtt_subfunction_(mqtt_subfunction) {
|
||||||
|
|||||||
Reference in New Issue
Block a user