forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor.cpp
More file actions
787 lines (690 loc) · 17 KB
/
sensor.cpp
File metadata and controls
787 lines (690 loc) · 17 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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
/*
* Copyright (c) 2016 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"
/*! 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;
}
/*! Constructor. */
Sensor::Sensor() :
m_deletedstate(Sensor::StateNormal),
m_name(""),
m_type("undefined"),
m_modelid(""),
m_manufacturer("unknown"),
m_swversion("1.0"),
m_mode(ModeTwoGroups),
m_resetRetryCount(0)
{
sensorTypes.append("CLIPSwitch");
sensorTypes.append("CLIPOpenClose");
sensorTypes.append("CLIPPresence");
sensorTypes.append("CLIPTemperature");
sensorTypes.append("CLIPGenericFlag");
sensorTypes.append("CLIPGenericStatus");
sensorTypes.append("CLIPHumidity");
sensorTypes.append("Daylight");
sensorTypes.append("ZGPSwitch");
sensorTypes.append("ZHASwitch");
sensorTypes.append("ZHALight");
sensorTypes.append("ZHAPresence");
}
/*! 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 the sensor name.
*/
const QString &Sensor::name() const
{
return m_name;
}
/*! Sets the sensor name.
\param name the sensor name
*/
void Sensor::setName(const QString &name)
{
m_name = name;
}
/*! Returns the sensor mode (Lighting Switch).
* 1 = Secenes
* 2 = Groups
* 3 = Color Temperature
*/
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 m_type;
}
/*! Sets the sensor type.
\param type the sensor type
*/
void Sensor::setType(const QString &type)
{
m_type = type;
}
/*! Returns the sensor modelId.
*/
const QString &Sensor::modelId() const
{
return m_modelid;
}
/*! Sets the sensor modelId.
\param mid the sensor modelId
*/
void Sensor::setModelId(const QString &mid)
{
m_modelid = 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;
}
/*! Returns the sensor manufacturer.
*/
const QString &Sensor::manufacturer() const
{
return m_manufacturer;
}
/*! Sets the sensor manufacturer.
\param manufacturer the sensor manufacturer
*/
void Sensor::setManufacturer(const QString &manufacturer)
{
m_manufacturer = manufacturer;
}
/*! Returns the sensor software version.
Not supported for ZGP Sensortype
*/
const QString &Sensor::swVersion() const
{
return m_swversion;
}
/*! Sets the sensor software version.
\param swVersion the sensor software version
*/
void Sensor::setSwVersion(const QString &swversion)
{
m_swversion = swversion;
}
/*! Returns the sensor state.
*/
SensorState &Sensor::state()
{
return m_state;
}
/*! Returns the sensor state.
*/
const SensorState &Sensor::state() const
{
return m_state;
}
/*! Sets the sensor state.
\param state the sensor state
*/
void Sensor::setState(const SensorState &state)
{
m_state = state;
}
/*! Returns the sensor config.
*/
const SensorConfig &Sensor::config() const
{
return m_config;
}
/*! Sets the sensor config.
\param config the sensor config
*/
void Sensor::setConfig(const SensorConfig &config)
{
m_config = config;
}
/*! Transfers state into JSONString.
\param state
*/
QString Sensor::stateToString(const SensorState &state)
{
QString jsonString = QString("{\"lastupdated\":\"%1\",\"flag\":\"%2\",\"status\":\"%3\",\"presence\":\"%4\",\"open\":\"%5\",\"buttonevent\":\"%6\",\"temperature\":\"%7\",\"humidity\":\"%8\",\"daylight\":\"%9\"}")
.arg(state.lastupdated())
.arg(state.flag())
.arg(state.status())
.arg(state.presence())
.arg(state.open())
.arg(state.buttonevent())
.arg(state.temperature())
.arg(state.humidity())
.arg(state.daylight());
return jsonString;
}
/*! Transfers config into JSONString.
\param config
*/
QString Sensor::configToString(const SensorConfig &config)
{
QString jsonString = QString("{\"on\": %1,\"reachable\": %2,\"battery\":\"%3\",\"url\":\"%4\",\"long\":\"%5\",\"lat\":\"%6\",\"sunriseoffset\":\"%7\",\"sunsetoffset\":\"%8\"}")
.arg(config.on())
.arg(config.reachable())
.arg(config.battery())
.arg(config.url())
.arg(config.longitude())
.arg(config.lat())
.arg(config.sunriseoffset())
.arg(config.sunsetoffset());
return jsonString;
}
/*! Parse the sensor state from a JSON string. */
SensorState Sensor::jsonToState(const QString &json)
{
bool ok;
QVariant var = (Json::parse(json, ok));
QVariantMap map = var.toMap();
SensorState state;
state.setButtonevent(map["buttonevent"].toDouble());
state.setDaylight(map["daylight"].toString());
state.setFlag(map["flag"].toString());
state.setHumidity(map["humidity"].toString());
state.setLastupdated(map["lastupdated"].toString());
state.setOpen(map["open"].toString());
state.setPresence(map["presence"].toString());
state.setStatus(map["status"].toString());
state.setTemperature(map["temperature"].toString());
return state;
}
/*! Parse the sensor config from a JSON string. */
SensorConfig Sensor::jsonToConfig(const QString &json)
{
bool ok;
SensorConfig config;
QVariant var = Json::parse(json, ok);
if (!ok)
{
return config;
}
QVariantMap map = var.toMap();
config.setOn(map["on"].toBool());
config.setReachable(map["reachable"].toBool());
uint battery = map["battery"].toUInt(&ok);
if (ok)
{
config.setBattery(battery);
}
config.setUrl(map["url"].toString());
config.setLongitude(map["long"].toString());
config.setLat(map["lat"].toString());
config.setSunriseoffset(map["sunriseoffset"].toString());
config.setSunsetoffset(map["sunsetoffset"].toString());
return config;
}
/*! Returns the sensor fingerprint. */
SensorFingerprint &Sensor::fingerPrint()
{
return m_fingerPrint;
}
/*! Returns the sensor fingerprint. */
const SensorFingerprint &Sensor::fingerPrint() const
{
return m_fingerPrint;
}
// Sensor state
/*! Constructor. */
SensorState::SensorState() :
// m_lastupdated(""),
m_flag(""),
m_status(""),
m_presence(""),
m_open(""),
m_buttonevent(-1),
m_temperature(""),
m_humidity(""),
m_daylight(""),
m_lux(0)
{
updateTime();
}
/*! Returns the sensor state lastupdated attribute.
Sensorstypes: all
*/
const QString &SensorState::lastupdated() const
{
return m_lastupdated;
}
/*! Sets the sensor state lastupdated attribute.
Sensorstypes: all
\param lastupdated the sensor state lastupdated
*/
void SensorState::setLastupdated(const QString &lastupdated)
{
m_lastupdated = lastupdated;
}
/*! Returns the sensor state flag attribute.
Sensorstypes: CLIPGenericFlag
*/
const QString &SensorState::flag() const
{
return m_flag;
}
/*! Sets the sensor state flag attribute.
Sensorstypes: CLIPGenericFlag
\param flag the sensor state flag
*/
void SensorState::setFlag(const QString &flag)
{
m_flag = flag;
}
/*! Returns the sensor state status attribute.
Sensortypes: CLIPGenericStatus
*/
const QString &SensorState::status() const
{
return m_status;
}
/*! Sets the sensor state status attribute.
Sensortypes: CLIPGenericStatus
\param status the sensor config status
*/
void SensorState::setStatus(const QString &status)
{
m_status = status;
}
/*! Returns the sensor state presence attribute.
*/
const QString &SensorState::presence() const
{
return m_presence;
}
/*! Sets the sensor state presence attribute.
Sensortypes: CLIPPresence
\param presence the sensor state presence
*/
void SensorState::setPresence(const QString &presence)
{
m_presence = presence;
}
/*! Returns the sensor state open attribute.
Sensortypes: CLIPOpenClose
*/
const QString &SensorState::open() const
{
return m_open;
}
/*! Sets the sensor state open attribute.
Sensortypes: CLIPOpenClose
\param open the sensor state open
*/
void SensorState::setOpen(const QString &open)
{
m_open = open;
}
/*! Returns the sensor state buttonevent attribute.
Sensortypes: ZGPSwitch
*/
int SensorState::buttonevent() const
{
return m_buttonevent;
}
/*! Sets the sensor state buttonevent attribute.
Sensortypes: ZGPSwitch
\param buttonevent the sensor state buttonevent
*/
void SensorState::setButtonevent(int buttonevent)
{
m_buttonevent = buttonevent;
}
/*! Returns the sensor state temperature attribute.
Sensortypes: CLIPTemperature
*/
const QString &SensorState::temperature() const
{
return m_temperature;
}
/*! Sets the sensor state temperature attribute.
Sensortypes: CLIPTemperature
\param temperature the sensor state temperature
*/
void SensorState::setTemperature(const QString &temperature)
{
m_temperature = temperature;
}
/*! Returns the sensor state humidity attribute.
Sensortypes: CLIPHumidity
*/
const QString &SensorState::humidity() const
{
return m_humidity;
}
/*! Sets the sensor state daylight attribute.
Sensortypes: Daylight
\param daylight the sensor state daylight
*/
void SensorState::setHumidity(const QString &humidity)
{
m_humidity = humidity;
}
/*! Returns the sensor state daylight attribute.
Sensortypes: CLIPHumidity
*/
const QString &SensorState::daylight() const
{
return m_daylight;
}
/*! Sets the sensor state humidity attribute.
Sensortypes: Daylight
\param humidity the sensor state humidity
*/
void SensorState::setDaylight(const QString &daylight)
{
m_daylight = daylight;
}
/*! Returns the sensor state lux attribute.
Sensortypes: ZHALight
*/
quint32 SensorState::lux() const
{
return m_lux;
}
/*! Sets the sensor state lux attribute.
Sensortypes: ZHALight
\param lux the sensor state measured lux value
*/
void SensorState::setLux(quint32 lux)
{
m_lux = lux;
}
/*! Refreshs the last updated time.
*/
void SensorState::updateTime()
{
QDateTime datetime = QDateTime::currentDateTimeUtc();
m_lastupdated = datetime.toString("yyyy-MM-ddTHH:mm:ss"); // ISO 8601
}
// Sensor Config
/*! Constructor. */
SensorConfig::SensorConfig() :
m_on(true),
m_reachable(false),
m_duration(-1),
m_battery(255), // invalid
m_url(""),
m_long(""),
m_lat(""),
m_sunriseoffset(""),
m_sunsetoffset("")
{
}
/*! Returns the sensor config on attribute.
Sensortypes: all
*/
bool SensorConfig::on() const
{
return m_on;
}
/*! Sets the sensor config on attribute.
Sensortypes: all
\param on the sensor config on
*/
void SensorConfig::setOn(bool on)
{
m_on = on;
}
/*! Returns the sensor config reachable attribute.
Sensortypes: all CLIP, Generic, General sensors
*/
bool SensorConfig::reachable() const
{
return m_reachable;
}
/*! Sets the sensor config reachable attribute.
Sensortypes: all CLIP, Generic, General sensors
\param reachable the sensor config reachable
*/
void SensorConfig::setReachable(bool reachable)
{
m_reachable = reachable;
}
/*! Returns the sensor config duration attribute.
Sensortypes: ZHAPresence
*/
double SensorConfig::duration() const
{
return m_duration;
}
/*! Sets the sensor config duration attribute.
Sensortypes: all ZHAPresence
\param duration the sensor config duration attribute
*/
void SensorConfig::setDuration(double duration)
{
DBG_Assert(duration >= 0 && duration < 65535);
if (duration >= 0 && duration <= 65535)
{
m_duration = duration;
}
}
/*! Returns the sensor config battery attribute.
Sensortypes: all CLIP, Generic, General sensors
*/
quint8 SensorConfig::battery() const
{
return m_battery;
}
/*! Sets the sensor config battery attribute.
Sensortypes: all CLIP, Generic, General sensors
\param battery the sensor config battery
*/
void SensorConfig::setBattery(quint8 battery)
{
m_battery = battery;
}
/*! Returns the sensor config url attribute.
Sensortypes: all CLIP, Generic, General sensors
*/
const QString &SensorConfig::url() const
{
return m_url;
}
/*! Sets the sensor config url attribute.
Sensortypes: all CLIP, Generic, General sensors
\param url the sensor config url
*/
void SensorConfig::setUrl(const QString &url)
{
m_url = url;
}
/*! Returns the sensor config longitude attribute.
Sensortypes: Daylight
*/
const QString &SensorConfig::longitude() const
{
return m_long;
}
/*! Sets the sensor config longitude attribute.
Sensortypes: Daylight
\param longitude the sensor config longitude
*/
void SensorConfig::setLongitude(const QString &longitude)
{
m_long = longitude;
}
/*! Returns the sensor config lat attribute.
Sensortypes: Daylight
*/
const QString &SensorConfig::lat() const
{
return m_lat;
}
/*! Sets the sensor config lat attribute.
Sensortypes: Daylight
\param lat the sensor config lat
*/
void SensorConfig::setLat(const QString &lat)
{
m_lat = lat;
}
/*! Returns the sensor config sunriseoffset attribute.
Sensortypes: Daylight
*/
const QString &SensorConfig::sunriseoffset() const
{
return m_sunriseoffset;
}
/*! Sets the sensor config url attribute.
Sensortypes: Daylight
\param url the sensor config url
*/
void SensorConfig::setSunriseoffset(const QString &sunriseoffset)
{
m_sunriseoffset = sunriseoffset;
}
/*! Returns the sensor config sunsetoffset attribute.
Sensortypes: Daylight
*/
const QString &SensorConfig::sunsetoffset() const
{
return m_sunsetoffset;
}
/*! Sets the sensor config sunsetoffset attribute.
Sensortypes: Daylight
\param sunsetoffset the sensor config sunsetoffset
*/
void SensorConfig::setSunsetoffset(const QString &sunsetoffset)
{
m_sunsetoffset = sunsetoffset;
}