-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtmqtt.cpp
168 lines (147 loc) · 3.99 KB
/
qtmqtt.cpp
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
#include "qtmqtt.h"
QtMqtt::QtMqtt(QObject *parent) : AbstractMqtt(parent)
{
m_client = new QMqttClient(this);
m_sslSocket = new QSslSocket(this);
m_sslConfig = new QSslConfiguration;
m_sslEnable = false;
connect(m_client, &QMqttClient::connected, this, &QtMqtt::connected);
connect(m_client, &QMqttClient::disconnected, this, &QtMqtt::disconnected);
connect(m_client, &QMqttClient::messageReceived, [this](const QByteArray &message, const QMqttTopicName &topic){
emit received(topic.name(), 0, message, false);
});
connect(m_client, &QMqttClient::stateChanged, this, [this](QMqttClient::ClientState state){
MqttStatus status = Disconnected;
switch (state) {
case QMqttClient::Disconnected:
status = Disconnected;
break;
case QMqttClient::Connecting:
status = Connecting;
break;
case QMqttClient::Connected:
status = Connected;
break;
}
emit this->statusChanged(status);
});
connect(m_client, &QMqttClient::errorChanged, this, [this](QMqttClient::ClientError error){
emit this->errorChanged(QString("%1").arg(error));
});
}
QtMqtt::~QtMqtt()
{
delete m_client;
delete m_sslConfig;
}
void QtMqtt::setBroker(const QString &broker)
{
QString hostname;
QString url = broker;
QString protocol;
quint16 port;
if (!broker.contains("//")) {
url = "tcp://" + broker;
}
QStringList uris = url.split(":");
if (uris.length() != 3) {
emit errorChanged("set broker format error");
return;
}
protocol = uris[0];
hostname = uris[1].replace("//", "");
port = uris[2].toUShort();
if (protocol == "tcp") {
m_sslEnable = false;
} else if (protocol == "ssl") {
m_sslEnable = true;
} else {
emit errorChanged(QString("unknown protocol:") + protocol);
return;
}
m_client->setHostname(hostname);
m_client->setPort(port);
}
void QtMqtt::setSslConfiguration(const QSslConfiguration &ssl)
{
*m_sslConfig = ssl;
m_sslSocket->setSslConfiguration(*m_sslConfig);
}
void QtMqtt::setCleanSession(const bool clean)
{
m_client->setCleanSession(clean);
}
void QtMqtt::setClientId(const QString &id)
{
m_client->setClientId(id);
}
void QtMqtt::setKeepAlive(const quint16 second)
{
m_client->setKeepAlive(second);
}
void QtMqtt::setUsername(const QString &username)
{
m_client->setUsername(username);
}
void QtMqtt::setPassword(const QString &password)
{
m_client->setPassword(password);
}
void QtMqtt::setWillTopic(const QString &topic)
{
m_client->setWillTopic(topic);
}
void QtMqtt::setWillQos(const quint8 qos)
{
m_client->setWillQoS(qos);
}
void QtMqtt::setWillRetain(const bool retain)
{
m_client->setWillRetain(retain);
}
void QtMqtt::setWillMessage(const QByteArray &msg)
{
m_client->setWillMessage(msg);
}
void QtMqtt::connectToHost()
{
if (m_sslEnable) {
m_client->setTransport(m_sslSocket, QMqttClient::AbstractSocket);
m_client->connectToHostEncrypted();
} else {
m_client->connectToHost();
}
}
void QtMqtt::disconnectFromHost()
{
m_client->disconnectFromHost();
}
void QtMqtt::publish(const QString &topic, const quint8 qos, const QByteArray &payload, const bool retain)
{
m_client->publish(topic, payload, qos, retain);
emit published(topic, qos, payload, retain);
}
void QtMqtt::subscribe(const QString &topic, const quint8 qos)
{
auto subscription = m_client->subscribe(topic, qos);
if (subscription) {
emit subscribed(topic, qos);
}
}
void QtMqtt::unsubscribe(const QString &topic)
{
m_client->unsubscribe(topic);
emit unsubscribed(topic);
}
AbstractMqtt::MqttStatus QtMqtt::status()
{
switch (m_client->state()) {
case QMqttClient::Disconnected:
return Disconnected;
case QMqttClient::Connecting:
return Connecting;
case QMqttClient::Connected:
return Connected;
}
return Disconnected;
}