-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
87 lines (64 loc) · 1.8 KB
/
index.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
'use strict';
var util = require('util');
var stream = require('stream');
var lifx = require('lifx');
//lifx.setDebug(true);
util.inherits(Driver, stream);
util.inherits(Bulb, stream);
module.exports = Driver;
function Driver(opts,app) {
var self = this;
this._app = app;
this._opts = opts;
this._opts.stations = opts.stations || [];
var devices = {};
app.once('client::up', function() {
self.log.info('Firing up!');
var lx = lifx.init();
lx.on('bulb', function(bulb) {
self.log.info('New bulb found: ', bulb);
var device = new Bulb(bulb, lx);
if (!devices[device.G]) {
self.log.info('Registering bulb');
devices[device.G] = device;
self.emit('register', device);
}
});
});
}
function Bulb(bulb, lx) {
var self = this;
this.lx = lx;
this.bulb = bulb;
this.writeable = true;
this.readable = true;
this.V = 0;
this.D = 1008;
this.G = 'Lifx' + bulb.lifxAddress.toString('hex');
this.name = 'Lifx - ' + (bulb.name||'(No Name)');
this.lx.on('packet', function(p) {
if (p.preamble.bulbAddress.toString('hex') === this.bulb.lifxAddress.toString('hex')) {
if (p.packetTypeShortName == 'lightStatus') {
if (this.log) this.log.trace('Light status', p);
this.emit('data', {
hue: p.payload.hue,
sat: p.payload.saturation / 256,
bri: p.payload.brightness / 256,
on: p.payload.power > 0
});
}
}
}.bind(this));
}
Bulb.prototype.write = function(data) {
if (typeof data === 'string') {
data = JSON.parse(data);
}
if (!data.on) {
this.lx.lightsOff(this.bulb);
}
this.lx.lightsColour(data.hue, data.sat*256, data.bri*256, 0x0dac/*TODO*/, data.transitiontime||200, this.bulb);
if (data.on) {
this.lx.lightsOn(this.bulb);
}
};