31 Event(
const esp_mqtt_event_t &event)
32 : event_id(event.event_id),
33 data(event.data, event.data + event.data_len),
34 total_data_len(event.total_data_len),
35 current_data_offset(event.current_data_offset),
36 topic(event.topic, event.topic_len),
41 session_present(event.session_present),
42 error_handle(*event.error_handle) {}
47 static const size_t MQTT_BUFFER_SIZE = 4096;
49 void set_keep_alive(uint16_t keep_alive)
final { this->keep_alive_ = keep_alive; }
50 void set_client_id(
const char *client_id)
final { this->client_id_ = client_id; }
55 this->username_ = username;
57 this->password_ = password;
59 void set_will(
const char *topic, uint8_t qos,
bool retain,
const char *payload)
final {
61 this->lwt_topic_ = topic;
64 this->lwt_message_ = payload;
65 this->lwt_retain_ = retain;
68 this->host_ = ip.str();
76 this->on_connect_.add(std::move(callback));
79 this->on_disconnect_.add(std::move(callback));
82 this->on_subscribe_.add(std::move(callback));
85 this->on_unsubscribe_.add(std::move(callback));
88 this->on_message_.add(std::move(callback));
90 void set_on_publish(std::function<on_publish_user_callback_t> &&callback)
final {
91 this->on_publish_.add(std::move(callback));
93 bool connected() const final {
return this->is_connected_; }
96 if (!is_initalized_) {
98 esp_mqtt_client_start(handler_.get());
104 esp_mqtt_client_disconnect(handler_.get());
108 return esp_mqtt_client_subscribe(handler_.get(), topic, qos) != -1;
110 bool unsubscribe(
const char *topic)
final {
return esp_mqtt_client_unsubscribe(handler_.get(), topic) != -1; }
112 bool publish(
const char *topic,
const char *payload,
size_t length, uint8_t qos,
bool retain)
final {
113 #if defined(USE_MQTT_IDF_ENQUEUE) 116 return esp_mqtt_client_enqueue(handler_.get(), topic, payload,
length, qos, retain,
true) != -1;
120 return esp_mqtt_client_publish(handler_.get(), topic, payload,
length, qos, retain) != -1;
134 void mqtt_event_handler_(
const Event &event);
135 static void mqtt_event_handler(
void *handler_args, esp_event_base_t base, int32_t
event_id,
void *event_data);
138 void operator()(esp_mqtt_client *client_handler) { esp_mqtt_client_destroy(client_handler); }
143 bool is_connected_{
false};
144 bool is_initalized_{
false};
146 esp_mqtt_client_config_t mqtt_cfg_{};
162 bool skip_cert_cn_check_{
false};
void operator()(esp_mqtt_client *client_handler)
CallbackManager< on_subscribe_callback_t > on_subscribe_
void set_on_disconnect(std::function< on_disconnect_callback_t > &&callback) final
void set_on_publish(std::function< on_publish_user_callback_t > &&callback) final
void set_server(const char *host, uint16_t port) final
Event(const esp_mqtt_event_t &event)
bool unsubscribe(const char *topic) final
void set_server(network::IPAddress ip, uint16_t port) final
optional< std::string > ca_certificate_
void set_on_subscribe(std::function< on_subscribe_callback_t > &&callback) final
void set_client_id(const char *client_id) final
CallbackManager< on_unsubscribe_callback_t > on_unsubscribe_
void set_cl_key(const std::string &key)
std::queue< Event > mqtt_events_
void set_ca_certificate(const std::string &cert)
std::unique_ptr< esp_mqtt_client, MqttClientDeleter > ClientHandler_
bool connected() const final
void set_credentials(const char *username, const char *password) final
void set_keep_alive(uint16_t keep_alive) final
esp_mqtt_error_codes_t error_handle
void set_clean_session(bool clean_session) final
void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final
void set_on_unsubscribe(std::function< on_unsubscribe_callback_t > &&callback) final
void set_skip_cert_cn_check(bool skip_check)
bool subscribe(const char *topic, uint8_t qos) final
CallbackManager< on_connect_callback_t > on_connect_
void set_on_message(std::function< on_message_callback_t > &&callback) final
esp_mqtt_event_id_t event_id
bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final
CallbackManager< on_message_callback_t > on_message_
CallbackManager< on_publish_user_callback_t > on_publish_
Implementation of SPI Controller mode.
void set_cl_certificate(const std::string &cert)
CallbackManager< on_disconnect_callback_t > on_disconnect_
optional< std::string > cl_certificate_
optional< std::string > cl_key_
void set_on_connect(std::function< on_connect_callback_t > &&callback) final
virtual bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain)=0