| 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 |
1×
1×
1×
52×
52×
52×
52×
52×
52×
52×
1×
5×
4×
3×
3×
3×
1×
5×
4×
3×
3×
3×
1×
23×
14×
12×
11×
10×
10×
10×
10×
10×
10×
1×
12×
3×
3×
3×
3×
1×
5×
2×
3×
3×
3×
3×
3×
1×
6×
3×
3×
1×
2×
2×
2×
1×
2×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
5×
4×
1×
3×
2×
1×
2×
2×
2×
1×
8×
2×
6×
2×
4×
1×
3×
2×
2×
2×
1×
2×
1×
1×
1×
1×
1×
2×
1×
1×
1×
1×
1×
10×
6×
4×
3×
3×
3×
3×
3×
3×
2×
1×
1×
18×
15×
13×
6×
4×
3×
2×
1×
1×
1×
1×
1×
1×
1×
1×
8×
2×
6×
2×
4×
1×
3×
2×
2×
2×
1× | 'use strict';
var _require = require('../lifx'),
packet = _require.packet,
constants = _require.constants,
validate = _require.validate,
utils = _require.utils;
var _require2 = require('lodash'),
assign = _require2.assign,
pick = _require2.pick,
includes = _require2.includes,
isNumber = _require2.isNumber;
/**
* A representation of a light bulb
* @class
* @param {Obj} constr constructor object
* @param {Lifx/Client} constr.client the client the light belongs to
* @param {String} constr.id the id used to target the light
* @param {String} constr.address ip address of the light
* @param {Number} constr.port port of the light
* @param {Number} constr.seenOnDiscovery on which discovery the light was last seen
*/
function Light(constr) {
this.client = constr.client;
this.id = constr.id; // Used to target the light
this.address = constr.address;
this.port = constr.port;
this.label = null;
this.status = 'on';
this.seenOnDiscovery = constr.seenOnDiscovery;
}
/**
* Turns the light off
* @example light('192.168.2.130').off()
* @param {Number} [duration] transition time in milliseconds
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.off = function (duration, callback) {
validate.optionalDuration(duration, 'light off method');
validate.optionalCallback(callback, 'light off method');
var packetObj = packet.create('setPower', { level: 0, duration: duration }, this.client.source);
packetObj.target = this.id;
this.client.send(packetObj, callback);
};
/**
* Turns the light on
* @example light('192.168.2.130').on()
* @param {Number} [duration] transition time in milliseconds
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.on = function (duration, callback) {
validate.optionalDuration(duration, 'light on method');
validate.optionalCallback(callback, 'light on method');
var packetObj = packet.create('setPower', { level: 65535, duration: duration }, this.client.source);
packetObj.target = this.id;
this.client.send(packetObj, callback);
};
/**
* Changes the color to the given HSBK value
* @param {Number} hue color hue from 0 - 360 (in °)
* @param {Number} saturation color saturation from 0 - 100 (in %)
* @param {Number} brightness color brightness from 0 - 100 (in %)
* @param {Number} [kelvin=3500] color kelvin between 2500 and 9000
* @param {Number} [duration] transition time in milliseconds
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.color = function (hue, saturation, brightness, kelvin, duration, callback) {
validate.colorHsb(hue, saturation, brightness, 'light color method');
validate.optionalKelvin(kelvin, 'light color method');
validate.optionalDuration(duration, 'light color method');
validate.optionalCallback(callback, 'light color method');
// Convert HSB values to packet format
hue = Math.round(hue / constants.HSBK_MAXIMUM_HUE * 65535);
saturation = Math.round(saturation / constants.HSBK_MAXIMUM_SATURATION * 65535);
brightness = Math.round(brightness / constants.HSBK_MAXIMUM_BRIGHTNESS * 65535);
var packetObj = packet.create('setColor', {
hue: hue,
saturation: saturation,
brightness: brightness,
kelvin: kelvin,
duration: duration
}, this.client.source);
packetObj.target = this.id;
this.client.send(packetObj, callback);
};
/**
* Changes the color to the given rgb value
* Note RGB poorly represents the color of light, prefer setting HSBK values with the color method
* @example light.colorRgb(255, 0, 0)
* @param {Integer} red value between 0 and 255 representing amount of red in color
* @param {Integer} green value between 0 and 255 representing amount of green in color
* @param {Integer} blue value between 0 and 255 representing amount of blue in color
* @param {Number} [duration] transition time in milliseconds
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.colorRgb = function (red, green, blue, duration, callback) {
validate.colorRgb(red, green, blue, 'light colorRgb method');
validate.optionalDuration(duration, 'light colorRgb method');
validate.optionalCallback(callback, 'light colorRgb method');
var hsbObj = utils.rgbToHsb({ r: red, g: green, b: blue });
this.color(hsbObj.h, hsbObj.s, hsbObj.b, 3500, duration, callback);
};
/**
* Changes the color to the given rgb value
* Note RGB poorly represents the color of light, prefer setting HSBK values with the color method
* @example light.colorRgb('#FF0000')
* @param {String} hexString rgb hex string starting with # char
* @param {Number} [duration] transition time in milliseconds
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.colorRgbHex = function (hexString, duration, callback) {
if (typeof hexString !== 'string') {
throw new TypeError('LIFX light colorRgbHex method expects first parameter hexString to a string');
}
validate.optionalDuration(duration, 'light colorRgbHex method');
validate.optionalCallback(callback, 'light colorRgbHex method');
var rgbObj = utils.rgbHexStringToObject(hexString);
var hsbObj = utils.rgbToHsb(rgbObj);
this.color(hsbObj.h, hsbObj.s, hsbObj.b, 3500, duration, callback);
};
/**
* Sets the Maximum Infrared brightness
* @param {Number} brightness infrared brightness from 0 - 100 (in %)
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.maxIR = function (brightness, callback) {
validate.irBrightness(brightness, 'light setMaxIR method');
brightness = Math.round(brightness / constants.IR_MAXIMUM_BRIGHTNESS * 65535);
if (callback !== undefined && typeof callback !== 'function') {
throw new TypeError('LIFX light setMaxIR method expects callback to be a function');
}
var packetObj = packet.create('setInfrared', {
brightness: brightness
}, this.client.source);
packetObj.target = this.id;
this.client.send(packetObj, callback);
};
/**
* Requests the current state of the light
* @param {Function} callback a function to accept the data
*/
Light.prototype.getState = function (callback) {
validate.callback(callback, 'light getState method');
var packetObj = packet.create('getLight', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateLight', function (err, msg) {
if (err) {
return callback(err, null);
}
// Convert HSB to readable format
msg.color.hue = Math.round(msg.color.hue * (constants.HSBK_MAXIMUM_HUE / 65535));
msg.color.saturation = Math.round(msg.color.saturation * (constants.HSBK_MAXIMUM_SATURATION / 65535));
msg.color.brightness = Math.round(msg.color.brightness * (constants.HSBK_MAXIMUM_BRIGHTNESS / 65535));
// Convert power to readable format
if (msg.power === 65535) {
msg.power = 1;
}
callback(null, {
color: msg.color,
power: msg.power,
label: msg.label
});
}, sqnNumber);
};
/**
* Requests the current maximum setting for the infrared channel
* @param {Function} callback a function to accept the data
*/
Light.prototype.getMaxIR = function (callback) {
validate.callback(callback, 'light getMaxIR method');
var packetObj = packet.create('getInfrared', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateInfrared', function (err, msg) {
if (err) {
return callback(err, null);
}
msg.brightness = Math.round(msg.brightness * (constants.HSBK_MAXIMUM_BRIGHTNESS / 65535));
callback(null, msg.brightness);
}, sqnNumber);
};
/**
* Requests hardware info from the light
* @param {Function} callback a function to accept the data with error and
* message as parameters
*/
Light.prototype.getHardwareVersion = function (callback) {
validate.callback(callback, 'light getHardwareVersion method');
var packetObj = packet.create('getVersion', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateVersion', function (err, msg) {
if (err) {
return callback(err, null);
}
var versionInfo = pick(msg, ['vendorId', 'productId', 'version']);
callback(null, assign(versionInfo, utils.getHardwareDetails(versionInfo.vendorId, versionInfo.productId)));
}, sqnNumber);
};
/**
* Requests uptime from the light
* @param {Function} callback a function to accept the data with error and
* message as parameters
*/
Light.prototype.getUptime = function (callback) {
if (typeof callback !== 'function') {
throw new TypeError('LIFX light getUptime method expects callback to be a function');
}
var packetObj = packet.create('getInfo', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateInfo', function (err, msg) {
if (err) {
return callback(err, null);
}
callback(null, msg.uptime);
}, sqnNumber);
};
/**
* Reboots the light
* @param {Function} callback called when light did receive message
*/
Light.prototype.reboot = function (callback) {
if (typeof callback !== 'function') {
throw new TypeError('LIFX light reboot method expects callback to be a function');
}
var packetObj = packet.create('rebootRequest', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('rebootResponse', callback, sqnNumber);
};
/**
* Requests used version from the microcontroller unit of the light
* @param {Function} callback a function to accept the data
*/
Light.prototype.getFirmwareVersion = function (callback) {
validate.callback(callback, 'light getFirmwareIgetFirmwareVersion method');
var packetObj = packet.create('getHostFirmware', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateHostFirmware', function (err, msg) {
if (err) {
return callback(err, null);
}
callback(null, pick(msg, ['majorVersion', 'minorVersion']));
}, sqnNumber);
};
/**
* Requests infos from the microcontroller unit of the light
* @param {Function} callback a function to accept the data
*/
Light.prototype.getFirmwareInfo = function (callback) {
validate.callback(callback, 'light getFirmwareInfo method');
var packetObj = packet.create('getHostInfo', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateHostInfo', function (err, msg) {
if (err) {
return callback(err, null);
}
callback(null, pick(msg, ['signal', 'tx', 'rx']));
}, sqnNumber);
};
/**
* Requests wifi infos from for the light
* @param {Function} callback a function to accept the data
*/
Light.prototype.getWifiInfo = function (callback) {
validate.callback(callback, 'light getWifiInfo method');
var packetObj = packet.create('getWifiInfo', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateWifiInfo', function (err, msg) {
if (err) {
return callback(err, null);
}
callback(null, pick(msg, ['signal', 'tx', 'rx']));
}, sqnNumber);
};
/**
* Requests used version from the wifi controller unit of the light (wifi firmware version)
* @param {Function} callback a function to accept the data
*/
Light.prototype.getWifiVersion = function (callback) {
validate.callback(callback, 'light getWifiVersion method');
var packetObj = packet.create('getWifiFirmware', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateWifiFirmware', function (err, msg) {
if (err) {
return callback(err, null);
}
return callback(null, pick(msg, ['majorVersion', 'minorVersion']));
}, sqnNumber);
};
/**
* Requests the label of the light
* @param {Function} callback a function to accept the data
* @param {Boolean} [cache=false] return cached result if existent
* @return {Function} callback(err, label)
*/
Light.prototype.getLabel = function (callback, cache) {
validate.callback(callback, 'light getLabel method');
if (cache !== undefined && typeof cache !== 'boolean') {
throw new TypeError('LIFX light getLabel method expects cache to be a boolean');
}
if (cache === true) {
if (typeof this.label === 'string' && this.label.length > 0) {
return callback(null, this.label);
}
}
var packetObj = packet.create('getLabel', {
target: this.id
}, this.client.source);
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateLabel', function (err, msg) {
if (err) {
return callback(err, null);
}
return callback(null, msg.label);
}, sqnNumber);
};
/**
* Sets the label of light
* @example light.setLabel('Kitchen')
* @param {String} label new label to be set, maximum 32 bytes
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.setLabel = function (label, callback) {
if (label === undefined || typeof label !== 'string') {
throw new TypeError('LIFX light setLabel method expects label to be a string');
}
if (Buffer.byteLength(label, 'utf8') > 32) {
throw new RangeError('LIFX light setLabel method expects a maximum of 32 bytes as label');
}
if (label.length < 1) {
throw new RangeError('LIFX light setLabel method expects a minimum of one char as label');
}
validate.optionalCallback(callback, 'light setLabel method');
var packetObj = packet.create('setLabel', { label: label }, this.client.source);
packetObj.target = this.id;
this.client.send(packetObj, callback);
};
/**
* Requests ambient light value of the light
* @param {Function} callback a function to accept the data
*/
Light.prototype.getAmbientLight = function (callback) {
validate.callback(callback, 'light getAmbientLight method');
var packetObj = packet.create('getAmbientLight', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('stateAmbientLight', function (err, msg) {
if (err) {
return callback(err, null);
}
return callback(null, msg.flux);
}, sqnNumber);
};
/**
* Requests the power level of the light
* @param {Function} callback a function to accept the data
*/
Light.prototype.getPower = function (callback) {
validate.callback(callback, 'light getPower method');
var packetObj = packet.create('getPower', {}, this.client.source);
packetObj.target = this.id;
var sqnNumber = this.client.send(packetObj);
this.client.addMessageHandler('statePower', function (err, msg) {
if (err) {
return callback(err, null);
}
if (msg.level === 65535) {
msg.level = 1;
}
return callback(null, msg.level);
}, sqnNumber);
};
/**
* Requests the current color zone states from a light
* @param {Number} startIndex start color zone index
* @param {Number} [endIndex] end color zone index
* @param {Function} callback a function to accept the data
*/
Light.prototype.getColorZones = function (startIndex, endIndex, callback) {
validate.zoneIndex(startIndex, 'light getColorZones method');
validate.optionalZoneIndex(endIndex, 'light getColorZones method');
validate.optionalCallback(callback, 'light getColorZones method');
var packetObj = packet.create('getColorZones', {}, this.client.source);
packetObj.target = this.id;
packetObj.startIndex = startIndex;
packetObj.endIndex = endIndex;
var sqnNumber = this.client.send(packetObj);
if (endIndex === undefined || startIndex === endIndex) {
this.client.addMessageHandler('stateZone', function (err, msg) {
if (err) {
return callback(err, null);
}
// Convert HSB to readable format
msg.color.hue = Math.round(msg.color.hue * (constants.HSBK_MAXIMUM_HUE / 65535));
msg.color.saturation = Math.round(msg.color.saturation * (constants.HSBK_MAXIMUM_SATURATION / 65535));
msg.color.brightness = Math.round(msg.color.brightness * (constants.HSBK_MAXIMUM_BRIGHTNESS / 65535));
callback(null, {
count: msg.count,
index: msg.index,
color: msg.color
});
}, sqnNumber);
} else {
this.client.addMessageHandler('stateMultiZone', function (err, msg) {
if (err) {
return callback(err, null);
}
// Convert HSB values to readable format
msg.color.forEach(function (color) {
color.hue = Math.round(color.hue * (constants.HSBK_MAXIMUM_HUE / 65535));
color.saturation = Math.round(color.saturation * (constants.HSBK_MAXIMUM_SATURATION / 65535));
color.brightness = Math.round(color.brightness * (constants.HSBK_MAXIMUM_BRIGHTNESS / 65535));
});
callback(null, {
count: msg.count,
index: msg.index,
color: msg.color
});
}, sqnNumber);
}
};
/**
* Changes a color zone range to the given HSBK value
* @param {Number} startIndex start zone index from 0 - 255
* @param {Number} endIndex start zone index from 0 - 255
* @param {Number} hue color hue from 0 - 360 (in °)
* @param {Number} saturation color saturation from 0 - 100 (in %)
* @param {Number} brightness color brightness from 0 - 100 (in %)
* @param {Number} [kelvin=3500] color kelvin between 2500 and 9000
* @param {Number} [duration] transition time in milliseconds
* @param {Boolean} [apply=true] apply changes immediately or leave pending for next apply
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.colorZones = function (startIndex, endIndex, hue, saturation, brightness, kelvin, duration, apply, callback) {
validate.zoneIndex(startIndex, 'color zones method');
validate.zoneIndex(endIndex, 'color zones method');
validate.colorHsb(hue, saturation, brightness, 'color zones method');
validate.optionalKelvin(kelvin, 'color zones method');
validate.optionalDuration(duration, 'color zones method');
validate.optionalBoolean(apply, 'apply', 'color zones method');
validate.optionalCallback(callback, 'color zones method');
// Convert HSB values to packet format
hue = Math.round(hue / constants.HSBK_MAXIMUM_HUE * 65535);
saturation = Math.round(saturation / constants.HSBK_MAXIMUM_SATURATION * 65535);
brightness = Math.round(brightness / constants.HSBK_MAXIMUM_BRIGHTNESS * 65535);
var appReq = apply === false ? constants.APPLICATION_REQUEST_VALUES.NO_APPLY : constants.APPLICATION_REQUEST_VALUES.APPLY;
var packetObj = packet.create('setColorZones', {
startIndex: startIndex,
endIndex: endIndex,
hue: hue,
saturation: saturation,
brightness: brightness,
kelvin: kelvin,
duration: duration,
apply: appReq
}, this.client.source);
packetObj.target = this.id;
this.client.send(packetObj, callback);
};
/**
* Changes a color zone range to the given HSBK value
* @param {String} effectName sets the desired effect, currently available options are: MOVE, OFF
* @param {Number} speed sets duration of one cycle of the effect, the higher the value the slower the effect animation
* @param {String} direction whether to animate from or towards the controller, available options are: TOWARDS, AWAY
* @param {Function} [callback] called when light did receive message
*/
Light.prototype.setMultiZoneEffect = function (effectName, speed, direction, callback) {
if (!includes(constants.MULTIZONE_EFFECTS, effectName)) {
throw new TypeError('light.setMultiZoneEffect expects effectName to be one of "MOVE" or "OFF"');
}
if (!isNumber(speed)) {
throw new TypeError('light.setMultiZoneEffect expects speed to be a number');
}
if (!includes(constants.MULTIZONE_EFFECTS_MOVE_DIRECTION, direction)) {
throw new TypeError('light.setMultiZoneEffect expects direction to be one of "TOWARDS" or "AWAY"');
}
validate.optionalCallback(callback, 'light.setMultiZoneEffect');
var packetObj = packet.create('setMultiZoneEffect', {
effectType: constants.MULTIZONE_EFFECTS.indexOf(effectName),
speed: speed,
parameter2: constants.MULTIZONE_EFFECTS_MOVE_DIRECTION.indexOf(direction)
}, this.client.source);
packetObj.target = this.id;
this.client.send(packetObj, callback);
};
exports.Light = Light; |