-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmqtt.cpp
38 lines (34 loc) · 1.3 KB
/
mqtt.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
#include "mqtt.h"
MQTT::MQTT(QObject *parent) :
QObject(parent)
{
// re-initialize connection whenever the
// host or the port changes
QObject::connect(this, SIGNAL(hostChanged(QString)),
this, SLOT(initializeConnection()));
QObject::connect(this, SIGNAL(portChanged(int)),
this, SLOT(initializeConnection()));
}
void MQTT::initializeConnection()
{
DEBUG;
if(m_host.isEmpty() || !m_port || m_topic.isEmpty())
{
DEBUG << "Please define a host and a port and a topic";
disconnect();
return;
}
this->client = new QMQTT::Client(m_host, m_port);
this->client->setClientId(QString(QUuid::createUuid().toString()));
// this->client->setUsername("user");
// this->client->setPassword("password");
this->client->connect();
QObject::connect(this->client, SIGNAL(received(const QMQTT::Message&)),
this, SLOT(processReceivedMessage(const QMQTT::Message&)));
QObject::connect(this->client, SIGNAL(connacked(quint8)),
this, SLOT(subscribeToTopic(quint8)));
QObject::connect(this->client, SIGNAL(subscribed(QString)),
this, SLOT(subscribedToTopic(QString)));
QObject::connect(this->client, SIGNAL(disconnected()),
this, SIGNAL(disconnected()));
}