-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmqtt.h
169 lines (144 loc) · 3.54 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef MQTT_H
#define MQTT_H
#include <QObject>
#include "qmqtt.h"
#include <QDebug>
#include <QUuid>
#define DEBUG if(1) qDebug() << __PRETTY_FUNCTION__
class MQTT : public QObject
{
Q_OBJECT
Q_PROPERTY(QString host READ host WRITE setHost NOTIFY hostChanged)
Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged)
Q_PROPERTY(QString topic READ topic WRITE setTopic NOTIFY topicChanged)
QString m_host;
int m_port;
QString m_topic;
public:
explicit MQTT(QObject *parent = 0);
QString host() const
{
return m_host;
}
int port() const
{
return m_port;
}
QString topic() const
{
return m_topic;
}
signals:
void hostChanged(QString arg);
void portChanged(int arg);
void topicChanged(QString arg);
// custom signals
void messageReceived(QString topic, QString message);
// pass-thru signals
void connected();
void error(QAbstractSocket::SocketError);
void connacked(quint8 ack);
void published(QMQTT::Message &message);
void pubacked(quint8 type, quint16 msgid);
void received(const QMQTT::Message &message);
void subscribed(const QString &topic);
void subacked(quint16 mid, quint8 qos);
void unsubscribed(const QString &topic);
void unsubacked(quint16 mid);
void pong();
void disconnected();
public slots:
void setHost(QString arg)
{
if (m_host != arg) {
m_host = arg;
emit hostChanged(arg);
}
}
void setPort(int arg)
{
if (m_port != arg) {
m_port = arg;
emit portChanged(arg);
}
}
void setTopic(QString arg)
{
if (m_topic != arg) {
m_topic = arg;
emit topicChanged(arg);
}
}
// custom slots
void processReceivedMessage(const QMQTT::Message &arg)
{
DEBUG << QString::fromLatin1(arg.payload());
emit messageReceived(arg.topic(),
QString::fromLatin1(arg.payload()));
}
void subscribedToTopic(QString topic)
{
DEBUG << "topic is " << topic;
}
void subscribeToTopic(quint8 ack)
{
DEBUG << "qos = " << ack;
if(!m_topic.isEmpty())
{
this->client->subscribe(m_topic, 0);
}
}
void publishMessage(QString message)
{
if(!message.isEmpty() && this->client->isConnected())
{
QMQTT::Message msg;
msg.setTopic(m_topic);
msg.setPayload(message.toLatin1());
this->client->publish(msg);
}
}
// pass-thru slots
void connect()
{
if(this->client)
this->client->connect();
}
quint16 publish(QMQTT::Message &message)
{
if(this->client)
return this->client->publish(message);
return 0;
}
void puback(quint8 type, quint16 msgid)
{
if(this->client)
this->client->puback(type, msgid);
}
quint16 subscribe(const QString &topic, quint8 qos)
{
if(this->client)
return this->client->subscribe(topic, qos);
return 0;
}
void unsubscribe(const QString &topic)
{
if(this->client)
this->client->unsubscribe(topic);
}
void ping()
{
if(this->client)
this->client->ping();
}
void disconnect()
{
if(this->client)
this->client->disconnect();
}
private slots:
void initializeConnection();
private:
QMQTT::Client *client;
};
#endif // MQTT_H