forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_files.cpp
486 lines (434 loc) · 24.1 KB
/
read_files.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QJsonParseError>
#include <sensor.h>
#include <deconz.h>
QJsonDocument readButtonMapJson(const QString &path)
{
QFile file;
file.setFileName(path);
if (file.exists())
{
DBG_Printf(DBG_INFO, "[INFO] - Found file containing button maps. Parsing data...\n");
QJsonParseError error;
file.open(QIODevice::ReadOnly | QIODevice::Text);
QJsonDocument buttonMaps = QJsonDocument::fromJson(file.readAll(), &error);
file.close();
if (buttonMaps.isNull() || buttonMaps.isEmpty())
{
DBG_Printf(DBG_INFO, "[ERROR] - Error: %s at offset: %d (in characters)\n", qPrintable(error.errorString()), error.offset);
return QJsonDocument();
}
else
{
return buttonMaps;
}
}
else
{
DBG_Printf(DBG_INFO, "[ERROR] - File containing button maps was NOT found.\n");
return QJsonDocument();
}
}
bool checkRootLevelObjectsJson(const QJsonDocument &buttonMaps, const QStringList &requiredJsonObjects)
{
// Check root level objects
for ( const auto& i : requiredJsonObjects )
{
if (buttonMaps.object().value(QString(i)) == QJsonValue::Undefined)
{
DBG_Printf(DBG_INFO, "[ERROR] - No object named '%s' found in JSON file. Skip to load button maps.\n", qPrintable(i));
return false;
}
else if (!buttonMaps.object().value(QString(i)).isObject())
{
DBG_Printf(DBG_INFO, "[ERROR] - Expected '%s' in JSON file to be an object, but it isn't. Skip to load button maps.\n", qPrintable(i));
return false;
}
}
return true;
}
QMap<QString, quint16> loadButtonMapClustersJson(const QJsonDocument &buttonMaps)
{
// Load button map clusters
QJsonObject clustersObj = buttonMaps.object().value(QLatin1String("clusters")).toObject();
QMap<QString, quint16> btnMapClusters;
quint8 counter = 0;
for (auto i = clustersObj.constBegin(); i != clustersObj.constEnd(); ++i) // Loop through cluster objects
{
++counter;
if (i.key().isNull() || i.key().isEmpty() || i.key().length() > 20)
{
DBG_Printf(DBG_INFO, "[ERROR] - Key #%d for object 'clusters' is no string or too long. Skipping entry...\n", counter);
continue;
}
else if (!i.value().isDouble() || i.value().toDouble() > 2000)
{
DBG_Printf(DBG_INFO, "[ERROR] - Value #%d for object 'clusters' is no number or too large. Skipping entry...\n", counter);
continue;
}
else
{
btnMapClusters.insert(i.key(), i.value().toInt()); // Store data in QMap for later use
}
}
return btnMapClusters;
}
QMap<QString, QMap<QString, quint16>> loadButtonMapCommadsJson(const QJsonDocument &buttonMaps)
{
// Load button map commands
QJsonObject commandsObj = buttonMaps.object().value(QLatin1String("commands")).toObject();
QMap<QString, QMap<QString, quint16>> btnMapClusterCommands;
quint8 counter = 0;
for (auto i = commandsObj.constBegin(); i != commandsObj.constEnd(); ++i) // Loop through commands objects
{
++counter;
if (i.key().isNull() || i.key().isEmpty() || i.key().length() > 20)
{
DBG_Printf(DBG_INFO, "[ERROR] - Key #%d for object 'commands' is no string or too long. Skipping entry...\n", counter);
continue;
}
else if (!i.value().isObject())
{
DBG_Printf(DBG_INFO, "[ERROR] - Expected '%s' in JSON file to be an object, but it isn't. Skipping entry...\n");
continue;
}
else
{
QJsonObject commandObj = i.value().toObject();
QString commandsObjName = i.key();
QMap<QString, quint16> commandMap;
quint8 counter2 = 0;
for (auto i = commandObj.constBegin(); i != commandObj.constEnd(); ++i) // Loop through cluster specific command objects
{
++counter2;
if (i.key().isNull() || i.key().isEmpty() || i.key().length() > 28)
{
DBG_Printf(DBG_INFO, "[ERROR] - Key #%d for object '%s' is no string or too long. Skipping entry...\n", counter2, qPrintable(commandsObjName));
continue;
}
else if (!i.value().isDouble() || i.value().toDouble() > 0xFF) // FIXME: value might be too small
{
DBG_Printf(DBG_INFO, "[ERROR] - Value #%d for object '%s' is no number or too large. Skipping entry...\n", counter2, qPrintable(commandsObjName));
continue;
}
else
{
commandMap.insert(i.key(), i.value().toInt()); // Store data in QMap for later use
}
}
btnMapClusterCommands.insert(commandsObjName, commandMap); // Store data in QMap for later use
}
}
return btnMapClusterCommands;
}
/*! Reads the associated modelIDs from all available button maps in JSON file.
*/
QMap<QString, QString> loadButtonMapModelIdsJson(const QJsonDocument &buttonMaps)
{
// Load ModelIDs
QMap<QString, QString> buttonMapForModelId;
QJsonObject allMapsObj = buttonMaps.object().value(QLatin1String("maps")).toObject(); // Get all button maps
for (auto i = allMapsObj.constBegin(); i != allMapsObj.constEnd(); ++i) // Loop through button maps
{
QString buttonMapName = i.key(); // Individual button map name
if (i.value().isObject()) // Check if individual button map is an object
{
QJsonObject buttonMapObj = i.value().toObject();
if (buttonMapObj.value(QString("modelids")).isArray())
{
QJsonArray buttonMapModelIds = buttonMapObj.value(QString("modelids")).toArray();
if (buttonMapModelIds.size() == 0)
{
DBG_Printf(DBG_INFO, "[WARNING] - Button map '%s' in JSON file has no assigned ModelIDs. Skip loading button map...\n", qPrintable(buttonMapName));
continue; // Skip button map
}
for (auto i = buttonMapModelIds.constBegin(); i != buttonMapModelIds.constEnd(); ++i) // Loop through modelIDs
{
QJsonValue val = *i;
if (val.isString() && val.toString().size() <= 32)
{
buttonMapForModelId.insert(val.toString(), buttonMapName); // Assign button map to modelIDs
}
else if (val.isString() && val.toString().size() > 32)
{
DBG_Printf(DBG_INFO, "[ERROR] - Entry of 'modelids', button map '%s' in JSON file is too long. Skipping entry...\n", qPrintable(buttonMapName));
continue;
}
else
{
DBG_Printf(DBG_INFO, "[ERROR] - Expected entry of 'modelids', button map '%s' in JSON file to be a string, but isn't. Skipping entry...\n", qPrintable(buttonMapName));
continue;
}
}
}
else
{
DBG_Printf(DBG_INFO, "[ERROR] - Expected 'modelids' of button map '%s' in JSON file to be an array, but isn't. Skip loading button map...\n", qPrintable(buttonMapName));
continue; // Skip button map
}
}
else
{
DBG_Printf(DBG_INFO, "[ERROR] - Expected '%s' in JSON file to be an object, but it isn't. Skip loading button map...\n", qPrintable(buttonMapName));
continue; // Skip button map
}
}
return buttonMapForModelId;
}
/*! Reads all available button maps from JSON file.
*/
QMap<QString, std::vector<Sensor::ButtonMap>> loadButtonMapsJson(const QJsonDocument &buttonMaps, const QMap<QString, quint16> &btnMapClusters,
const QMap<QString, QMap<QString, quint16>> &btnMapClusterCommands)
{
QMap<QString, quint16> buttons;
QMap<QString, quint8> actions;
quint8 counter = 0;
// Load button map buttons
QJsonObject buttonsObj = buttonMaps.object().value(QLatin1String("buttons")).toObject();
for (auto i = buttonsObj.constBegin(); i != buttonsObj.constEnd(); ++i) // Loop through button objects
{
++counter;
if (i.key().isNull() || i.key().isEmpty() || i.key().length() > 11)
{
DBG_Printf(DBG_INFO, "[ERROR] - Key #%d for object 'buttons' is no string or too long. Skipping entry...\n", counter);
continue;
}
else if (!i.value().isDouble() || i.value().toDouble() > 32000)
{
DBG_Printf(DBG_INFO, "[ERROR] - Value #%d for object 'buttons' is no number or too large. Skipping entry...\n", counter);
continue;
}
else
{
buttons.insert(i.key(), i.value().toInt()); // Store data in QMap for later use
}
}
counter = 0;
QJsonObject actionsObj = buttonMaps.object().value(QLatin1String("buttonActions")).toObject();
for (auto i = actionsObj.constBegin(); i != actionsObj.constEnd(); ++i) // Loop through button action objects
{
++counter;
if (i.key().isNull() || i.key().isEmpty() || i.key().length() > 33)
{
DBG_Printf(DBG_INFO, "[ERROR] - Key #%d for object 'buttonActions' is no string or too long. Skipping entry...\n", counter);
continue;
}
else if (!i.value().isDouble() || i.value().toDouble() > 16)
{
DBG_Printf(DBG_INFO, "[ERROR] - Value #%d for object 'buttonActions' is no number or too large. Skipping entry...\n", counter);
continue;
}
else
{
actions.insert(i.key(), i.value().toInt()); // Store data in QMap for later use
}
}
// Load button maps
QMap<QString, std::vector<Sensor::ButtonMap>> buttonMapData;
QJsonObject allMapsObj = buttonMaps.object().value(QLatin1String("maps")).toObject(); // Get all button maps
for (auto i = allMapsObj.constBegin(); i != allMapsObj.constEnd(); ++i) // Loop through button maps
{
QString buttonMapName = i.key(); // Individual button map name
//DBG_Printf(DBG_INFO, "[INFO] - Button map name: %s\n", qPrintable(buttonMapName));
quint8 mapItem = 0;
if (i.value().isObject()) // Check if individual button map is an object
{
QJsonObject buttonMapObj = i.value().toObject();
if (buttonMapObj.value(QString("map")).isArray()) // Check if button map is an array of arrays
{
std::vector<Sensor::ButtonMap> btnMapVec;
QJsonArray buttonMapArr = buttonMapObj.value(QString("map")).toArray();
//DBG_Printf(DBG_INFO, "[INFO] - Button map size: %d\n", i.value().toArray().size());
for (auto i = buttonMapArr.constBegin(); i != buttonMapArr.constEnd(); ++i) // Loop through button map items
{
QJsonValue val = *i;
if (val.isArray())
{
QJsonArray buttonMapItemArr = val.toArray();
if (buttonMapItemArr.size() != 8)
{
DBG_Printf(DBG_INFO, "[ERROR] - Button map item #%d for '%s' has an incorrect size. Expected 8, got %d\n",
mapItem, qPrintable(buttonMapName), buttonMapItemArr.size());
mapItem++;
continue;
}
else
{
bool ok;
quint16 btn = 0;
Sensor::ButtonMap btnMap;
// Initialize with defaults
btnMap.mode = Sensor::ModeNone;
btnMap.endpoint = 0;
btnMap.clusterId = 0;
btnMap.zclCommandId = 0;
btnMap.zclParam0 = 0;
btnMap.button = 0;
btnMap.name = "";
if (buttonMapItemArr.at(0).isDouble())
{
const auto mode = buttonMapItemArr.at(0).toInt();
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #1: %d\n", buttonMapItemArr.at(0).toUint());
if (mode == 0) { btnMap.mode = Sensor::ModeNone; }
else if (mode == 1) { btnMap.mode = Sensor::ModeScenes; }
else if (mode == 2) { btnMap.mode = Sensor::ModeTwoGroups; }
else if (mode == 3) { btnMap.mode = Sensor::ModeColorTemperature; }
else if (mode == 4) { btnMap.mode = Sensor::ModeDimmer; }
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #1 for '%s' does not seem to be an integer.\n",
mapItem, qPrintable(buttonMapName));
}
if (buttonMapItemArr.at(1).isString() && buttonMapItemArr.at(1).toString().startsWith(QLatin1String("0x")) &&
buttonMapItemArr.at(1).toString().length() == 4)
{
QString ep = buttonMapItemArr.at(1).toString();
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #2: %d\n", ep.toUint(&ok, 0));
btnMap.endpoint = ep.toUInt(&ok, 0);
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #2 for '%s' has an incorrect format.\n",
mapItem, qPrintable(buttonMapName));
}
if (buttonMapItemArr.at(2).isString() && buttonMapItemArr.at(2).toString().startsWith(QLatin1String("0x")) &&
buttonMapItemArr.at(2).toString().length() == 6)
{
QString cid = buttonMapItemArr.at(2).toString();
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #3: %d\n", cid.toUint(&ok, 0));
btnMap.clusterId = cid.toUInt(&ok, 0);
}
else if (buttonMapItemArr.at(2).isString() && !buttonMapItemArr.at(2).toString().startsWith(QLatin1String("0x")) &&
buttonMapItemArr.at(2).toString().length() <= 20)
{
QString cid = buttonMapItemArr.at(2).toString();
if (btnMapClusters.value(cid, 65535) != 65535) { btnMap.clusterId = btnMapClusters.value(buttonMapItemArr.at(2).toString()); }
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #3 for '%s' was not found in object 'clusters'.\n",
mapItem, qPrintable(buttonMapName));
}
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #3 for '%s' has an incorrect format.\n",
mapItem, qPrintable(buttonMapName));
}
if (buttonMapItemArr.at(3).isString() && buttonMapItemArr.at(3).toString().startsWith(QLatin1String("0x")) &&
buttonMapItemArr.at(3).toString().length() == 4)
{
QString cmd = buttonMapItemArr.at(3).toString();
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #4: %d\n", cmd.toUint(&ok, 0));
btnMap.zclCommandId = cmd.toUInt(&ok, 0);
}
else if (buttonMapItemArr.at(3).isString() && !buttonMapItemArr.at(3).toString().startsWith(QLatin1String("0x")) &&
buttonMapItemArr.at(3).toString().length() <= 28)
{
QString cid = buttonMapItemArr.at(2).toString();
QString cmd = buttonMapItemArr.at(3).toString();
if (btnMapClusters.value(cid, 65535) != 65535)
{
QMap<QString, quint16> temp = btnMapClusterCommands.value(cid);
if (!temp.empty() && temp.value(cmd, 65535) != 65535) { btnMap.zclCommandId = temp.value(cmd); }
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #4 for '%s' was not found in object 'commands' for cluster '%s'.\n",
mapItem, qPrintable(buttonMapName), qPrintable(cid));
}
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #4 for '%s' was not found as cluster in object 'commands'.\n",
mapItem, qPrintable(buttonMapName));
}
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #4 for '%s' has an incorrect format.\n",
mapItem, qPrintable(buttonMapName));
}
if (buttonMapItemArr.at(4).isString() && buttonMapItemArr.at(4).toString().length() <= 3)
{
QString para = buttonMapItemArr.at(4).toString();
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #5: %d\n", para.toUint(&ok, 0));
btnMap.zclParam0 = para.toUInt(&ok, 0);
}
else if (buttonMapItemArr.at(4).isString() && buttonMapItemArr.at(4).toString().startsWith(QLatin1String("0x")) &&
(buttonMapItemArr.at(4).toString().length() == 4 || buttonMapItemArr.at(4).toString().length() == 6))
{
QString para = buttonMapItemArr.at(4).toString();
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #5: %d\n", para.toUint(&ok, 0));
btnMap.zclParam0 = para.toUInt(&ok, 0);
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #5 for '%s' has an incorrect format.\n",
mapItem, qPrintable(buttonMapName));
}
if (buttonMapItemArr.at(5).isString() && buttonMapItemArr.at(5).toString().length() <= 11)
{
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #6: %s\n", qPrintable(buttonMapItemArr.at(5).toString()));
btn = buttons.value(buttonMapItemArr.at(5).toString(), 0);
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #6 for '%s' is unknown.\n",
mapItem, qPrintable(buttonMapName));
}
if (buttonMapItemArr.at(6).isString() && buttonMapItemArr.at(6).toString().length() <= 32)
{
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #7: %s\n", qPrintable(buttonMapItemArr.at(6).toString()));
btn += actions.value(buttonMapItemArr.at(6).toString(), 0);
btnMap.button = btn;
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #7 for '%s' is unknown.\n",
mapItem, qPrintable(buttonMapName));
}
if (buttonMapItemArr.at(7).isString() && buttonMapItemArr.at(7).toString().length() <= 40)
{
//DBG_Printf(DBG_INFO, "[INFO] - Button map item #8: %s\n", qPrintable(buttonMapItemArr.at(7).toString()));
btnMap.name = buttonMapItemArr.at(7).toString();
}
else
{
DBG_Printf(DBG_INFO, "[INFO] - Button map item #%d, field #8 for '%s' is too long.\n",
mapItem, qPrintable(buttonMapName));
}
//DBG_Printf(DBG_INFO, "[INFO] - btnMap item #6: %d\n", btnMap.button);
//DBG_Printf(DBG_INFO, "[INFO] - btnMap item #7: %s\n", qPrintable(btnMap.name));
btnMapVec.push_back(btnMap);
mapItem++;
}
}
else
{
DBG_Printf(DBG_INFO, "[ERROR] - Button map item #%d for '%s' in JSON must be an array, but isn't.\n",
mapItem, qPrintable(buttonMapName));
mapItem++;
continue;
}
}
buttonMapData.insert(buttonMapName, btnMapVec); // Assign vector of button maps to QMap
}
else
{
DBG_Printf(DBG_INFO, "[ERROR] - Expected 'map' of button map '%s' in JSON file to be an array, but isn't. Skip loading button map...\n", qPrintable(buttonMapName));
continue; // Skip button map
}
}
else
{
DBG_Printf(DBG_INFO, "[ERROR] - Expected '%s' in JSON file to be an object, but it isn't. Skip loading button map...\n", qPrintable(buttonMapName));
continue; // Skip button map
}
}
DBG_Printf(DBG_INFO, "[INFO] - Button maps loaded.\n");
return buttonMapData;
}