forked from magicmonkey/lifxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifx.js
285 lines (239 loc) · 7.21 KB
/
lifx.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
var dgram = require('dgram');
var net = require('net');
var util = require('util');
var events = require('events');
var clone = require('clone');
var packet = require('./packet');
var port = 56700;
var debug = false;
function init() {
var l = new Lifx();
l.startDiscovery();
return l;
}
function Lifx() {
events.EventEmitter.call(this);
this.gateways = [];
this.bulbs = [];
this._intervalID = null;
}
Lifx.prototype.__proto__ = events.EventEmitter.prototype;
Lifx.prototype.startDiscovery = function() {
var self = this;
var UDPClient = dgram.createSocket("udp4");
UDPClient.unref(); // Stop this from preventing Node from ending
UDPClient.on("error", function (err) {
console.log("UDP error " + err);
});
UDPClient.on("message", function (msg, rinfo) {
if (debug) console.log(" U- " + msg.toString("hex"));
var pkt = packet.fromBytes(msg);
if (pkt.packetTypeShortName == 'panGateway' && pkt.payload.service == 1) {
var gw = new Gateway(rinfo.address, pkt.payload.port, pkt.preamble.site);
self.foundGateway(gw);
}
});
UDPClient.bind(port, "0.0.0.0", function() {
UDPClient.setBroadcast(true);
var intervalID;
// Now send the discovery packets
self._intervalID = setInterval(function() {
var message = packet.getPanGateway({protocol:21504});
if (debug) console.log(" U+ " + message.toString("hex"));
UDPClient.send(message, 0, message.length, port, "255.255.255.255", function(err, bytes) {
});
}, 1000);
});
};
Lifx.prototype.foundGateway = function(gw) {
var found = false;
for (var i in this.gateways) {
if (this.gateways[i].ipAddress.ip.toString('hex') == gw.ipAddress.ip.toString('hex')) {
found = true;
}
}
if (!found) {
this.gateways.push(gw);
// Look for bulbs on this gateway
gw.connect();
gw.on('_packet', this._getPacketHandler());
gw.findBulbs();
this.emit("gateway", gw);
}
};
Lifx.prototype._getPacketHandler = function() {
var self = this;
return function(p, gw) {self._gotPacket(p, gw);};
};
Lifx.prototype._gotPacket = function(data, gw) {
if (debug) console.log(" T- " + data.toString("hex"));
var p = packet.fromBytes(data);
switch (p.packetTypeShortName) {
case 'lightStatus':
this.foundBulb(p, gw);
break;
}
this.emit('packet', clone(p));
};
Lifx.prototype._gotPacket_old = function(data, gw) {
if (debug) console.log(" T- " + data.toString("hex"));
switch (data[32]) {
case 0x6b:
this.foundBulb(data, gw);
break;
case 0x16:
var bulb = this.getBulbByLifxAddress(data.slice(8,14));
if (data[37] == 0xff) {
this.emit('bulbonoff', {bulb:clone(bulb),on:true});
if (debug) console.log(" * Light is on");
} else {
this.emit('bulbonoff', {bulb:clone(bulb),on:false});
if (debug) console.log(" * Light is off");
}
break;
}
};
Lifx.prototype.foundBulb = function(bulb, gw) {
var bulbName = bulb.payload.bulbLabel;
var lifxAddress = bulb.preamble.bulbAddress;
if (debug) console.log(" * Found a bulb: " + bulbName + " (address " + util.inspect(lifxAddress) + ")");
var foundBulb = null;
for (var bulbId in this.bulbs) {
if (this.bulbs[bulbId].lifxAddress.toString("hex") == lifxAddress.toString("hex")) {
foundBulb = this.bulbs[bulbId];
}
}
if (!foundBulb) {
var newBulb = new Bulb(lifxAddress, bulbName);
if (debug) console.log("*** New bulb found (" + newBulb.name + ") by gateway " + gw.ipAddress.ip + " ***");
this.bulbs.push(newBulb);
this.emit('bulb', clone(newBulb));
foundBulb = newBulb;
}
this.emit('bulbstate', {bulb:foundBulb, state:bulb.payload});
};
// This represents each individual bulb
function Bulb(_lifxAddress, _name) {
this.lifxAddress = _lifxAddress;
this.name = _name;
}
// This represents the gateway, and its respective functions (eg discovery, send-to-all etc)
function Gateway(ipAddress, port, site) {
// TODO: validation...
this.ipAddress = {ip:ipAddress, port:port};
this.lifxAddress = site;
this.tcpClient = null;
this.reconnect = true;
events.EventEmitter.call(this);
}
// Make the Gateway into an event emitter
Gateway.prototype.__proto__ = events.EventEmitter.prototype;
Lifx.prototype.getBulbByLifxAddress = function(lifxAddress) {
var addrToSearch = lifxAddress;
if (typeof lifxAddress != 'string') {
addrToSearch = lifxAddress.toString('hex');
}
for (var i in this.bulbs) {
if (this.bulbs[i].lifxAddress.toString('hex') == addrToSearch) {
return this.bulbs[i];
}
}
return false;
};
// Open a control connection (over TCP) to the gateway node
Gateway.prototype.connect = function() {
var self = this;
if (debug) console.log("Connecting to " + this.ipAddress.ip + ":" + this.ipAddress.port);
this.tcpClient = net.connect(this.ipAddress.port, this.ipAddress.ip);
this.tcpClient.on('data', function(data) {
self.emit('_packet', data, self);
});
this.tcpClient.on('error', function(err) {
console.log(err);
});
this.tcpClient.on('end', function() {
console.log('TCP client disconnected');
self.tcpClient.destroy();
if (self.reconnect) {
self.connect();
}
});
};
Lifx.prototype.findBulbs = function() {
this.gateways.forEach(function(g) {
g.findBulbs();
});
};
// This method requests that the gateway tells about all of its bulbs
Gateway.prototype.findBulbs = function() {
this.send(packet.getLightState());
};
// Send a raw command
Gateway.prototype.send = function(sendBuf) {
if (debug) console.log(" T+ " + sendBuf.toString("hex"));
var siteAddress = this.lifxAddress;
siteAddress.copy(sendBuf, 16);
this.tcpClient.write(sendBuf);
};
// Close the connection to this gateway
Gateway.prototype.close = function() {
this.reconnect = false;
this.tcpClient.end();
};
Lifx.prototype.close = function() {
clearInterval(this._intervalID);
this.gateways.forEach(function(g) {
g.close();
});
};
Lifx.prototype.sendToAll = function(command) {
this._sendToOneOrAll(command);
};
Lifx.prototype.sendToOne = function(command, bulb) {
this._sendToOneOrAll(command, bulb);
};
Lifx.prototype._sendToOneOrAll = function(command, bulb) {
this.gateways.forEach(function(g) {
var siteAddress = g.lifxAddress;
siteAddress.copy(command, 16);
if (typeof bulb == 'undefined') {
g.send(command);
} else {
// Overwrite the bulb address here
var target;
if (Buffer.isBuffer(bulb)) {
target = bulb;
} else if (typeof bulb.lifxAddress != 'undefined') {
target = bulb.lifxAddress;
} else {
throw "Unknown bulb";
}
target.copy(command, 8);
g.send(command);
}
});
};
/////// Fun methods ////////
// Turn all lights on
Lifx.prototype.lightsOn = function(bulb) {
this._sendToOneOrAll(packet.setPowerState({onoff:1}), bulb);
};
// Turn all lights off
Lifx.prototype.lightsOff = function(bulb) {
this._sendToOneOrAll(packet.setPowerState({onoff:0}), bulb);
};
// Set all bulbs to a particular colour
// Pass in 16-bit numbers for each param - they will be byte shuffled as appropriate
Lifx.prototype.lightsColour = function(hue, sat, lum, whitecol, timing, bulb) {
var params = {stream:0, hue:hue, saturation:sat, brightness:lum, kelvin:whitecol, fadeTime:timing};
var message = packet.setLightColour(params);
this._sendToOneOrAll(message, bulb);
};
// Request status from bulbs
Lifx.prototype.requestStatus = function() {
this._sendToOneOrAll(packet.getLightState());
};
module.exports = {
init:init,
setDebug:function(d){debug=d;}
};