mqtt_client.h
Go to the documentation of this file.
1 #ifndef MQTT_CLIENT_H
2 #define MQTT_CLIENT_H
3 
4 #include "mosquitto_config.h"
5 #include <string>
6 #include <functional>
7 #include <thread>
8 #include <unordered_map>
9 #include <unordered_set>
10 #include <mutex>
11 #include <memory>
12 #include <QObject>
13 
14 class MQTTClient : public QObject
15 {
16  Q_OBJECT
17 
18 public:
19  using Ptr = std::shared_ptr<MQTTClient>;
20 
21  MQTTClient();
22  ~MQTTClient();
23 
24  bool connect(const MosquittoConfig& config);
25 
26  void disconnect();
27 
28  bool isConnected() const;
29 
30  using TopicCallback = std::function<void(const mosquitto_message*)>;
31  void addMessageCallback(const std::string& topic, TopicCallback callback);
32 
33  bool _connected = false;
34 
35  void onMessageReceived(const mosquitto_message* message);
36 
37  const MosquittoConfig& config() const;
38 
39  std::unordered_set<std::string> getTopicList();
40 
41  void subscribe(const std::string& topic, int qos);
42 
43  void unsubscribe(const std::string& topic);
44 
45 signals:
46 
47  void disconnected();
48 
49 private:
51 
52  mosquitto* _mosq = nullptr;
53  std::unordered_map<std::string, TopicCallback> _message_callbacks;
54  std::unordered_set<std::string> _topics_set;
55  std::mutex _mutex;
57  std::thread _thread;
58 };
59 
60 #endif // MQTT_CLIENT_H
MQTTClient::subscribe
void subscribe(const std::string &topic, int qos)
Definition: mqtt_client.cpp:290
MQTTClient::connect
bool connect(const MosquittoConfig &config)
Definition: mqtt_client.cpp:101
MQTTClient::configureMosquitto
bool configureMosquitto(const MosquittoConfig &config)
Definition: mqtt_client.cpp:125
MQTTClient::onMessageReceived
void onMessageReceived(const mosquitto_message *message)
Definition: mqtt_client.cpp:266
MQTTClient::disconnect
void disconnect()
Definition: mqtt_client.cpp:236
MQTTClient::MQTTClient
MQTTClient()
Definition: mqtt_client.cpp:81
MQTTClient::TopicCallback
std::function< void(const mosquitto_message *)> TopicCallback
Definition: mqtt_client.h:30
MQTTClient::_thread
std::thread _thread
Definition: mqtt_client.h:57
MQTTClient
Definition: mqtt_client.h:14
MQTTClient::config
const MosquittoConfig & config() const
Definition: mqtt_client.cpp:279
MQTTClient::_connected
bool _connected
Definition: mqtt_client.h:33
MQTTClient::_topics_set
std::unordered_set< std::string > _topics_set
Definition: mqtt_client.h:54
mosquitto_config.h
MQTTClient::_config
MosquittoConfig _config
Definition: mqtt_client.h:56
start_test_publisher.topic
topic
Definition: start_test_publisher.py:22
MQTTClient::unsubscribe
void unsubscribe(const std::string &topic)
Definition: mqtt_client.cpp:298
MosquittoConfig
Definition: mosquitto_config.h:8
nlohmann::detail::void
j template void())
Definition: json.hpp:4061
MQTTClient::disconnected
void disconnected()
MQTTClient::_message_callbacks
std::unordered_map< std::string, TopicCallback > _message_callbacks
Definition: mqtt_client.h:53
MQTTClient::~MQTTClient
~MQTTClient()
Definition: mqtt_client.cpp:92
MQTTClient::Ptr
std::shared_ptr< MQTTClient > Ptr
Definition: mqtt_client.h:19
MQTTClient::_mosq
mosquitto * _mosq
Definition: mqtt_client.h:52
MQTTClient::isConnected
bool isConnected() const
Definition: mqtt_client.cpp:254
MQTTClient::addMessageCallback
void addMessageCallback(const std::string &topic, TopicCallback callback)
Definition: mqtt_client.cpp:259
MQTTClient::_mutex
std::mutex _mutex
Definition: mqtt_client.h:55
MQTTClient::getTopicList
std::unordered_set< std::string > getTopicList()
Definition: mqtt_client.cpp:284


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45