iobroker.sonoff 2.5.3 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -22
- package/README.md +212 -204
- package/admin/i18n/de/translations.json +54 -52
- package/admin/i18n/en/translations.json +53 -51
- package/admin/i18n/es/translations.json +54 -52
- package/admin/i18n/fr/translations.json +54 -52
- package/admin/i18n/it/translations.json +54 -52
- package/admin/i18n/nl/translations.json +54 -52
- package/admin/i18n/pl/translations.json +54 -52
- package/admin/i18n/pt/translations.json +54 -52
- package/admin/i18n/ru/translations.json +54 -52
- package/admin/i18n/zh-cn/translations.json +54 -52
- package/admin/index.html +177 -177
- package/admin/index_m.html +218 -218
- package/admin/jsonConfig.json +112 -0
- package/admin/words.js +56 -54
- package/io-package.json +37 -26
- package/lib/datapoints.js +193 -193
- package/lib/server.js +1760 -1765
- package/lib/tools.js +99 -99
- package/main.js +101 -93
- package/package.json +9 -8
package/lib/server.js
CHANGED
|
@@ -1,1765 +1,1760 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
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
|
-
const
|
|
126
|
-
|
|
127
|
-
let
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
let
|
|
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
|
-
if (
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (
|
|
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
|
-
idAlive
|
|
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
|
-
adapter.setState(idAlive, false, false);
|
|
404
|
-
idAlive = `${deviceDesc}.
|
|
405
|
-
adapter.setState(idAlive, 0, false);
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
const dim =
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
const
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
const dim =
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
const
|
|
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
|
-
taskCallbacks
|
|
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
|
-
adapter.
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
if (typeof
|
|
837
|
-
cb =
|
|
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
|
-
obj
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
let
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
obj.data.common.role = 'level.color.
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
xObj
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
xObj
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
xObj =
|
|
997
|
-
updateState(xObj);
|
|
998
|
-
|
|
999
|
-
xObj = addObject('
|
|
1000
|
-
xObj.data.common.read = allowReadColors;
|
|
1001
|
-
updateState(xObj, allowReadColors ? 100 * parseInt(data[attr].substring(
|
|
1002
|
-
|
|
1003
|
-
xObj = addObject('
|
|
1004
|
-
xObj.data.common.read = allowReadColors;
|
|
1005
|
-
updateState(xObj, allowReadColors ? 100 * parseInt(data[attr].substring(
|
|
1006
|
-
|
|
1007
|
-
xObj = addObject('
|
|
1008
|
-
xObj.data.common.read = allowReadColors;
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
xObj = addObject('
|
|
1019
|
-
xObj.data.common.read = allowReadColors;
|
|
1020
|
-
updateState(xObj, allowReadColors ?
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
xObj = addObject('
|
|
1029
|
-
xObj.data.common.read = allowReadColors;
|
|
1030
|
-
updateState(xObj, allowReadColors ?
|
|
1031
|
-
|
|
1032
|
-
xObj = addObject('
|
|
1033
|
-
xObj.data.common.read = allowReadColors;
|
|
1034
|
-
updateState(xObj, allowReadColors ? (
|
|
1035
|
-
|
|
1036
|
-
xObj = addObject('
|
|
1037
|
-
xObj.data.common.read = allowReadColors;
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
//
|
|
1082
|
-
//
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
(adapter.config.
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
heapdump.writeSnapshot(
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
//
|
|
1193
|
-
// "
|
|
1194
|
-
// "
|
|
1195
|
-
// "
|
|
1196
|
-
//
|
|
1197
|
-
//
|
|
1198
|
-
//
|
|
1199
|
-
//
|
|
1200
|
-
//
|
|
1201
|
-
//
|
|
1202
|
-
//
|
|
1203
|
-
//
|
|
1204
|
-
//
|
|
1205
|
-
//
|
|
1206
|
-
//
|
|
1207
|
-
//
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
if (
|
|
1223
|
-
|
|
1224
|
-
}
|
|
1225
|
-
|
|
1226
|
-
if (
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
}
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
if (stateId.match(/^(
|
|
1259
|
-
checkData(client, packet.topic, '
|
|
1260
|
-
} else if (stateId.match(/^(
|
|
1261
|
-
checkData(client, packet.topic, '
|
|
1262
|
-
}
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
}
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
// /
|
|
1293
|
-
// /
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
if (
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
client.
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
}
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
adapter.
|
|
1407
|
-
}
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
}
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
//
|
|
1482
|
-
|
|
1483
|
-
//
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
client.
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
//
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
client.on('
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
}
|
|
1551
|
-
|
|
1552
|
-
if (
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
packet.
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
}
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
}
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
return;
|
|
1636
|
-
}
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
}
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
}
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
}
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
}
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
client.
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
}
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
client.
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
}
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
});
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
config.
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
return this;
|
|
1763
|
-
}
|
|
1764
|
-
|
|
1765
|
-
module.exports = MQTTServer;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const mqtt = require('mqtt-connection');
|
|
4
|
+
const net = require('net');
|
|
5
|
+
const types = require('./datapoints');
|
|
6
|
+
// const memwatch = require('memwatch-next');
|
|
7
|
+
// const heapdump = require('heapdump');
|
|
8
|
+
const hueCalc = true;
|
|
9
|
+
// var heapTest = 0;
|
|
10
|
+
const FORBIDDEN_CHARS = /[\]\[*,;'"`<>\\?]/g;
|
|
11
|
+
const mappingClients = {};
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* HSV to RGB color conversion
|
|
15
|
+
*
|
|
16
|
+
* H runs from 0 to 360 degrees
|
|
17
|
+
* S and V run from 0 to 100
|
|
18
|
+
*
|
|
19
|
+
* Ported from the excellent java algorithm by Eugene Vishnevsky at:
|
|
20
|
+
* http://www.cs.rit.edu/~ncs/color/t_convert.html
|
|
21
|
+
*/
|
|
22
|
+
function hsvToRgb(h, s, v) {
|
|
23
|
+
let r, g, b;
|
|
24
|
+
let i;
|
|
25
|
+
let f, p, q, t;
|
|
26
|
+
|
|
27
|
+
// Make sure our arguments stay in-range
|
|
28
|
+
h = Math.max(0, Math.min(360, h));
|
|
29
|
+
s = Math.max(0, Math.min(100, s));
|
|
30
|
+
v = Math.max(0, Math.min(100, v));
|
|
31
|
+
|
|
32
|
+
// We accept saturation and value arguments from 0 to 100 because that's
|
|
33
|
+
// how Photoshop represents those values. Internally, however, the
|
|
34
|
+
// saturation and value are calculated from a range of 0 to 1. We make
|
|
35
|
+
// That conversion here.
|
|
36
|
+
s /= 100;
|
|
37
|
+
v /= 100;
|
|
38
|
+
|
|
39
|
+
if (s === 0) {
|
|
40
|
+
// Achromatic (grey)
|
|
41
|
+
r = g = b = v;
|
|
42
|
+
return [
|
|
43
|
+
Math.round(r * 255),
|
|
44
|
+
Math.round(g * 255),
|
|
45
|
+
Math.round(b * 255)
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
h /= 60; // sector 0 to 5
|
|
50
|
+
i = Math.floor(h);
|
|
51
|
+
f = h - i; // factorial part of h
|
|
52
|
+
p = v * (1 - s);
|
|
53
|
+
q = v * (1 - s * f);
|
|
54
|
+
t = v * (1 - s * (1 - f));
|
|
55
|
+
|
|
56
|
+
switch (i) {
|
|
57
|
+
case 0:
|
|
58
|
+
r = v;
|
|
59
|
+
g = t;
|
|
60
|
+
b = p;
|
|
61
|
+
break;
|
|
62
|
+
|
|
63
|
+
case 1:
|
|
64
|
+
r = q;
|
|
65
|
+
g = v;
|
|
66
|
+
b = p;
|
|
67
|
+
break;
|
|
68
|
+
|
|
69
|
+
case 2:
|
|
70
|
+
r = p;
|
|
71
|
+
g = v;
|
|
72
|
+
b = t;
|
|
73
|
+
break;
|
|
74
|
+
|
|
75
|
+
case 3:
|
|
76
|
+
r = p;
|
|
77
|
+
g = q;
|
|
78
|
+
b = v;
|
|
79
|
+
break;
|
|
80
|
+
|
|
81
|
+
case 4:
|
|
82
|
+
r = t;
|
|
83
|
+
g = p;
|
|
84
|
+
b = v;
|
|
85
|
+
break;
|
|
86
|
+
|
|
87
|
+
default: // case 5:
|
|
88
|
+
r = v;
|
|
89
|
+
g = p;
|
|
90
|
+
b = q;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return [
|
|
94
|
+
Math.round(r * 255),
|
|
95
|
+
Math.round(g * 255),
|
|
96
|
+
Math.round(b * 255)
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function componentToHex(c) {
|
|
101
|
+
const hex = c.toString(16);
|
|
102
|
+
return hex.length === 1 ? `0${hex}` : hex;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function toPaddedHexString(num, len) {
|
|
106
|
+
if (len === 2) {
|
|
107
|
+
if (num > 255) {
|
|
108
|
+
num = 255;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const str = num.toString(16);
|
|
112
|
+
if (str.length >= len) return str;
|
|
113
|
+
return '0'.repeat(len - str.length) + str;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function MQTTServer(adapter) {
|
|
117
|
+
if (!(this instanceof MQTTServer)) {
|
|
118
|
+
return new MQTTServer(adapter);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const NO_PREFIX = '';
|
|
122
|
+
|
|
123
|
+
let server = new net.Server();
|
|
124
|
+
const clients = {};
|
|
125
|
+
const tasks = [];
|
|
126
|
+
let taskCallbacks = [];
|
|
127
|
+
let messageId = 1;
|
|
128
|
+
let persistentSessions = {};
|
|
129
|
+
let resending = false;
|
|
130
|
+
let resendTimer = null;
|
|
131
|
+
|
|
132
|
+
const cacheAddedObjects = {};
|
|
133
|
+
const cachedModeExor = {};
|
|
134
|
+
const cachedReadColors = {};
|
|
135
|
+
|
|
136
|
+
this.destroy = cb => {
|
|
137
|
+
if (resendTimer) {
|
|
138
|
+
clearInterval(resendTimer);
|
|
139
|
+
resendTimer = null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (server) {
|
|
143
|
+
let cnt = 0;
|
|
144
|
+
Object.keys(clients).forEach(id => {
|
|
145
|
+
cnt++;
|
|
146
|
+
adapter.setForeignState(`${adapter.namespace}.${clients[id].iobId}.alive`, false, true, () => {
|
|
147
|
+
if (!--cnt) {
|
|
148
|
+
// to release all resources
|
|
149
|
+
server.close(() => cb && cb());
|
|
150
|
+
server = null;
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
if (!cnt) {
|
|
156
|
+
// to release all resources
|
|
157
|
+
server.close(() => cb && cb());
|
|
158
|
+
server = null;
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
cb && cb();
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/*
|
|
166
|
+
memwatch.on('leak', (info) => {
|
|
167
|
+
//console.error('Memory leak detected:\n', info);
|
|
168
|
+
adapter.log.info('Memory leak detected:\n', info);
|
|
169
|
+
var filename='/opt/iobroker/dumps/' + Date.now() + '.heapsnapshot';
|
|
170
|
+
heapdump.writeSnapshot((err,filename) => {
|
|
171
|
+
if (err) {
|
|
172
|
+
console.error(err);
|
|
173
|
+
}
|
|
174
|
+
else console.error('Wrote snapshot: ' + filename);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
*/
|
|
178
|
+
/*
|
|
179
|
+
memwatch.on('stats', function(stats) {
|
|
180
|
+
adapter.log.info("stats:",stats);
|
|
181
|
+
});
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
function setColor(channelId, val) {
|
|
185
|
+
//adapter.log.info('color write: '+ val);
|
|
186
|
+
const stateId = 'Color';
|
|
187
|
+
if (clients[channelId] && clients[channelId]._map && clients[channelId]._map[stateId]) {
|
|
188
|
+
setImmediate(sendState2Client, clients[channelId], clients[channelId]._map[stateId] || 'cmnd/sonoff/Color', val, adapter.config.defaultQoS);
|
|
189
|
+
} else if (clients[channelId] && clients[channelId]._fallBackName) {
|
|
190
|
+
setImmediate(sendState2Client, clients[channelId], `cmnd/${clients[channelId]._fallBackName}/${stateId}`, val, adapter.config.defaultQoS);
|
|
191
|
+
} else {
|
|
192
|
+
adapter.log.warn(`Unknown mapping for "${stateId}"`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function setPower(channelId, val) {
|
|
197
|
+
const stateId = 'POWER';
|
|
198
|
+
if (val === '' || val === null || val === undefined) {
|
|
199
|
+
return adapter.log.debug('Empty power was ignored');
|
|
200
|
+
}
|
|
201
|
+
if (clients[channelId]._map && clients[channelId]._map[stateId]) {
|
|
202
|
+
setImmediate(sendState2Client, clients[channelId], clients[channelId]._map[stateId] || 'cmnd/sonoff/POWER', val ? 'ON' : 'OFF', adapter.config.defaultQoS);
|
|
203
|
+
} else if (clients[channelId]._fallBackName) {
|
|
204
|
+
setImmediate(sendState2Client, clients[channelId], `cmnd/${clients[channelId]._fallBackName}/${stateId}`, val, adapter.config.defaultQoS);
|
|
205
|
+
} else {
|
|
206
|
+
adapter.log.warn(`Unknown mapping for "${stateId}"`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function setStateImmediate(channelId, stateId, val) {
|
|
211
|
+
if (clients[channelId] && clients[channelId]._map && clients[channelId]._map[stateId]) {
|
|
212
|
+
setImmediate(sendState2Client, clients[channelId], clients[channelId]._map[stateId] || (`cmnd/sonoff/${stateId}`), val, adapter.config.defaultQoS);
|
|
213
|
+
} else if (clients[channelId] && clients[channelId]._fallBackName) {
|
|
214
|
+
setImmediate(sendState2Client, clients[channelId], `cmnd/${clients[channelId]._fallBackName}/${stateId}`, val, adapter.config.defaultQoS);
|
|
215
|
+
} else {
|
|
216
|
+
adapter.log.warn(`Unknown mapping for "${stateId}"`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function _setState(id, val, callback) {
|
|
221
|
+
adapter.log.debug(`Set State after task: ${id}`);
|
|
222
|
+
adapter.setForeignState(id, val, true, callback);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function updateState(task, val, callback) {
|
|
226
|
+
if (typeof val === 'function') {
|
|
227
|
+
callback = val;
|
|
228
|
+
val = undefined;
|
|
229
|
+
}
|
|
230
|
+
if (val !== undefined) {
|
|
231
|
+
task.cb = _setState;
|
|
232
|
+
task.cbArg = val;
|
|
233
|
+
}
|
|
234
|
+
tasks.push(task);
|
|
235
|
+
|
|
236
|
+
if (callback) {
|
|
237
|
+
taskCallbacks.push(callback);
|
|
238
|
+
}
|
|
239
|
+
adapter.log.debug(`updateState ${task.id} - ${tasks.length}`);
|
|
240
|
+
if (tasks.length === 1) {
|
|
241
|
+
processTasks();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const specVars = ['Red', 'Green', 'Blue', 'WW', 'CW', 'Color', 'RGB_POWER', 'WW_POWER', 'CW_POWER', 'Hue', 'Saturation'];
|
|
246
|
+
function onStateChangedColors(id, state, channelId, stateId) {
|
|
247
|
+
if (!channelId) {
|
|
248
|
+
let parts = id.split('.');
|
|
249
|
+
stateId = parts.pop();
|
|
250
|
+
|
|
251
|
+
if (stateId === 'level' || stateId === 'state' || stateId === 'red' || stateId === 'blue' || stateId === 'green') {
|
|
252
|
+
stateId = `${parts.pop()}.${stateId}`;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
channelId = parts.splice(2, parts.length).join('.');
|
|
256
|
+
}
|
|
257
|
+
const ledModeIdExor = `${adapter.namespace}.${channelId}.modeLedExor`;
|
|
258
|
+
if (cachedModeExor[ledModeIdExor] === undefined) {
|
|
259
|
+
return adapter.getForeignState(ledModeIdExor, (err, _state) => {
|
|
260
|
+
cachedModeExor[ledModeIdExor] = _state ? _state.val || false : true;
|
|
261
|
+
setImmediate(() => onStateChangedColors(id, state, channelId, stateId));
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// ledstripe objects
|
|
266
|
+
const exorWhiteLeds = cachedModeExor[ledModeIdExor]; // exor for white leds and color leds => if white leds are switched on, color leds are switched off and vice versa (default on)
|
|
267
|
+
|
|
268
|
+
// now evaluate ledstripe vars
|
|
269
|
+
// adaptions for magichome tasmota
|
|
270
|
+
if (stateId.match(/Color\d?/)) {
|
|
271
|
+
//adapter.log.info('sending color');
|
|
272
|
+
// id = sonoff.0.DVES_96ABFA.Color
|
|
273
|
+
// statid=Color
|
|
274
|
+
// state = {"val":"#faadcf","ack":false,"ts":1520146102580,"q":0,"from":"system.adapter.web.0","lc":1520146102580}
|
|
275
|
+
|
|
276
|
+
// set white to rgb or rgbww
|
|
277
|
+
adapter.getObject(id, (err, obj) => {
|
|
278
|
+
if (!obj) {
|
|
279
|
+
adapter.log.warn(`invalid rgbww obj for ${id}`);
|
|
280
|
+
} else if (typeof state.val !== 'string') {
|
|
281
|
+
adapter.log.warn(`invalid rgbww state value for ${id} : ${JSON.stringify(state.val)} needs to be a string`);
|
|
282
|
+
} else {
|
|
283
|
+
const role = obj.common.role;
|
|
284
|
+
let color;
|
|
285
|
+
|
|
286
|
+
//adapter.log.info(state.val);
|
|
287
|
+
if (role === 'level.color.rgbww') {
|
|
288
|
+
// rgbww
|
|
289
|
+
if (state.val.toUpperCase() === '#FFFFFF') {
|
|
290
|
+
// transform white to WW
|
|
291
|
+
//color='000000FF';
|
|
292
|
+
color = `${state.val.substring(1)}00`;
|
|
293
|
+
} else {
|
|
294
|
+
// strip # char and add ww
|
|
295
|
+
color = `${state.val.substring(1)}00`;
|
|
296
|
+
}
|
|
297
|
+
} else if (role === 'level.color.rgbcwww') {
|
|
298
|
+
color = `${state.val.substring(1)}0000`;
|
|
299
|
+
} else {
|
|
300
|
+
// rgb, strip # char
|
|
301
|
+
color = state.val.substring(1);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
//adapter.log.info('color :' + color + ' : ' + role);
|
|
305
|
+
// strip # char
|
|
306
|
+
//color=state.val.substring(1);
|
|
307
|
+
|
|
308
|
+
setColor(channelId, color);
|
|
309
|
+
|
|
310
|
+
// set rgb too
|
|
311
|
+
const hidE = id.split('.');
|
|
312
|
+
const deviceDesc = `${hidE[0]}.${hidE[1]}.${hidE[2]}`;
|
|
313
|
+
let idAlive = `${deviceDesc}.Red`;
|
|
314
|
+
adapter.setState(idAlive, 100 * parseInt(color.substring(0, 2), 16) / 255, true);
|
|
315
|
+
|
|
316
|
+
idAlive = `${deviceDesc}.Green`;
|
|
317
|
+
adapter.setState(idAlive, 100 * parseInt(color.substring(2, 4), 16) / 255, true);
|
|
318
|
+
|
|
319
|
+
idAlive = `${deviceDesc}.Blue`;
|
|
320
|
+
adapter.setState(idAlive, 100 * parseInt(color.substring(4, 6), 16) / 255, true);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
} else {
|
|
324
|
+
const hidE = id.split('.');
|
|
325
|
+
const deviceDesc = `${hidE[0]}.${hidE[1]}.${hidE[2]}`;
|
|
326
|
+
if (stateId.match(/Red\d?/)) {
|
|
327
|
+
// set red component
|
|
328
|
+
if (state.val > 100) state.val = 100;
|
|
329
|
+
const red = toPaddedHexString(Math.floor(255 * state.val / 100), 2);
|
|
330
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
331
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
332
|
+
if (!state) {
|
|
333
|
+
adapter.setState(idAlive, '#000000', false);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const color = state.val.substring(1);
|
|
337
|
+
// replace red component
|
|
338
|
+
const out = red + color.substring(2, 10);
|
|
339
|
+
adapter.setState(idAlive, `#${out}`, false);
|
|
340
|
+
setColor(channelId, out);
|
|
341
|
+
});
|
|
342
|
+
} else
|
|
343
|
+
if (stateId.match(/Green\d?/)) {
|
|
344
|
+
// set green component
|
|
345
|
+
if (state.val > 100) state.val = 100;
|
|
346
|
+
const green = toPaddedHexString(Math.floor(255 * state.val / 100), 2);
|
|
347
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
348
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
349
|
+
if (!state) {
|
|
350
|
+
adapter.setState(idAlive, '#000000', false);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const color = state.val.substring(1);
|
|
354
|
+
// replace green component
|
|
355
|
+
const out = color.substring(0, 2) + green + color.substring(4, 10);
|
|
356
|
+
adapter.setState(idAlive, `#${out}`, false);
|
|
357
|
+
setColor(channelId, out);
|
|
358
|
+
});
|
|
359
|
+
} else
|
|
360
|
+
if (stateId.match(/Blue\d?/)) {
|
|
361
|
+
// set blue component
|
|
362
|
+
if (state.val > 100) state.val = 100;
|
|
363
|
+
const blue = toPaddedHexString(Math.floor(255 * state.val / 100), 2);
|
|
364
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
365
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
366
|
+
if (!state) {
|
|
367
|
+
adapter.setState(idAlive, '#000000', false);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const color = state.val.substring(1);
|
|
371
|
+
// replace blue component
|
|
372
|
+
const out = color.substring(0, 4) + blue + color.substring(6, 10);
|
|
373
|
+
adapter.setState(idAlive, `#${out}`, false);
|
|
374
|
+
setColor(channelId, out);
|
|
375
|
+
});
|
|
376
|
+
} else
|
|
377
|
+
if (stateId.match(/RGB_POWER\d?/)) {
|
|
378
|
+
// set ww component
|
|
379
|
+
const rgbpow = state.val === 'true' || state.val === true || state.val === 1 || state.val === '1';
|
|
380
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
381
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
382
|
+
if (!state || typeof state.val !== 'string') {
|
|
383
|
+
adapter.log.info(`invalid state Color for ${idAlive}, correcting value to #000000`);
|
|
384
|
+
adapter.setState(idAlive, '#000000', false);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const color = state.val.substring(1);
|
|
388
|
+
let rgb = '000000';
|
|
389
|
+
if (rgbpow === true) {
|
|
390
|
+
rgb = 'FFFFFF';
|
|
391
|
+
}
|
|
392
|
+
// replace rgb component
|
|
393
|
+
let out = rgb + color.substring(6, 10);
|
|
394
|
+
|
|
395
|
+
if (rgbpow && exorWhiteLeds) {
|
|
396
|
+
//adapter.log.info('reset white');
|
|
397
|
+
out = `${rgb}0000`;
|
|
398
|
+
let idAlive = `${deviceDesc}.WW_POWER`;
|
|
399
|
+
adapter.setState(idAlive, false, false);
|
|
400
|
+
idAlive = `${deviceDesc}.WW`;
|
|
401
|
+
adapter.setState(idAlive, 0, false);
|
|
402
|
+
idAlive = `${deviceDesc}.CW_POWER`;
|
|
403
|
+
adapter.setState(idAlive, false, false);
|
|
404
|
+
idAlive = `${deviceDesc}.CW`;
|
|
405
|
+
adapter.setState(idAlive, 0, false);
|
|
406
|
+
}
|
|
407
|
+
setColor(channelId, out);
|
|
408
|
+
adapter.setState(idAlive, `#${out}`, false);
|
|
409
|
+
if (rgbpow) {
|
|
410
|
+
setPower(channelId, true)
|
|
411
|
+
}
|
|
412
|
+
// if led_mode&1, exor white leds
|
|
413
|
+
|
|
414
|
+
});
|
|
415
|
+
} else
|
|
416
|
+
// calc hue + saturation params to rgb
|
|
417
|
+
if (hueCalc && stateId.match(/Hue\d?/)) {
|
|
418
|
+
let hue = state.val;
|
|
419
|
+
if (hue > 359) hue = 359;
|
|
420
|
+
// recalc color by hue
|
|
421
|
+
const idAlive = `${deviceDesc}.Dimmer`;
|
|
422
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
423
|
+
if (!state) {
|
|
424
|
+
const dim = 100;
|
|
425
|
+
adapter.setState(idAlive, dim, true);
|
|
426
|
+
//adapter.log.warn('ill state Dimmer');
|
|
427
|
+
} else {
|
|
428
|
+
const dim = state.val;
|
|
429
|
+
let idAlive = `${deviceDesc}.Saturation`;
|
|
430
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
431
|
+
if (!state) {
|
|
432
|
+
const sat = 100;
|
|
433
|
+
adapter.setState(idAlive, sat, true);
|
|
434
|
+
} else {
|
|
435
|
+
const sat = state.val;
|
|
436
|
+
const rgb = hsvToRgb(hue, sat, dim);
|
|
437
|
+
const hexVal = componentToHex(rgb[0]) + componentToHex(rgb[1]) + componentToHex(rgb[2]);
|
|
438
|
+
let idAlive = `${deviceDesc}.Color`;
|
|
439
|
+
adapter.setState(idAlive, `#${hexVal}`, false);
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
} else
|
|
445
|
+
if (hueCalc && stateId.match(/Saturation\d?/)) {
|
|
446
|
+
let sat = state.val;
|
|
447
|
+
if (sat > 100) sat = 100;
|
|
448
|
+
// recalc color by saturation
|
|
449
|
+
const idAlive = `${deviceDesc}.Dimmer`;
|
|
450
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
451
|
+
if (!state) {
|
|
452
|
+
const dim = 100;
|
|
453
|
+
adapter.setState(idAlive, dim, true);
|
|
454
|
+
//adapter.log.warn('ill state Dimmer');
|
|
455
|
+
} else {
|
|
456
|
+
const dim = state.val;
|
|
457
|
+
const idAlive = `${deviceDesc}.Hue`;
|
|
458
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
459
|
+
if (!state) {
|
|
460
|
+
const hue = 100;
|
|
461
|
+
adapter.setState(idAlive, hue, true);
|
|
462
|
+
} else {
|
|
463
|
+
const hue = state.val;
|
|
464
|
+
const rgb = hsvToRgb(hue, sat, dim);
|
|
465
|
+
const hexVal = componentToHex(rgb[0]) + componentToHex(rgb[1]) + componentToHex(rgb[2]);
|
|
466
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
467
|
+
adapter.setState(idAlive, `#${hexVal}`, false);
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
} else {
|
|
473
|
+
// get color attributes to check other ledstripe vars
|
|
474
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
475
|
+
adapter.getForeignObject(idAlive, (err, obj) => {
|
|
476
|
+
if (!obj) {
|
|
477
|
+
// no color object
|
|
478
|
+
adapter.log.warn(`unknown object: ${id}: ${state}`);
|
|
479
|
+
} else {
|
|
480
|
+
const role = obj.common.role;
|
|
481
|
+
//if (role='level.color.rgb') return;
|
|
482
|
+
let wwindex;
|
|
483
|
+
if (role === 'level.color.rgbww') {
|
|
484
|
+
wwindex = 6;
|
|
485
|
+
} else {
|
|
486
|
+
wwindex = 8;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
if (stateId.match(/WW_POWER\d?/)) {
|
|
490
|
+
// set ww component
|
|
491
|
+
const wwpow = state.val === 'true' || state.val === true || state.val === 1 || state.val === '1';
|
|
492
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
493
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
494
|
+
if (!state) {
|
|
495
|
+
adapter.log.warn('ill state Color');
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const color = state.val.substring(1);
|
|
499
|
+
let ww = '00';
|
|
500
|
+
if (wwpow) {
|
|
501
|
+
ww = 'FF';
|
|
502
|
+
}
|
|
503
|
+
// replace ww component
|
|
504
|
+
let out = color.substring(0, wwindex) + ww;
|
|
505
|
+
if (wwpow && exorWhiteLeds) {
|
|
506
|
+
//adapter.log.info('reset white');
|
|
507
|
+
out = `000000${ww}`;
|
|
508
|
+
let idAlive = `${deviceDesc}.RGB_POWER`;
|
|
509
|
+
adapter.setState(idAlive,false, false);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
let idAlive = `${deviceDesc}.Color`;
|
|
513
|
+
adapter.setState(idAlive,`#${out}`, false);
|
|
514
|
+
setColor(channelId, out);
|
|
515
|
+
|
|
516
|
+
// set ww channel
|
|
517
|
+
idAlive = `${deviceDesc}.WW`;
|
|
518
|
+
adapter.setState(idAlive, 100 * parseInt(out.substring(6, 8), 16) / 255, true);
|
|
519
|
+
|
|
520
|
+
// in case POWER is off, switch it on
|
|
521
|
+
wwpow && setPower(channelId, true)
|
|
522
|
+
});
|
|
523
|
+
} else
|
|
524
|
+
if (stateId.match(/CW_POWER\d?/)) {
|
|
525
|
+
// set ww component
|
|
526
|
+
const cwpow = state.val === 'true' || state.val === true || state.val === 1 || state.val === '1';
|
|
527
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
528
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
529
|
+
if (!state) {
|
|
530
|
+
adapter.log.warn('ill state Color');
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
const color = state.val.substring(1);
|
|
534
|
+
let cw = '00';
|
|
535
|
+
if (cwpow) {
|
|
536
|
+
cw = 'FF';
|
|
537
|
+
}
|
|
538
|
+
// replace cw component
|
|
539
|
+
let out = color.substring(0, 6) + cw + color.substring(8, 10);
|
|
540
|
+
if (cwpow && exorWhiteLeds) {
|
|
541
|
+
//adapter.log.info('reset white');
|
|
542
|
+
out = `000000${cw}${color.substring(8, 10)}`;
|
|
543
|
+
let idAlive = `${deviceDesc}.RGB_POWER`;
|
|
544
|
+
adapter.setState(idAlive, false, false);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
let idAlive = `${deviceDesc}.Color`;
|
|
548
|
+
adapter.setState(idAlive, `#${out}`, false);
|
|
549
|
+
setColor(channelId, out);
|
|
550
|
+
|
|
551
|
+
// set cw channel
|
|
552
|
+
idAlive = `${deviceDesc}.CW`;
|
|
553
|
+
adapter.setState(idAlive, 100 * parseInt(out.substring(6, 8), 16) / 255, true);
|
|
554
|
+
|
|
555
|
+
// in case POWER is off, switch it on
|
|
556
|
+
if (cwpow) {
|
|
557
|
+
let idAlive = `${deviceDesc}.POWER`;
|
|
558
|
+
adapter.setState(idAlive, true, false);
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
} else
|
|
562
|
+
if (stateId.match(/WW\d?/)) {
|
|
563
|
+
// set ww component
|
|
564
|
+
const ww = toPaddedHexString(Math.floor(255 * state.val / 100), 2);
|
|
565
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
566
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
567
|
+
if (!state) {
|
|
568
|
+
adapter.setState(idAlive, '#000000', false);
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const color = state.val.substring(1);
|
|
572
|
+
// replace ww component
|
|
573
|
+
const out = color.substring(0, wwindex) + ww;
|
|
574
|
+
setColor(channelId, out);
|
|
575
|
+
});
|
|
576
|
+
} else
|
|
577
|
+
if (stateId.match(/CW\d?/)) {
|
|
578
|
+
// set ww component
|
|
579
|
+
const cw = toPaddedHexString(Math.floor(255 * state.val / 100), 2);
|
|
580
|
+
const idAlive = `${deviceDesc}.Color`;
|
|
581
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
582
|
+
if (!state) {
|
|
583
|
+
adapter.setState(idAlive, '#000000', false);
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const color = state.val.substring(1);
|
|
587
|
+
// replace cw component
|
|
588
|
+
const out = color.substring(0, 6) + cw + color.substring(8, 10);
|
|
589
|
+
setColor(channelId, out);
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
this.onStateChange = (id, state) => {
|
|
599
|
+
adapter.log.debug(`onStateChange ${id}: ${JSON.stringify(state)}`);
|
|
600
|
+
if (server && state && !state.ack) {
|
|
601
|
+
// find client.id
|
|
602
|
+
let parts = id.split('.');
|
|
603
|
+
const stateId = parts.pop();
|
|
604
|
+
const channelId = parts.splice(2, parts.length).join('.');
|
|
605
|
+
if (clients[mappingClients[channelId]]) {
|
|
606
|
+
// check for special led-stripe vars
|
|
607
|
+
if (!specVars.includes(stateId)) {
|
|
608
|
+
// other objects
|
|
609
|
+
adapter.getObject(id, (err, obj) => {
|
|
610
|
+
if (!obj) {
|
|
611
|
+
adapter.log.warn(`invalid obj ${id}`);
|
|
612
|
+
} else {
|
|
613
|
+
const type = obj.common.type;
|
|
614
|
+
switch (type) {
|
|
615
|
+
case 'boolean':
|
|
616
|
+
setStateImmediate(mappingClients[channelId], stateId, state.val ? 'ON' : 'OFF');
|
|
617
|
+
break;
|
|
618
|
+
case 'number':
|
|
619
|
+
setStateImmediate(mappingClients[channelId], stateId, (state.val === null) ? '' : state.val.toString());
|
|
620
|
+
break;
|
|
621
|
+
case 'string':
|
|
622
|
+
setStateImmediate(mappingClients[channelId], stateId, (state.val === null) ? '' : state.val.toString());
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
} else if (state.val !== null) {
|
|
628
|
+
onStateChangedColors(id, state, channelId, stateId);
|
|
629
|
+
}
|
|
630
|
+
} else {
|
|
631
|
+
//Client:"DVES_96ABFA : MagicHome" not connected => State: sonoff.0.myState - Value: 0, ack: false, time stamp: 1520369614189, last changed: 1520369614189
|
|
632
|
+
// if (server && state && !state.ack) {
|
|
633
|
+
// server = false
|
|
634
|
+
// or state = false
|
|
635
|
+
// or state.ack = true
|
|
636
|
+
// or clients[channelId] = false
|
|
637
|
+
adapter.log.warn(`Client "${channelId}" not connected`);
|
|
638
|
+
|
|
639
|
+
/*
|
|
640
|
+
if (!clients[channelId]) {
|
|
641
|
+
var idAlive='sonoff.0.'+channelId+'.INFO.IPAddress';
|
|
642
|
+
adapter.getForeignState(idAlive, function (err, state) {
|
|
643
|
+
if (!state) {
|
|
644
|
+
adapter.log.warn('Client "' + channelId + '" could not get ip adress');
|
|
645
|
+
} else {
|
|
646
|
+
var ip=state.val;
|
|
647
|
+
adapter.log.warn('Clients ip "' + ip);
|
|
648
|
+
|
|
649
|
+
request('http://'+ip+'/cm?cmnd=Restart 1', function(error, response, body) {
|
|
650
|
+
if (error || response.statusCode !== 200) {
|
|
651
|
+
log('Fehler beim Neustart von Sonoff: ' + channelId + ' (StatusCode = ' + response.statusCode + ')');
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
}*/
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
function processTasks(callback) {
|
|
662
|
+
if (callback) {
|
|
663
|
+
taskCallbacks.push(callback);
|
|
664
|
+
}
|
|
665
|
+
if (!tasks || !tasks.length) {
|
|
666
|
+
const doCallbacks = taskCallbacks;
|
|
667
|
+
taskCallbacks = [];
|
|
668
|
+
doCallbacks.forEach(cb => typeof cb === 'function' && cb());
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
let task = tasks[0];
|
|
672
|
+
adapter.log.debug(`process task: ${JSON.stringify(task)}`);
|
|
673
|
+
if (task.type === 'addObject') {
|
|
674
|
+
if (!cacheAddedObjects[task.id]) {
|
|
675
|
+
cacheAddedObjects[task.id] = true;
|
|
676
|
+
adapter.getForeignObject(task.id, (err, obj) => {
|
|
677
|
+
if (!obj || !obj.common) {
|
|
678
|
+
adapter.setForeignObject(task.id, task.data, (err) => {
|
|
679
|
+
if (err) {
|
|
680
|
+
adapter.log.warn(`New Object creation error: ${err.message}`)
|
|
681
|
+
} else {
|
|
682
|
+
adapter.log.info(`new object created: ${task.id}`);
|
|
683
|
+
}
|
|
684
|
+
if (task.cb) {
|
|
685
|
+
task.cb(task.id, task.cbArg, () => {
|
|
686
|
+
tasks.shift();
|
|
687
|
+
setImmediate(processTasks);
|
|
688
|
+
});
|
|
689
|
+
} else {
|
|
690
|
+
tasks.shift();
|
|
691
|
+
setImmediate(processTasks);
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
} else if (obj.common.type !== task.data.common.type) {
|
|
695
|
+
adapter.extendObject(task.id, {common: {type: task.data.common.type}}, err => {
|
|
696
|
+
if (err) {
|
|
697
|
+
adapter.log.warn(`Object update error: ${err.message}`)
|
|
698
|
+
} else {
|
|
699
|
+
adapter.log.info(`Object updated: ${task.id}`);
|
|
700
|
+
}
|
|
701
|
+
if (task.cb) {
|
|
702
|
+
task.cb(task.id, task.cbArg, () => {
|
|
703
|
+
tasks.shift();
|
|
704
|
+
setImmediate(processTasks);
|
|
705
|
+
});
|
|
706
|
+
} else {
|
|
707
|
+
tasks.shift();
|
|
708
|
+
setImmediate(processTasks);
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
} else {
|
|
712
|
+
if (task.cb) {
|
|
713
|
+
task.cb(task.id, task.cbArg, () => {
|
|
714
|
+
tasks.shift();
|
|
715
|
+
setImmediate(processTasks);
|
|
716
|
+
});
|
|
717
|
+
} else {
|
|
718
|
+
tasks.shift();
|
|
719
|
+
setImmediate(processTasks);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
} else {
|
|
724
|
+
if (task.cb) {
|
|
725
|
+
task.cb(task.id, task.cbArg, () => {
|
|
726
|
+
tasks.shift();
|
|
727
|
+
setImmediate(processTasks);
|
|
728
|
+
});
|
|
729
|
+
} else {
|
|
730
|
+
tasks.shift();
|
|
731
|
+
setImmediate(processTasks);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
} else if (task.type === 'extendObject') {
|
|
735
|
+
adapter.extendObject(task.id, task.data, (/* err */) => {
|
|
736
|
+
if (task.cb) {
|
|
737
|
+
task.cb(task.id, task.cbArg, () => {
|
|
738
|
+
tasks.shift();
|
|
739
|
+
setImmediate(processTasks);
|
|
740
|
+
});
|
|
741
|
+
} else {
|
|
742
|
+
tasks.shift();
|
|
743
|
+
setImmediate(processTasks);
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
} else if (task.type === 'deleteState') {
|
|
747
|
+
adapter.deleteState('', '', task.id, (/* err */) => {
|
|
748
|
+
if (task.cb) {
|
|
749
|
+
task.cb(task.id, task.cbArg, () => {
|
|
750
|
+
tasks.shift();
|
|
751
|
+
setImmediate(processTasks);
|
|
752
|
+
});
|
|
753
|
+
} else {
|
|
754
|
+
tasks.shift();
|
|
755
|
+
setImmediate(processTasks);
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
} else {
|
|
759
|
+
adapter.log.error(`Unknown task name: ${JSON.stringify(task)}`);
|
|
760
|
+
if (task.cb) {
|
|
761
|
+
task.cb(task.id, task.cbArg, () => {
|
|
762
|
+
tasks.shift();
|
|
763
|
+
setImmediate(processTasks);
|
|
764
|
+
});
|
|
765
|
+
} else {
|
|
766
|
+
tasks.shift();
|
|
767
|
+
setImmediate(processTasks);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
function createClient(client, callback) {
|
|
773
|
+
// mqtt.0.cmnd.sonoff.POWER
|
|
774
|
+
// mqtt.0.stat.sonoff.POWER
|
|
775
|
+
let isStart = !tasks.length;
|
|
776
|
+
|
|
777
|
+
let id = `${adapter.namespace}.${client.iobId}`;
|
|
778
|
+
let obj = {
|
|
779
|
+
_id: id,
|
|
780
|
+
common: {
|
|
781
|
+
name: client.id,
|
|
782
|
+
desc: ''
|
|
783
|
+
},
|
|
784
|
+
native: {
|
|
785
|
+
clientId: client.id
|
|
786
|
+
},
|
|
787
|
+
type: 'channel'
|
|
788
|
+
};
|
|
789
|
+
tasks.push({type: 'addObject', id: obj._id, data: obj});
|
|
790
|
+
|
|
791
|
+
obj = {
|
|
792
|
+
_id: `${id}.alive`,
|
|
793
|
+
common: {
|
|
794
|
+
type: 'boolean',
|
|
795
|
+
role: 'indicator.reachable',
|
|
796
|
+
read: true,
|
|
797
|
+
write: false,
|
|
798
|
+
name: `${client.id} alive`
|
|
799
|
+
},
|
|
800
|
+
type: 'state'
|
|
801
|
+
};
|
|
802
|
+
tasks.push({type: 'addObject', id: obj._id, data: obj});
|
|
803
|
+
if (isStart) {
|
|
804
|
+
processTasks(callback);
|
|
805
|
+
} else {
|
|
806
|
+
typeof callback === 'function' && callback();
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
function updateClients() {
|
|
811
|
+
let text = '';
|
|
812
|
+
if (clients) {
|
|
813
|
+
for (let id in clients) {
|
|
814
|
+
text += `${text ? ',' : ''}${id}`;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
adapter.setState('info.connection', text, true);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
function updateAlive(client, alive) {
|
|
822
|
+
let idAlive = `${adapter.namespace}.${client.iobId}.alive`;
|
|
823
|
+
|
|
824
|
+
adapter.getForeignState(idAlive, (err, state) => {
|
|
825
|
+
if (!state || state.val !== alive) {
|
|
826
|
+
adapter.setForeignState(idAlive, alive, true);
|
|
827
|
+
}
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
function sendState2Client(client, topic, state, qos, retain, cb) {
|
|
832
|
+
if (typeof qos === 'function') {
|
|
833
|
+
cb = qos;
|
|
834
|
+
qos = undefined;
|
|
835
|
+
}
|
|
836
|
+
if (typeof retain === 'function') {
|
|
837
|
+
cb = retain;
|
|
838
|
+
retain = undefined;
|
|
839
|
+
}
|
|
840
|
+
adapter.log.debug(`Send to "${client.id}": ${topic} = ${state}`);
|
|
841
|
+
client.publish({topic: topic, payload: state, qos: qos, retain: retain, messageId: messageId++}, cb);
|
|
842
|
+
messageId &= 0xFFFFFFFF;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
function resendMessages2Client(client, messages, i) {
|
|
846
|
+
i = i || 0;
|
|
847
|
+
if (messages && i < messages.length) {
|
|
848
|
+
try {
|
|
849
|
+
messages[i].ts = Date.now();
|
|
850
|
+
messages[i].count++;
|
|
851
|
+
adapter.log.debug(`Client [${client.id}] Resend messages on connect: ${messages[i].topic} = ${messages[i].payload}`);
|
|
852
|
+
if (messages[i].cmd === 'publish') {
|
|
853
|
+
client.publish(messages[i]);
|
|
854
|
+
}
|
|
855
|
+
} catch (e) {
|
|
856
|
+
adapter.log.warn(`Client [${client.id}] Cannot resend message: ${e}`);
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
if (adapter.config.sendInterval) {
|
|
860
|
+
setTimeout(() => resendMessages2Client(client, messages, i + 1), adapter.config.sendInterval);
|
|
861
|
+
} else {
|
|
862
|
+
setImmediate(() => resendMessages2Client(client, messages, i + 1));
|
|
863
|
+
}
|
|
864
|
+
} else {
|
|
865
|
+
//return;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
function addObject(attr, client, prefix, path) {
|
|
870
|
+
let replaceAttr = types[attr].replace || attr;
|
|
871
|
+
let id = `${adapter.namespace}.${client.iobId}.${prefix ? `${prefix}.` : ''}${path.length ? `${path.join('_')}_` : ''}${replaceAttr.replace(FORBIDDEN_CHARS, '_')}`;
|
|
872
|
+
let obj = {
|
|
873
|
+
type: 'addObject',
|
|
874
|
+
id: id,
|
|
875
|
+
data: {
|
|
876
|
+
_id: id,
|
|
877
|
+
common: Object.assign({}, types[attr]),
|
|
878
|
+
native: {},
|
|
879
|
+
type: 'state'
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
obj.data.common.name = `${client.id} ${prefix ? `${prefix} ` : ''}${path.length ? `${path.join(' ')} ` : ''} ${replaceAttr}`;
|
|
883
|
+
return obj;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
function checkData(client, topic, prefix, data, unit, path) {
|
|
887
|
+
if (!data || typeof data !== 'object') {
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
const ledModeReadColorsID = `${adapter.namespace}.${client.iobId}.modeReadColors`;
|
|
892
|
+
|
|
893
|
+
if (cachedReadColors[ledModeReadColorsID] === undefined) {
|
|
894
|
+
return adapter.getForeignState(ledModeReadColorsID, (err, state) => {
|
|
895
|
+
cachedReadColors[ledModeReadColorsID] = (state && state.val) || false;
|
|
896
|
+
setImmediate(() => checkData(client, topic, prefix, data, unit, path));
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
path = path || [];
|
|
901
|
+
prefix = prefix || '';
|
|
902
|
+
|
|
903
|
+
for (let attr in data) {
|
|
904
|
+
if (!data.hasOwnProperty(attr)) {
|
|
905
|
+
adapter.log.warn(`[${client.id}] attr error: ${attr}${data[attr]}`);
|
|
906
|
+
continue;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
if (typeof data[attr] === 'object') {
|
|
910
|
+
// check for arrays
|
|
911
|
+
if (types[attr]) {
|
|
912
|
+
if (types[attr].type === 'array') {
|
|
913
|
+
// transform to array of attributes
|
|
914
|
+
for (let i = 1; i <= 10; i++) {
|
|
915
|
+
let val = data[attr][i - 1];
|
|
916
|
+
if (typeof val === 'undefined') {
|
|
917
|
+
break;
|
|
918
|
+
}
|
|
919
|
+
// define new object
|
|
920
|
+
let replaceAttr = attr.replace(FORBIDDEN_CHARS, '_') + i.toString();
|
|
921
|
+
let id = `${adapter.namespace}.${client.iobId}.${prefix ? `${prefix}.` : ''}${path.length ? `${path.join('_')}_` : ''}${replaceAttr}`;
|
|
922
|
+
let obj = {
|
|
923
|
+
type: 'addObject',
|
|
924
|
+
id: id,
|
|
925
|
+
data: {
|
|
926
|
+
_id: id,
|
|
927
|
+
common: Object.assign({}, types[attr]),
|
|
928
|
+
native: {},
|
|
929
|
+
type: 'state'
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
obj.data.common.name = `${client.id} ${prefix ? `${prefix} ` : ''}${path.length ? `${path.join(' ')} ` : ''} ${replaceAttr}`;
|
|
933
|
+
obj.data.common.type = typeof val;
|
|
934
|
+
if (!['number', 'string', 'boolean'].includes(obj.data.common.type)) {
|
|
935
|
+
obj.data.common.type = 'string';
|
|
936
|
+
}
|
|
937
|
+
updateState(obj, val);
|
|
938
|
+
}
|
|
939
|
+
} else {
|
|
940
|
+
let nPath = Object.assign([], path);
|
|
941
|
+
nPath.push(attr.replace(FORBIDDEN_CHARS, '_'));
|
|
942
|
+
checkData(client, topic, prefix, data[attr], unit, nPath);
|
|
943
|
+
nPath = undefined;
|
|
944
|
+
}
|
|
945
|
+
} else {
|
|
946
|
+
let nPath = Object.assign([], path);
|
|
947
|
+
nPath.push(attr.replace(FORBIDDEN_CHARS, '_'));
|
|
948
|
+
if (adapter.config.OBJ_TREE) {
|
|
949
|
+
checkData(client, topic, `${prefix}.${nPath}`, data[attr], unit);
|
|
950
|
+
} else {
|
|
951
|
+
checkData(client, topic, prefix, data[attr], unit, nPath);
|
|
952
|
+
}
|
|
953
|
+
nPath = undefined;
|
|
954
|
+
}
|
|
955
|
+
} else if (types[attr]) {
|
|
956
|
+
let allowReadColors;
|
|
957
|
+
|
|
958
|
+
// create object
|
|
959
|
+
const obj = addObject(attr, client, prefix, path);
|
|
960
|
+
let replaceAttr = types[attr].replace || attr;
|
|
961
|
+
|
|
962
|
+
if (obj.data.common.storeMap) {
|
|
963
|
+
delete obj.data.common.storeMap;
|
|
964
|
+
client._map[replaceAttr] = topic.replace(/$\w+\//, 'cmnd/').replace(/\/\w+$/, `/${replaceAttr}`);
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// adaptions for magichome tasmota
|
|
968
|
+
if (attr === 'Color') {
|
|
969
|
+
// read vars
|
|
970
|
+
allowReadColors = cachedReadColors[ledModeReadColorsID]; // allow for color read from MQTT (default off)
|
|
971
|
+
|
|
972
|
+
// if ledFlags bit 2, read color from tasmota, else ignore
|
|
973
|
+
if (data[attr].length === 10) {
|
|
974
|
+
obj.data.common.role = 'level.color.rgbcwww'; // RGB + cold white + white???
|
|
975
|
+
} else if (data[attr].length === 8) {
|
|
976
|
+
obj.data.common.role = 'level.color.rgbww'; // RGB + White
|
|
977
|
+
} else {
|
|
978
|
+
obj.data.common.role = 'level.color.rgb';
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
if (hueCalc) {
|
|
982
|
+
// Create LEDs modes if required
|
|
983
|
+
let xObj = addObject('modeReadColors', client, prefix, path);
|
|
984
|
+
updateState(xObj);
|
|
985
|
+
|
|
986
|
+
xObj = addObject('modeLedExor', client, prefix, path);
|
|
987
|
+
updateState(xObj);
|
|
988
|
+
|
|
989
|
+
xObj = addObject('Hue', client, prefix, path);
|
|
990
|
+
updateState(xObj);
|
|
991
|
+
|
|
992
|
+
xObj = addObject('Saturation', client, prefix, path);
|
|
993
|
+
updateState(xObj);
|
|
994
|
+
|
|
995
|
+
xObj = addObject('Red', client, prefix, path);
|
|
996
|
+
xObj.data.common.read = allowReadColors;
|
|
997
|
+
updateState(xObj, allowReadColors ? 100 * parseInt(data[attr].substring(0, 2), 16) / 255 : undefined);
|
|
998
|
+
|
|
999
|
+
xObj = addObject('Green', client, prefix, path);
|
|
1000
|
+
xObj.data.common.read = allowReadColors;
|
|
1001
|
+
updateState(xObj, allowReadColors ? 100 * parseInt(data[attr].substring(2, 4), 16) / 255 : undefined);
|
|
1002
|
+
|
|
1003
|
+
xObj = addObject('Blue', client, prefix, path);
|
|
1004
|
+
xObj.data.common.read = allowReadColors;
|
|
1005
|
+
updateState(xObj, allowReadColors ? 100 * parseInt(data[attr].substring(4, 6), 16) / 255 : undefined);
|
|
1006
|
+
|
|
1007
|
+
xObj = addObject('RGB_POWER', client, prefix, path);
|
|
1008
|
+
xObj.data.common.read = allowReadColors;
|
|
1009
|
+
let val = parseInt(data[attr].substring(0, 6), 16);
|
|
1010
|
+
updateState(xObj, allowReadColors ? val > 0 : undefined);
|
|
1011
|
+
|
|
1012
|
+
if (obj.data.common.role === 'level.color.rgbww') {
|
|
1013
|
+
// rgbww
|
|
1014
|
+
xObj = addObject('WW', client, prefix, path);
|
|
1015
|
+
xObj.data.common.read = allowReadColors;
|
|
1016
|
+
updateState(xObj, allowReadColors ? 100 * parseInt(data[attr].substring(6, 8), 16) / 255 : undefined);
|
|
1017
|
+
|
|
1018
|
+
xObj = addObject('WW_POWER', client, prefix, path);
|
|
1019
|
+
xObj.data.common.read = allowReadColors;
|
|
1020
|
+
updateState(xObj, allowReadColors ? (val > 0) : undefined);
|
|
1021
|
+
} else
|
|
1022
|
+
if (obj.data.common.role === 'level.color.rgbcwww') {
|
|
1023
|
+
// rgbcwww
|
|
1024
|
+
xObj = addObject('CW', client, prefix, path);
|
|
1025
|
+
xObj.data.common.read = allowReadColors;
|
|
1026
|
+
updateState(xObj, allowReadColors ? 100 * parseInt(data[attr].substring(6, 8), 16) / 255 : undefined);
|
|
1027
|
+
|
|
1028
|
+
xObj = addObject('CW_POWER', client, prefix, path);
|
|
1029
|
+
xObj.data.common.read = allowReadColors;
|
|
1030
|
+
updateState(xObj, allowReadColors ? (val > 0) : undefined);
|
|
1031
|
+
|
|
1032
|
+
xObj = addObject('WW', client, prefix, path);
|
|
1033
|
+
xObj.data.common.read = allowReadColors;
|
|
1034
|
+
updateState(xObj, allowReadColors ? 100 * parseInt(data[attr].substring(8, 10), 16) / 255 : undefined);
|
|
1035
|
+
|
|
1036
|
+
xObj = addObject('WW_POWER', client, prefix, path);
|
|
1037
|
+
xObj.data.common.read = allowReadColors;
|
|
1038
|
+
val = parseInt(data[attr].substring(8, 10), 16);
|
|
1039
|
+
updateState(xObj, allowReadColors ? (val > 0) : undefined);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
let val;
|
|
1045
|
+
if (obj.data.common.type === 'number') {
|
|
1046
|
+
val = parseFloat(data[attr]);
|
|
1047
|
+
} else if (obj.data.common.type === 'boolean') {
|
|
1048
|
+
const _value = (data[attr] || '').toUpperCase();
|
|
1049
|
+
val = _value === 'ON' || _value === 'TRUE' || _value === '1';
|
|
1050
|
+
} else {
|
|
1051
|
+
if (attr === 'Color') {
|
|
1052
|
+
// add # char
|
|
1053
|
+
if (allowReadColors) {
|
|
1054
|
+
val = `#${data[attr]}`;
|
|
1055
|
+
}
|
|
1056
|
+
} else {
|
|
1057
|
+
val = data[attr];
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
let powerStateFound = false;
|
|
1061
|
+
// due to some (for ex. NSPanel) versions do not automatically update .POWER from .STATE.POWER
|
|
1062
|
+
// but this is generally a default for all devices (state cannot differ from switch) we now keep
|
|
1063
|
+
// STATE changes to POWER in sync with POWER switches
|
|
1064
|
+
if (obj.id.includes('.STATE.POWER')) {
|
|
1065
|
+
powerStateFound = true;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
updateState(obj, val);
|
|
1069
|
+
|
|
1070
|
+
if (powerStateFound) {
|
|
1071
|
+
adapter.log.debug(`Got .STATE.POWER update, auto syncing .POWER (src: ${obj.id})`);
|
|
1072
|
+
let powerObj = Object.assign({}, obj);
|
|
1073
|
+
powerObj.id = powerObj.data._id = powerObj.id.replace('.STATE.POWER', '.POWER');
|
|
1074
|
+
updateState(powerObj, val);
|
|
1075
|
+
}
|
|
1076
|
+
} else {
|
|
1077
|
+
// not in list, auto insert
|
|
1078
|
+
//if (client.id=='DVES_008ADB') {
|
|
1079
|
+
// adapter.log.warn('[' + client.id + '] Received attr not in list: ' + attr + '' + data[attr]);
|
|
1080
|
+
//}
|
|
1081
|
+
// tele/sonoff/SENSOR tele/sonoff/STATE => read only
|
|
1082
|
+
// stat/sonoff/RESULT => read,write
|
|
1083
|
+
|
|
1084
|
+
let parts = topic.split('/');
|
|
1085
|
+
// auto generate objects
|
|
1086
|
+
if ((parts[0] === 'tele' && (
|
|
1087
|
+
(adapter.config.TELE_SENSOR && parts[2] === 'SENSOR') ||
|
|
1088
|
+
(adapter.config.STAT_RESULT && parts[2] === 'RESULT') ||
|
|
1089
|
+
(adapter.config.TELE_STATE && parts[2] === 'STATE'))) ||
|
|
1090
|
+
(parts[0] === 'stat' &&
|
|
1091
|
+
(adapter.config.STAT_RESULT && parts[2] === 'RESULT'))
|
|
1092
|
+
) {
|
|
1093
|
+
let val = data[attr];
|
|
1094
|
+
let attributes = {role: 'value', read: true, write: false};
|
|
1095
|
+
if (parts[2] === 'RESULT') {
|
|
1096
|
+
// control
|
|
1097
|
+
attributes.write = true;
|
|
1098
|
+
attributes.role = 'level';
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
attributes.type = typeof val;
|
|
1102
|
+
if (!['number', 'string', 'boolean'].includes(attributes.type)) {
|
|
1103
|
+
attributes.type = 'string';
|
|
1104
|
+
val = (val || '').toString();
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
let replaceAttr = attr ;
|
|
1108
|
+
let id = `${adapter.namespace}.${client.iobId}.${prefix ? `${prefix}.` : ''}${path.length ? `${path.join('_')}_` : ''}${replaceAttr}`;
|
|
1109
|
+
let obj = {
|
|
1110
|
+
type: 'addObject',
|
|
1111
|
+
id: id,
|
|
1112
|
+
data: {
|
|
1113
|
+
_id: id,
|
|
1114
|
+
common: attributes,
|
|
1115
|
+
native: {},
|
|
1116
|
+
type: 'state'
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
obj.data.common.name = `${client.id} ${prefix ? `${prefix} ` : ''}${path.length ? `${path.join(' ')} ` : ''} ${replaceAttr}`;
|
|
1120
|
+
// due to some (for ex. NSPanel) versions do not automatically send .POWER switch data
|
|
1121
|
+
// but this is generally a default for all devices (state cannot exist without switch) we now add
|
|
1122
|
+
// POWER switches automatically if we get POWER STATE
|
|
1123
|
+
let powerStateFound = false;
|
|
1124
|
+
if (obj.id.includes('.STATE.POWER')) {
|
|
1125
|
+
powerStateFound = true;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
updateState(obj, val);
|
|
1129
|
+
|
|
1130
|
+
if (powerStateFound) {
|
|
1131
|
+
adapter.log.debug(`Got .STATE.POWER value message -> auto add .POWER object from ${obj.id}`);
|
|
1132
|
+
let powerId = obj.id.replace('.STATE.POWER', '.POWER');
|
|
1133
|
+
let powerName = obj.data.common.name.replace(' STATE POWER', ' POWER');
|
|
1134
|
+
let powerObj = Object.assign({}, obj);
|
|
1135
|
+
powerObj.id = powerObj.data._id = powerId;
|
|
1136
|
+
powerObj.data.common.name = powerName;
|
|
1137
|
+
updateState(powerObj, val);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
function receivedTopic(packet, client) {
|
|
1145
|
+
if (!packet) {
|
|
1146
|
+
return adapter.log.warn(`Empty packet received: ${JSON.stringify(packet)}`);
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
client.states = client.states || {};
|
|
1150
|
+
client.states[packet.topic] = {
|
|
1151
|
+
message: packet.payload,
|
|
1152
|
+
retain: packet.retain,
|
|
1153
|
+
qos: packet.qos
|
|
1154
|
+
};
|
|
1155
|
+
|
|
1156
|
+
// update alive state
|
|
1157
|
+
updateAlive(client, true);
|
|
1158
|
+
|
|
1159
|
+
if (client._will && client._will.topic && packet.topic === client._will.topic) {
|
|
1160
|
+
client._will.payload = packet.payload;
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
let val = packet.payload.toString('utf8');
|
|
1165
|
+
adapter.log.debug(`[${client.id}] Received: ${packet.topic} = ${val}`);
|
|
1166
|
+
|
|
1167
|
+
/*
|
|
1168
|
+
adapter.getForeignState('system.adapter.sonoff.0.memRss', (err, state) => {
|
|
1169
|
+
adapter.log.info('mem: ' + state.val + ' MB');
|
|
1170
|
+
if (heapTest==0 || (heapTest==1 && state.val>37.5)) {
|
|
1171
|
+
heapTest+=1;
|
|
1172
|
+
heapdump.writeSnapshot('/opt/iobroker/' + Date.now() + '.heapsnapshot');
|
|
1173
|
+
adapter.log.info('Wrote snapshot: ');
|
|
1174
|
+
|
|
1175
|
+
var filename='/opt/iobroker/' + Date.now() + '.heapsnapshot';
|
|
1176
|
+
heapdump.writeSnapshot((err,filename) => {
|
|
1177
|
+
if (err) {
|
|
1178
|
+
adapter.log.info('heap: ' + err);
|
|
1179
|
+
} else {
|
|
1180
|
+
adapter.log.info('Wrote snapshot: ' + filename);
|
|
1181
|
+
}
|
|
1182
|
+
});
|
|
1183
|
+
|
|
1184
|
+
}
|
|
1185
|
+
});
|
|
1186
|
+
*/
|
|
1187
|
+
|
|
1188
|
+
// [DVES_BD3B4D] Received: tele/sonoff2/STATE = {
|
|
1189
|
+
// "Time":"2017-10-01T12:37:18",
|
|
1190
|
+
// "Uptime":0,
|
|
1191
|
+
// "Vcc":3.224,
|
|
1192
|
+
// "POWER":"ON",
|
|
1193
|
+
// "POWER1":"OFF",
|
|
1194
|
+
// "POWER2":"ON"
|
|
1195
|
+
// "Wifi":{
|
|
1196
|
+
// "AP":1,
|
|
1197
|
+
// "SSId":"FuckOff",
|
|
1198
|
+
// "RSSI":62,
|
|
1199
|
+
// "APMac":"E0:28:6D:EC:21:EA"
|
|
1200
|
+
// }
|
|
1201
|
+
// }
|
|
1202
|
+
// [DVES_BD3B4D] Received: tele/sonoff2/SENSOR = {
|
|
1203
|
+
// "Time":"2017-10-01T12:37:18",
|
|
1204
|
+
// "Switch1":"ON",
|
|
1205
|
+
// "DS18B20":{"Temperature":20.6},
|
|
1206
|
+
// "TempUnit":"C"
|
|
1207
|
+
// }
|
|
1208
|
+
client._map = client._map || {};
|
|
1209
|
+
|
|
1210
|
+
const parts = packet.topic.split('/');
|
|
1211
|
+
|
|
1212
|
+
if (!client._fallBackName) {
|
|
1213
|
+
client._fallBackName = parts[1];
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
const stateId = parts.pop();
|
|
1217
|
+
|
|
1218
|
+
if (stateId === 'LWT') {
|
|
1219
|
+
return;
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
if (val.includes('nan')) {
|
|
1223
|
+
val = val.replace(/:nan,/g, ':"NaN",').replace(/:nan}/g, ':"NaN"}').replace(/:nan]/g, ':"NaN"]');
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
if (stateId === 'RESULT') {
|
|
1227
|
+
// ignore: stat/Sonoff/RESULT = {"POWER":"ON"}
|
|
1228
|
+
// testserver.js reports error, so reject above cmd
|
|
1229
|
+
const str = val.replace(/\s+/g, '');
|
|
1230
|
+
if (str.startsWith('{"POWER":"ON"}')) return;
|
|
1231
|
+
if (str.startsWith('{"POWER":"OFF"}')) return;
|
|
1232
|
+
|
|
1233
|
+
if (parts[0] === 'stat' || parts[0] === 'tele') {
|
|
1234
|
+
try {
|
|
1235
|
+
if (adapter.config.OBJ_TREE) {
|
|
1236
|
+
checkData(client, packet.topic, 'RESULT', JSON.parse(val));
|
|
1237
|
+
} else {
|
|
1238
|
+
checkData(client, packet.topic, NO_PREFIX, JSON.parse(val));
|
|
1239
|
+
}
|
|
1240
|
+
} catch (e) {
|
|
1241
|
+
adapter.log.warn(`Cannot parse data "${stateId}": _${val}_ - ${e}`);
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
// tele/sonoff_4ch/STATE = {"Time":"2017-10-02T19:26:06", "Uptime":0, "Vcc":3.226, "POWER1":"OFF", "POWER2":"OFF", "POWER3":"OFF", "POWER4":"OFF", "Wifi":{"AP":1, "SSId":"AAA", "RSSI": 15}}
|
|
1248
|
+
// tele/sonoff/SENSOR = {"Time":"2017-10-05T17:43:19", "DS18x20":{"DS1":{"Type":"DS18B20", "Address":"28FF9A9876815022A", "Temperature":12.2}}, "TempUnit":"C"}
|
|
1249
|
+
// tele/sonoff5/SENSOR = {"Time":"2017-10-03T14:02:25", "AM2301-14":{"Temperature":21.6, "Humidity":54.7}, "TempUnit":"C"}
|
|
1250
|
+
// tele/sonoff/SENSOR = {"Time":"2018-02-23T17:36:59", "Analog0":298}
|
|
1251
|
+
if (parts[0] === 'tele' && stateId.match(/^(RESULT|STATE|SENSOR|WAKEUP)\d?$/)) {
|
|
1252
|
+
try {
|
|
1253
|
+
if (adapter.config.OBJ_TREE) {
|
|
1254
|
+
if (stateId.match(/^(STATE)\d?$/)) {
|
|
1255
|
+
checkData(client, packet.topic, 'STATE', JSON.parse(val));
|
|
1256
|
+
} else if (stateId.match(/^(SENSOR)\d?$/)) {
|
|
1257
|
+
checkData(client, packet.topic, 'SENSOR', JSON.parse(val));
|
|
1258
|
+
} else if (stateId.match(/^(WAKEUP)\d?$/)) {
|
|
1259
|
+
checkData(client, packet.topic, 'WAKEUP', JSON.parse(val));
|
|
1260
|
+
} else if (stateId.match(/^(RESULT)\d?$/)) {
|
|
1261
|
+
checkData(client, packet.topic, 'WAKEUP', JSON.parse(val));
|
|
1262
|
+
}
|
|
1263
|
+
} else {
|
|
1264
|
+
checkData(client, packet.topic, NO_PREFIX, JSON.parse(val));
|
|
1265
|
+
//adapter.log.warn('log sensor parse"' + stateId + '": _' + val);
|
|
1266
|
+
}
|
|
1267
|
+
} catch (e) {
|
|
1268
|
+
adapter.log.warn(`Cannot parse data "${stateId}": _${val}_ - ${e}`);
|
|
1269
|
+
}
|
|
1270
|
+
} else if (parts[0] === 'tele' && stateId.match(/^INFO\d?$/)) {
|
|
1271
|
+
// tele/SonoffPOW/INFO1 = {"Module":"Sonoff Pow", "Version":"5.8.0", "FallbackTopic":"SonoffPOW", "GroupTopic":"sonoffs"}
|
|
1272
|
+
// tele/SonoffPOW/INFO2 = {"WebServerMode":"Admin", "Hostname":"Sonoffpow", "IPAddress":"192.168.2.182"}
|
|
1273
|
+
// tele/SonoffPOW/INFO3 = {"RestartReason":"Software/System restart"}
|
|
1274
|
+
try {
|
|
1275
|
+
checkData(client, packet.topic, 'INFO', JSON.parse(val));
|
|
1276
|
+
} catch (e) {
|
|
1277
|
+
adapter.log.warn(`Cannot parse data"${stateId}": _${val}_ - ${e}`);
|
|
1278
|
+
}
|
|
1279
|
+
} else if (parts[0] === 'tele' && stateId.match(/^(ENERGY)\d?$/)) {
|
|
1280
|
+
// tele/sonoff_4ch/ENERGY = {"Time":"2017-10-02T19:24:32", "Total":1.753, "Yesterday":0.308, "Today":0.205, "Period":0, "Power":3, "Factor":0.12, "Voltage":221, "Current":0.097}
|
|
1281
|
+
try {
|
|
1282
|
+
checkData(client, packet.topic, 'ENERGY', JSON.parse(val));
|
|
1283
|
+
} catch (e) {
|
|
1284
|
+
adapter.log.warn(`Cannot parse data"${stateId}": _${val}_ - ${e}`);
|
|
1285
|
+
}
|
|
1286
|
+
} else if (types[stateId]) {
|
|
1287
|
+
// /ESP_BOX/BM280/Pressure = 1010.09
|
|
1288
|
+
// /ESP_BOX/BM280/Humidity = 42.39
|
|
1289
|
+
// /ESP_BOX/BM280/Temperature = 25.86
|
|
1290
|
+
// /ESP_BOX/BM280/Approx. Altitude = 24
|
|
1291
|
+
|
|
1292
|
+
// cmnd/sonoff/POWER
|
|
1293
|
+
// stat/sonoff/POWER
|
|
1294
|
+
|
|
1295
|
+
if (types[stateId]) {
|
|
1296
|
+
let id = `${adapter.namespace}.${client.iobId}.${stateId.replace(/[-.+\s]+/g, '_')}`;
|
|
1297
|
+
let obj = {
|
|
1298
|
+
type: 'addObject',
|
|
1299
|
+
id: id,
|
|
1300
|
+
data: {
|
|
1301
|
+
_id: id,
|
|
1302
|
+
common: JSON.parse(JSON.stringify(types[stateId])),
|
|
1303
|
+
native: {},
|
|
1304
|
+
type: 'state'
|
|
1305
|
+
}
|
|
1306
|
+
};
|
|
1307
|
+
obj.data.common.name = `${client.id} ${stateId}`;
|
|
1308
|
+
|
|
1309
|
+
// push only new objects
|
|
1310
|
+
updateState(obj, () => {
|
|
1311
|
+
if (parts[0] === 'cmnd') {
|
|
1312
|
+
|
|
1313
|
+
// Set Object fix
|
|
1314
|
+
if (obj.data.common.type === 'number') {
|
|
1315
|
+
adapter.setState(id, parseFloat(val), true);
|
|
1316
|
+
} else if (obj.data.common.type === 'boolean') {
|
|
1317
|
+
if (val === 'ON' || val === '1' || val === 'true' || val === 'on') {
|
|
1318
|
+
adapter.setState(id, true, true);
|
|
1319
|
+
} else if (val === 'OFF' || val === '0' || val === 'false' || val === 'off') {
|
|
1320
|
+
adapter.setState(id, false, true);
|
|
1321
|
+
}
|
|
1322
|
+
} else {
|
|
1323
|
+
adapter.setState(id, val, true);
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
// remember POWER topic
|
|
1327
|
+
client._map[stateId] = packet.topic;
|
|
1328
|
+
} else {
|
|
1329
|
+
if (obj.data.common.type === 'number') {
|
|
1330
|
+
adapter.setState(id, parseFloat(val), true);
|
|
1331
|
+
} else if (obj.data.common.type === 'boolean') {
|
|
1332
|
+
if (val === 'ON' || val === '1' || val === 'true' || val === 'on') {
|
|
1333
|
+
adapter.setState(id, true, true);
|
|
1334
|
+
} else if (val === 'OFF' || val === '0' || val === 'false' || val === 'off') {
|
|
1335
|
+
adapter.setState(id, false, true);
|
|
1336
|
+
}
|
|
1337
|
+
} else {
|
|
1338
|
+
adapter.setState(id, val, true);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
});
|
|
1342
|
+
} else {
|
|
1343
|
+
adapter.log.debug(`Cannot process: ${packet.topic}`);
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
function clientClose(client, reason) {
|
|
1349
|
+
if (!client) return;
|
|
1350
|
+
|
|
1351
|
+
if (persistentSessions[client.id]) {
|
|
1352
|
+
persistentSessions[client.id].connected = false;
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
if (client._resendonStart) {
|
|
1356
|
+
clearTimeout(client._resendonStart);
|
|
1357
|
+
client._resendonStart = null;
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
try {
|
|
1361
|
+
if (clients[client.id] && (client.__secret === clients[client.id].__secret)) {
|
|
1362
|
+
adapter.log.info(`Client [${client.id}] connection closed: ${reason}`);
|
|
1363
|
+
updateAlive(client, false);
|
|
1364
|
+
delete clients[client.id];
|
|
1365
|
+
updateClients();
|
|
1366
|
+
if (client._will) {
|
|
1367
|
+
receivedTopic(client._will, client, () => client.destroy());
|
|
1368
|
+
} else {
|
|
1369
|
+
client.destroy();
|
|
1370
|
+
}
|
|
1371
|
+
} else {
|
|
1372
|
+
client.destroy();
|
|
1373
|
+
}
|
|
1374
|
+
} catch (e) {
|
|
1375
|
+
adapter.log.warn(`Client [${client.id}] Cannot close client: ${e}`);
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
function checkResends() {
|
|
1380
|
+
const now = Date.now();
|
|
1381
|
+
resending = true;
|
|
1382
|
+
for (const clientId in clients) {
|
|
1383
|
+
if (clients.hasOwnProperty(clientId) && clients[clientId] && clients[clientId]._messages) {
|
|
1384
|
+
for (let m = clients[clientId]._messages.length - 1; m >= 0; m--) {
|
|
1385
|
+
const message = clients[clientId]._messages[m];
|
|
1386
|
+
if (now - message.ts >= adapter.config.retransmitInterval) {
|
|
1387
|
+
if (message.count > adapter.config.retransmitCount) {
|
|
1388
|
+
adapter.log.warn(`Client [${clientId}] Message ${message.messageId} deleted after ${message.count} retries`);
|
|
1389
|
+
clients[clientId]._messages.splice(m, 1);
|
|
1390
|
+
continue;
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
// resend this message
|
|
1394
|
+
message.count++;
|
|
1395
|
+
message.ts = now;
|
|
1396
|
+
try {
|
|
1397
|
+
adapter.log.debug(`Client [${clientId}] Resend message topic: ${message.topic}, payload: ${message.payload}`);
|
|
1398
|
+
if (message.cmd === 'publish') {
|
|
1399
|
+
clients[clientId].publish(message);
|
|
1400
|
+
}
|
|
1401
|
+
} catch (e) {
|
|
1402
|
+
adapter.log.warn(`Client [${clientId}] Cannot publish message: ${e}`);
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
if (adapter.config.sendInterval) {
|
|
1406
|
+
setTimeout(checkResends, adapter.config.sendInterval);
|
|
1407
|
+
} else {
|
|
1408
|
+
setImmediate(checkResends);
|
|
1409
|
+
}
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
// delete old sessions
|
|
1417
|
+
if (adapter.config.storeClientsTime !== -1) {
|
|
1418
|
+
for (const id in persistentSessions) {
|
|
1419
|
+
if (persistentSessions.hasOwnProperty(id)) {
|
|
1420
|
+
if (now - persistentSessions[id].lastSeen > adapter.config.storeClientsTime * 60000) {
|
|
1421
|
+
delete persistentSessions[id];
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
resending = false;
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
(function _constructor(config) {
|
|
1431
|
+
if (config.timeout === undefined) {
|
|
1432
|
+
config.timeout = 300;
|
|
1433
|
+
} else {
|
|
1434
|
+
config.timeout = parseInt(config.timeout, 10);
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
server.on('connection', stream => {
|
|
1438
|
+
let client = mqtt(stream);
|
|
1439
|
+
// Store unique connection identifier
|
|
1440
|
+
client.__secret = `${Date.now()}_${Math.round(Math.random() * 10000)}`;
|
|
1441
|
+
|
|
1442
|
+
// client connected
|
|
1443
|
+
client.on('connect', options => {
|
|
1444
|
+
// acknowledge the connect packet
|
|
1445
|
+
client.id = options.clientId;
|
|
1446
|
+
client.iobId = client.id.replace(FORBIDDEN_CHARS, '_');
|
|
1447
|
+
mappingClients[client.iobId] = client.id;
|
|
1448
|
+
|
|
1449
|
+
// get possible old client
|
|
1450
|
+
let oldClient = clients[client.id];
|
|
1451
|
+
|
|
1452
|
+
if (config.user) {
|
|
1453
|
+
if (options.password) {
|
|
1454
|
+
options.password = options.password.toString();
|
|
1455
|
+
}
|
|
1456
|
+
if (config.user !== options.username ||
|
|
1457
|
+
config.password !== options.password) {
|
|
1458
|
+
adapter.log.warn(`Client [${client.id}] has invalid password or username`);
|
|
1459
|
+
client.connack({returnCode: 4});
|
|
1460
|
+
if (oldClient) {
|
|
1461
|
+
// delete existing client
|
|
1462
|
+
delete clients[client.id];
|
|
1463
|
+
updateAlive(oldClient, false);
|
|
1464
|
+
updateClients();
|
|
1465
|
+
oldClient.destroy();
|
|
1466
|
+
}
|
|
1467
|
+
client.destroy();
|
|
1468
|
+
return;
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
if (oldClient) {
|
|
1473
|
+
adapter.log.info(`Client [${client.id}] reconnected. Old secret ${clients[client.id].__secret}. New secret ${client.__secret}`);
|
|
1474
|
+
// need to destroy the old client
|
|
1475
|
+
|
|
1476
|
+
if (client.__secret !== clients[client.id].__secret) {
|
|
1477
|
+
// it is another socket!!
|
|
1478
|
+
|
|
1479
|
+
// It was following situation:
|
|
1480
|
+
// - old connection was active
|
|
1481
|
+
// - new connection is on the same TCP
|
|
1482
|
+
// Just forget him
|
|
1483
|
+
// oldClient.destroy();
|
|
1484
|
+
}
|
|
1485
|
+
} else {
|
|
1486
|
+
adapter.log.info(`Client [${client.id}] connected with secret ${client.__secret}`);
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
let sessionPresent = false;
|
|
1490
|
+
|
|
1491
|
+
if (!client.cleanSession && adapter.config.storeClientsTime !== 0) {
|
|
1492
|
+
if (persistentSessions[client.id]) {
|
|
1493
|
+
sessionPresent = true;
|
|
1494
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1495
|
+
} else {
|
|
1496
|
+
persistentSessions[client.id] = {
|
|
1497
|
+
_subsID: {},
|
|
1498
|
+
_subs: {},
|
|
1499
|
+
messages: [],
|
|
1500
|
+
lastSeen: Date.now()
|
|
1501
|
+
};
|
|
1502
|
+
}
|
|
1503
|
+
client._messages = persistentSessions[client.id].messages;
|
|
1504
|
+
persistentSessions[client.id].connected = true;
|
|
1505
|
+
} else if (client.cleanSession && persistentSessions[client.id]) {
|
|
1506
|
+
delete persistentSessions[client.id];
|
|
1507
|
+
}
|
|
1508
|
+
client._messages = client._messages || [];
|
|
1509
|
+
|
|
1510
|
+
client.connack({returnCode: 0, sessionPresent});
|
|
1511
|
+
clients[client.id] = client;
|
|
1512
|
+
updateClients();
|
|
1513
|
+
|
|
1514
|
+
client._will = options.will;
|
|
1515
|
+
createClient(client, () => {
|
|
1516
|
+
if (persistentSessions[client.id]) {
|
|
1517
|
+
client._subsID = persistentSessions[client.id]._subsID;
|
|
1518
|
+
client._subs = persistentSessions[client.id]._subs;
|
|
1519
|
+
if (persistentSessions[client.id].messages.length) {
|
|
1520
|
+
// give to the client a little bit time
|
|
1521
|
+
client._resendonStart = setTimeout(clientId => {
|
|
1522
|
+
client._resendonStart = null;
|
|
1523
|
+
resendMessages2Client(client, persistentSessions[clientId].messages);
|
|
1524
|
+
}, 100, client.id);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
});
|
|
1528
|
+
});
|
|
1529
|
+
|
|
1530
|
+
// timeout idle streams after 5 minutes
|
|
1531
|
+
if (config.timeout) {
|
|
1532
|
+
stream.setTimeout(config.timeout * 1000);
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
// connection error handling
|
|
1536
|
+
client.on('close', had_error => clientClose(client, had_error ? 'closed because of error' : 'closed'));
|
|
1537
|
+
client.on('error', e => clientClose(client, e));
|
|
1538
|
+
client.on('disconnect', () => clientClose(client, 'disconnected'));
|
|
1539
|
+
// stream timeout
|
|
1540
|
+
stream.on('timeout', () => clientClose(client, 'timeout'));
|
|
1541
|
+
|
|
1542
|
+
client.on('publish', packet => {
|
|
1543
|
+
if (clients[client.id] && client.__secret !== clients[client.id].__secret) {
|
|
1544
|
+
!adapter.config.ignorePings && adapter.log.warn(`Old client ${client.id} with secret ${client.__secret} sends publish. Ignore! Actual secret is ${clients[client.id].__secret}`);
|
|
1545
|
+
return;
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
if (persistentSessions[client.id]) {
|
|
1549
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
if (packet.qos === 1) {
|
|
1553
|
+
// send PUBACK to client
|
|
1554
|
+
client.puback({messageId: packet.messageId});
|
|
1555
|
+
} else if (packet.qos === 2) {
|
|
1556
|
+
const pack = client._messages && client._messages.find(e => e.messageId === packet.messageId);
|
|
1557
|
+
if (pack) {
|
|
1558
|
+
// duplicate message => ignore
|
|
1559
|
+
adapter.log.warn(`Client [${client.id}] Ignored duplicate message with ID: ${packet.messageId}`);
|
|
1560
|
+
return;
|
|
1561
|
+
} else {
|
|
1562
|
+
packet.ts = Date.now();
|
|
1563
|
+
packet.cmd = 'pubrel';
|
|
1564
|
+
packet.count = 0;
|
|
1565
|
+
client._messages.push(packet);
|
|
1566
|
+
|
|
1567
|
+
client.pubrec({messageId: packet.messageId});
|
|
1568
|
+
return;
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
|
|
1573
|
+
receivedTopic(packet, client)
|
|
1574
|
+
});
|
|
1575
|
+
|
|
1576
|
+
// response for QoS2
|
|
1577
|
+
client.on('pubrec', packet => {
|
|
1578
|
+
if (clients[client.id] && client.__secret !== clients[client.id].__secret) {
|
|
1579
|
+
!adapter.config.ignorePings && adapter.log.warn(`Old client ${client.id} with secret ${client.__secret} sends pubrec. Ignore! Actual secret is ${clients[client.id].__secret}`);
|
|
1580
|
+
return;
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
if (persistentSessions[client.id]) {
|
|
1584
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
let pos = null;
|
|
1588
|
+
// remove this message from queue
|
|
1589
|
+
client._messages && client._messages.forEach((e, i) => {
|
|
1590
|
+
if (e.messageId === packet.messageId) {
|
|
1591
|
+
pos = i;
|
|
1592
|
+
return false;
|
|
1593
|
+
}
|
|
1594
|
+
});
|
|
1595
|
+
if (pos !== null) {
|
|
1596
|
+
client.pubrel({messageId: packet.messageId});
|
|
1597
|
+
} else {
|
|
1598
|
+
adapter.log.warn(`Client [${client.id}] Received pubrec on ${client.id} for unknown messageId ${packet.messageId}`);
|
|
1599
|
+
}
|
|
1600
|
+
});
|
|
1601
|
+
|
|
1602
|
+
// response for QoS2
|
|
1603
|
+
client.on('pubcomp', packet => {
|
|
1604
|
+
if (clients[client.id] && client.__secret !== clients[client.id].__secret) {
|
|
1605
|
+
!adapter.config.ignorePings && adapter.log.warn(`Old client ${client.id} with secret ${client.__secret} sends pubcomp. Ignore! Actual secret is ${clients[client.id].__secret}`);
|
|
1606
|
+
return;
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
if (persistentSessions[client.id]) {
|
|
1610
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
let pos = null;
|
|
1614
|
+
// remove this message from queue
|
|
1615
|
+
client._messages && client._messages.forEach((e, i) => {
|
|
1616
|
+
if (e.messageId === packet.messageId) {
|
|
1617
|
+
pos = i;
|
|
1618
|
+
return false;
|
|
1619
|
+
}
|
|
1620
|
+
});
|
|
1621
|
+
if (pos !== null) {
|
|
1622
|
+
client._messages.splice(pos, 1);
|
|
1623
|
+
} else {
|
|
1624
|
+
adapter.log.warn(`Client [${client.id}] Received pubcomp for unknown message ID: ${packet.messageId}`);
|
|
1625
|
+
}
|
|
1626
|
+
});
|
|
1627
|
+
|
|
1628
|
+
// response for QoS2
|
|
1629
|
+
client.on('pubrel', packet => {
|
|
1630
|
+
if (!client._messages) {
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1633
|
+
if (clients[client.id] && client.__secret !== clients[client.id].__secret) {
|
|
1634
|
+
!adapter.config.ignorePings && adapter.log.warn(`Old client ${client.id} with secret ${client.__secret} sends pubrel. Ignore! Actual secret is ${clients[client.id].__secret}`);
|
|
1635
|
+
return;
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
if (persistentSessions[client.id]) {
|
|
1639
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
let pos = null;
|
|
1643
|
+
// remove this message from queue
|
|
1644
|
+
client._messages && client._messages.forEach((e, i) => {
|
|
1645
|
+
if (e.messageId === packet.messageId) {
|
|
1646
|
+
pos = i;
|
|
1647
|
+
return false;
|
|
1648
|
+
}
|
|
1649
|
+
});
|
|
1650
|
+
|
|
1651
|
+
if (pos !== null) {
|
|
1652
|
+
client.pubcomp({messageId: packet.messageId});
|
|
1653
|
+
|
|
1654
|
+
receivedTopic(client._messages[pos], client);
|
|
1655
|
+
} else {
|
|
1656
|
+
adapter.log.warn(`Client [${client.id}] Received pubrel on ${client.id} for unknown messageId ${packet.messageId}`);
|
|
1657
|
+
}
|
|
1658
|
+
});
|
|
1659
|
+
|
|
1660
|
+
// response for QoS1
|
|
1661
|
+
client.on('puback', packet => {
|
|
1662
|
+
if (clients[client.id] && client.__secret !== clients[client.id].__secret) {
|
|
1663
|
+
!adapter.config.ignorePings && adapter.log.warn(`Old client ${client.id} with secret ${client.__secret} sends puback. Ignore! Actual secret is ${clients[client.id].__secret}`);
|
|
1664
|
+
return;
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
if (persistentSessions[client.id]) {
|
|
1668
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
// remove this message from queue
|
|
1672
|
+
let pos = null;
|
|
1673
|
+
// remove this message from queue
|
|
1674
|
+
client._messages && client._messages.forEach((e, i) => {
|
|
1675
|
+
if (e.messageId === packet.messageId) {
|
|
1676
|
+
pos = i;
|
|
1677
|
+
return false;
|
|
1678
|
+
}
|
|
1679
|
+
});
|
|
1680
|
+
|
|
1681
|
+
if (pos !== null) {
|
|
1682
|
+
adapter.log.debug(`Client [${client.id}] Received puback for ${client.id} message ID: ${packet.messageId}`);
|
|
1683
|
+
client._messages && client._messages.splice(pos, 1);
|
|
1684
|
+
} else {
|
|
1685
|
+
adapter.log.warn(`Client [${client.id}] Received puback for unknown message ID: ${packet.messageId}`);
|
|
1686
|
+
}
|
|
1687
|
+
});
|
|
1688
|
+
|
|
1689
|
+
client.on('unsubscribe', packet => {
|
|
1690
|
+
if (clients[client.id] && client.__secret !== clients[client.id].__secret) {
|
|
1691
|
+
!adapter.config.ignorePings && adapter.log.warn(`Old client ${client.id} with secret ${client.__secret} sends unsubscribe. Ignore! Actual secret is ${clients[client.id].__secret}`);
|
|
1692
|
+
return;
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
if (persistentSessions[client.id]) {
|
|
1696
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
client.unsuback({messageId: packet.messageId});
|
|
1700
|
+
});
|
|
1701
|
+
|
|
1702
|
+
client.on('subscribe', packet => {
|
|
1703
|
+
if (clients[client.id] && client.__secret !== clients[client.id].__secret) {
|
|
1704
|
+
!adapter.config.ignorePings && adapter.log.warn(`Old client ${client.id} with secret ${client.__secret} sends subscribe. Ignore! Actual secret is ${clients[client.id].__secret}`);
|
|
1705
|
+
return;
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1708
|
+
if (persistentSessions[client.id]) {
|
|
1709
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
// just confirm the request.
|
|
1713
|
+
// we expect subscribe for 'cmnd.sonoff.#'
|
|
1714
|
+
let granted = packet.subscriptions.map(subs => subs.qos);
|
|
1715
|
+
|
|
1716
|
+
client.suback({granted: granted, messageId: packet.messageId});
|
|
1717
|
+
});
|
|
1718
|
+
|
|
1719
|
+
client.on('pingreq', (/*packet*/) => {
|
|
1720
|
+
if (clients[client.id] && client.__secret !== clients[client.id].__secret) {
|
|
1721
|
+
!adapter.config.ignorePings && adapter.log.warn(`Old client ${client.id} with secret ${client.__secret} sends pingreq. Ignore! Actual secret is ${clients[client.id].__secret}`);
|
|
1722
|
+
return;
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
if (persistentSessions[client.id]) {
|
|
1726
|
+
persistentSessions[client.id].lastSeen = Date.now();
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
adapter.log.debug(`Client [${client.id}] pingreq`);
|
|
1730
|
+
client.pingresp();
|
|
1731
|
+
});
|
|
1732
|
+
});
|
|
1733
|
+
|
|
1734
|
+
server.on('error', err =>
|
|
1735
|
+
adapter.log.error(`Can not start Server ${err}`));
|
|
1736
|
+
|
|
1737
|
+
config.port = parseInt(config.port, 10) || 1883;
|
|
1738
|
+
|
|
1739
|
+
config.retransmitInterval = config.retransmitInterval || 2000;
|
|
1740
|
+
config.retransmitCount = config.retransmitCount || 10;
|
|
1741
|
+
config.storeClientsTime = config.storeClientsTime === undefined ? 1440 : parseInt(config.storeClientsTime, 10) || 0;
|
|
1742
|
+
config.defaultQoS = parseInt(config.defaultQoS, 10) || 0;
|
|
1743
|
+
|
|
1744
|
+
// Update connection state
|
|
1745
|
+
updateClients();
|
|
1746
|
+
|
|
1747
|
+
// to start
|
|
1748
|
+
server.listen(config.port, config.bind, () => {
|
|
1749
|
+
adapter.log.info(`Starting MQTT ${config.user ? 'authenticated ' : ''} server on port ${config.port}`);
|
|
1750
|
+
|
|
1751
|
+
resendTimer = setInterval(() =>
|
|
1752
|
+
!resending && checkResends(),
|
|
1753
|
+
adapter.config.retransmitInterval || 2000);
|
|
1754
|
+
});
|
|
1755
|
+
})(adapter.config);
|
|
1756
|
+
|
|
1757
|
+
return this;
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
module.exports = MQTTServer;
|