@@ -2,7 +2,7 @@ var lifx = require('../../lifx');
2
2
var util = require ( 'util' ) ;
3
3
var mqtt = require ( 'mqtt' ) ;
4
4
5
- var broker = '10.1.0.1 ' ;
5
+ var broker = 'localhost ' ;
6
6
var lx = lifx . init ( ) ;
7
7
8
8
console . log ( "Searching for a LIFX gateway..." ) ;
@@ -24,11 +24,9 @@ lx.on('gateway', function(g) {
24
24
mqttClient . publish ( '/lifx/gateway' , JSON . stringify ( { ipAddress :g . ipAddress , lifxAddress :g . lifxAddress . toString ( 'hex' ) } ) ) ;
25
25
} ) ;
26
26
lx . on ( 'bulb' , function ( b ) {
27
- b . lifxAddress = b . lifxAddress . toString ( 'hex' ) ;
28
27
mqttClient . publish ( '/lifx/newbulb' , JSON . stringify ( { bulb :b , mqttTopicBase :"/lifx/bulbcmd/" + b . lifxAddress . toString ( 'hex' ) } ) ) ;
29
28
} ) ;
30
29
lx . on ( 'bulbstate' , function ( s ) {
31
- s . bulb . lifxAddress = s . bulb . lifxAddress . toString ( 'hex' ) ;
32
30
mqttClient . publish ( '/lifx/bulb/' + s . bulb . lifxAddress . toString ( 'hex' ) , JSON . stringify ( s ) ) ;
33
31
} ) ;
34
32
lx . on ( 'bulbonoff' , function ( s ) {
@@ -38,14 +36,25 @@ lx.on('bulbonoff', function(s) {
38
36
mqttClient . subscribe ( '/lifx/bulbcmd/#' ) ;
39
37
mqttClient . on ( 'message' , function ( topic , message ) {
40
38
console . log ( topic + " " + message ) ;
41
- if ( matches = topic . match ( / b u l b c m d \/ ( [ 0 - 9 a - f ] { 16 } ) \/ ( [ a - z ] * ) $ / ) ) {
39
+ if ( matches = topic . match ( / b u l b c m d \/ ( [ 0 - 9 a - f ] { 12 } ) \/ ( [ a - z ] * ) $ / ) ) {
42
40
var lifxAddress = matches [ 1 ] ;
43
41
var cmd = matches [ 2 ] ;
44
42
try {
45
43
var params = JSON . parse ( message ) ;
46
44
} catch ( e ) {
47
45
console . log ( "Could not parse JSON message: " + message ) ;
48
46
}
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
+ }
49
58
switch ( cmd ) {
50
59
51
60
case "colour" :
@@ -65,22 +74,22 @@ mqttClient.on('message', function(topic, message) {
65
74
case "off" :
66
75
if ( typeof params . on != 'undefined' ) {
67
76
if ( params . on ) {
68
- lx . lightsOn ( lifxAddress ) ;
77
+ lx . lightsOn ( bulb ) ;
69
78
} else {
70
- lx . lightsOff ( lifxAddress ) ;
79
+ lx . lightsOff ( bulb ) ;
71
80
}
72
81
} else if ( typeof params . off != 'undefined' ) {
73
82
if ( params . off ) {
74
- lx . lightsOff ( lifxAddress ) ;
83
+ lx . lightsOff ( bulb ) ;
75
84
} else {
76
- lx . lightsOn ( lifxAddress ) ;
85
+ lx . lightsOn ( bulb ) ;
77
86
}
78
87
} else {
79
88
console . log ( "Incomplete message; expecting one of on/off in message " + message ) ;
80
89
}
81
90
break ;
82
91
}
83
- }
92
+ }
84
93
if ( matches = topic . match ( / b u l b c m d \/ a l l \/ ( [ a - z ] * ) $ / ) ) {
85
94
var cmd = matches [ 1 ] ;
86
95
try {
0 commit comments