-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
hue-light.js
741 lines (658 loc) · 23.8 KB
/
hue-light.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
module.exports = function(RED)
{
"use strict";
function HueLight(config)
{
RED.nodes.createNode(this, config);
const scope = this;
const bridge = RED.nodes.getNode(config.bridge);
// SAVE FUTURE PATCH
this.futurePatchState = {};
// SAVE LAST COMMAND
this.lastCommand = null;
// HELPER
const colorUtils = require('./utils/color');
const merge = require('./utils/merge');
//
// CHECK CONFIG
if(bridge == null)
{
this.status({fill: "red", shape: "ring", text: "hue-light.node.not-configured"});
return false;
}
//
// UNIVERSAL MODE?
if(!config.lightid)
{
this.status({fill: "grey", shape: "dot", text: "hue-light.node.universal"});
}
//
// UPDATE STATE
if(typeof bridge.disableupdates != 'undefined' || bridge.disableupdates == false)
{
this.status({fill: "grey", shape: "dot", text: "hue-light.node.init"});
}
//
// SUBSCRIBE TO UPDATES FROM THE BRIDGE
bridge.subscribe("light", config.lightid, function(info)
{
let currentState = bridge.get("light", info.id, { colornames: config.colornamer ? true : false });
// RESOURCE FOUND?
if(currentState !== false)
{
// SEND MESSAGE
if(!config.skipevents && (config.initevents || info.suppressMessage == false))
{
// SET LAST COMMAND
if(scope.lastCommand !== null)
{
currentState.command = scope.lastCommand;
}
// SEND STATE
scope.send(currentState);
// RESET LAST COMMAND
scope.lastCommand = null;
}
// NOT IN UNIVERAL MODE? -> CHANGE UI STATES
if(config.lightid)
{
if(currentState.payload.reachable === true)
{
if(currentState.payload.on === true)
{
// APPLY FUTURE STATE COMMANDS
if(Object.values(scope.futurePatchState).length > 0)
{
scope.applyCommands({}, null, null);
}
if(currentState.payload.brightness !== false)
{
scope.status({fill: "yellow", shape: "dot", text: RED._("hue-light.node.turned-on-percent",{ percent: currentState.payload.brightness })});
}
else
{
scope.status({fill: "yellow", shape: "dot", text: "hue-light.node.turned-on"});
}
}
else
{
scope.status({fill: "grey", shape: "dot", text: "hue-light.node.turned-off"});
}
}
else
{
var offNotReachableStatus = RED._("hue-light.node.turned-off") + " (" + RED._("hue-light.node.not-reachable") + ")";
scope.status({fill: "red", shape: "ring", text: offNotReachableStatus});
}
}
}
});
//
// CONTROL LIGHT
this.on('input', function(msg, send, done) { scope.applyCommands(msg, send, done); });
//
// APPLY COMMANDS
this.applyCommands = async function(msg, send = null, done = null)
{
// REDEFINE SEND AND DONE IF NOT AVAILABLE
send = send || function() { scope.send.apply(scope,arguments); }
done = done || function() { scope.done.apply(scope,arguments); }
// SAVE LAST COMMAND
scope.lastCommand = RED.util.cloneMessage(msg);
// CREATE PATCH
let patchObject = {};
// DEFINE SENSOR ID & CURRENT STATE
const tempLightID = (!config.lightid && typeof msg.topic != 'undefined' && bridge.validResourceID.test(msg.topic) === true) ? msg.topic : config.lightid;
let currentState = bridge.get("light", tempLightID, { colornames: config.colornamer ? true : false });
// CHECK IF LIGHT ID IS SET
if(!tempLightID)
{
scope.error(RED._("hue-light.node.error-no-id"));
return false;
}
// GET CURRENT STATE
if( (typeof msg.payload != 'undefined' && typeof msg.payload.status != 'undefined') || (typeof msg.__user_inject_props__ != 'undefined' && msg.__user_inject_props__ == "status") )
{
// SET LAST COMMAND
if(scope.lastCommand !== null)
{
currentState.command = scope.lastCommand;
}
// SEND STATE
scope.send(currentState);
// RESET LAST COMMAND
scope.lastCommand = null;
if(done) { done(); }
return true;
}
// GET FUTURE STATE
if(Object.values(scope.futurePatchState).length > 0)
{
patchObject = Object.assign({}, scope.futurePatchState);
scope.futurePatchState = {};
}
// COLORLOOP EFFECT
if(typeof msg.payload != 'undefined' && typeof msg.payload.colorloop != 'undefined' && msg.payload.colorloop > 0)
{
patchObject = {
"on": true,
"effect": "colorloop",
"bri": msg.payload.brightness ? Math.round((254/100)*msg.payload.brightness) : currentState.brightnessLevel
};
bridge.patch("light", currentState.info.idV1 + "/state", patchObject, 1)
.then(function(status) {
// RESET COLORLOOP ANIMATION AFTER X SECONDS
setTimeout(function()
{
bridge.patch("light", currentState.info.idV1 + "/state", { "effect": "none" }, 1);
if(done) { done(); }
}, parseInt(msg.payload.colorloop) * 1000);
})
.catch(function(errors) {
scope.error(errors);
scope.status({fill: "red", shape: "ring", text: "hue-light.node.error-input"});
});
return false;
}
// ALERT EFFECT
if(typeof msg.payload != 'undefined' && typeof msg.payload.alert != 'undefined' && msg.payload.alert > 0)
{
// SAVE PREVIOUS STATE
scope.context().set('lightPreviousState', currentState);
// TURN ON LIGHT
if(currentState.payload.on === false)
{
patchObject["on"] = { on: true };
}
// SET BRIGHTNESS
if(!msg.payload.brightness && currentState.payload.brightness != 100)
{
patchObject["dimming"] = { brightness: 100 };
}
else if(msg.payload.brightness)
{
patchObject["dimming"] = { brightness: parseInt(msg.payload.brightness) };
}
// SET TRANSITION
patchObject["dynamics"] = { duration: 0 };
// CAN CHANGE COLOR?
if(currentState.payload.xyColor)
{
let XYAlertColor = {};
if(typeof msg.payload.rgb != 'undefined')
{
XYAlertColor = colorUtils.rgbToXy(msg.payload.rgb[0], msg.payload.rgb[1], msg.payload.rgb[2], currentState.info.model.colorGamut);
}
else if(typeof msg.payload.hex != 'undefined')
{
let rgbFromHex = colorUtils.hexRgb((msg.payload.hex).toString());
XYAlertColor = colorUtils.rgbToXy(rgbFromHex[0], rgbFromHex[1], rgbFromHex[2], currentState.info.model.colorGamut);
}
else if(typeof msg.payload.color != 'undefined')
{
if(new RegExp("random|any|whatever").test(msg.payload.color))
{
const randomColor = colorUtils.randomHexColor();
let rgbFromHex = colorUtils.hexRgb(rgbFromHex);
XYAlertColor = colorUtils.rgbToXy(rgbFromHex[0], rgbFromHex[1], rgbFromHex[2], currentState.info.model.colorGamut);
}
else
{
var colorHex = colorUtils.colornames(msg.payload.color);
if(colorHex)
{
let rgbFromHex = colorUtils.hexRgb(colorHex);
XYAlertColor = colorUtils.rgbToXy(rgbFromHex[0], rgbFromHex[1], rgbFromHex[2], currentState.info.model.colorGamut);
}
}
}
else
{
XYAlertColor = colorUtils.rgbToXy(255, 0, 0, currentState.info.model.colorGamut);
}
patchObject["color"] = {
xy: XYAlertColor
};
}
// CHANGE NODE UI STATE
if(config.lightid)
{
scope.status({fill: "grey", shape: "ring", text: "hue-light.node.command"});
}
// 1. TURN ON THE LIGHT BULB
bridge.patch("light", tempLightID, patchObject)
.then(function(status) {
// 2. APPLY ALERT EFFECT
const alertEffect = { alert: { action: "breathe" }};
return bridge.patch("light", tempLightID, alertEffect);
})
.then(function(status) {
// 3. RESET PREVIOUS STATE (AFTER X SECONDS)
setTimeout(function()
{
const tempPreviousState = scope.context().get('lightPreviousState');
var tempPreviousStatePatch = {};
tempPreviousStatePatch.dimming = { brightness: tempPreviousState.payload.brightness };
if(tempPreviousState.payload.xyColor)
{
tempPreviousStatePatch.color = { xy: tempPreviousState.payload.xyColor };
}
else if(tempPreviousState.payload.colorTemp)
{
tempPreviousStatePatch.color_temperature = { mirek: tempPreviousState.payload.colorTemp };
}
bridge.patch("light", tempLightID, tempPreviousStatePatch).
then(function(status)
{
return bridge.patch("light", tempLightID, { on: { on: false } })
.then(function() { if(done) { done(); }});
})
.then(function(status) {
if(tempPreviousState.payload.on === true)
{
bridge.patch("light", tempLightID, { on: { on: true } })
.then(function() { if(done) { done(); }});
}
});
}, parseInt(msg.payload.alert) * 1000);
})
.catch(function(errors) {
scope.error(errors);
scope.status({fill: "red", shape: "ring", text: "hue-light.node.error-input"});
});
}
// ANIMATION STARTED?
else if(typeof msg.animation != 'undefined' && msg.animation.status == true && msg.animation.restore == true)
{
// SAVE PREVIOUS STATE
scope.context().set('lightPreviousState', currentState);
}
// ANIMATION STOPPED AND RESTORE ACTIVE?
else if(typeof msg.animation != 'undefined' && msg.animation.status == false && msg.animation.restore == true)
{
const tempPreviousState = scope.context().get('lightPreviousState');
var tempPreviousStatePatch = {};
tempPreviousStatePatch.dimming = { brightness: tempPreviousState.payload.brightness };
if(tempPreviousState.payload.xyColor)
{
tempPreviousStatePatch.color = { xy: tempPreviousState.payload.xyColor };
}
else if(tempPreviousState.payload.colorTemp)
{
tempPreviousStatePatch.color_temperature = { mirek: tempPreviousState.payload.colorTemp };
}
bridge.patch("light", tempLightID, tempPreviousStatePatch).
then(function(status)
{
if(tempPreviousState.payload.on === false)
{
bridge.patch("light", tempLightID, { on: { on: false } })
.then(function() { if(done) { done(); }});
}
else
{
bridge.patch("light", tempLightID, { on: { on: false } })
.then(function(status) {
if(done) { done(); }
return bridge.patch("light", tempLightID, { on: { on: true } })
});
}
});
}
// EXTENDED COMMANDS
else
{
// SET LIGHT STATE SIMPLE MODE
if(msg.payload === true||msg.payload === false)
{
if(msg.payload !== currentState.payload.on) { patchObject["on"] = { on: msg.payload }; }
}
// SET LIGHT STATE
if(typeof msg.payload != 'undefined' && typeof msg.payload.on != 'undefined' && (msg.payload.on === true || msg.payload.on === false))
{
if(msg.payload.on !== currentState.payload.on) { patchObject["on"] = { on: msg.payload.on }; }
}
// TOGGLE ON / OFF
if(typeof msg.payload != 'undefined' && typeof msg.payload.toggle != 'undefined')
{
patchObject["on"] = { on: !currentState.payload.on };
}
// SET BRIGHTNESS
if(typeof msg.payload != 'undefined' && typeof msg.payload.brightness != 'undefined')
{
// AUTO BRIGHTNESS BASED ON DAY TIME
if(new RegExp("auto|automatic").test(msg.payload.brightness))
{
let ct = colorUtils.colorTemperature();
let autoBrightness = ((300-ct)/2)+100;
autoBrightness = (autoBrightness > 100) ? 100 : autoBrightness;
autoBrightness = (autoBrightness < 20) ? 20 : autoBrightness;
// SET CALCULATED BRIGHTNESS
patchObject["dimming"] = { brightness: autoBrightness };
}
else
{
if(msg.payload.brightness > 100 || msg.payload.brightness < 0)
{
scope.error("Invalid brightness setting. Only 0 - 100 percent allowed");
return false;
}
else if(msg.payload.brightness == 0)
{
if(currentState.payload.on !== false) { patchObject["on"] = { on: false }; }
}
else
{
patchObject["dimming"] = { brightness: msg.payload.brightness };
}
}
}
else if(typeof msg.payload != 'undefined' && typeof msg.payload.brightnessLevel != 'undefined')
{
if(msg.payload.brightnessLevel > 254 || msg.payload.brightnessLevel < 0)
{
scope.error("Invalid brightness setting. Only 0 - 254 allowed");
return false;
}
else if(msg.payload.brightness == 0)
{
if(currentState.payload.on !== false) { patchObject["on"] = { on: false }; }
}
else
{
patchObject["dimming"] = { brightness: Math.round((100/254)*msg.payload.brightnessLevel) };
}
}
else if(typeof msg.payload != 'undefined' && typeof msg.payload.incrementBrightness != 'undefined')
{
let incrementBy = (isNaN(msg.payload.incrementBrightness)) ? 10 : msg.payload.incrementBrightness;
let targetBrightness = Math.round(currentState.payload.brightness + incrementBy);
targetBrightness = (targetBrightness > 100) ? 100 : targetBrightness;
patchObject["dimming"] = { brightness: targetBrightness };
}
else if(typeof msg.payload != 'undefined' && typeof msg.payload.decrementBrightness != 'undefined')
{
let decrementBy = (isNaN(msg.payload.decrementBrightness)) ? 10 : msg.payload.decrementBrightness;
let targetBrightness = Math.round(currentState.payload.brightness - decrementBy);
targetBrightness = (targetBrightness < 0) ? 0 : targetBrightness;
if(targetBrightness < 1)
{
if(currentState.payload.on !== false) { patchObject["on"] = { on: false }; }
}
patchObject["dimming"] = { brightness: targetBrightness };
}
// SET HUMAN READABLE COLOR OR RANDOM
if(typeof msg.payload != 'undefined' && typeof msg.payload.color != 'undefined' && typeof currentState.payload.xyColor != 'undefined')
{
let XYAlertColor = {};
if(new RegExp("random|any|whatever").test(msg.payload.color))
{
const randomColor = colorUtils.randomHexColor();
let rgbFromHex = colorUtils.hexRgb(randomColor);
XYAlertColor = colorUtils.rgbToXy(rgbFromHex[0], rgbFromHex[1], rgbFromHex[2], currentState.info.model.colorGamut);
}
else
{
var colorHex = colorUtils.colornames(msg.payload.color);
if(colorHex)
{
let rgbFromHex = colorUtils.hexRgb(colorHex);
XYAlertColor = colorUtils.rgbToXy(rgbFromHex[0], rgbFromHex[1], rgbFromHex[2], currentState.info.model.colorGamut);
}
}
patchObject["color"] = {
xy: XYAlertColor
};
}
// SET HEX COLOR
if(typeof msg.payload != 'undefined' && typeof msg.payload.hex != 'undefined' && typeof currentState.payload.xyColor != 'undefined')
{
let rgbFromHex = colorUtils.hexRgb((msg.payload.hex).toString());
patchObject["color"] = {
xy: colorUtils.rgbToXy(rgbFromHex[0], rgbFromHex[1], rgbFromHex[2], currentState.info.model.colorGamut)
};
}
// SET RGB COLOR
if(typeof msg.payload != 'undefined' && typeof msg.payload.rgb != 'undefined' && typeof currentState.payload.xyColor != 'undefined' && msg.payload.rgb.length === 3)
{
patchObject["color"] = {
xy: colorUtils.rgbToXy(msg.payload.rgb[0], msg.payload.rgb[1], msg.payload.rgb[2], currentState.info.model.colorGamut)
};
}
// SET XY COLOR
if(typeof msg.payload != 'undefined' && typeof msg.payload.xyColor != 'undefined' && typeof currentState.payload.xyColor != 'undefined')
{
patchObject["color"] = {
xy: msg.payload.xyColor
};
}
// MIX CURRENT COLOR WITH NEW COLOR
if(typeof msg.payload != 'undefined' && typeof msg.payload.mixColor != 'undefined' && typeof currentState.payload.xyColor != 'undefined')
{
let RGBColor = [];
if(typeof msg.payload.mixColor.color != 'undefined')
{
if(new RegExp("random|any|whatever").test(msg.payload.mixColor.color))
{
RGBColor = colorUtils.hexRgb(colorUtils.randomHexColor());
}
else
{
var colorHex = colorUtils.colornames(msg.payload.mixColor.color);
if(colorHex)
{
RGBColor = colorUtils.hexRgb(colorHex);
}
}
}
else if(typeof msg.payload.mixColor.rgb != 'undefined')
{
RGBColor = msg.payload.mixColor.rgb;
}
else if(typeof msg.payload.mixColor.hex != 'undefined')
{
RGBColor = colorUtils.hexRgb((msg.payload.mixColor.hex).toString());
}
else if(typeof msg.payload.mixColor.xyColor != 'undefined')
{
RGBColor = colorUtils.xyBriToRgb(msg.payload.mixColor.xyColor.x, msg.payload.mixColor.xyColor.y, 100);
}
// GET MIXING AMOUNT
let mixingAmount = 0.5;
if(typeof msg.payload.mixColor.amount != 'undefined' && msg.payload.mixColor.amount > 0 && msg.payload.mixColor.amount <= 100)
{
mixingAmount = msg.payload.mixColor.amount/100;
}
// HAS CURRENT COLOR SETTING?
if(currentState.payload.rgb !== false)
{
let mixedRGBColor = colorUtils.mixColors(currentState.payload.rgb, RGBColor, mixingAmount);
patchObject["color"] = {
xy: colorUtils.rgbToXy(mixedRGBColor[0], mixedRGBColor[1], mixedRGBColor[2], currentState.info.model.colorGamut)
};
}
else
{
patchObject["color"] = {
xy: colorUtils.rgbToXy(RGBColor[0], RGBColor[1], RGBColor[2], currentState.info.model.colorGamut)
};
}
}
// SET COLOR TEMPERATURE
if(typeof msg.payload != 'undefined' && typeof msg.payload.colorTemp != 'undefined' && typeof currentState.payload.colorTemp != 'undefined')
{
// DETERMINE IF AUTOMATIC, WARM, COLD, INT
if(!isNaN(msg.payload.colorTemp))
{
let colorTemp = parseInt(msg.payload.colorTemp);
if(colorTemp >= 153 && colorTemp <= 500)
{
patchObject["color_temperature"] = { mirek: colorTemp };
}
else
{
scope.error("Invalid color temprature. Only 153 - 500 allowed");
return false;
}
}
else if(msg.payload.colorTemp == "cold")
{
patchObject["color_temperature"] = { mirek: 153 };
}
else if(msg.payload.colorTemp == "normal")
{
patchObject["color_temperature"] = { mirek: 240 };
}
else if(msg.payload.colorTemp == "warm")
{
patchObject["color_temperature"] = { mirek: 400 };
}
else if(msg.payload.colorTemp == "hot")
{
patchObject["color_temperature"] = { mirek: 500 };
}
else
{
// SET TEMPERATURE
patchObject["color_temperature"] = { mirek: colorUtils.colorTemperature() };
}
}
else if(typeof msg.payload != 'undefined' && typeof msg.payload.incrementColorTemp != 'undefined' && typeof currentState.payload.colorTemp != 'undefined')
{
let incrementBy = (isNaN(msg.payload.incrementColorTemp)) ? 50 : msg.payload.incrementColorTemp;
let targetColorTemperature = currentState.payload.colorTemp + parseInt(incrementBy);
targetColorTemperature = (targetColorTemperature > 500) ? 500 : targetColorTemperature;
targetColorTemperature = (targetColorTemperature < 153) ? 153 : targetColorTemperature;
// SET TEMPERATURE
patchObject["color_temperature"] = { mirek: targetColorTemperature };
}
else if(typeof msg.payload != 'undefined' && typeof msg.payload.decrementColorTemp != 'undefined' && typeof currentState.payload.colorTemp != 'undefined')
{
let decrementBy = (isNaN(msg.payload.decrementColorTemp)) ? 50 : msg.payload.decrementColorTemp;
let targetColorTemperature = currentState.payload.colorTemp - parseInt(decrementBy);
targetColorTemperature = (targetColorTemperature > 500) ? 500 : targetColorTemperature;
targetColorTemperature = (targetColorTemperature < 153) ? 153 : targetColorTemperature;
// SET TEMPERATURE
patchObject["color_temperature"] = { mirek: targetColorTemperature };
}
// SET TRANSITION TIME
if(typeof msg.payload != 'undefined' && typeof msg.payload.transitionTime != 'undefined')
{
let targetTransitionTime = parseFloat(msg.payload.transitionTime)*1000;
targetTransitionTime = (targetTransitionTime > 6000000) ? 6000000 : targetTransitionTime;
targetTransitionTime = (targetTransitionTime < 0) ? 0 : targetTransitionTime;
patchObject["dynamics"] = { duration: targetTransitionTime };
}
// SET DOMINANT COLORS FROM IMAGE
if(typeof msg.payload != 'undefined' && typeof msg.payload.image != 'undefined' && typeof currentState.payload.xyColor != 'undefined')
{
var colors = await colorUtils.getColors(msg.payload.image);
if(colors.length > 0)
{
var colorsHEX = colors.map(color => color.hex());
let rgbFromHex = colorUtils.hexRgb(colorsHEX[0]);
patchObject["color"] = {
xy: colorUtils.rgbToXy(rgbFromHex[0], rgbFromHex[1], rgbFromHex[2], currentState.info.model.colorGamut)
};
}
}
// SET SATURATION
if(typeof msg.payload != 'undefined' && typeof msg.payload.saturation != 'undefined' && typeof currentState.payload.xyColor != 'undefined')
{
if(msg.payload.saturation > 100 || msg.payload.saturation < 0)
{
scope.error("Invalid saturation setting. Only 0 - 100 allowed");
return false;
}
else
{
let currentColor = patchObject["color"] ? colorUtils.xyBriToRgb(patchObject["color"].xy.x, patchObject["color"].xy.y, 100) : currentState.payload.rgb;
let currentColorInHSL = colorUtils.rgbToHsl(currentColor[0], currentColor[1], currentColor[2]);
let saturationFactor = (msg.payload.saturation/100);
// CHANGE SATURATION
currentColorInHSL[1] = currentColorInHSL[1]*saturationFactor;
// CONVERT BACK TO RGB
let saturatedRGBColor = colorUtils.hslToRgb(currentColorInHSL[0], currentColorInHSL[1], currentColorInHSL[2]);
patchObject["color"] = {
xy: colorUtils.rgbToXy(saturatedRGBColor[0], saturatedRGBColor[1], saturatedRGBColor[2], currentState.info.model.colorGamut)
};
}
}
// SET GRADIENT
if(typeof msg.payload != 'undefined' && typeof msg.payload.gradient != 'undefined' && typeof currentState.payload.gradient != 'undefined')
{
let XYColorSet = [];
if(typeof msg.payload.gradient.hex != 'undefined' && Array.isArray(msg.payload.gradient.hex) == true)
{
XYColorSet = [];
for(let oneColor in msg.payload.gradient.hex)
{
let rgbFromHex = colorUtils.hexRgb(oneColor);
XYColorSet.push({
color: { xy: colorUtils.rgbToXy(rgbFromHex[0], rgbFromHex[1], rgbFromHex[2], currentState.info.model.colorGamut) }
});
}
}
if(typeof msg.payload.gradient.rgb != 'undefined' && Array.isArray(msg.payload.gradient.rgb) == true)
{
XYColorSet = [];
for(let oneColor in msg.payload.gradient.rgb)
{
XYColorSet.push({
color: { xy: colorUtils.rgbToXy(oneColor[0], oneColor[1], oneColor[2], currentState.info.model.colorGamut) }
});
}
}
if(typeof msg.payload.gradient.xyColor != 'undefined' && Array.isArray(msg.payload.gradient.xyColor) == true)
{
XYColorSet = [];
for(let oneColor in msg.payload.gradient.xyColor)
{
XYColorSet.push({
color: { xy: oneColor }
});
}
}
patchObject["gradient"] = { points: XYColorSet };
}
//
// SHOULD PATCH?
if(Object.values(patchObject).length > 0)
{
// IS FOR LATER?
if(currentState.payload.on === false || currentState.payload.reachable === false)
{
if(!patchObject["on"] || !patchObject["on"]["on"])
{
scope.futurePatchState = merge.deep(scope.futurePatchState, patchObject);
return false;
}
}
// CHANGE NODE UI STATE
if(config.lightid)
{
scope.status({fill: "grey", shape: "ring", text: "hue-light.node.command"});
}
// PATCH!
bridge.patch("light", tempLightID, patchObject)
.then(function() { if(done) { done(); }})
.catch(function(errors) { scope.error(errors); });
}
else
{
// JUST SEND CURRENT STATE
if(scope.lastCommand !== null)
{
currentState.command = scope.lastCommand;
}
// SEND STATE
scope.send(currentState);
// RESET LAST COMMAND
scope.lastCommand = null;
if(done) { done(); }
}
}
}
}
RED.nodes.registerType("hue-light", HueLight);
}