diff --git a/hardware/hue_manage/104-hue_manage.js b/hardware/hue_manage/104-hue_manage.js index 8d3bf05d..954a8f16 100644 --- a/hardware/hue_manage/104-hue_manage.js +++ b/hardware/hue_manage/104-hue_manage.js @@ -72,41 +72,50 @@ function HueNode(n) { var msg2 = {}; msg2.topic = this.topic; if (err) throw err; - //save the IP address of the 1st bridge found - this.gw_ipaddress = result[0].ipaddress; + //check for found bridges + if(result[0]!=null) { + //save the IP address of the 1st bridge found + this.gw_ipaddress = result[0].ipaddress; - //set light status - var api = new HueApi(this.gw_ipaddress, node.username); - var lightState = hue.lightState; - var state = lightState.create(); + //set light status + var api = new HueApi(this.gw_ipaddress, node.username); + var lightState = hue.lightState; + var state = lightState.create(); - var status; - if(msg.payload=="ALERT"){ - status = "ALERT"; - } - else if(node.lamp_status=="ON" || msg.payload=="ON") status = "ON"; - else if(node.lamp_status=="OFF" || msg.payload=="OFF") status = "OFF"; + var status; + if(msg.payload=="ALERT"){ + status = "ALERT"; + } + else if(node.lamp_status=="ON" || msg.payload=="ON") status = "ON"; + else if(node.lamp_status=="OFF" || msg.payload=="OFF") status = "OFF"; - if(status=="ALERT") { - api.setLightState(node.lamp_id, state.alert()).then(displayResult).fail(displayError).done(); - } - else if(status=="ON") { - if(node.color==null) { - api.setLightState(node.lamp_id, state.on().rgb(hexToRgb(msg.topic).r,hexToRgb(msg.topic).g,hexToRgb(msg.topic).b)).then(displayResult).fail(displayError).done(); + if(status=="ALERT") { + api.setLightState(node.lamp_id, state.alert()).then(displayResult).fail(displayError).done(); + } + else if(status=="ON") { + if(node.color==null) { + api.setLightState(node.lamp_id, state.on().rgb(hexToRgb(msg.topic).r,hexToRgb(msg.topic).g,hexToRgb(msg.topic).b)).then(displayResult).fail(displayError).done(); + } + else { + api.setLightState(node.lamp_id, state.on().rgb(hexToRgb(node.color).r,hexToRgb(node.color).g,hexToRgb(node.color).b)).then(displayResult).fail(displayError).done(); + } } else { - api.setLightState(node.lamp_id, state.on().rgb(hexToRgb(node.color).r,hexToRgb(node.color).g,hexToRgb(node.color).b)).then(displayResult).fail(displayError).done(); + api.setLightState(node.lamp_id, state.off()).then(displayResult).fail(displayError).done(); } + + msg2.payload = 'Light with ID: '+node.lamp_id+ ' was set to '+status; + node.send(msg2); } else { - api.setLightState(node.lamp_id, state.off()).then(displayResult).fail(displayError).done(); + //bridge not found: + var msg = {}; + msg.payload = "Bridge not found!"; + node.send(msg); } - msg2.payload = 'Light with ID: '+node.lamp_id+ ' was set to '+status; - node.send(msg2); - }); });