ESPHome  2022.12.8
mqtt_backend_arduino.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ARDUINO
4 
5 #include "mqtt_backend.h"
6 #include <AsyncMqttClient.h>
7 
8 namespace esphome {
9 namespace mqtt {
10 
11 class MQTTBackendArduino final : public MQTTBackend {
12  public:
13  void set_keep_alive(uint16_t keep_alive) final { mqtt_client_.setKeepAlive(keep_alive); }
14  void set_client_id(const char *client_id) final { mqtt_client_.setClientId(client_id); }
15  void set_clean_session(bool clean_session) final { mqtt_client_.setCleanSession(clean_session); }
16  void set_credentials(const char *username, const char *password) final {
17  mqtt_client_.setCredentials(username, password);
18  }
19  void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final {
20  mqtt_client_.setWill(topic, qos, retain, payload);
21  }
22  void set_server(network::IPAddress ip, uint16_t port) final {
23  mqtt_client_.setServer(IPAddress(static_cast<uint32_t>(ip)), port);
24  }
25  void set_server(const char *host, uint16_t port) final { mqtt_client_.setServer(host, port); }
26 #if ASYNC_TCP_SSL_ENABLED
27  void set_secure(bool secure) { mqtt_client.setSecure(secure); }
28  void add_server_fingerprint(const uint8_t *fingerprint) { mqtt_client.addServerFingerprint(fingerprint); }
29 #endif
30 
31  void set_on_connect(std::function<on_connect_callback_t> &&callback) final {
32  this->mqtt_client_.onConnect(std::move(callback));
33  }
34  void set_on_disconnect(std::function<on_disconnect_callback_t> &&callback) final {
35  auto async_callback = [callback](AsyncMqttClientDisconnectReason reason) {
36  // int based enum so casting isn't a problem
37  callback(static_cast<MQTTClientDisconnectReason>(reason));
38  };
39  this->mqtt_client_.onDisconnect(std::move(async_callback));
40  }
41  void set_on_subscribe(std::function<on_subscribe_callback_t> &&callback) final {
42  this->mqtt_client_.onSubscribe(std::move(callback));
43  }
44  void set_on_unsubscribe(std::function<on_unsubscribe_callback_t> &&callback) final {
45  this->mqtt_client_.onUnsubscribe(std::move(callback));
46  }
47  void set_on_message(std::function<on_message_callback_t> &&callback) final {
48  auto async_callback = [callback](const char *topic, const char *payload,
49  AsyncMqttClientMessageProperties async_properties, size_t len, size_t index,
50  size_t total) { callback(topic, payload, len, index, total); };
51  mqtt_client_.onMessage(std::move(async_callback));
52  }
53  void set_on_publish(std::function<on_publish_user_callback_t> &&callback) final {
54  this->mqtt_client_.onPublish(std::move(callback));
55  }
56 
57  bool connected() const final { return mqtt_client_.connected(); }
58  void connect() final { mqtt_client_.connect(); }
59  void disconnect() final { mqtt_client_.disconnect(true); }
60  bool subscribe(const char *topic, uint8_t qos) final { return mqtt_client_.subscribe(topic, qos) != 0; }
61  bool unsubscribe(const char *topic) final { return mqtt_client_.unsubscribe(topic) != 0; }
62  bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final {
63  return mqtt_client_.publish(topic, qos, retain, payload, length, false, 0) != 0;
64  }
66 
67  protected:
68  AsyncMqttClient mqtt_client_;
69 };
70 
71 } // namespace mqtt
72 } // namespace esphome
73 
74 #endif // defined(USE_ARDUINO)
void add_server_fingerprint(const uint8_t *fingerprint)
void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final
void set_on_disconnect(std::function< on_disconnect_callback_t > &&callback) final
bool subscribe(const char *topic, uint8_t qos) final
void set_on_unsubscribe(std::function< on_unsubscribe_callback_t > &&callback) final
void set_client_id(const char *client_id) final
void set_on_connect(std::function< on_connect_callback_t > &&callback) final
void set_server(network::IPAddress ip, uint16_t port) final
void set_keep_alive(uint16_t keep_alive) final
bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final
std::string size_t len
Definition: helpers.h:281
void set_on_publish(std::function< on_publish_user_callback_t > &&callback) final
void set_credentials(const char *username, const char *password) final
void set_on_subscribe(std::function< on_subscribe_callback_t > &&callback) final
Definition: a4988.cpp:4
void set_clean_session(bool clean_session) final
bool unsubscribe(const char *topic) final
void set_server(const char *host, uint16_t port) final
void set_on_message(std::function< on_message_callback_t > &&callback) final
virtual bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain)=0