-
Notifications
You must be signed in to change notification settings - Fork 6
/
providerbase.cpp
98 lines (91 loc) · 3.75 KB
/
providerbase.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
#include "providerbase.h"
ProviderBase::ProviderBase(QObject *parent) : QObject(parent)
{
}
ProviderConnectionBase::ProviderConnectionBase(QObject *parent)
: QObject(parent)
{
connect(&m_client, &QMqttClient::messageSent, this, [this](qint32 id) {
emit this->statusUpdate(QStringLiteral("Incoming"),
QStringLiteral("Message"),
QString(),
QString::number(id),
QStringLiteral("Confirmed"));
});
connect(&m_client, &QMqttClient::stateChanged, this, [this](QMqttClient::ClientState s) {
QString state;
switch (s) {
case QMqttClient::Disconnected: state = QStringLiteral("Disconnected"); break;
case QMqttClient::Connecting: state = QStringLiteral("Connecting"); break;
case QMqttClient::Connected: state = QStringLiteral("Connected"); break;
}
emit this->statusUpdate(QStringLiteral("General"),
QStringLiteral("Connection"),
QString(),
QString(),
state);
});
}
qint32 ProviderConnectionBase::publish(const QMqttTopicName &topic, const QByteArray &content, quint8 qos)
{
auto id = m_client.publish(topic, content, qos);
emit statusUpdate(QStringLiteral("Out"),
QStringLiteral("Publish"),
topic.name(),
QString::number(id),
content);
return id;
}
static void delayedSubscriptionDeleter(QMqttSubscription *s)
{
s->unsubscribe();
s->connect(s, &QMqttSubscription::stateChanged, s, [&s](QMqttSubscription::SubscriptionState state) {
if (state == QMqttSubscription::Unsubscribed)
s->deleteLater();
});
}
bool ProviderConnectionBase::subscribe(const QMqttTopicFilter &topic, quint8 qos)
{
m_sub.reset(m_client.subscribe(topic, qos), delayedSubscriptionDeleter);
emit statusUpdate(QStringLiteral("Outgoing"),
QStringLiteral("Subscription"),
topic.filter(),
QString(),
m_sub ? QString() : QStringLiteral("Failed"));
if (!m_sub)
return false;
m_sub->connect(m_sub.data(), &QMqttSubscription::stateChanged, this, [this](QMqttSubscription::SubscriptionState s) {
QString stateString;
switch(s) {
case QMqttSubscription::Subscribed:
stateString = QStringLiteral("Subscribed");
break;
case QMqttSubscription::SubscriptionPending:
stateString = QStringLiteral("Pending");
break;
case QMqttSubscription::Unsubscribed:
stateString = QStringLiteral("Unsubscribed");
break;
case QMqttSubscription::UnsubscriptionPending:
stateString = QStringLiteral("Unsubscription Pending");
break;
case QMqttSubscription::Error:
stateString = QStringLiteral("Error");
break;
}
emit this->statusUpdate(QStringLiteral("Incoming"),
QStringLiteral("Subscription"),
QString(),
QString(),
stateString);
});
m_sub->connect(m_sub.data(), &QMqttSubscription::messageReceived, m_sub.data(),
[this](const QMqttMessage &msg) {
emit this->statusUpdate(QStringLiteral("Incoming"),
QStringLiteral("Subscription Message"),
msg.topic().name(),
QString::number(msg.id()),
msg.payload());
});
return m_sub;
}