-
Notifications
You must be signed in to change notification settings - Fork 4
/
light-transition.js
396 lines (370 loc) · 20.3 KB
/
light-transition.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
module.exports = function (RED) {
let Looper = require('loop');
let unitMap = [
{ unit: 'Millisecond', multiplier: 1, duration: 1 },
{ unit: 'Second', multiplier: 1000, get duration() { return unitMap[0].duration * unitMap[1].multiplier } },
{ unit: 'Minute', multiplier: 60, get duration() { return unitMap[1].duration * unitMap[2].multiplier } },
{ unit: 'Hour', multiplier: 60, get duration() { return unitMap[2].duration * unitMap[3].multiplier } },
{ unit: 'Day', multiplier: 24, get duration() { return unitMap[3].duration * unitMap[4].multiplier } }
]
function CalcDuration(unit, duration) {
return (unitMap.find(o => o.unit === unit).duration * duration);
}
// Takes in ms and returns a string with human readable times i.e. "45 Seconds" / "3 Hours"
function readableDuration(duration) {
let maxSeconds = 90;
let maxMinutes = 59;
let seconds = parseInt(duration) / 1000;
if(seconds <= maxSeconds) {
return Math.round(seconds * 10) / 10 + ' ' + (seconds > 1 ? RED._('node-red:delay.secs') : RED._('node-red:delay.sec'));
} else if(seconds/60 <= maxMinutes) {
return Math.round(seconds/6) / 10 + ' ' + (seconds/60 > 1 ? RED._('node-red:delay.mins') : RED._('node-red:delay.min'));
} else {
return Math.round(seconds/360) / 10 + ' ' + (seconds/3600 > 1 ? RED._('node-red:delay.hours') : RED._('node-red:delay.hour'));
}
}
// Takes in "#fa12b3" and returns an array with decimal values i.e. [250,18,179]
function hexToRGB(hex) {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16)
] : null;
}
// Linear interpolation of input y given starting and ending ranges
function scale(y, range1 = [0,100], range2 = [0,255]) {
const [xMin, xMax] = range2;
const [yMin, yMax] = range1;
const percent = (y - yMin) / (yMax - yMin);
const ans = percent * (xMax - xMin) + xMin;
return Math.round(ans);
}
function LightTransition(n) {
RED.nodes.createNode(this, n);
let node = this;
let transition = null;
let timeout = null;
let stopped = false;
let stopmsg = null;
let mlmsg = null;
let rgbRegex = new RegExp('^(#(?:(?:[A-F0-9]{2}){3,4}|[A-F0-9]{3})|)$', 'i');
this.on('input', function (msg) {
transition = Looper();
this.nodeduration = n.nodeduration || 1;
this.nodemaxtimeout = n.nodemaxtimeout || 1;
this.startRGB = n.startRGB || '#ff0000';
this.transitionRGB = n.transitionRGB || this.startRGB;
this.endRGB = n.endRGB || '#ffffff';
this.startMired = parseInt(n.startMired) || 200;
this.endMired = parseInt(n.endMired) || 600;
this.transitionTime = parseInt(n.transitionTime) || 15;
this.transitionTimeUnits = n.transitionTimeUnits || 'Minute';
this.steps = parseInt(n.steps) || 30;
this.startBright = parseInt(n.startBright) || 0;
this.endBright = parseInt(n.endBright) || 0;
this.brightnessType = n.brightnessType || 'Percent';
this.transitionType = n.transitionType || 'Linear';
this.colorTransitionType = n.colorTransitionType || 'Weighted';
if (this.transitionTime <= 1) this.transitionTime = 1;
// Input checking of any received msg
if (msg.transition != undefined) {
if (msg.transition.startRGB != undefined) {
if (rgbRegex.test(msg.transition.startRGB)) {
node.startRGB = msg.transition.startRGB;
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.startRGB ' + RED._('light-transition.errors.formatted') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.startRGB - '+ RED._('light-transition.errors.formatted'));
return;
}
}
if (msg.transition.transitionRGB != undefined) {
if (rgbRegex.test(msg.transition.transitionRGB)) {
node.transitionRGB = msg.transition.transitionRGB;
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.transitionRGB '+ RED._('light-transition.errors.formatted') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.transitionRGB - '+ RED._('light-transition.errors.formatted'));
return;
}
}
if (msg.transition.endRGB != undefined) {
if (rgbRegex.test(msg.transition.endRGB)) {
node.endRGB = msg.transition.endRGB;
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.endRGB '+ RED._('light-transition.errors.formatted') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.endRGB - '+ RED._('light-transition.errors.formatted'));
return;
}
}
if (msg.transition.startMired != undefined) {
if (typeof msg.transition.startMired === 'number' && msg.transition.startMired > 0) {
node.startMired = parseInt(msg.transition.startMired);
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.startMired ' + RED._('light-transition.errors.posInt') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.startMired - ' + RED._('light-transition.errors.posInt'));
return;
}
}
if (msg.transition.endMired != undefined) {
if (typeof msg.transition.endMired === 'number' && msg.transition.endMired > 0) {
node.endMired = parseInt(msg.transition.endMired);
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.endMired ' + RED._('light-transition.errors.posInt') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.endMired - ' + RED._('light-transition.errors.posInt'));
return;
}
}
if (msg.transition.duration != undefined) {
if (typeof msg.transition.duration === 'number' && msg.transition.duration > 0) {
node.transitionTime = parseInt(msg.transition.duration);
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.duration ' + RED._('light-transition.errors.posInt') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.duration - ' + RED._('light-transition.errors.posInt'));
return;
}
}
if (msg.transition.steps != undefined) {
if (typeof msg.transition.steps === 'number' && msg.transition.steps > 0) {
node.steps = parseInt(msg.transition.steps);
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.steps ' + RED._('light-transition.errors.posInt') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.steps - ' + RED._('light-transition.errors.posInt'));
return;
}
}
if (msg.transition.startBright != undefined) {
if (typeof msg.transition.startBright === 'number' && msg.transition.startBright >= 0 && msg.transition.startBright <= 255) {
node.startBright = parseInt(msg.transition.startBright);
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.startBright ' + RED._('light-transition.errors.brightnessVal') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.startBright - ' + RED._('light-transition.errors.brightnessVal'));
return;
}
}
if (msg.transition.endBright != undefined) {
if (typeof msg.transition.endBright === 'number' && msg.transition.endBright >= 0 && msg.transition.endBright <= 255) {
node.endBright = parseInt(msg.transition.endBright);
} else {
node.status({ fill: 'red', shape: 'ring', text: 'msg.transition.endBright ' + RED._('light-transition.errors.brightnessVal') });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.endBright - ' + RED._('light-transition.errors.brightnessVal'));
return;
}
}
if (msg.transition.units != undefined) {
if ((msg.transition.units === 'Second') || (msg.transition.units === 'Minute') || (msg.transition.units === 'Hour')) {
node.transitionTimeUnits = msg.transition.units;
} else {
node.status({ fill: 'red', shape: 'ring', text: RED._('light-transition.errors.invalidA') + ' msg.transition.units' });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.units, ' + RED._('light-transition.errors.allowedVals') + ' Second, Minute, and Hour');
return;
}
}
if (msg.transition.brightnessType != undefined) {
if ((msg.transition.brightnessType === 'Percent') || (msg.transition.brightnessType === 'Integer')) {
node.brightnessType = msg.transition.brightnessType;
} else {
node.status({ fill: 'red', shape: 'ring', text: RED._('light-transition.errors.invalidA') + ' msg.transition.brightnessType' });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.brightnessType, ' + RED._('light-transition.errors.allowedVals') + ' Percent or Integer');
return;
}
}
if (msg.transition.transitionType != undefined) {
if ((msg.transition.transitionType === 'Linear') || (msg.transition.transitionType === 'Exponential')) {
node.transitionType = msg.transition.transitionType;
} else {
node.status({ fill: 'red', shape: 'ring', text: RED._('light-transition.errors.invalidA') + ' msg.transition.transitionType' });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.transitionType, ' + RED._('light-transition.errors.allowedVals') + ' Linear or Exponential');
return;
}
}
if (msg.transition.colorTransitionType != undefined) {
if ((msg.transition.colorTransitionType === 'Weighted') || (msg.transition.colorTransitionType === 'Half') || (msg.transition.colorTransitionType === 'None')) {
node.colorTransitionType = msg.transition.colorTransitionType;
} else {
node.status({ fill: 'red', shape: 'ring', text: RED._('light-transition.errors.invalidA') + ' msg.transition.colorTransitionType' });
node.error(RED._('light-transition.errors.invalidA') + ': msg.transition.colorTransitionType, ' + RED._('light-transition.errors.allowedVals') + ' Weighted, Half or None');
return;
}
}
} // End Input Checking
node.nodeduration = CalcDuration(node.transitionTimeUnits, node.transitionTime / node.steps);
node.nodemaxtimeout = CalcDuration(node.transitionTimeUnits, node.transitionTime + 5);
transition.setTimeout(node.nodemaxtimeout);
transition.setMaxLoop(node.steps - 1);
//Make sure starting and ending brightness are within 0-100%, scaling if not using 'Percent'
if(node.startBright < 0) node.startBright = 0;
if(node.endBright < 0) node.endBright = 0;
switch(node.brightnessType) {
case 'Percent':
if(node.startBright > 100) node.startBright = 100;
if(node.endBright > 100) node.endBright = 100;
break;
case 'Integer':
if(node.startBright > 255) node.startBright = 255;
if(node.endBright > 255) node.endBright = 255;
node.startBright = scale(node.startBright, [0,255], [0,100]);
node.endBright = scale(node.endBright, [0,255], [0,100]);
break;
}
let colors = [];
let deltas = [];
let means = [];
let changeDist = [];
colors.push(hexToRGB(node.startRGB));
colors.push(hexToRGB(node.transitionRGB));
colors.push(hexToRGB(node.endRGB));
if (stopped === false || msg._timerpass !== true) {
stopped = false;
clearTimeout(timeout);
timeout = null;
if (msg.payload !== undefined && msg.payload.toString().toLowerCase() === 'stop') {
// If msg.payload is sent a stop command, stop the loop and quit
node.status({ fill: 'red', shape: 'ring', text: `node-red:inject.stopped` });
stopped = true;
stopmsg = RED.util.cloneMessage(msg);
stopmsg.payload = 'stopped';
delete stopmsg['_timerpass'];
node.send([null, stopmsg]);
stopped = false;
} else {
// Initial loop output
let lightMsg = RED.util.cloneMessage(msg); // Copy msg object to preserve user keys i.e. msg.topic
delete lightMsg['_timerpass']; // Remove internal timerpass key
lightMsg.payload = {
brightness_pct: node.startBright,
brightness: scale(node.startBright),
rgb_color: colors[0],
color_temp: node.startMired,
};
node.send([lightMsg, null]);
msg._timerpass = true;
transition.run(function (next, err, data) {
node.status({ fill: 'green', shape: 'ring', text: `${RED._('light-transition.description.running')} ${data + 1}/${node.steps} - ${RED._('light-transition.description.every')} ${readableDuration(node.nodeduration)}` });
data++;
timeout = setTimeout(function () {
if (stopped === false) {
if (data == node.steps - 1) {
// Final loop output
let lightMsg = RED.util.cloneMessage(msg);
delete lightMsg['_timerpass'];
lightMsg.payload = {
brightness_pct: node.endBright,
brightness: scale(node.endBright),
rgb_color: colors[2],
color_temp: node.endMired,
};
mlmsg = RED.util.cloneMessage(msg);
mlmsg.payload = 'complete';
delete mlmsg['_timerpass'];
node.send([lightMsg, mlmsg]);
let now = new Date;
let splitDate = now.toDateString().split(' ');
let splitTime = now.toTimeString().split(':');
let goodDate = splitDate[1] + ' ' + splitDate[2] + ', ' + splitTime[0] + ':' + splitTime[1];
node.status({ fill: 'green', shape: 'dot', text: `${RED._('light-transition.description.completed')} ${goodDate}` });
timeout = null;
next('break');
} else {
// All other loop outputs
node.status({});
if ((data * node.nodeduration) <= node.nodemaxtimeout) {
let colorChange = [0,0,0];
for(let i = 0; i < colorChange.length; i++) {
switch (node.colorTransitionType) {
case 'Weighted':
// Finds the 'distance' between the starting/transition/ending colors using the redmean approximation
// https://en.wikipedia.org/wiki/Color_difference#sRGB
// d1 array will have the distance between starting & transition colors per steps required to arrive there. d2 - transition & ending.
// tMid will be the number of steps to the transition color.
for (let i = 0; i < 2; i++) {
deltas[i] = [colors[i][0] - colors[i + 1][0], colors[i][1] - colors[i + 1][1], colors[i][2] - colors[i + 1][2]];
means[i] = (colors[i][0] + colors[i + 1][0]) / 2;
changeDist[i] = Math.sqrt((2 + means[i] / 256) * deltas[i][0] * deltas[i][0] + 4 * deltas[i][1] * deltas[i][1] + (2 + (255 - means[i] / 256)) * deltas[i][2] * deltas[i][2]);
}
let tMid = Math.trunc(node.steps * changeDist[0] / (changeDist[0] + changeDist[1]));
let d1 = [];
let d2 = [];
for (let i = 0; i < 3; i++) {
d1[i] = (colors[1][i] - colors[0][i]) / tMid;
d2[i] = (colors[2][i] - colors[1][i]) / (node.steps - tMid);
}
// Either add the transition 'distance' to the starting RGB values or the transition RGB value
if(data < tMid) {
colorChange[i] = Math.trunc(colors[0][i] + data * d1[i]);
} else if(data === tMid) {
colorChange[i] = colors[1][i];
}
else colorChange[i] = Math.trunc(colors[1][i] + (data - tMid) * d2[i]);
break;
case 'Half':
// Find the middle number of total steps, then subtract the average distance / steps from the starting/transition RGB values
let midPt = Math.floor((node.steps - 1) / 2);
if(data == midPt) colorChange = hexToRGB(node.transitionRGB);
else if(data <= midPt) colorChange[i] = colors[0][i] - Math.floor((colors[0][i] - colors[1][i]) / midPt * data);
else colorChange[i] = colors[1][i] - Math.floor((colors[1][i] - colors[2][i]) / (node.steps - midPt - 1) * (data - midPt));
break;
case 'None':
// Just increments the starting RGB value by the average of the starting & ending RGB values / num of steps.
colorChange[i] = colors[0][i] - Math.floor((colors[0][i] - colors[2][i]) / node.steps * data);
break;
}
}
// Just increments the mired value by the average of the starting & ending mired values / num of steps.
let miredChange = Math.floor((node.endMired - node.startMired) / (node.steps - 1) * data + node.startMired);
let brightnessChange = 0;
switch(node.transitionType) {
case 'Exponential':
const endBrightExp = node.endBright === 0 ? 1 : node.endBright;
const startBrightExp = node.startBright === 0 ? 1 : node.startBright;
let exponB = 0;
if (node.startBright <= node.endBright) {
exponB = Math.log(node.endBright / startBrightExp) / (node.steps - 1);
brightnessChange = Math.floor(startBrightExp * Math.exp(exponB * data));
} else {
exponB = Math.log(startBrightExp / endBrightExp) / (node.steps - 1);
brightnessChange = startBrightExp - Math.floor(endBrightExp * Math.exp(exponB * data)) + endBrightExp;
}
break;
case 'Linear':
brightnessChange = Math.trunc((node.endBright - node.startBright) / (node.steps - 1) * data + node.startBright);
break;
}
let lightMsg = RED.util.cloneMessage(msg);
delete lightMsg['_timerpass'];
lightMsg.payload = {
brightness_pct: brightnessChange,
brightness: scale(brightnessChange),
rgb_color: colorChange,
color_temp: miredChange,
};
node.send([lightMsg, null]);
timeout = null;
if (((data + 1) * node.nodeduration) > node.nodemaxtimeout) {
next('break');
} else {
next(undefined, data);
}
} else {
timeout = null;
next('break');
}
}
} else {
timeout = null;
next('break');
}
}, node.nodeduration);
}, 0);
}
}
});
this.on('close', function () {
stopped = false;
if (timeout) {
clearTimeout(timeout);
}
node.status({});
});
}
RED.nodes.registerType('light-transition', LightTransition);
}