forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zcl_tasks.cpp
1418 lines (1190 loc) · 45.7 KB
/
zcl_tasks.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
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013-2019 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 <QString>
#include <QVariantMap>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
#include "colorspace.h"
/** @brief Min of A and B */
#define MIN(A,B) (((A) <= (B)) ? (A) : (B))
/** @brief Max of A and B */
#define MAX(A,B) (((A) >= (B)) ? (A) : (B))
/** @brief Min of A, B, and C */
#define MIN3(A,B,C) (((A) <= (B)) ? MIN(A,C) : MIN(B,C))
/** @brief Max of A, B, and C */
#define MAX3(A,B,C) (((A) >= (B)) ? MAX(A,C) : MAX(B,C))
/*! Add a MoveLevel task to the queue
\param task - the task item
\param withOnOff - if true command is send with on/off
\param upDirection - true is up, false is down
\param rate - the move rate
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskMoveLevel(TaskItem &task, bool withOnOff, bool upDirection, quint8 rate)
{
task.taskType = TaskMoveLevel;
task.req.setClusterId(LEVEL_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
if (rate > 0)
{
if (withOnOff)
{
task.zclFrame.setCommandId(0x05); // move level with on/off
}
else
{
task.zclFrame.setCommandId(0x01); // move level
}
}
else
{
task.zclFrame.setCommandId(0x03); // stop
}
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
if (rate > 0)
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
quint8 direction = upDirection ? 0x00 : 0x01;
stream << direction;
stream << rate;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a OnOff task to the queue
*
* \param task - the task item
* \param cmd - command to send ONOFF_COMMAND_ON, ONOFF_COMMAND_OFF, ONOFF_COMMAND_TOGGLE or ONOFF_COMMAND_ON_WITH_TIMED_OFF
* \param ontime - ontime, used only for command ONOFF_COMMAND_ON_WITH_TIMED_OFF
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetOnOff(TaskItem &task, quint8 cmd, quint16 ontime, quint8 flags)
{
DBG_Assert(cmd == ONOFF_COMMAND_ON || cmd == ONOFF_COMMAND_OFF || cmd == ONOFF_COMMAND_TOGGLE || cmd == ONOFF_COMMAND_ON_WITH_TIMED_OFF);
if (!(cmd == ONOFF_COMMAND_ON || cmd == ONOFF_COMMAND_OFF || cmd == ONOFF_COMMAND_TOGGLE || cmd == ONOFF_COMMAND_ON_WITH_TIMED_OFF))
{
return false;
}
task.taskType = TaskSendOnOffToggle;
task.onOff = cmd;
task.req.setClusterId(ONOFF_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(cmd);
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
if (cmd == ONOFF_COMMAND_ON_WITH_TIMED_OFF)
{
const quint16 offWaitTime = 0;
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
// stream << (quint8)0x80; // 0x01 accept only when on --> no, 0x80 overwrite ontime (yes, non standard)
stream << flags;
stream << ontime;
stream << offWaitTime;
}
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
return addTask(task);
}
/*!
* Add a brightness task to the queue
*
* \param task - the task item
* \param bri - brightness level 0..255
* \param withOnOff - also set onOff state
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetBrightness(TaskItem &task, uint8_t bri, bool withOnOff)
{
task.taskType = TaskSetLevel;
task.level = bri;
task.req.setClusterId(LEVEL_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
if (withOnOff)
{
task.zclFrame.setCommandId(0x04); // Move to level (with on/off)
}
else
{
task.zclFrame.setCommandId(0x00); // Move to level
}
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.level;
stream << task.transitionTime;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a color temperature increase task to the queue
*
* \param task - the task item
* \param ct - step size -65534 ..65534, 0 to stop running step
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskIncColorTemperature(TaskItem &task, int32_t ct)
{
task.taskType = TaskIncColorTemperature;
task.inc = ct;
task.req.setClusterId(COLOR_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
if (ct == 0)
{
task.zclFrame.setCommandId(0x47); // Stop move step
}
else
{ // payload
task.zclFrame.setCommandId(0x4C); // Step color temperature
quint8 direction = ct > 0 ? 1 : 3; // up, down
quint16 stepSize = ct > 0 ? ct : -ct;
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << direction;
stream << stepSize;
stream << task.transitionTime;
stream << (quint16)0; // min dummy
stream << (quint16)0; // max dummy
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a brightness increase task to the queue
*
* \param task - the task item
* \param bri - step size -254 ..254, 0 to stop running step
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskIncBrightness(TaskItem &task, int16_t bri)
{
task.taskType = TaskIncBrightness;
task.inc = bri;
task.req.setClusterId(LEVEL_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
if (bri == 0)
{
task.zclFrame.setCommandId(0x03); // Stop
}
else
{ // payload
task.zclFrame.setCommandId(0x02); // Step level
quint8 mode = bri > 0 ? 0 : 1; // up, down
quint8 stepSize = (bri > 0) ? bri : bri * -1;
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << mode;
stream << stepSize;
stream << task.transitionTime;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a stop brightness task to the queue
*
* \param task - the task item
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskStopBrightness(TaskItem &task)
{
task.taskType = TaskStopLevel;
task.req.setClusterId(LEVEL_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x03); // Stop
task.zclFrame.payload().clear();
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a set color temperature task to the queue
*
* \param task - the task item
* \param ct - mired color temperature
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetColorTemperature(TaskItem &task, uint16_t ct)
{
// Workaround for interim FLS-H
// which does not support the color temperature ZCL command
if (task.lightNode && (task.lightNode->manufacturerCode() == VENDOR_ATMEL) && (task.lightNode->modelId() == "FLS-H"))
{
float ctMin = 153;
float ctMax = 500;
float sat = (static_cast<float>(ct) - ctMin) / (ctMax - ctMin) * 254;
if (sat > 254)
{
sat = 254;
}
bool ret = addTaskSetSaturation(task, static_cast<quint8>(sat));
// overwrite for later use
task.taskType = TaskSetColorTemperature;
task.colorTemperature = ct;
if (task.lightNode)
{
if (task.lightNode->colorMode() != QLatin1String("ct"))
{
task.lightNode->setColorMode(QLatin1String("ct"));
Event e(RLights, RStateColorMode, task.lightNode->id());
enqueueEvent(e);
}
}
return ret;
}
if (task.lightNode)
{
ResourceItem *ctMin = task.lightNode->item(RConfigCtMin);
ResourceItem *ctMax = task.lightNode->item(RConfigCtMax);
// keep ct in supported bounds
if (ctMin && ctMax && ctMin->toNumber() > 0 && ctMax->toNumber() > 0)
{
if (ct < ctMin->toNumber()) { ct = static_cast<quint16>(ctMin->toNumber()); }
else if (ct > ctMax->toNumber()) { ct = static_cast<quint16>(ctMax->toNumber()); }
}
if (task.lightNode->colorMode() != QLatin1String("ct"))
{
task.lightNode->setColorMode(QLatin1String("ct"));
Event e(RLights, RStateColorMode, task.lightNode->id());
enqueueEvent(e);
}
// If light does not support "ct" but does suport "xy", we can emulate the former:
ResourceItem *colorCaps = task.lightNode->item(RConfigColorCapabilities);
bool supportsXy = colorCaps && colorCaps->toNumber() & 0x0008;
bool supportsCt = colorCaps && colorCaps->toNumber() & 0x0010;
bool useXy = supportsXy && !supportsCt;
// IKEA lights need to use "xy" because move to color temperature is broken and
// won't update x,y values resulting in broken scenes.
useXy = useXy || task.lightNode->manufacturerCode() == VENDOR_IKEA;
if (useXy)
{
quint16 x;
quint16 y;
MiredColorTemperatureToXY(ct, &x, &y);
qreal xr = x / 65535.0;
qreal yr = y / 65535.0;
if (xr < 0) { xr = 0; }
else if (xr > 1) { xr = 1; }
if (yr < 0) { yr = 0; }
else if (yr > 1) { yr = 1; }
return addTaskSetXyColor(task, xr, yr);
}
DBG_Printf(DBG_INFO, "send move to color temperature %u to 0x%016llX\n", ct, task.lightNode->address().ext());
}
task.taskType = TaskSetColorTemperature;
task.colorTemperature = ct;
task.req.setClusterId(COLOR_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x0a); // Move to color temperature
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.colorTemperature;
stream << task.transitionTime;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a set enhanced hue task to the queue
*
* \param task - the task item
* \param hue - brightness level 0..65535
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetEnhancedHue(TaskItem &task, uint16_t hue)
{
task.taskType = TaskSetEnhancedHue;
task.hueReal = hue / (360.0 * 182.04444);
if (task.lightNode)
{
if (task.lightNode->colorMode() != QLatin1String("hs"))
{
task.lightNode->setColorMode(QLatin1String("hs"));
Event e(RLights, RStateColorMode, task.lightNode->id());
enqueueEvent(e);
}
}
if (task.hueReal < 0.0)
{
task.hueReal = 0.0;
}
else if (task.hueReal > 1.0)
{
task.hueReal = 1.0;
}
task.hue = static_cast<quint8>(task.hueReal * 254.0);
// task.enhancedHue = hue * 360.0 * 182.04444;
task.enhancedHue = hue;
task.req.setClusterId(COLOR_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x40); // Enhanced move to hue
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
uint8_t direction = 0x00;
stream << task.enhancedHue;
stream << direction;
stream << task.transitionTime;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a set saturation task to the queue
*
* \param task - the task item
* \param sat - brightness level 0..255
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetSaturation(TaskItem &task, uint8_t sat)
{
task.taskType = TaskSetSat;
task.sat = sat;
if (task.lightNode)
{
if (task.lightNode->colorMode() != QLatin1String("hs"))
{
task.lightNode->setColorMode(QLatin1String("hs"));
Event e(RLights, RStateColorMode, task.lightNode->id());
enqueueEvent(e);
}
}
if (sat == 255)
{
sat = 254;
}
task.req.setClusterId(COLOR_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x03); // Move to saturation
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.sat;
stream << task.transitionTime;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a set hue and saturation task to the queue
*
* \param task - the task item
* \param sat - brightness level 0..254
* \param hue - hue 0..254
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetHueAndSaturation(TaskItem &task, uint8_t hue, uint8_t sat)
{
task.taskType = TaskSetHueAndSaturation;
task.sat = sat;
task.hue = hue;
task.hueReal = hue / 254.0;
task.enhancedHue = static_cast<quint16>(task.hueReal * 360.0 * 182.04444);
if (task.lightNode)
{
if (task.lightNode->colorMode() != QLatin1String("hs"))
{
task.lightNode->setColorMode(QLatin1String("hs"));
Event e(RLights, RStateColorMode, task.lightNode->id());
enqueueEvent(e);
}
}
if (sat == 255)
{
sat = 254;
}
task.req.setClusterId(COLOR_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x06); // Move to hue and saturation
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.hue;
stream << task.sat;
stream << task.transitionTime;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a set xy color task but as hue and saturation task to the queue
*
* \param task - the task item
* \param x - normalized x coordinate 0.0 .. 1.0
* \param y - normalized y coordinate 0.0 .. 1.0
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetXyColorAsHueAndSaturation(TaskItem &task, double x, double y)
{
num r, g, b;
num h, s, v;
num X, Y, Z;
// prevent division through zero
if (x <= 0.0) {
x = 0.00000001;
}
// prevent division through zero
if (y <= 0.0) {
y = 0.00000001;
}
if (task.lightNode)
{
ResourceItem *item = task.lightNode->item(RStateBri);
if (item)
{
Y = item->toNumber() / 255.0;
}
else
{
Y = 1.0;
}
}
else {
Y = 1.0;
}
X = (Y / y) * x;
Z = (Y / y) * (1.0 - x - y);
num min = MIN3(X,Y,Z);
if (min < 0)
{
X += min;
Y += min;
Z += min;
}
num max = MAX3(X,Y,Z);
if (max > 1)
{
X /= max;
Y /= max;
Z /= max;
}
DBG_Printf(DBG_INFO, "xy = (%f, %f), XYZ = (%f, %f, %f)\n",x, y, X, Y, Z);
r = (num)( 3.2406*X - 1.5372*Y - 0.4986*Z);
g = (num)(-0.9689*X + 1.8758*Y + 0.0415*Z);
b = (num)( 0.0557*X - 0.2040*Y + 1.0570*Z);
// clip
if (r > 1) r = 1;
if (r < 0) r = 0;
if (g > 1) g = 1;
if (g < 0) g = 0;
if (b > 1) b = 1;
if (b < 0) b = 0;
Rgb2Hsv(&h, &s, &v, r, g, b);
// normalize
h /= 360.0;
if (h > 1.0)
{
h = 1.0;
}
else if (h < 0.0)
{
h = 0.0;
}
uint8_t hue = h * 254.0;
uint8_t sat = s * 254.0;
return addTaskSetHueAndSaturation(task, hue, sat);
}
/*!
* Add a set xy color task to the queue
*
* \param task - the task item
* \param x - normalized x coordinate 0.0 .. 1.0
* \param y - normalized y coordinate 0.0 .. 1.0
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetXyColor(TaskItem &task, double x, double y)
{
task.taskType = TaskSetXyColor;
DBG_Assert(x >= 0);
DBG_Assert(x <= 1);
DBG_Assert(y >= 0);
DBG_Assert(y <= 1);
// The CurrentX attribute contains the current value of the normalized chromaticity value x
// The value of x SHALL be related to the CurrentX attribute by the relationship
// x = CurrentX / 65536 (CurrentX in the range 0 to 65279 inclusive)
task.colorX = static_cast<quint16>(x * 65535.0); // current X in range 0 .. 65279
task.colorY = static_cast<quint16>(y * 65535.0); // current Y in range 0 .. 65279
if (task.colorX > 65279) { task.colorX = 65279; }
else if (task.colorX == 0) { task.colorX = 1; }
if (task.colorY > 65279) { task.colorY = 65279; }
else if (task.colorY == 0) { task.colorY = 1; }
if (task.lightNode)
{
if (task.lightNode->colorMode() != QLatin1String("xy"))
{
task.lightNode->setColorMode(QLatin1String("xy"));
Event e(RLights, RStateColorMode, task.lightNode->id());
enqueueEvent(e);
}
// convert xy coordinates to hue and saturation
// due the old FLS-PP don't support this mode
if (task.lightNode->manufacturerCode() == VENDOR_ATMEL && task.lightNode->modelId() == QLatin1String("FLS-PP"))
{
task.lightNode->setColorXY(task.colorX, task.colorY); // update here
return addTaskSetXyColorAsHueAndSaturation(task, x, y);
}
}
task.req.setClusterId(COLOR_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x07); // Move to color
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.colorX;
stream << task.colorY;
stream << task.transitionTime;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*!
* Add a set color loop task to the queue
*
* \param task - the task item
* \param colorLoopActive - wherever the color loop shall be activated
* \param speed - time in seconds for a whole color loop cycle
* \return true - on success
* false - on error
*/
bool DeRestPluginPrivate::addTaskSetColorLoop(TaskItem &task, bool colorLoopActive, uint8_t speed)
{
task.colorLoop = colorLoopActive;
task.taskType = TaskSetColorLoop;
if (task.lightNode && colorLoopActive)
{
if (task.lightNode->colorMode() != QLatin1String("hs"))
{
task.lightNode->setColorMode(QLatin1String("hs"));
Event e(RLights, RStateColorMode, task.lightNode->id());
enqueueEvent(e);
}
}
task.req.setClusterId(COLOR_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x44); // Color loop set
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
uint8_t updateFlags = 0x07; // update action 0x1, direction 0x02, time 0x04
uint8_t action = colorLoopActive ? 0x02 /* activate color loop from current hue */
: 0x00 /* stop color loop */;
uint8_t direction = 0x01; // up
uint16_t time = speed; // seconds to go through whole color loop. default: 15
uint16_t startHue = 0; // start hue
stream << updateFlags;
stream << action;
stream << direction;
stream << time;
stream << startHue;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*! Add a identify task to the queue.
\param task - the task item
\param identifyTime - the time in seconds to identify
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskIdentify(TaskItem &task, uint16_t identifyTime)
{
task.taskType = TaskIdentify;
task.identifyTime = identifyTime;
task.req.setClusterId(IDENTIFY_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x00); // Identify
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.identifyTime;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*! Add a trigger effect task to the queue.
\param task - the task item
\param effectIdentifier - the effect
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskTriggerEffect(TaskItem &task, uint8_t effectIdentifier)
{
task.taskType = TaskTriggerEffect;
task.effectIdentifier = effectIdentifier;
task.req.setClusterId(IDENTIFY_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x40); // Trigger Effect
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.effectIdentifier;
stream << (uint8_t) 0x00; // default effectVariant
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*! Add a warning task to the queue.
\param task - the task item
\param options - the options
\param duration - the duration
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskWarning(TaskItem &task, uint8_t options, uint16_t duration)
{
task.taskType = TaskWarning;
task.options = options;
task.duration = duration;
uint8_t strobe_duty_cycle = 10;
uint8_t strobe_level = 0;
task.req.setClusterId(IAS_WD_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x00); // Start Warning
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.options;
stream << task.duration;
stream << strobe_duty_cycle;
stream << strobe_level;
}
{ // ZCL frame
task.req.asdu().clear(); // cleanup old request data if there is any
QDataStream stream(&task.req.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
task.zclFrame.writeToStream(stream);
}
return addTask(task);
}
/*! Add a add to group task to the queue.
\param task - the task item
\param groupId - the group to which a node shall be added
\return true - on success
false - on error
*/
bool DeRestPluginPrivate::addTaskAddToGroup(TaskItem &task, uint16_t groupId)
{
task.taskType = TaskAddToGroup;
task.groupId = groupId;
task.req.setClusterId(GROUP_CLUSTER_ID);
task.req.setProfileId(HA_PROFILE_ID);
task.zclFrame.payload().clear();
task.zclFrame.setSequenceNumber(zclSeq++);
task.zclFrame.setCommandId(0x00); // Add to group
task.zclFrame.setFrameControl(deCONZ::ZclFCClusterCommand |
deCONZ::ZclFCDirectionClientToServer |
deCONZ::ZclFCDisableDefaultResponse);
{ // payload
QDataStream stream(&task.zclFrame.payload(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << task.groupId;
uint8_t cstrlen = 0;