| 2 |
raymond |
1 |
#ifndef __I_MQTT_REMOTE_H__
|
|
|
2 |
#define __I_MQTT_REMOTE_H__
|
|
|
3 |
|
|
|
4 |
#include <cstdint>
|
|
|
5 |
#include <functional>
|
|
|
6 |
#include <string>
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* @brief Inteface to separate the concerns between the actual MQTT implementation that has a lifecycle, connection
|
|
|
10 |
* handling, and consumers who only need the API itself.
|
|
|
11 |
*/
|
|
|
12 |
class IMQTTRemote {
|
|
|
13 |
public:
|
|
|
14 |
// First parameter is topic, second one is the message.
|
|
|
15 |
typedef std::function<void(std::string, std::string)> SubscriptionCallback;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @brief Publish a message.
|
|
|
19 |
*
|
|
|
20 |
* @param topic the topic to publish to.
|
|
|
21 |
* @param message The message to send. This cannot be larger than the value set for max_message_size in the
|
|
|
22 |
* constructor.
|
|
|
23 |
* @param retain True to set this message as retained.
|
|
|
24 |
* @param qos quality of service for published message (0 (default), 1 or 2)
|
|
|
25 |
* @returns true on success, or false on failure.
|
|
|
26 |
*/
|
|
|
27 |
virtual bool publishMessage(std::string topic, std::string message, bool retain = false, uint8_t qos = 0) = 0;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Same as publishMessage(), but will print the message and topic and the result in console.
|
|
|
31 |
*/
|
|
|
32 |
virtual bool publishMessageVerbose(std::string topic, std::string message, bool retain = false, uint8_t qos = 0) = 0;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @brief Subscribe to a topic. The callback will be invoked on every new message.
|
|
|
36 |
* There can only be one callback per topic. If trying to subscribe to an already subscribe topic, it will be ignored.
|
|
|
37 |
* Don't do have operations in the callback or delays as this will block the MQTT callback.
|
|
|
38 |
* If not connected, will subscribe to this topic once connected.
|
|
|
39 |
*
|
|
|
40 |
* @param message_callback a message callback with the topic and the message. The topic is repeated for convinience,
|
|
|
41 |
* but it will always be for the subscribed topic.
|
|
|
42 |
*/
|
|
|
43 |
virtual bool subscribe(std::string topic, SubscriptionCallback message_callback) = 0;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* @brief Unsubscribe a topic.
|
|
|
47 |
*/
|
|
|
48 |
virtual bool unsubscribe(std::string topic) = 0;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @brief returns if there is a connection to the MQTT server.
|
|
|
52 |
*/
|
|
|
53 |
virtual bool connected() = 0;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* @brief The client ID for this device. This is used for the last will / status
|
|
|
57 |
* topic.Example, if this is "esp_now_router", then the status/last will topic will be "esp_now_router/status". This
|
|
|
58 |
* has to be [a-zA-Z0-9_] only.
|
|
|
59 |
*/
|
|
|
60 |
virtual std::string &clientId() = 0;
|
|
|
61 |
};
|
|
|
62 |
|
|
|
63 |
#endif // __I_MQTT_REMOTE_H__
|