-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmlclient.cpp
105 lines (85 loc) · 2.38 KB
/
qmlclient.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
#include "qmlclient.h"
QmlClient::QmlClient(QObject *parent)
:QObject(parent)
, _client(new QMQTT::Client())
{
_client->setParent(parent);
_client->setAutoReconnect(true);
// connect(_client, SIGNAL(connected()), this, SLOT(connected()));
connect(_client, SIGNAL(connected()), this, SLOT(setConnectedTrue()));
connect(_client, SIGNAL(disconnected()), this, SLOT(setConnectedFalse()));
connect(_client, SIGNAL(subscribed(QString,quint8)), this, SLOT(subscribed(QString)));
connect(_client, SIGNAL(received(QMQTT::Message)), this, SLOT(received(QMQTT::Message)));
}
QString QmlClient::hostString() const
{
return _client->host().toString();
}
void QmlClient::setHostString(const QString host)
{
QHostAddress hostAdress(host);
if (hostAdress.isNull()){
return;
}else{
_client->disconnectFromHost();
_client->setHost(hostAdress);
_client->connectToHost();
emit hostStringChanged();
}
}
int QmlClient::port() const
{
return _client->port();
}
void QmlClient::setPort(const int port)
{
_client->setPort(port);
emit portChanged();
}
bool QmlClient::isConnected() const
{
return this->_connected;
}
void QmlClient::setIsConnected(const bool status)
{
qDebug() << "Connection status change to " << status << endl;
if (status != this->_connected) {
this->_connected = status;
emit isConnectedChanged();
}
}
QString QmlClient::subscribeTopic() const
{
return this->_currnetTopic;
}
void QmlClient::setSubscribeTopic(const QString topic)
{
if (topic != this->_currnetTopic){
this->_client->unsubscribe(this->_currnetTopic);
this->_currnetTopic = topic;
this->_client->subscribe(this->_currnetTopic);
emit subscribeTopicChanged();
}
}
void QmlClient::publishMsg(const quint16 id, const QString &topic, const QString &message)
{
QMQTT::Message message_object(id, topic, message.toUtf8());
_client->publish(message_object);
}
void QmlClient::subscribed(QString topic)
{
qDebug() << "Subscribed to " << topic << endl;
}
void QmlClient::received(QMQTT::Message message)
{
emit newMessage(message.topic(),QString::fromUtf8(message.payload()));
}
void QmlClient::setConnectedTrue()
{
_client->subscribe(this->_currnetTopic);
this->setIsConnected(true);
}
void QmlClient::setConnectedFalse()
{
this->setIsConnected(false);
}