1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

added a no bridge check

This commit is contained in:
Charalampos Doukas 2013-11-11 16:36:25 +01:00
parent b35ff010e2
commit 6474f5888d

View File

@ -72,41 +72,50 @@ function HueNode(n) {
var msg2 = {}; var msg2 = {};
msg2.topic = this.topic; msg2.topic = this.topic;
if (err) throw err; if (err) throw err;
//save the IP address of the 1st bridge found //check for found bridges
this.gw_ipaddress = result[0].ipaddress; if(result[0]!=null) {
//save the IP address of the 1st bridge found
this.gw_ipaddress = result[0].ipaddress;
//set light status //set light status
var api = new HueApi(this.gw_ipaddress, node.username); var api = new HueApi(this.gw_ipaddress, node.username);
var lightState = hue.lightState; var lightState = hue.lightState;
var state = lightState.create(); var state = lightState.create();
var status; var status;
if(msg.payload=="ALERT"){ if(msg.payload=="ALERT"){
status = "ALERT"; status = "ALERT";
} }
else if(node.lamp_status=="ON" || msg.payload=="ON") status = "ON"; else if(node.lamp_status=="ON" || msg.payload=="ON") status = "ON";
else if(node.lamp_status=="OFF" || msg.payload=="OFF") status = "OFF"; else if(node.lamp_status=="OFF" || msg.payload=="OFF") status = "OFF";
if(status=="ALERT") { if(status=="ALERT") {
api.setLightState(node.lamp_id, state.alert()).then(displayResult).fail(displayError).done(); api.setLightState(node.lamp_id, state.alert()).then(displayResult).fail(displayError).done();
} }
else if(status=="ON") { else if(status=="ON") {
if(node.color==null) { 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(); 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 { 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 { 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);
}); });
}); });