1
1
#include < QtDebug>
2
2
#include " qtozwdaemon.h"
3
3
4
+
4
5
qtozwdaemon::qtozwdaemon (QObject *parent) : QObject(parent)
5
6
{
6
7
this ->m_openzwave = new QTOpenZwave (this );
7
8
this ->m_qtozwmanager = this ->m_openzwave ->GetManager ();
8
9
QObject::connect (this ->m_qtozwmanager , &QTOZWManager::ready, this , &qtozwdaemon::QTOZW_Ready);
9
10
this ->m_qtozwmanager ->initilizeSource (true );
11
+
12
+ this ->m_client = new QMqttClient (this );
13
+ this ->m_client ->setHostname (" 10.51.107.19" );
14
+ this ->m_client ->setPort (1883 );
15
+
16
+ connect (this ->m_client , &QMqttClient::stateChanged, this , &qtozwdaemon::updateLogStateChange);
17
+ connect (this ->m_client , &QMqttClient::disconnected, this , &qtozwdaemon::brokerDisconnected);
18
+
19
+ connect (this ->m_client , &QMqttClient::messageReceived, this , &qtozwdaemon::handleMessage);
20
+ connect (m_client, &QMqttClient::pingResponseReceived, this , [this ]() {
21
+ const QString content = QDateTime::currentDateTime ().toString ()
22
+ + QLatin1String (" PingResponse" )
23
+ + QLatin1Char (' \n ' );
24
+ qDebug () << content;
25
+ });
26
+ this ->m_client ->connectToHost ();
10
27
}
11
28
12
29
void qtozwdaemon::QTOZW_Ready () {
@@ -19,3 +36,25 @@ void qtozwdaemon::startOZW() {
19
36
}
20
37
// this->m_qtozwmanager->open(this->getSerialPort());
21
38
}
39
+
40
+ void qtozwdaemon::updateLogStateChange ()
41
+ {
42
+ const QString content = QDateTime::currentDateTime ().toString ()
43
+ + QLatin1String (" : State Change: " )
44
+ + QString::number (m_client->state ());
45
+ qDebug () << content;
46
+ if (this ->m_client ->state () == QMqttClient::ClientState::Connected) {
47
+ this ->m_client ->subscribe (QMqttTopicFilter (" /OpenZWave/commands" ));
48
+ this ->m_client ->publish (QMqttTopicName (" /OpenZWave/commands" ), QString (" testhaha" ).toLocal8Bit ());
49
+ }
50
+
51
+ }
52
+
53
+ void qtozwdaemon::brokerDisconnected ()
54
+ {
55
+ qDebug () << " Disconnnected" ;
56
+ }
57
+
58
+ void qtozwdaemon::handleMessage (const QByteArray &message, const QMqttTopicName &topic) {
59
+ qDebug () << " Received: " << topic.name () << " :" << message;
60
+ }
0 commit comments