-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMqtt.h
60 lines (50 loc) · 1.45 KB
/
Mqtt.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
#ifndef SUBSCRIBER_H
#define SUBSCRIBER_H
#include <cstring>
#include <string>
#include <stdio.h>
#include <mosquittopp.h>
#include <json-c/json.h>
#include <unistd.h>
#include <vector>
class myMosq : public mosqpp::mosquittopp
{
public:
struct MqttMessage
{
const char *command;
double seconds;
};
private:
const char *MQTT_SERVER = "localhost";
const char *MQTT_SUB_TOPIC = "/telega";
const char *MQTT_PUB_TOPIC = "abot/command/alex";
int KEEP_ALIVE = 60;
int MQTT_PORT = 1883;
std::vector<double> colorRequest_;
bool isConnected_ = false;
bool isSubscribed_ = false;
bool isRunning_ = true;
bool inProcess_ = true;
virtual void on_connect(int rc);
virtual void on_subscribe(int mid, int qos_count, const int *granted_qos);
virtual void on_message(const struct mosquitto_message *msg);
public:
myMosq(const char* mqttServer, const char* mqttSubTopic, const char* mqttPubTopic, const char *id = NULL, bool clean_session = true)
: mosquittopp(id, clean_session),
MQTT_SERVER{mqttServer}, MQTT_SUB_TOPIC{mqttSubTopic}, MQTT_PUB_TOPIC{mqttPubTopic}
{
mosqpp::lib_init();
loop_start();
}
~myMosq()
{
loop_stop(true);
mosqpp::lib_cleanup();
}
void Subscribe();
void SendToServer(const char *data);
void *Publish(const MqttMessage* message);
std::vector<double> GetMessage() const { return colorRequest_; }
};
#endif