mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-30 10:39:12 +03:00
initial commit with refactored mqtt commands
This commit is contained in:
43
src/mqtt.h
43
src/mqtt.h
@@ -44,6 +44,8 @@ using uuid::console::Shell;
|
||||
namespace emsesp {
|
||||
|
||||
using mqtt_subfunction_p = std::function<void(const char * message)>;
|
||||
using mqtt_cmdfunction_p = std::function<void(const char * data, const int8_t id)>;
|
||||
|
||||
using namespace std::placeholders; // for `_1`
|
||||
|
||||
struct MqttMessage {
|
||||
@@ -68,20 +70,24 @@ class Mqtt {
|
||||
|
||||
static constexpr uint8_t MQTT_TOPIC_MAX_SIZE = 100;
|
||||
|
||||
static void subscribe(const uint8_t device_id, const std::string & topic, mqtt_subfunction_p cb);
|
||||
static void subscribe(const uint8_t device_type, const std::string & topic, mqtt_subfunction_p cb);
|
||||
static void subscribe(const std::string & topic, mqtt_subfunction_p cb);
|
||||
static void resubscribe();
|
||||
|
||||
static void add_command(const uint8_t device_type, const __FlashStringHelper * cmd, mqtt_cmdfunction_p cb);
|
||||
|
||||
static void publish(const std::string & topic, const std::string & payload, bool retain = false);
|
||||
static void publish(const std::string & topic, const JsonDocument & payload, bool retain = false);
|
||||
static void publish(const std::string & topic, const bool value);
|
||||
static void publish(const std::string & topic);
|
||||
|
||||
static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_id);
|
||||
static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type);
|
||||
static void show_mqtt(uuid::console::Shell & shell);
|
||||
|
||||
static void on_connect();
|
||||
|
||||
static bool call_command(const uint8_t device_type, const char * cmd, const char * value, const int8_t id);
|
||||
|
||||
void disconnect() {
|
||||
mqttClient_->disconnect();
|
||||
}
|
||||
@@ -102,6 +108,20 @@ class Mqtt {
|
||||
|
||||
static std::string hostname_;
|
||||
|
||||
class MQTTCmdFunction {
|
||||
public:
|
||||
MQTTCmdFunction(const uint8_t device_type, const __FlashStringHelper * cmd, mqtt_cmdfunction_p mqtt_cmdfunction);
|
||||
~MQTTCmdFunction() = default;
|
||||
|
||||
const uint8_t device_type_;
|
||||
const __FlashStringHelper * cmd_;
|
||||
mqtt_cmdfunction_p mqtt_cmdfunction_;
|
||||
};
|
||||
|
||||
static std::vector<MQTTCmdFunction> commands() {
|
||||
return mqtt_cmdfunctions_;
|
||||
}
|
||||
|
||||
private:
|
||||
static uuid::log::Logger logger_;
|
||||
|
||||
@@ -127,29 +147,32 @@ class Mqtt {
|
||||
static constexpr uint32_t MQTT_PUBLISH_WAIT = 200; // delay between sending publishes, to account for large payloads
|
||||
static constexpr uint8_t MQTT_PUBLISH_MAX_RETRY = 3; // max retries for giving up on publishing
|
||||
|
||||
static std::shared_ptr<const MqttMessage> queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, const bool retain, bool no_prefix = false);
|
||||
static std::shared_ptr<const MqttMessage>
|
||||
queue_message(const uint8_t operation, const std::string & topic, const std::string & payload, const bool retain, bool no_prefix = false);
|
||||
static std::shared_ptr<const MqttMessage> queue_publish_message(const std::string & topic, const std::string & payload, const bool retain);
|
||||
static std::shared_ptr<const MqttMessage> queue_subscribe_message(const std::string & topic);
|
||||
|
||||
void on_publish(uint16_t packetId);
|
||||
void on_message(char * topic, char * payload, size_t len);
|
||||
void process_queue();
|
||||
void process_all_queue();
|
||||
void on_publish(uint16_t packetId);
|
||||
void on_message(char * topic, char * payload, size_t len);
|
||||
void process_queue();
|
||||
void process_all_queue();
|
||||
|
||||
static uint16_t mqtt_publish_fails_;
|
||||
|
||||
// function handlers for MQTT subscriptions
|
||||
class MQTTSubFunction {
|
||||
public:
|
||||
MQTTSubFunction(const uint8_t device_id, const std::string && topic, mqtt_subfunction_p mqtt_subfunction);
|
||||
MQTTSubFunction(const uint8_t device_type, const std::string && topic, const std::string && full_topic, mqtt_subfunction_p mqtt_subfunction);
|
||||
~MQTTSubFunction() = default;
|
||||
|
||||
const uint8_t device_id_; // which device ID owns this
|
||||
const uint8_t device_type_; // which device type, from DeviceType::
|
||||
const std::string topic_;
|
||||
mqtt_subfunction_p mqtt_subfunction_;
|
||||
const std::string full_topic_; // the fully qualified topic name, usually with the hostname prefixed
|
||||
mqtt_subfunction_p mqtt_subfunction_; // can be empty
|
||||
};
|
||||
|
||||
static std::vector<MQTTSubFunction> mqtt_subfunctions_; // list of mqtt subscribe callbacks for all devices
|
||||
static std::vector<MQTTCmdFunction> mqtt_cmdfunctions_; // list of commands
|
||||
|
||||
uint32_t last_mqtt_poll_ = 0;
|
||||
uint32_t last_publish_ = 0;
|
||||
|
||||
Reference in New Issue
Block a user