-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.js
1286 lines (1082 loc) · 41.8 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/*
* Created with @iobroker/create-adapter v1.21.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const dmWled = require('./lib/devicemgmt.js');
// Load your modules here, e.g.:
const rgbHex = require('rgb-hex'); // Lib to translate rgb to hex
const hexRgb = require('hex-rgb'); // Lib to translate hex to rgb
const WebSocket = require('ws'); // Lib to handle Websocket
const {default: axios} = require('axios'); // Lib to handle http requests
const bonjour = require('bonjour')(); // load Bonjour library
const stateAttr = require('./lib/stateAttr.js'); // Load attribute library
let watchDogStartDelay = null; // Timer to delay watchdog start
const watchdogTimer = {}; // Array containing all times for watchdog loops
const watchdogWsTimer = {}; // Array containing all times for WS-Ping loops
const stateExpire = {}; // Array containing all times for online state expire
const ws = {}; // Array containing websocket connections
const warnMessages = {}; // Array containing sentry messages
const disableSentry = false; // Ensure to set to true during development !
class Wled extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
// @ts-ignore
super({
...options,
name: 'wled',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
this.on('message', this.onMessage.bind(this));
this.devices = {};
this.effects = {};
this.palettes = {};
this.createdStatesDetails = {};
this.deviceManagement = new dmWled(this);
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
await this.resetOnlineStates();
// Read already known devices
await this.tryKnownDevices();
// Run Autodetect (Bonjour - Service, mDNS to be handled)
await this.scanDevices();
// Set connection state to online when adapter is ready
this.setState('info.connection', true, true);
// Reset timer (if running) and start 10s delay to r un watchdog
if (watchDogStartDelay) {
clearTimeout(watchDogStartDelay);
watchDogStartDelay = null;
}
watchDogStartDelay = setTimeout(() => {
// Start watchdog for each device
for (const i in this.devices) {
this.watchDog(i);
}
this.log.info('WLED initialisation finalized, ready to do my job have fun !');
}, (10000));
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Clear running polling timers
if (watchDogStartDelay) {
clearTimeout(watchDogStartDelay);
watchDogStartDelay = null;
}
for (const device in watchdogWsTimer){
if (watchdogWsTimer[device]) {
clearTimeout(watchdogWsTimer[device]);
delete watchdogWsTimer[device];
}
}
for (const device in watchdogTimer){
if (watchdogTimer[device]) {
clearTimeout(watchdogTimer[device]);
delete watchdogTimer[device];
}
}
for (const device in stateExpire){
if (stateExpire[device]) {
clearTimeout(stateExpire[device]);
delete stateExpire[device];
}
}
// Close WebSocket connections
for (const device in ws){
try {
// Close socket connection
ws[device].close();
} catch (error) {
let message = error;
if (error instanceof Error && error.stack != null) message = error.stack;
this.log.error(`Error closing webSocket connection to ${device} | ${message}`);
}
}
// Set all online states to false
for (const i in this.devices) {
this.setState(this.devices[i].mac + '._info' + '._online', {val: false, ack: true});
}
try {
this.log.info('cleaned everything up...');
this.setState('info.connection', false, true);
callback();
} catch (error) {
this.errorHandler(`[onStateChange]`, error);
this.setState('info.connection', false, true);
callback();
}
} catch (error) {
this.errorHandler(`[onStateChange]`, error);
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
try {
if (state && state.ack === false) {
// The state was changed and is not (yet) acknowledged
this.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
// Split state name in segments to be used later
const deviceId = id.split('.');
// Prepare array's needed to send new states
let values = null, rgb_all = null;
// Build send command for state changes
if (deviceId[4] === undefined) {
this.log.debug('Send state');
values = {
[deviceId[3]]: state.val
};
this.log.debug('values 4 ' + JSON.stringify(values));
} else {
// Send command 1 - level nesting
if (deviceId[5] === undefined) {
this.log.debug('Send nested state');
// Send state change & value state forward to WLED API
values = {
[deviceId[3]]: {
[deviceId[4]]: state.val
}
};
this.log.debug('values 5 ' + JSON.stringify(values));
}
// Handle segments logic
if (deviceId[3] === 'seg') {
this.log.debug('Send seg');
const valAsNumbers = parseFloat(deviceId[4]);
this.log.debug('test number : ' + valAsNumbers);
// Check if changed state is related to color
if (deviceId[5] === 'col') {
this.log.debug('Send col');
const color_root = deviceId[2] + '.' + deviceId[3] + '.' + deviceId[4] + '.' + deviceId[5];
this.log.debug(color_root);
// Handle logic for HEX values, must be translated to RGB which is used by WLED
if (deviceId[6] === '0_HEX' || deviceId[6] === '1_HEX' || deviceId[6] === '2_HEX') {
this.log.debug('HEX color change initiated, convert to RGB and send data');
try {
// Get all 3 HEX values from states
const colorPrimaryHex = await this.getStateAsync(color_root + '.0_HEX');
if (!colorPrimaryHex) return;
const colorSecondaryHex = await this.getStateAsync(color_root + '.1_HEX');
if (!colorSecondaryHex) return;
const colorTertiaryHex = await this.getStateAsync(color_root + '.1_HEX');
if (!colorTertiaryHex) return;
// Use library to translate HEX values into proper RGB
//hex RGB calculate Alpha channel in 0 to 1 Wled need a value between 0 and 255 so the alpha channel from HEXRGB has to multiple by 255
const colorPrimaryRGB = hexRgb(colorPrimaryHex.val);
colorPrimaryRGB.alpha = colorPrimaryRGB.alpha*255
const colorSecondaryRGB = hexRgb(colorSecondaryHex.val);
colorSecondaryRGB.alpha = colorSecondaryRGB.alpha*255
const colorTertiaryRGB = hexRgb(colorTertiaryHex.val);
colorTertiaryRGB.alpha = colorTertiaryRGB.alpha*255
// Build RGB JSON string to be send to WLED
//add aditional Alpha channel when RGBW is used
rgb_all = [
[colorPrimaryRGB.red, colorPrimaryRGB.green, colorPrimaryRGB.blue,colorPrimaryRGB.alpha],
[colorSecondaryRGB.red, colorSecondaryRGB.green, colorSecondaryRGB.blue,colorSecondaryRGB.alpha],
[colorTertiaryRGB.red, colorTertiaryRGB.green, colorTertiaryRGB.blue,colorTertiaryRGB.alpha]
];
this.log.debug('Converted RGB values of HEX input : ' + colorPrimaryRGB + ' : ' + colorSecondaryRGB + ' : ' + colorTertiaryRGB);
} catch (error) {
this.log.error('Hex conversion failed : ' + error);
return;
}
// Handle logic for RGB values, must be translated to RGB which is used by WLED
} else if ((deviceId[6] === '0' || deviceId[6] === '1' || deviceId[6] === '2')) {
this.log.debug('RGB color change initiated, convert to RGB and send data');
try {
// Get all 3 RGB values from states and ensure all 3 color's are always submitted in 1 JSON string !
//Val String is [R,G,B] so the first part of array will be '[R' which will result in NAN on parseInt. The '[' has to be cut out of the string before split and parseInt
let color_primary = await this.getStateAsync(color_root + '.0');
if (!color_primary) return;
this.log.debug('Primary color before split : ' + color_primary.val);
try {
color_primary.val = color_primary.val.replace("[","");
color_primary = color_primary.val.split(',').map(s => parseInt(s));
} catch (error) {
if (!color_primary) return;
color_primary = color_primary.val;
}
let color_secondary = await this.getStateAsync(color_root + '.1');
if (!color_secondary) return;
this.log.debug('Secondary color : ' + color_secondary.val);
try {
color_secondary.val = color_secondary.val.replace("[","");
color_secondary = color_secondary.val.split(',').map(s => parseInt(s));
} catch (error) {
if (!color_secondary) return;
color_secondary = color_secondary.val;
}
let color_tertiary = await this.getStateAsync(color_root + '.2');
if (!color_tertiary) return;
this.log.debug('Tertiary color : ' + color_tertiary.val);
try {
color_tertiary.val = color_tertiary.val.replace("[","");
color_tertiary = color_tertiary.val.split(',').map(s => parseInt(s));
} catch (error) {
if (!color_tertiary) return;
color_tertiary = color_tertiary.val;
}
this.log.debug('Color values from states : ' + color_primary + ' : ' + color_secondary + ' : ' + color_tertiary);
// Build proper RGB array in WLED format with all 3 color states
rgb_all = [color_primary, color_secondary, color_tertiary];
} catch (error) {
this.log.error(error);
return;
}
}
// Build JSON string to be send to WLED, cancel function if
values = {
'seg': {
'id': valAsNumbers,
'col': rgb_all
}
};
} else {
// Send state change & value state forward to WLED API
values = {
[deviceId[3]]: {
id: valAsNumbers,
[deviceId[5]]: state.val
}
};
}
this.log.debug('values segment ' + JSON.stringify(values));
}
}
this.log.debug('Prepare API call for device : ' + deviceId[2] + ' and values + ' + values);
let device_ip = await this.getForeignObjectAsync('wled.' + this.instance + '.' + deviceId[2]);
if (!device_ip) return;
device_ip = device_ip.native.ip;
// Only make API call when values are correct
if (values !== null && device_ip !== null) {
// If websocket is connected, send message by websocket otherwise http-API
if (this.devices[device_ip].wsConnected){
this.log.debug(`Sending state change by websocket ${JSON.stringify(values)}`);
ws[device_ip].send(JSON.stringify(values));
} else {
this.log.debug(`Sending state change by http-API ${JSON.stringify(values)}`);
// Send API Post command
const result = await this.postAPI('http://' + device_ip + '/json', values);
if (!result) return;
this.log.debug('API feedback' + JSON.stringify(result));
if (result.success === true) {
// Set state acknowledgement if API call was successfully
this.setState(id, {
val: state.val,
ack: true
});
}
}
}
}
} catch (error) {
this.errorHandler(`[onStateChange]`, error);
}
}
//ToDo: Review
/**
* Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
* Using this method requires "common.message" property to be set to true in io-package.json
* @param {ioBroker.Message} obj
*/
async onMessage(obj) {
// If the message starts with dm: it is a device management message so ignore it
if (obj.command.startsWith('dm:')) return;
try {
// responds to the adapter that sent the original message
const respond = (response, that) => {
if (obj.callback)
that.sendTo(obj.from, obj.command, response, obj.callback);
};
this.log.debug('Data from configuration received : ' + JSON.stringify(obj));
//Response to manual adding of devices & device deletion
switch (obj.command) {
case 'addDevice':
// Disable check of const declaration in case function
// eslint-disable-next-line no-case-declarations
const result = await this.getDeviceJSON(obj.message);
this.log.debug('Response from Read Data : ' + JSON.stringify(result));
if (result === 'success') {
respond('success', this);
} else {
respond('failed', this);
}
break;
}
} catch (error) {
this.errorHandler(`[onMessage]`, error);
}
}
/**
* Websocket connection handler
* @param {string} deviceIP IP-Address of device
*/
async handleWebSocket(deviceIP){
ws[deviceIP] = new WebSocket(`ws://${deviceIP}/ws`);
// Websocket connected, handle routine to initiates all objects and states
ws[deviceIP].on('open', () => {
this.log.info(`${this.devices[deviceIP].name} connected`);
this.devices[deviceIP].wsConnected = true;
this.devices[deviceIP].wsPingSupported = false;
// Request pong to handle watchdog
ws[deviceIP].send('ping');
// Option to subscribe live data
// ws[deviceIP].send('{"lv":true}');
});
// Handle messages received from socket connection
ws[deviceIP].on('message', async (data) => {
this.log.debug(`Websocket WLED message received for ${deviceIP} : ${data}`);
data = data.toString();
try {
if (data !== 'pong'){
data = JSON.parse(data);
if (data.state) await this.handleStates(data, deviceIP); // If message contains state updates, handle state data
} else if (data === 'pong'){
this.log.debug(`Pong received by websocket`);
// Clear pong reset timer
if (watchdogWsTimer[deviceIP]) {
clearTimeout(watchdogWsTimer[deviceIP]);
watchdogTimer[deviceIP] = null;
}
this.devices[deviceIP].wsPingSupported = true;
this.devices[deviceIP].wsPong = true;
} else {
this.log.warn(`Unhandled message received ${data}`);
}
} catch (error) {
let message = error;
if (error instanceof Error && error.stack != null) message = error.stack;
this.log.error(`WebSocket message error for ${deviceIP} | ${message}`);
}
});
// Handle closure of socket connection
ws[deviceIP].on('close', () => {
if (this.devices[deviceIP]?.wsSupported === true){
this.log.info(`${this.devices[deviceIP].name} disconnected`);
this.devices[deviceIP].connected = false;
this.devices[deviceIP].initialized = false;
this.devices[deviceIP].wsConnected = false;
// Set Device status to off and brightness to 0 if devices disconnects
this.setState(`${this.devices[deviceIP].mac}.on`, {val: false, ack: true});
this.setState(`${this.devices[deviceIP].mac}.bri`, {val: 0, ack: true});
}
});
// Handle errors on socket connection
ws[deviceIP].on('error', (error) => {
// Optimise error messages
if (error.message.includes('404')){
this.log.warn(`Client ${deviceIP} does not support websocket, please consider upgrading your WLED firmware to >= 0.12. Switching to http-APi !`);
this.devices[deviceIP].wsSupported = false;
} else {
this.log.error(`Websocket connection error : ${error}`);
}
});
}
/**
* Create basic device structure
* @param {string} deviceIP IP-Address of device
* @param {object} deviceData WLED info & state data
*/
async handleBasicStates(deviceIP, deviceData){
try {
const device_id = deviceData.info.mac;
if (!this.devices[deviceIP]) this.devices[deviceIP] = {};
this.devices[deviceIP].ip = deviceIP;
this.devices[deviceIP].mac = deviceData.info.mac;
this.devices[deviceIP].connected = false;
this.devices[deviceIP].initialized = false;
this.devices[deviceIP].name = deviceData.info.name;
// Create Device, channel id by MAC-Address and ensure relevant information for polling and instance configuration is part of device object
if (!this.devices[deviceIP].initialized){
await this.extendObjectAsync(device_id, {
type: 'device',
common: {
name: deviceData.info.name,
statusStates: {
onlineId: `${this.namespace}.${device_id}._info._online`
}
},
native: {
ip: deviceIP,
mac: device_id,
}
});
}
// Store / Update effects
try {
const effects = deviceData.effects;
// Store effects array
this.effects[device_id] = {};
for (const i in effects) {
this.effects[device_id][i] = effects[i];
}
} catch (e){
this.log.debug(`Cannot create effect dropdown`);
this.errorHandler(`[handleBasicStates]`, `Cannot create effect dropdown`, true);
}
// Store / Update pallets
const palettes = deviceData.palettes;
// Store pallets array
this.palettes[device_id] = {};
for (const i in palettes) {
this.palettes[device_id][i] = palettes[i];
}
// Create additional states not included in JSON-API of WLED but available as SET command
await this.create_state(device_id + '.tt', 'tt', '');
await this.create_state(device_id + '.psave', 'psave', '');
await this.create_state(device_id + '.udpn.nn', 'nn', '');
await this.create_state(device_id + '.time', 'time', null);
await this.create_state(device_id + '.time', 'time', null);
// Create structure for all states
await this.handleStates(deviceData, deviceIP);
} catch (error) {
this.errorHandler(`[handleBasicStates]`, error);
}
}
/**
* Data handler
* @param {object} deviceData WLED info & state data
* @param {string} ipAddress IP Address of device
*/
async handleStates(deviceData,ipAddress ){
try {
const infoStates = deviceData.info;
const deviceStates = deviceData.state;
infoStates.ip = infoStates.ip !== undefined ? infoStates.ip : ipAddress;
if (!this.devices[ipAddress].initialized) {
await this.setObjectNotExistsAsync(infoStates.mac + '._info', {
type: 'channel',
common: {
name: 'Basic information',
},
native: {},
});
}
// Write data to info channel
for (const i in infoStates) {
// Create Info channels
if (!this.devices[ipAddress].initialized) {
// Create Channels for led and wifi configuration
switch (i) {
case ('leds'):
await this.setObjectNotExistsAsync(infoStates.mac + '._info.leds', {
type: 'channel',
common: {
name: 'LED stripe configuration ',
},
native: {},
});
break;
case ('wifi'):
await this.setObjectNotExistsAsync(infoStates.mac + '._info.wifi', {
type: 'channel',
common: {
name: 'Wifi configuration ',
},
native: {},
});
break;
case ('u'):
await this.localDeleteState(infoStates.mac + '._info.u');
break;
default:
}
}
// Create states, ensure object structures are reflected in tree
if (typeof (infoStates[i]) !== 'object') {
// Default channel creation
this.log.debug('State created : ' + i + ' : ' + JSON.stringify(infoStates[i]));
await this.create_state(infoStates.mac + '._info.' + i, i, infoStates[i]);
} else {
for (const y in infoStates[i]) {
this.log.debug(`State created : ${y} : ${JSON.stringify(infoStates[i][y])}`);
await this.create_state(`${infoStates.mac}._info.${i}.${y}`, y, infoStates[i][y]);
}
}
}
// Write data of states
for (const i in deviceStates) {
this.log.debug('Datatype : ' + typeof (deviceStates[i]));
// Create Channels for nested states
switch (i) {
case ('ccnf'):
if (!this.devices[ipAddress].initialized) await this.setObjectNotExistsAsync(infoStates.mac + '.ccnf', {
type: 'channel',
common: {
name: 'ccnf',
},
native: {},
});
break;
case ('nl'):
if (!this.devices[ipAddress].initialized) await this.setObjectNotExistsAsync(infoStates.mac + '.nl', {
type: 'channel',
common: {
name: 'Nightlight',
},
native: {},
});
break;
case ('udpn'):
if (!this.devices[ipAddress].initialized) await this.setObjectNotExistsAsync(infoStates.mac + '.udpn', {
type: 'channel',
common: {
name: 'Broadcast (UDP sync)',
},
native: {},
});
break;
case ('seg'):
this.log.debug('Segment Array : ' + JSON.stringify(deviceStates[i]));
if (!this.devices[ipAddress].initialized) await this.setObjectNotExistsAsync(infoStates.mac + '.seg', {
type: 'channel',
common: {
name: 'Segmentation',
},
native: {},
});
for (const y in deviceStates[i]) {
if (!this.devices[ipAddress].initialized) await this.setObjectNotExistsAsync(infoStates.mac + '.seg.' + y, {
type: 'channel',
common: {
name: 'Segment ' + y,
},
native: {},
});
for (const x in deviceStates[i][y]) {
this.log.debug('Object states created for channel ' + i + ' with parameter : ' + y + ' : ' + JSON.stringify(deviceStates[i][y]));
if (x !== 'col') {
await this.create_state(infoStates.mac + '.' + i + '.' + y + '.' + x, x, deviceStates[i][y][x]);
} else {
this.log.debug('Naming : ' + x + ' with content : ' + JSON.stringify(deviceStates[i][y][x][0]));
// Translate RGB values to HEX
//added additional Alpha channel, necessary if WLED is setup for RGBW Stripes.
//so on normal RGB Stripes Hex has 6 digits on RGBW Stripes Hex as 8 digits. The 2 additional digits for the white channel slider
const primaryRGB = deviceStates[i][y][x][0].toString().split(',');
const primaryHex = rgbHex(parseInt(primaryRGB[0]), parseInt(primaryRGB[1]), parseInt(primaryRGB[2]),isNaN(parseInt(primaryRGB[3]) /255) ? undefined : parseInt(primaryRGB[3]) /255);
const secondaryRGB = deviceStates[i][y][x][1].toString().split(',');
const secondaryHex = rgbHex(parseInt(secondaryRGB[0]), parseInt(secondaryRGB[1]), parseInt(secondaryRGB[2]),isNaN(parseInt(secondaryRGB[3]) /255) ? undefined : parseInt(secondaryRGB[3]) /255);
const tertiaryRGB = deviceStates[i][y][x][2].toString().split(',');
const tertiaryHex = rgbHex(parseInt(tertiaryRGB[0]), parseInt(tertiaryRGB[1]), parseInt(tertiaryRGB[2]), isNaN(parseInt(tertiaryRGB[3]) /255) ? undefined : parseInt(tertiaryRGB[3]) /255);
// Write RGB and HEX information to states
await this.create_state(infoStates.mac + '.' + i + '.' + y + '.' + x + '.0', 'Primary Color RGB', deviceStates[i][y][x][0]);
await this.create_state(infoStates.mac + '.' + i + '.' + y + '.' + x + '.0_HEX', 'Primary Color HEX', '#' + primaryHex);
await this.create_state(infoStates.mac + '.' + i + '.' + y + '.' + x + '.1', 'Secondary Color RGB (background)', deviceStates[i][y][x][1]);
await this.create_state(infoStates.mac + '.' + i + '.' + y + '.' + x + '.1_HEX', 'Secondary Color HEX (background)', '#' + secondaryHex);
await this.create_state(infoStates.mac + '.' + i + '.' + y + '.' + x + '.2', 'Tertiary Color RGB', deviceStates[i][y][x][2]);
await this.create_state(infoStates.mac + '.' + i + '.' + y + '.' + x + '.2_HEX', 'Tertiary Color HEX', '#' + tertiaryHex);
}
}
}
break;
case ('u'):
await this.localDeleteState(infoStates.mac + ' .u');
break;
default:
}
// Create states, ensure object structures are reflected in tree
if (typeof (deviceStates[i]) !== 'object') {
// Default channel creation
this.log.debug('Default state created : ' + i + ' : ' + JSON.stringify(deviceStates[i]));
await this.create_state(infoStates.mac + '.' + i, i, deviceStates[i]);
} else {
for (const y in deviceStates[i]) {
if (typeof (deviceStates[i][y]) !== 'object') {
this.log.debug('Object states created for channel ' + i + ' with parameter : ' + y + ' : ' + JSON.stringify(deviceStates[i][y]));
await this.create_state(infoStates.mac + '.' + i + '.' + y, y, deviceStates[i][y]);
}
}
}
}
if (!this.devices[ipAddress].initialized) {
this.devices[ipAddress].initialized = true;
// Start websocket connection and and listen to state changes
await this.handleWebSocket(ipAddress);
}
// Update device working state
if (!this.devices[ipAddress].connected) this.devices[ipAddress].connected = true;
this.create_state(infoStates.mac + '._info' + '._online', `Online status`, true);
} catch (error) {
this.errorHandler(`[handleStates]`, error);
}
}
/**
* Frequent watchdog to validate connection
* @param {string} deviceIP IP-Address of device
*/
async watchDog(deviceIP) {
// Check if device is in list of devices
if (!this.devices[deviceIP]) return;
try {
this.log.debug('Watchdog for ' + JSON.stringify(this.devices[deviceIP]));
// Check if used WLED version supports websocket Ping-Pong messages and connection, if not handle watchdog by http-API
if (this.devices[deviceIP].wsPingSupported) {
if (this.devices[deviceIP].wsConnected) {
try {
// Send ping by websocket
this.devices[deviceIP].wsPong = false;
ws[deviceIP].send('ping');
if (watchdogWsTimer[deviceIP]) {
clearTimeout(watchdogWsTimer[deviceIP]);
watchdogTimer[deviceIP] = null;
}
watchdogWsTimer[deviceIP] = setTimeout(() => {
if (!this.devices[deviceIP].wsPong) {
this.devices[deviceIP].initialized = false;
this.devices[deviceIP].wsConnected = false;
this.devices[deviceIP].wsConnected = false;
// Update device working state
if (this.devices[deviceIP].mac != null) {
this.create_state(this.devices[deviceIP].mac + '._info' + '._online', 'Online status', false);
}
}
// Close socket
try {
ws[deviceIP].close();
} catch (e) {
console.error(e);
}
}, (this.config.Time_Sync * 1000));
} catch (e) {
// Try http-API in case of error
this.log.error(`WS Ping error : ${e}`);
await this.getDeviceJSON(deviceIP);
}
} else { // If WS-Ping not supported, use http-API
try {
await this.getDeviceJSON(deviceIP);
} catch (error) {
this.errorHandler(`[watchDog]`, error);
}
}
} else { // If WS-Ping not supported, use http-API
try {
await this.getDeviceJSON(deviceIP);
} catch (error) {
this.errorHandler(`[watchDog]`, error);
}
}
} catch (error) {
this.errorHandler(`[handleStates]`, error);
}
// Reset timer (if running) and start new one for next watchdog interval
if (watchdogTimer[deviceIP]) {
clearTimeout(watchdogTimer[deviceIP]);
watchdogTimer[deviceIP] = null;
}
watchdogTimer[deviceIP] = setTimeout(() => {
this.watchDog(deviceIP);
}, (this.config.Time_Sync * 1000));
}
/**
* Request device data by http-API
* @param {string} deviceIP IP-Address of device
*/
async getDeviceJSON(deviceIP) {
try {
this.log.debug(`getDeviceJSON called for : ${deviceIP}`);
try {
const requestDeviceDataByAPI = async () => {
try {
const response = await axios.get(`http://${deviceIP}/json`, {timeout: 3000}); // Timout of 3 seconds for API call
this.log.debug(JSON.stringify('API response data : ' + response.data));
const deviceData = response.data;
return deviceData;
} catch (e) {
this.log.debug(`[requestDeviceDataByAPI] ${e}`)
}
};
// Check if connection is handled by websocket before proceeding
if (this.devices[deviceIP]
&& (this.devices[deviceIP].connected && this.devices[deviceIP].wsConnected && this.devices[deviceIP].wsPingSupported)) {
// Nothing to do, device is connected by websocket and will handle state updates
} else { // No Websocket connection, handle data by http_API
const deviceData = await requestDeviceDataByAPI();
// If device is initialised, only handle state updates otherwise complete initialisation
if (this.devices[deviceIP]
&& (this.devices[deviceIP].connected && this.devices[deviceIP].initialized)){
if (!deviceData) {
this.log.warn(`Heartbeat of device ${deviceIP} failed, will try to reconnect`);
this.devices[deviceIP].connected = false;
this.devices[deviceIP].initialized = false;
this.devices[deviceIP].wsConnected = false;
await this.create_state(this.devices[deviceIP].mac + '._info' + '._online', 'Online status', false);
return 'failed';
} else {
this.log.debug(`Heartbeat of device ${deviceIP} successfully`);
if (this.devices[deviceIP].connected && this.devices[deviceIP].wsConnected) {
// Only reset heartbeat, device is connected by websocket and will handle state updates
this.setStateChanged(this.devices[deviceIP].mac + '._info' + '._online', {val: true, ack: true});
return 'success';
} else {
await this.handleStates(deviceData, deviceIP);
this.devices[deviceIP].wsConnected = false;
this.setStateChanged(this.devices[deviceIP].mac + '._info' + '._online', {val: true, ack: true});
return 'success';
}
}
} else {
if (!deviceData) {
this.log.warn(`Unable to initialise ${deviceIP} will retry in scheduled interval !`);
this.devices[deviceIP].initialized = false;
// Update device working state
if (this.devices[deviceIP].mac != null) {
await this.create_state(this.devices[deviceIP].mac + '._info' + '._online', 'Online status', {val: false, ack: true});
}
return 'failed';
} else {
this.log.debug('Info Data received from WLED device ' + JSON.stringify(deviceData));
this.log.info(`Initialising : " ${deviceData.info.name}" on IP : ${deviceIP}`);
await this.handleBasicStates(deviceIP, deviceData);
return 'success';
}
}
}
} catch (error) {
if (this.devices[deviceIP] && this.devices[deviceIP].connected){
this.log.warn(`Device ${deviceIP} offline, will try to reconnect`);
if (this.devices[deviceIP].mac != null) {
await this.create_state(this.devices[deviceIP].mac + '._info' + '._online', 'Online status', false);
// Set Device status to off and brightness to 0 if devices disconnects
this.setState(`${this.devices[deviceIP].mac}.on`, {val: false, ack: true});
this.setState(`${this.devices[deviceIP].mac}.bri`, {val: 0, ack: true});
}
this.devices[deviceIP].connected = false;
this.devices[deviceIP].wsConnected = false;
this.devices[deviceIP].initialized = false;
try {
ws[deviceIP].close();
} catch (e) {
console.error(e);
}
}
return 'failed';
}
} catch (error) {
this.errorHandler(`[getDeviceJSON]`, error);
}
}
/**
* API post call to WLED device
* @param {string} url API url
* @param {object} values JSON data to send
*/
async postAPI(url, values) {
this.log.debug('Post API called for : ' + url + ' and values : ' + JSON.stringify(values));
try {
const result = axios.post(url, values)
.then((response) => {
return response.data;
})
.catch((error) => {
this.log.error('Sending command to WLED device + ' + url + ' failed with error ' + error);
return error;
});
return result;
} catch (error) {
this.errorHandler(`[postAPI]`, error);
}
}
/**
* Try to contact to contact and read data of already known devices
*/
async tryKnownDevices() {
try {
const knownDevices = await this.getDevicesAsync();
if (!knownDevices) return; // exit function if no known device are detected
if (knownDevices.length > 0) this.log.info(`Try to contact ${knownDevices.length} known devices`);
// Get IP-Address of known devices and try too establish connection
for (const i in knownDevices) {
const deviceIP = knownDevices[i].native.ip;
this.log.info('Try to contact : "' + knownDevices[i].common.name + '" on IP : ' + deviceIP);
// Add IP address to polling array
this.devices[deviceIP] = {};
this.devices[deviceIP].ip = knownDevices[i].native.ip;
this.devices[deviceIP].mac = knownDevices[i].native.mac;
this.devices[deviceIP].connected = false;
this.devices[deviceIP].initialized = false;
await this.getDeviceJSON(deviceIP);
}
}catch (error) {
this.errorHandler(`[tryKnownDevices]`, error);
}
}
/**
* Scan network with Bonjour service to detect new WLED devices
*/
async scanDevices() {
try {
// Browse and listen for WLED devices
const browser = await bonjour.find({
'type': 'wled'
});
this.log.info('Bonjour service started, new devices will be detected automatically');
// Event listener if new devices are detected
browser.on('up', (data) => {
const id = data.txt.mac;
const ip = data.referer.address;
// Check if device is already know
if (this.devices[ip] == null) {
this.log.info('New WLED device found ' + data.name + ' on IP ' + data.referer.address);