-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMosqClient.h
86 lines (70 loc) · 1.91 KB
/
MosqClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* @file MosqClient.h
* @Author BeeeOn team
* @date
* @brief
*/
#ifndef MYMOSQ_H
#define MYMOSQ_H
#include <iostream>
#include <thread>
#include <Poco/Runnable.h>
#include <mosquittopp.h>
#include "Aggregator.h"
#include "utils.h"
class Aggregator;
/**
* Class for communication over MQTT protocol.
*/
class MosqClient : public mosqpp::mosquittopp, public Poco::Runnable {
private:
int keepalive;
std::string id;
std::string topic;
std::string msg_prefix;
std::string host;
int port;
Poco::Logger& log;
Aggregator* agg;
public:
bool connected;
void run() override;
private:
void on_connect(int rc);
void on_disconnect(int rc);
//void on_publish(int mid);
//void on_log(int level, const char* str);
void on_message(const struct mosquitto_message* message);
void on_subscribe(int mid, int qos_count, const int* granted_qos);
void on_error();
void on_unsubscribe(int mid);
public:
MosqClient(std::string client_id_, std::string main_topic_, std::string msg_prefix, std::string host_ = "localhost", int port_=1883);
~MosqClient();
void setAgg(Aggregator* agg_);
void newMessageFromPAN(std::string msg);
void newMessageToMQTTDataModule(std::string msg);
/**
* Send message to default topic (specified in constructor).
* @param message Message to send
* @param qos QoS for the message
* @return true for successful send, false otherwise
*/
bool send_message(std::string message, int qos=0);
/**
* Send message to speficied topic
* @param message Message to send
* @param topic Topic to send
* @param qos QoS for the message
* @return true for successful send, false otherwise
*/
bool send_message(std::string message, std::string topic, int qos=0);
/**
* Subscribe to new topic
* @param topic Topic name
* @return true on successful subscribe, false otherwise
*/
bool add_topic_to_subscribe(std::string topic);
void askTheServer(std::string msg_text);
};
#endif /* MYMOSQ_H */