-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmilight.js
683 lines (649 loc) · 31.5 KB
/
milight.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
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
var utils = require('@iobroker/adapter-core'); // Get common adapter utils
var stateCommands = require(__dirname + '/lib/commands');
var light = null;
var zones = [];
var commands;
var nameStates = {
v6 :{
basic: ['state', 'on', 'off', 'whiteMode', 'brightnessUp', 'brightnessDown', 'brightness', 'colorUp', 'colorDown', 'color', 'rgb', 'mode'],
White: ['state', 'on', 'off', 'maxBright', 'brightnessUp', 'nightMode', 'brightnessDown', 'warmer', 'cooler', 'nightModeSwitch'],
RGBO: ['state', 'on', 'off', 'brightnessUp', 'brightnessDown', 'colorUp', 'colorDown', 'color', 'rgb','modeSpeedUp', 'modeSpeedDown', 'effectModeNext', 'effectModePrev'],
RGBW: ['state', 'on', 'off', 'colorMode', 'whiteMode', 'nightMode', 'brightnessUp', 'brightnessDown', 'brightness', 'colorUp', 'colorDown', 'color', 'rgb', 'hue', 'mode', 'modeSpeedUp', 'modeSpeedDown', 'link', 'unlink'],
RGBWW: ['state', 'on', 'off', 'colorMode', 'whiteMode', 'nightMode', 'brightnessUp', 'brightnessDown', 'brightness', 'colorUp', 'colorDown', 'color', 'rgb', 'hue', 'mode', 'modeSpeedUp', 'modeSpeedDown', 'link', 'unlink', 'saturationUp', 'saturationDown', 'saturation', 'colorTempUp', 'colorTempDown', 'colorTemp']
},
v5 :{
basic: ['state', 'on', 'off', 'hue', 'rgb', 'whiteMode', 'brightness', 'brightness2', 'effectModeNext', 'effectSpeedUp', 'effectSpeedDown'],
RGBO: ['state', 'on', 'off', 'brightUp', 'brightDown', 'speedUp', 'speedDown', 'effectSpeedUp', 'effectSpeedDown'],
White: ['state', 'on', 'off', 'maxBright', 'brightUp', 'brightDown', 'warmer', 'cooler'],
RGBW: ['state', 'on', 'off', 'colorMode', 'hue', 'rgb', 'whiteMode', 'nightMode', 'brightness', 'brightness2', 'effectModeNext', 'effectSpeedUp', 'effectSpeedDown']
}
};
function rgbToHsv(r, g, b) {
r /= 0xFF;
g /= 0xFF;
b /= 0xFF;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, v = max;
var d = max - min;
s = max == 0 ? 0 : d / max;
if (max == min) {
h = 0;
}
else {
switch (max) {
case r:
h = ((g - b) / d + (g < b ? 6 : 0)); break;
case g:
h = ((b - r) / d + 2); break;
case b:
h = ((r - g) / d + 4); break;
}
h = Math.round(h * 60);
s = Math.round(s * 100);
v = Math.round(v * 100);
}
return [h, s, v];
}
function hsvToRgb(h, s, v) {
/**
* HSV to RGB color conversion
*
* H runs from 0 to 360 degrees
* S and V run from 0 to 100
*
* Ported from the excellent java algorithm by Eugene Vishnevsky at:
* http://www.cs.rit.edu/~ncs/color/t_convert.html
*/
var r, g, b;
var i;
var f, p, q, t;
// Make sure our arguments stay in-range
h = Math.max(0, Math.min(360, h));
s = Math.max(0, Math.min(100, s));
v = Math.max(0, Math.min(100, v));
// We accept saturation and value arguments from 0 to 100
s /= 100;
v /= 100;
if (s == 0) {
// Achromatic (grey)
r = g = b = v;
var hex = Math.round(b * 255) | (Math.round(g * 255) << 8) | (Math.round(r * 255) << 16);
return '#' + (0x1000000 + hex).toString(16).slice(1);
}
h /= 60; // sector 0 to 5
i = Math.floor(h);
f = h - i; // factorial part of h
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch (i) {
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default:
// case 5:
r = v;
g = p;
b = q;
}
var hex = Math.round(b * 255) | (Math.round(g * 255) << 8) | (Math.round(r * 255) << 16);
return '#' + (0x1000000 + hex).toString(16).slice(1);
}
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: 'milight',
unload: function (cb) {
if (light) {
light.close();
light = null;
}
if (typeof cb === 'function') cb();
},
message: function (obj) {
var wait = false;
if (obj) {
switch (obj.command) {
case 'browse':
var discoverBridges = require('node-milight-promise').discoverBridges;
adapter.log.info('Discover bridges...');
discoverBridges({
type: 'all'
}).then(function (results) {
adapter.log.info('Discover bridges: ' + JSON.stringify(results));
if (obj.callback) adapter.sendTo(obj.from, obj.command, results, obj.callback);
});
wait = true;
break;
default:
adapter.log.warn('Unknown command: ' + obj.command);
break;
}
}
if (!wait && obj.callback) {
adapter.sendTo(obj.from, obj.command, obj.message, obj.callback);
}
return true;
},
stateChange: function (id, state) {
if (state && !state.ack && light) {
var tmp = id.split('.');
var dp = tmp.pop();
var strZone = tmp.slice(2).join('.'); //ZoneX
var zone;
switch (strZone) {
case 'zone1':
zone = 1;
break;
case 'zone2':
zone = 2;
break;
case 'zone3':
zone = 3;
break;
case 'zone4':
zone = 4;
break;
case 'zoneAll':
default:
zone = 0;
break;
}
if (dp === 'rgb') dp = 'colorRGB';
if (dp === 'color') dp = 'colorMode'; //colorSet nowhere else used
if (dp === 'saturation') dp = 'saturationSet';
if (dp === 'colorTemp') dp = 'colorTempSet';
if (adapter.config.version === '6') {
if (dp === 'brightness') dp = 'brightnessSet';
if (zones[zone]) {
if (dp === 'hue'){
var colorhex = hsvToRgb(state.val,80,100);
var val = splitColor(colorhex);
adapter.log.debug('Send to zone ' + zone + ' "' + dp + '": ' + JSON.stringify(val) + " (which is "+colorhex+" out of hue"+state.val+ ")");
zones[zone].command('colorRGB', val, function (err) {
if (!err) {
adapter.setForeignState(id, state.val, true);
// adapter.setForeignState(id.replace('.hue','.rgb'), colorhex, true); //Nachführung von rgb
} else {
adapter.log.error('V6 Cannot control: ' + err);
}
});
} else
if (dp === 'colorMode') {
if (state.val === 'true' || state.val === true || state.val === 1 || state.val === 'on' || state.val === 'ON') {
adapter.log.debug('Send to zone ' + zone + ' whiteMode');
zones[zone].command('whiteMode', function (err) {
if (!err) {
adapter.setForeignState(id, true, true);
} else {
adapter.log.error('V6 Cannot control: ' + err);
}
});
} else {
adapter.log.debug('Send to zone ' + zone + ' nightMode');
zones[zone].command('nightMode', function (err) {
if (!err) {
adapter.setForeignState(id, false, true);
} else {
adapter.log.error('V6 Cannot control: ' + err);
}
});
}
} else
if (dp === 'state') {
if (state.val === 'true' || state.val === true || state.val === 1 || state.val === 'on' || state.val === 'ON') {
adapter.log.debug('Send to zone ' + zone + ' ON');
zones[zone].command('on', function (err) {
if (!err) {
adapter.setForeignState(id, true, true);
} else {
adapter.log.error('V6 Cannot control: ' + err);
}
});
} else {
adapter.log.debug('Send to zone ' + zone + ' OFF');
zones[zone].command('off', function (err) {
if (!err) {
adapter.setForeignState(id, false, true);
} else {
adapter.log.error('V6 Cannot control: ' + err);
}
});
}
} else
if (dp === 'nightModeSwitch') {
if (state.val === 'true' || state.val === true || state.val === 1 || state.val === 'on' || state.val === 'ON') {
adapter.log.debug('Send to zone ' + zone + ' nightMode');
zones[zone].command('nightMode', function (err) {
if (!err) {
adapter.setForeignState(id, false, true); //automatisches zurücksetzen für den nächsten Alexa Befehl
} else {
adapter.log.error('V6 Cannot control: ' + err);
}
});
}
} else
if (typeof zones[zone][dp] === 'function') {
var val;
if (dp === 'colorRGB') {
val = splitColor(state.val);
adapter.log.debug('Send to zone ' + zone + ' "' + dp + '": ' + JSON.stringify(val));
} else if (dp === 'brightnessSet') {
val = Math.round(parseFloat(state.val)); //from 0x00 to 0x64
if (val < 0) val = 0;
if (val > 100) val = 100;
adapter.log.debug('V6 Send to zone ' + zone + ' "' + dp + '": ' + val);
} else {
val = parseInt(state.val, 10);
adapter.log.debug('V6 Send to zone ' + zone + ' "' + dp + '": ' + val);
}
zones[zone].command(dp, val, function (err) {
if (!err) {
adapter.setForeignState(id, state.val, true);
if (dp === 'on'){
adapter.setForeignState(id, false, true); //Taste auf 0 setzen
adapter.setForeignState(id.replace('.on','.state'), true, true); //Nachführung von state
}
if (dp === 'off'){
adapter.setForeignState(id, false, true); //Taste auf 0 setzen
adapter.setForeignState(id.replace('.off','.state'), false, true); //Nachführung von state
}
if (dp === 'colorRGB'){
var h = rgbToHsv(val[0],val[1],val[2]);
// adapter.setForeignState(id.replace('.rgb','.hue'), h[0], true); //Nachführung von hue
}
} else {
adapter.log.error('V6 Cannot control: ' + err);
}
});
} else {
adapter.log.error('V6 Unknown command: ' + dp);
}
} else {
adapter.log.error('V6 Zone is disabled');
}
} else {
// version 5
if (dp === 'colorMode') {
if (state.val === 'hs' || state.val === 'true' || state.val === true || state.val === 1 || state.val === 'on' || state.val === 'ON') {
adapter.log.debug('V5 Send to zone ' + zone + ' color Mode with hue=55');
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], 'hue')) return;
light.sendCommands(zones[zone].on(zone), zones[zone].hue(55)).then(function () {
adapter.setForeignState(id, state.val, true);
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
} else {
adapter.log.debug('V5 Send to zone ' + zone + ' white Mode via colorMode');
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], 'whiteMode')) return;
light.sendCommands(zones[zone].on(zone), zones[zone].whiteMode(zone)).then(function () {
adapter.setForeignState(id, state.val, true);
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
}
} else
if (dp === 'state') {
if (state.val === 'true' || state.val === true || state.val === 1 || state.val === 'on' || state.val === 'ON') {
adapter.log.debug('V5 Send to zone ' + zone + ' ON');
if (adapter.config.v5onFullBright === 'true' || adapter.config.v5onFullBright === true || adapter.config.v5onFullBright === 'on' || adapter.config.v5onFullBright === 'ON' || adapter.config.v5onFullBright === 1){
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], 'brightness')) return;
light.sendCommands(zones[zone].on(zone), zones[zone].brightness(100), zones[zone].whiteMode(zone)).then(function () {
adapter.setForeignState(id, true, true);
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
}
else {
if (!checkMethod(zones[zone], 'on')) return;
light.sendCommands(zones[zone].on(zone)).then(function () {
adapter.setForeignState(id, true, true);
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
}
} else {
adapter.log.debug('V5 Send to zone ' + zone + ' OFF');
if (!checkMethod(zones[zone], 'off')) return;
light.sendCommands(zones[zone].off(zone)).then(function () {
adapter.setForeignState(id, false, true);
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
}
} else
if (dp === 'brightness2' || dp === 'brightness') { //now 2 variants of brightness can be used in v5
var val = state.val;
if (state.val < 0) val = 0;
if (state.val > 100) val = 100;
adapter.log.debug('V5 brightness Send to zone ' + zone + ' "' + dp + '": ' + val);
if (state.val !== 0) { //if dim to 0% was chosen turn light off - error handling for iobroker.cloud combo with amazon alexa
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], dp)) return;
light.sendCommands(zones[zone].on(zone), zones[zone][dp](val)).then(function () {
adapter.setForeignState(id, state.val, true);
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
}
else { // bei 0 wird ausgeschaltet
if (!checkMethod(zones[zone], 'off')) return;
light.sendCommands(zones[zone].off(zone)).then(function () {
adapter.setForeignState(id, state.val, true);
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
}
} else
if (dp === 'hue') {
var val = state.val;
if (state.val < 0) val = 0;
if (state.val > 255) val = 255;
var colorhex = hsvToRgb(val,80,100);
adapter.log.debug('V5 Send to zone ' + zone + ' "' + dp + '": ' + val);
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], 'hue')) return;
light.sendCommands(zones[zone].on(zone), zones[zone].hue(val)).then(function () {
adapter.setForeignState(id, val, true);
// adapter.setForeignState(id.replace('.hue','.rgb'), colorhex, true); //Nachführung von rgb
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
} else
if (dp === 'colorRGB'){
var val;
dp = 'rgb';
val = splitColor(state.val);
adapter.log.debug('V5 Send to zone ' + zone + ' "' + dp + '": ' + val);
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], 'rgb255')) return;
var rbgAsHue = zones[zone].rgb255(val[0], val[1], val[2])
light.sendCommands(zones[zone].on(zone), rbgAsHue).then(function () {
// adapter.setForeignState(id, state.val, true); // causes infinite loop but it doesn't matter as we either set it, or not
// var h = rgbToHsv(val[0],val[1],val[2]);
// adapter.setForeignState(id.replace('.rgb','.hue'), h[0], true); //Nachführung von hue
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
} else
if (dp === 'on'){
adapter.log.debug('V5 Send to zone ' + zone + ' on');
if (adapter.config.v5onFullBright === 'true' || adapter.config.v5onFullBright === true || adapter.config.v5onFullBright === 'on' || adapter.config.v5onFullBright === 'ON' || adapter.config.v5onFullBright === 1){
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], 'brightness')) return;
light.sendCommands(zones[zone].on(zone), zones[zone].brightness(100), zones[zone].whiteMode(zone)).then(function () {
adapter.setForeignState(id, false, true); //tastendruck rückgängig machen
adapter.setForeignState(id.replace('.on','.state'), true, true); //Nachführung von state
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
} else {
if (!checkMethod(zones[zone], 'on')) return;
light.sendCommands(zones[zone].on(zone)).then(function () {
adapter.setForeignState(id, false, true); //tastendruck rückgängig machen
adapter.setForeignState(id.replace('.on','.state'), true, true); //Nachführung von state
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
}
} else
if (dp === 'off'){
adapter.log.debug('V5 Send to zone ' + zone + ' off');
if (!checkMethod(zones[zone], 'off')) return;
light.sendCommands(zones[zone].off(zone)).then(function () {
adapter.setForeignState(id, false, true); //tastendruck rückgängig machen
adapter.setForeignState(id.replace('off','state'), false, true); //Nachführung von state
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
} else
if (dp === 'nightMode' || dp === 'whiteMode' ){
adapter.log.debug('V5 Send to zone ' + zone + ' Mode on');
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], dp)) return;
light.sendCommands(zones[zone].on(zone), zones[zone][dp](zone)).then(function () {
adapter.setForeignState(id, true, true); //status behalten?
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
} else
if ( dp === 'maxBright' || dp === 'brightUp' || dp === 'brightDown' || dp === 'speedUp' || dp === 'speedDown' || dp === 'effectSpeedUp' || dp === 'effectSpeedDown' || dp ==='effectModeNext' || dp ==='cooler'|| dp ==='warmer') {
adapter.log.debug('V5 Send to zone ' + zone + ' "' + dp + '": ' + state.val);
if (!checkMethod(zones[zone], 'on') || !checkMethod(zones[zone], dp)) return;
light.sendCommands(zones[zone].on(zone), zones[zone][dp]()).then(function () {
adapter.setForeignState(id, false, true); //tastendruck rückgängig machen
}, function (err) {
adapter.log.error('Cannot control: ' + err);
});
} else {
adapter.log.error('Unknown command: ' + dp);
}
}
}
},
ready: function (){
main()
}
});
adapter = new utils.Adapter(options);
return adapter;
};
function checkMethod(zone, funcName) {
if (zone && typeof zone === 'object' && typeof zone[funcName] === 'function') {
return true;
} else {
var keys = [];
if (typeof zone === 'object') {
for (var name in zone) {
if (zone.hasOwnProperty(name) && typeof zone[name] === 'function') {
keys.push(name);
}
}
}
adapter.log.warn('Property "' + funcName + '" does not exist. Please use on of ' + keys.join(', '));
return false;
}
}
function splitColor(rgb) {
if (!rgb) rgb = '#000000';
rgb = rgb.toString().toUpperCase();
if (rgb[0] === '#') rgb = rgb.substring(1);
if (rgb.length < 6) rgb = rgb[0] + rgb[0] + rgb[1] + rgb[1] + rgb[2] + rgb[2];
var r = parseInt(rgb[0] + rgb[1], 16);
var g = parseInt(rgb[2] + rgb[3], 16);
var b = parseInt(rgb[4] + rgb[5], 16);
if (rgb.length >= 8) {
return [r, g, b, parseInt(rgb[6] + rgb[7], 16)];
} else {
return [r, g, b];
}
}
function mergeObject(obj, cb) {
adapter.getForeignObject(obj._id, function (err, _obj) {
if (_obj) {
var changed = false;
for (var attr in obj) {
if (!obj.hasOwnProperty(attr)) continue;
if (typeof obj[attr] === 'object') {
for (var _attr in obj[attr]) {
if (obj[attr].hasOwnProperty(_attr) && (!_obj[attr] || _obj[attr][_attr] !== obj[attr][_attr])) {
_obj[attr] = _obj[attr] || {};
_obj[attr][_attr] = obj[attr][_attr];
changed = true;
}
}
} else {
if (obj[attr] !== _obj[attr]) {
_obj[attr] = _obj[attr];
changed = true;
}
}
}
if (changed) {
adapter.setForeignObject(obj._id, _obj, function () {
cb && cb();
});
} else {
cb && cb();
}
} else {
adapter.setForeignObject(obj._id, obj, function () {
cb && cb();
});
}
});
}
function mergeObjects(objs, cb) {
if (!objs || !objs.length) {
if (typeof cb === 'function') {
cb();
}
return;
}
mergeObject(objs.shift(), function () {
setTimeout(mergeObjects, 0, objs, cb);
});
}
function main() {
adapter.config.commandRepeat = parseInt(adapter.config.commandRepeat, 10) || 2;
if (!adapter.config.ip) {
adapter.log.warn('No IP address defined');
return;
}
if (adapter.config.version === '6') {
adapter.setState('info.connection', false, true);
light = new require(__dirname + '/lib/bridge.js')({
ip: adapter.config.ip,
port: parseInt(adapter.config.port, 10) || 5987,
reconnectTimeout: 10000,
disconnectTimeout: 10000,
keepAliveTimeout: 10000,
delayBetweenCommands: 50,
commandRepeat: adapter.config.commandRepeat,
debug: true,
log: {
log: function (text) {
adapter.log.debug(text);
},
error: function (text) {
adapter.log.error(text);
}
}
});
light.on('connected', function () {
adapter.setState('info.connection', true, true);
});
light.on('disconnected', function () {
adapter.setState('info.connection', false, true);
});
zones[0] = light.baseCtlFactory();
} else {
adapter.setState('info.connection', true, true);
var Milight = require('node-milight-promise').MilightController;
commands = require('node-milight-promise').commands2;
light = new Milight({
ip: adapter.config.ip,
delayBetweenCommands: adapter.delayBetweenCommands,
commandRepeat: adapter.config.commandRepeat
});
}
var objs = [];
var nameStatesV = nameStates['v' + adapter.config.version];
for (var n = 0; n < nameStatesV.basic.length; n++) {
if (!stateCommands[nameStatesV.basic[n]]) {
adapter.log.error('Unknown command: ' + nameStatesV.basic[n]);
continue;
}
var _obj = JSON.parse(JSON.stringify(stateCommands[nameStatesV.basic[n]]));
if (!_obj) {
adapter.log.error('Unknown state: ' + nameStatesV.basic[n]);
continue;
}
_obj.common.name = 'All Zones ' + _obj.common.name;
_obj._id = adapter.namespace + '.zoneAll.' + nameStatesV.basic[n];
objs.push(_obj);
}
if (adapter.config.version === '6') {
zones[0] = light.baseCtlFactory();
} else {
zones[0] = commands.rgbw;
}
for (var z = 1; z <= 4; z++) {
var type = adapter.config['zone' + z];
var names = nameStatesV[type];
if (names) {
if (adapter.config.version === '6') {
if (type === 'basic') {
zones[z] = light.baseCtlFactory();
} else
if (type === 'White') {
zones[z] = light.zoneCtlWhiteFactory(z);
} else
if (type === 'RGBO') {
zones[z] = light.zoneCtlRGBFactory(z);
} else
if (type === 'RGBW') {
zones[z] = light.zoneCtlRGBWFactory(z);
} else
if (type === 'RGBWW') {
zones[z] = light.zoneCtlRGBWWFactory(z);
}
} else {
if (type === 'RGBO') {
zones[z] = commands.rgb;
} else
if (type === 'RGBW') {
zones[z] = commands.rgbw;
} else
if (type === 'White') {
zones[z] = commands.white;
}
}
for (var s = 0; s < names.length; s++) {
if (!stateCommands[names[s]]) {
adapter.log.error('State ' + names[s] + ' unknown');
continue;
}
var obj = JSON.parse(JSON.stringify(stateCommands[names[s]]));
if (!obj) {
adapter.log.error('Unknown state: ' + names[s]);
continue;
}
obj.common.name = 'Zone ' + z + ' ' + obj.common.name;
obj._id = adapter.namespace + '.zone' + z + '.' + names[s];
objs.push(obj);
}
}
}
mergeObjects(objs, function () {
adapter.subscribeStates('*');
});
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}