-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket.cpp
More file actions
299 lines (226 loc) · 8.05 KB
/
websocket.cpp
File metadata and controls
299 lines (226 loc) · 8.05 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "websocket.h"
#include <QDebug>
#include "animation.h"
#include "socketpropertynames.h"
WebSocket::WebSocket(QObject *parent) : QObject(parent) {
_socket = new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, this);
connect(_socket, SIGNAL(textMessageReceived(QString)), this, SLOT(onMessage(QString)));
connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(onStateChanged(QAbstractSocket::SocketState)));
_connectionState = ConnectionState::Enum::DISCONNECTED;
// Set initial state
_connectionState = ConnectionState::Enum::DISCONNECTED;
}
void WebSocket::open(const QString& url) {
qDebug() << "OPENING" << url;
_socket->open(QUrl(url));
}
void WebSocket::close() {
_socket->close();
}
ConnectionState::Enum WebSocket::connectionState() const {
return _connectionState;
}
void WebSocket::onStateChanged(QAbstractSocket::SocketState state) {
ConnectionState::Enum newState;
switch (state) {
case QAbstractSocket::ConnectedState:
newState = ConnectionState::Enum::CONNECTED;
break;
case QAbstractSocket::HostLookupState:
case QAbstractSocket::ConnectingState:
newState = ConnectionState::Enum::CONNECTING;
break;
default:
newState = ConnectionState::Enum::DISCONNECTED;
break;
}
if (newState != _connectionState) {
_connectionState = newState;
emit connectionStateChanged(_connectionState);
}
}
void WebSocket::onMessage(const QString& message) {
try {
// Parse message to root object
QJsonDocument msgJson = QJsonDocument::fromJson(message.toUtf8());
if (msgJson.isNull() || msgJson.isEmpty() || !msgJson.isObject()) {
throw new QString("Invalid JSON message object");
}
QJsonObject root = msgJson.object();
// Read message type
QJsonValue msgTypeValue = root.value("msg");
if (!msgTypeValue.isString()) {
throw new QString("Invalid 'msg' property");
}
QString msgType = msgTypeValue.toString().toLower();
// Fetch data object
QJsonValue dataValue = root.value("data");
// Channel message to correct method
if (msgType == WS_MSG_TYPE_CONFIG) {
hanleWallConfigMsg(dataValue);
} else if (msgType == WS_MSG_TYPE_PROPERTIES) {
handlePropertyChangesMsg(dataValue);
} else {
throw new QString("Unknown message type");
}
} catch (const QString& error) {
qWarning() << "Disregarding invalid message:" << error;
}
}
void WebSocket::hanleWallConfigMsg(const QJsonValue& data) {
try {
// Check data
if (data.isNull() || !data.isObject()) {
throw new QString("Invalid data");
}
QJsonObject dataObj = data.toObject();
// @TODO Static wall properties
// Animations
QJsonValue animationListValue = dataObj.value("animations");
if (!animationListValue.isArray()) {
throw new QString("Invalid 'animations' property");
}
QJsonArray animationList = animationListValue.toArray();
QList<Animation*> animationResultList;
QJsonArray::iterator it;
for (it = animationList.begin(); it != animationList.end(); it++) {
// Fetch object
QJsonValue animationValue = *it;
if (!animationValue.isObject()) {
throw new QString("Invalid animation object");
}
QJsonObject animation = animationValue.toObject();
// Fetch properties
QJsonValue idValue = animation.value("id");
if (!idValue.isDouble()) {
throw new QString("Invalid animation 'id' property");
}
int id = idValue.toInt();
QJsonValue nameValue = animation.value("name");
if (!nameValue.isString()) {
throw new QString("Invalid animation 'name' property");
}
QString name = nameValue.toString();
QJsonValue descriptionValue = animation.value("description");
if (!descriptionValue.isString()) {
throw new QString("Invalid animation 'description' property");
}
QString description = descriptionValue.toString();
Animation* animationObj = new Animation(id, name, description);
connect(this, SIGNAL(activeAnimationIdChanged(int)), animationObj, SLOT(onActiveAnimationChanged(int)));
animationResultList.append(animationObj);
}
emit animationsChanged(animationResultList);
} catch (const QString& error) {
qWarning() << "Disregarding invalid wall config message:" << error;
}
}
void WebSocket::handlePropertyChangesMsg(const QJsonValue& data) {
try {
// Check data
if (data.isNull() || !data.isArray()) {
throw new QString("Invalid data");
}
QJsonArray propertyArray = data.toArray();
// Loop over properties
QJsonArray::iterator it;
for (it = propertyArray.begin(); it != propertyArray.end(); it++) {
QJsonValue propertyVal = *it;
try {
// Parse to object
if (!propertyVal.isObject()) {
throw new QString("invalid object");
}
QJsonObject propertyObj = propertyVal.toObject();
// Parse scope
QJsonValue scopeValue = propertyObj.value("scope");
if (!scopeValue.isString()) {
throw new QString("'scope' property invalid");
}
QString scope = scopeValue.toString();
// Parse name
QJsonValue nameValue = propertyObj.value("name");
if (!nameValue.isString()) {
throw new QString("'name' property invalid");
}
QString name = nameValue.toString();
// Parse value
QJsonValue valueValue = propertyObj.value("value");
if (valueValue.isNull()) {
throw new QString("'value' property invalid");
}
QVariant value = valueValue.toVariant();
if (scope == "wall") {
// Handle property change
handleWallPropertyChange(name, value);
} else if (scope == "animation") {
QJsonValue animationIdValue = propertyObj.value("animationId");
if (!animationIdValue.isDouble()) {
throw new QString("'animationId' property invalid");
}
int animationId = animationIdValue.toInt();
// Handle property change
handleAnimationPropertyChange(animationId, name, value);
} else {
throw new QString("'scope' property value invalid");
}
} catch (const QString& error) {
qWarning() << "Ignoring property msg, " << error;
}
}
} catch (const QString& error) {
qWarning() << "Disregarding invalid active animation change message:" << error;
}
}
void WebSocket::handleWallPropertyChange(const QString& propertyName, const QVariant& value) {
if (propertyName == WS_WALL_ACTIVE_ANIMATION_ID) {
emit activeAnimationIdChanged(value.toInt());
} else if (propertyName == WS_WALL_BRIGHTNESS) {
emit brightnessChanged(value.toInt());
} else if (propertyName == WS_WALL_PLAYMODE) {
emit playModeChange(value.toInt());
}
}
void WebSocket::handleAnimationPropertyChange(int animationId, const QString& propertyName, const QVariant& value) {
qDebug() << "handle animProp" << animationId << propertyName << value;
}
void WebSocket::sendActivateAnimationId(int activeAnimationId) {
sendWallPropertyChange(WS_WALL_ACTIVE_ANIMATION_ID, activeAnimationId);
}
void WebSocket::sendBrightness(int brightness) {
sendWallPropertyChange(WS_WALL_BRIGHTNESS, brightness);
}
void WebSocket::sendPlayMode(int playmode) {
sendWallPropertyChange(WS_WALL_PLAYMODE, playmode);
}
void WebSocket::sendWallPropertyChange(const QString& propertyName, const QVariant& value) {
// Add property
QJsonObject propertyObject;
propertyObject.insert("scope", "wall");
propertyObject.insert("name", propertyName);
propertyObject.insert("value", QJsonValue::fromVariant(value));
// Set data
QJsonArray data;
data.append(propertyObject);
QJsonObject rootObject;
rootObject.insert("msg", WS_MSG_TYPE_PROPERTIES);
rootObject.insert("data", data);
// Create document root
QJsonDocument document;
document.setObject(rootObject);
// Send message
_socket->sendTextMessage(document.toJson());
}
void WebSocket::sendAnimationPropertyChange(int animationId, const QString& propertyName, const QVariant& value) {
}
QString WebSocket::errorString() const {
return _errorString;
}
void WebSocket::onSocketError(QAbstractSocket::SocketError error) {
if (_errorString != _socket->errorString()) {
_errorString = _socket->errorString();
qWarning() << "Socket error:" << error << _errorString;
emit errorString(_errorString);
}
}