-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrytotriggermosquittoerror.cpp
81 lines (68 loc) · 2.79 KB
/
trytotriggermosquittoerror.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
#include "trytotriggermosquittoerror.h"
#include <QDateTime>
#include <QCoreApplication>
TryToTriggerMosquittoError::TryToTriggerMosquittoError(const QString &hostname, const int port, const QString &username, const QString &password, QObject *parent) : QObject(parent),
installations({GRONINGEN_REDFLOW, "0017eb2b37ac", "d0ff500097c0", "883314fc2fa6"})
{
qsrand(QDateTime::currentMSecsSinceEpoch());
QString clientID = QString("TryingToBreakSSL_%1").arg(qrand());
mMqttClient.reset(new QMQTT::Client(hostname, port, true, true)); // TODO: I ignoreSelfSigned can be false and it still works?
mMqttClient->setUsername(username);
mMqttClient->setPassword(password);
mMqttClient->setCleanSession(true);
mMqttClient->setClientId(clientID);
connect(mMqttClient.data(), &QMQTT::Client::received, this, &TryToTriggerMosquittoError::onReceived);
connect(mMqttClient.data(), &QMQTT::Client::error, this, &TryToTriggerMosquittoError::onError);
connect(mMqttClient.data(), &QMQTT::Client::connected, this, &TryToTriggerMosquittoError::onConnect);
}
void TryToTriggerMosquittoError::doStuff()
{
mMqttClient->connectToHost();
}
void TryToTriggerMosquittoError::onReceived(const QMQTT::Message &message)
{
//return;
QString line = QString("%1: %2").arg(message.topic()).arg(QString::fromUtf8(message.payload()));
qDebug() << line;
}
void TryToTriggerMosquittoError::onError(const QMQTT::ClientError error)
{
QString line = QString("Error occurred. QMQTT error code %1").arg(error);
qDebug() << line;
QCoreApplication::quit();
}
void TryToTriggerMosquittoError::onConnect()
{
qDebug() << "Connection established";
bool politeSubscribe = false;
if (politeSubscribe)
{
foreach(const QString portalid, installations)
{
const QString subscribePath = QString("N/%1/#").arg(portalid);
QString line = QString("Publishing %1").arg(subscribePath);
qDebug() << line;
mMqttClient->subscribe(subscribePath, 0);
}
}
else
{
// Subscribing to wildcards causes a lot of ACL checks in Mosquitto, so this would show any deficiencies there.
mMqttClient->subscribe("#", 0);
}
connect(&mPublishTimer, &QTimer::timeout, this, &TryToTriggerMosquittoError::keepAlivePublish);
mPublishTimer.setSingleShot(false);
mPublishTimer.start(50000);
keepAlivePublish();
}
void TryToTriggerMosquittoError::keepAlivePublish()
{
foreach(const QString portalid, installations)
{
const QString publishPath = QString("R/%1/system/0/Serial").arg(portalid);
qDebug() << QString("Publishing %1 for keepalive").arg(publishPath);
const int qos = 2;
QMQTT::Message message(qrand(), publishPath, QByteArray(), qos);
mMqttClient->publish(message);
}
}