-
Notifications
You must be signed in to change notification settings - Fork 1
/
r3client.cpp
101 lines (83 loc) · 2.74 KB
/
r3client.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
#include "r3client.h"
#include <QJsonObject>
#include <QJsonDocument>
R3Client::R3Client(QObject *parent) :
QObject{parent}
{
connect(&m_webSocket, &QWebSocket::connected, this, &R3Client::connected);
connect(&m_webSocket, &QWebSocket::disconnected, this, &R3Client::disconnected);
connect(&m_webSocket, qOverload<QAbstractSocket::SocketError>(&QWebSocket::error), this, &R3Client::error);
connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &R3Client::textMessageReceived);
connect(&m_webSocket, &QWebSocket::binaryMessageReceived, this, &R3Client::binaryMessageReceived);
}
void R3Client::open()
{
m_webSocket.open(QUrl{"ws://licht.realraum.at/sock"});
}
void R3Client::close(QWebSocketProtocol::CloseCode closeCode, const QString &reason)
{
m_webSocket.close(closeCode, reason);
}
void R3Client::sendMQTT(const QString &ctx, const QJsonValue &data)
{
const QJsonObject m{{"ctx", ctx}, {"data", data}};
//qDebug() << m;
const auto text = QJsonDocument{m}.toJson(QJsonDocument::Compact);
//qDebug() << "sendTextMessage()" << text;
m_webSocket.sendTextMessage(text);
}
void R3Client::sendYmhButton(const QString &btn)
{
sendMQTT(mqtttopic_golightctrl(btn), QJsonObject{{"Action", "send"}});
}
QString R3Client::mqtttopic_golightctrl(const QString &lightname)
{
return QString{"action/GoLightCtrl/%0"}.arg(lightname);
}
void R3Client::textMessageReceived(const QString &message)
{
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(message.toUtf8(), &error);
if (error.error != QJsonParseError::NoError)
{
qWarning() << "could not parse JSON" << error.errorString();
qDebug() << "textMessageReceived()" << message;
return;
}
if (!document.isObject())
{
qWarning() << "json is not an object";
qDebug() << "textMessageReceived()" << message;
return;
}
const auto obj = document.object();
if (!obj.contains("ctx"))
{
qWarning() << "json does not contain ctx";
qDebug() << "textMessageReceived()" << message;
return;
}
QString ctx;
{
const auto ctxValue = obj.value("ctx");
if (!ctxValue.isString())
{
qWarning() << "ctx is not a string!";
qDebug() << "textMessageReceived()" << message;
return;
}
ctx = ctxValue.toString();
}
if (!obj.contains("data"))
{
qWarning() << "json does not contain data";
qDebug() << "textMessageReceived()" << message;
return;
}
const auto data = obj.value("data");
emit statusReceived(ctx, data);
}
void R3Client::binaryMessageReceived(const QByteArray &message)
{
qWarning() << "binaryMessageReceived()" << message;
}