forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.cpp
575 lines (494 loc) · 13.3 KB
/
sensor.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
* Copyright (c) 2013-2020 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include "sensor.h"
#include "json.h"
#include "product_match.h"
/*! Returns a fingerprint as JSON string. */
QString SensorFingerprint::toString() const
{
if (endpoint == 0xFF || profileId == 0xFFFF)
{
return QString();
}
QVariantMap map;
map["ep"] = (double)endpoint;
map["p"] = (double)profileId;
map["d"] = (double)deviceId;
if (!inClusters.empty())
{
QVariantList ls;
for (uint i = 0; i < inClusters.size(); i++)
{
ls.append((double)inClusters[i]);
}
map["in"] = ls;
}
if (!outClusters.empty())
{
QVariantList ls;
for (uint i = 0; i < outClusters.size(); i++)
{
ls.append((double)outClusters[i]);
}
map["out"] = ls;
}
return deCONZ::jsonStringFromMap(map);
}
/*! Parses a fingerprint from JSON string.
\returns true on success
*/
bool SensorFingerprint::readFromJsonString(const QString &json)
{
if (json.isEmpty())
{
return false;
}
bool ok = false;
QVariant var = Json::parse(json, ok);
if (!ok)
{
return false;
}
QVariantMap map = var.toMap();
if (map.contains("ep") && map.contains("p") && map.contains("d"))
{
endpoint = map["ep"].toUInt(&ok);
if (!ok) { return false; }
profileId = map["p"].toUInt(&ok);
if (!ok) { return false; }
deviceId = map["d"].toUInt(&ok);
if (!ok) { return false; }
inClusters.clear();
outClusters.clear();
if (map.contains("in") && map["in"].type() == QVariant::List)
{
QVariantList ls = map["in"].toList();
QVariantList::const_iterator i = ls.constBegin();
QVariantList::const_iterator end = ls.constEnd();
for (; i != end; ++i)
{
quint16 clusterId = i->toUInt(&ok);
if (ok)
{
inClusters.push_back(clusterId);
}
}
}
if (map.contains("out") && map["out"].type() == QVariant::List)
{
QVariantList ls = map["out"].toList();
QVariantList::const_iterator i = ls.constBegin();
QVariantList::const_iterator end = ls.constEnd();
for (; i != end; ++i)
{
quint16 clusterId = i->toUInt(&ok);
if (ok)
{
outClusters.push_back(clusterId);
}
}
}
return true;
}
return false;
}
/*! Returns true if server cluster is part of the finger print.
*/
bool SensorFingerprint::hasInCluster(quint16 clusterId) const
{
for (size_t i = 0; i < inClusters.size(); i++)
{
if (inClusters[i] == clusterId)
{
return true;
}
}
return false;
}
/*! Returns true if server cluster is part of the finger print.
*/
bool SensorFingerprint::hasOutCluster(quint16 clusterId) const
{
for (size_t i = 0; i < outClusters.size(); i++)
{
if (outClusters[i] == clusterId)
{
return true;
}
}
return false;
}
/*! Constructor. */
Sensor::Sensor() :
Resource(RSensors),
m_deletedstate(Sensor::StateNormal),
m_mode(ModeTwoGroups),
m_resetRetryCount(0),
m_rxCounter(0)
{
QDateTime now = QDateTime::currentDateTime();
lastStatePush = now;
durationDue = QDateTime();
// common sensor items
addItem(DataTypeString, RAttrName);
addItem(DataTypeString, RAttrManufacturerName);
addItem(DataTypeString, RAttrModelId);
addItem(DataTypeString, RAttrType);
addItem(DataTypeString, RAttrSwVersion);
addItem(DataTypeString, RAttrId);
addItem(DataTypeString, RAttrUniqueId);
addItem(DataTypeBool, RConfigOn);
addItem(DataTypeBool, RConfigReachable);
addItem(DataTypeTime, RStateLastUpdated);
previousDirection = 0xFF;
previousCt = 0xFFFF;
previousSequenceNumber = 0xFF;
previousCommandId = 0xFF;
}
/*! Returns the sensor deleted state.
*/
Sensor::DeletedState Sensor::deletedState() const
{
return m_deletedstate;
}
/*! Sets the sensor deleted state.
\param deletedState the sensor deleted state
*/
void Sensor::setDeletedState(DeletedState deletedstate)
{
m_deletedstate = deletedstate;
}
/*! Returns true if the sensor is reachable.
*/
bool Sensor::isAvailable() const
{
const ResourceItem *i = item(RConfigReachable);
if (i)
{
return i->toBool();
}
return true;
}
/*! Returns the sensor name.
*/
const QString &Sensor::name() const
{
return item(RAttrName)->toString();
}
/*! Sets the sensor name.
\param name the sensor name
*/
void Sensor::setName(const QString &name)
{
item(RAttrName)->setValue(name);
}
/*! Returns the sensor mode.
*/
Sensor::SensorMode Sensor::mode() const
{
return m_mode;
}
/*! Sets the sensor mode (Lighting Switch).
* 1 = Secenes
* 2 = Groups
* 3 = Color Temperature
\param mode the sensor mode
*/
void Sensor::setMode(SensorMode mode)
{
m_mode = mode;
}
/*! Returns the sensor type.
*/
const QString &Sensor::type() const
{
return item(RAttrType)->toString();
}
/*! Sets the sensor type.
\param type the sensor type
*/
void Sensor::setType(const QString &type)
{
item(RAttrType)->setValue(type);
}
/*! Returns the sensor modelId.
*/
const QString &Sensor::modelId() const
{
return item(RAttrModelId)->toString();
}
/*! Sets the sensor modelId.
\param mid the sensor modelId
*/
void Sensor::setModelId(const QString &mid)
{
item(RAttrModelId)->setValue(mid.trimmed());
}
/*! Returns the resetRetryCount.
*/
uint8_t Sensor::resetRetryCount() const
{
return m_resetRetryCount;
}
/*! Sets the resetRetryCount.
\param resetRetryCount the resetRetryCount
*/
void Sensor::setResetRetryCount(uint8_t resetRetryCount)
{
m_resetRetryCount = resetRetryCount;
}
/*! Returns the zdpResetSeq number.
*/
uint8_t Sensor::zdpResetSeq() const
{
return m_zdpResetSeq;
}
/*! Sets the zdpResetSeq number.
\param resetRetryCount the resetRetryCount
*/
void Sensor::setZdpResetSeq(uint8_t zdpResetSeq)
{
m_zdpResetSeq = zdpResetSeq;
}
void Sensor::updateStateTimestamp()
{
ResourceItem *i = item(RStateLastUpdated);
if (i)
{
i->setValue(QDateTime::currentDateTimeUtc());
m_rxCounter++;
}
}
/*! Increments the number of received commands during this session. */
void Sensor::incrementRxCounter()
{
m_rxCounter++;
}
/*! Returns number of received commands during this session. */
int Sensor::rxCounter() const
{
return m_rxCounter;
}
/*! Returns the sensor manufacturer.
*/
const QString &Sensor::manufacturer() const
{
return item(RAttrManufacturerName)->toString();
}
/*! Sets the sensor manufacturer.
\param manufacturer the sensor manufacturer
*/
void Sensor::setManufacturer(const QString &manufacturer)
{
item(RAttrManufacturerName)->setValue(manufacturer.trimmed());
}
/*! Returns the sensor software version.
Not supported for ZGP Sensortype
*/
const QString &Sensor::swVersion() const
{
return item(RAttrSwVersion)->toString();
}
/*! Sets the sensor software version.
\param swVersion the sensor software version
*/
void Sensor::setSwVersion(const QString &swversion)
{
item(RAttrSwVersion)->setValue(swversion.trimmed());
}
/*! Transfers state into JSONString.
*/
QString Sensor::stateToString()
{
QVariantMap map;
for (int i = 0; i < itemCount(); i++)
{
ResourceItem *item = itemForIndex(i);
const ResourceItemDescriptor &rid = item->descriptor();
if (strncmp(rid.suffix, "state/", 6) == 0)
{
const char *key = item->descriptor().suffix + 6;
map[key] = item->toVariant();
}
}
return Json::serialize(map);
}
/*! Transfers config into JSONString.
*/
QString Sensor::configToString()
{
QVariantMap map;
for (int i = 0; i < itemCount(); i++)
{
ResourceItem *item = itemForIndex(i);
const ResourceItemDescriptor &rid = item->descriptor();
if (strncmp(rid.suffix, "config/", 7) == 0)
{
const char *key = item->descriptor().suffix + 7;
map[key] = item->toVariant();
}
}
return Json::serialize(map);
}
/*! Parse the sensor state from a JSON string. */
void Sensor::jsonToState(const QString &json)
{
bool ok;
QVariant var = Json::parse(json, ok);
if (!ok)
{
return;
}
QVariantMap map = var.toMap();
if (map.contains("lastset"))
{
QString lastset = map["lastset"].toString();
QString format = QLatin1String("yyyy-MM-ddTHH:mm:ssZ");
QDateTime ls = QDateTime::fromString(lastset, format);
ls.setTimeSpec(Qt::UTC);
map["lastset"] = ls;
}
// use old time stamp before deCONZ was started
QDateTime dt = QDateTime::currentDateTime().addSecs(-120);
if (map.contains("lastupdated"))
{
QString lastupdated = map["lastupdated"].toString();
QString format = lastupdated.length() == 19 ? QLatin1String("yyyy-MM-ddTHH:mm:ss") : QLatin1String("yyyy-MM-ddTHH:mm:ss.zzz");
QDateTime lu = QDateTime::fromString(lastupdated, format);
if (lu < dt)
{
dt = lu;
}
lu.setTimeSpec(Qt::UTC);
map["lastupdated"] = lu;
}
if (map.contains("localtime"))
{
QString localtime = map["localtime"].toString();
QString format = QLatin1String("yyyy-MM-ddTHH:mm:ss");
QDateTime lt = QDateTime::fromString(localtime, format);
map["localtime"] = lt;
}
if (map.contains("utc"))
{
QString utc = map["utc"].toString();
QString format = QLatin1String("yyyy-MM-ddTHH:mm:ssZ");
QDateTime u = QDateTime::fromString(utc, format);
u.setTimeSpec(Qt::UTC);
map["utc"] = u;
}
for (int i = 0; i < itemCount(); i++)
{
ResourceItem *item = itemForIndex(i);
const ResourceItemDescriptor &rid = item->descriptor();
if (strncmp(rid.suffix, "state/", 6) == 0)
{
const char *key = item->descriptor().suffix + 6;
if (map.contains(QLatin1String(key)))
{
item->setValue(map[key]);
item->setTimeStamps(dt);
}
}
}
}
/*! Parse the sensor config from a JSON string. */
void Sensor::jsonToConfig(const QString &json)
{
bool ok;
QVariant var = Json::parse(json, ok);
if (!ok)
{
return;
}
QVariantMap map = var.toMap();
if (map.contains("lastchange_time"))
{
QString lastchange_time = map["lastchange_time"].toString();
QString format = QLatin1String("yyyy-MM-ddTHH:mm:ssZ");
QDateTime lct = QDateTime::fromString(lastchange_time, format);
lct.setTimeSpec(Qt::UTC);
map["lastchange_time"] = lct;
}
QDateTime dt = QDateTime::currentDateTime().addSecs(-120);
for (int i = 0; i < itemCount(); i++)
{
ResourceItem *item = itemForIndex(i);
const ResourceItemDescriptor &rid = item->descriptor();
if (type().startsWith(QLatin1String("CLIP")))
{}
else if (item->descriptor().suffix == RConfigReachable)
{ // set only from live data
item->setValue(false);
continue;
}
if (strncmp(rid.suffix, "config/", 7) == 0 && rid.suffix != RConfigPending)
{
const char *key = item->descriptor().suffix + 7;
if (map.contains(QLatin1String(key)))
{
QVariant val = map[key];
if (val.isNull())
{
if (rid.suffix == RConfigOn)
{
map[key] = true; // default value
setNeedSaveDatabase(true);
}
else
{
continue;
}
}
item->setValue(map[key]);
item->setTimeStamps(dt);
}
}
}
}
/*! Returns the sensor fingerprint. */
SensorFingerprint &Sensor::fingerPrint()
{
return m_fingerPrint;
}
/*! Returns the sensor fingerprint. */
const SensorFingerprint &Sensor::fingerPrint() const
{
return m_fingerPrint;
}
const std::vector<Sensor::ButtonMap> Sensor::buttonMap(const QMap<QString, std::vector<Sensor::ButtonMap>> &buttonMapData, QMap<QString, QString> &buttonMapForModelId)
{
if (m_buttonMap.empty())
{
QString modelid;
if (isTuyaManufacturerName(item(RAttrManufacturerName)->toString()))
{
// for Tuya devices use manufacturer name as modelid
modelid = item(RAttrManufacturerName)->toString();
}
else
{
modelid = item(RAttrModelId)->toString();
}
for (auto i = buttonMapForModelId.constBegin(); i != buttonMapForModelId.constEnd(); ++i)
{
if (i.key().isEmpty())
{
continue;
}
if (modelid == i.key())
{
m_buttonMap = buttonMapData.value(i.value());
break;
}
}
}
return m_buttonMap;
}