Skip to content

Commit 25cf6bd

Browse files
committed
Fix bulb address length and the need to pass bulbs as objects instead of hex string address
1 parent d71dbe5 commit 25cf6bd

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

mqtt/bin/lifxmqtt.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var lifx = require('../../lifx');
22
var util = require('util');
33
var mqtt = require('mqtt');
44

5-
var broker = '10.1.0.1';
5+
var broker = 'localhost';
66
var lx = lifx.init();
77

88
console.log("Searching for a LIFX gateway...");
@@ -24,11 +24,9 @@ lx.on('gateway', function(g) {
2424
mqttClient.publish('/lifx/gateway', JSON.stringify({ipAddress:g.ipAddress, lifxAddress:g.lifxAddress.toString('hex')}));
2525
});
2626
lx.on('bulb', function(b) {
27-
b.lifxAddress = b.lifxAddress.toString('hex');
2827
mqttClient.publish('/lifx/newbulb', JSON.stringify({bulb:b,mqttTopicBase:"/lifx/bulbcmd/"+b.lifxAddress.toString('hex')}));
2928
});
3029
lx.on('bulbstate', function(s) {
31-
s.bulb.lifxAddress = s.bulb.lifxAddress.toString('hex');
3230
mqttClient.publish('/lifx/bulb/'+s.bulb.lifxAddress.toString('hex'), JSON.stringify(s));
3331
});
3432
lx.on('bulbonoff', function(s) {
@@ -38,14 +36,25 @@ lx.on('bulbonoff', function(s) {
3836
mqttClient.subscribe('/lifx/bulbcmd/#');
3937
mqttClient.on('message', function(topic, message) {
4038
console.log(topic + " " + message);
41-
if (matches = topic.match(/bulbcmd\/([0-9a-f]{16})\/([a-z]*)$/)) {
39+
if (matches = topic.match(/bulbcmd\/([0-9a-f]{12})\/([a-z]*)$/)) {
4240
var lifxAddress = matches[1];
4341
var cmd = matches[2];
4442
try{
4543
var params = JSON.parse(message);
4644
} catch (e) {
4745
console.log("Could not parse JSON message: " + message);
4846
}
47+
// Find bulb
48+
var bulb = null;
49+
for (var b in lx.bulbs) {
50+
if (lx.bulbs[b].lifxAddress.toString("hex") == lifxAddress) {
51+
bulb = lx.bulbs[b];
52+
}
53+
}
54+
if (!bulb) {
55+
console.log("Bulb " + lifxAddress + " not found");
56+
return;
57+
}
4958
switch (cmd) {
5059

5160
case "colour":
@@ -65,22 +74,22 @@ mqttClient.on('message', function(topic, message) {
6574
case "off":
6675
if (typeof params.on != 'undefined') {
6776
if (params.on) {
68-
lx.lightsOn(lifxAddress);
77+
lx.lightsOn(bulb);
6978
} else {
70-
lx.lightsOff(lifxAddress);
79+
lx.lightsOff(bulb);
7180
}
7281
} else if (typeof params.off != 'undefined') {
7382
if (params.off) {
74-
lx.lightsOff(lifxAddress);
83+
lx.lightsOff(bulb);
7584
} else {
76-
lx.lightsOn(lifxAddress);
85+
lx.lightsOn(bulb);
7786
}
7887
} else {
7988
console.log("Incomplete message; expecting one of on/off in message " + message);
8089
}
8190
break;
8291
}
83-
}
92+
}
8493
if (matches = topic.match(/bulbcmd\/all\/([a-z]*)$/)) {
8594
var cmd = matches[1];
8695
try{

0 commit comments

Comments
 (0)