Reorganise/de-duplicate Philips Hue node

This commit is contained in:
Dave C-J
2014-01-12 12:06:13 +00:00
parent f61b8ae034
commit 4cd6be3cde
9 changed files with 62 additions and 465 deletions

View File

@@ -13,7 +13,6 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="Discover">
@@ -29,25 +28,27 @@
<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="Discover">
<p>This node looks for a Philips Hue Bridge in the local network.</p><p> The node has 2 outputs, the first one contains the IP address of the first discovered bridge and the second one the lights registered to that bridge (in JSON format).</p> <p>To use the node you need to have obtained a valid auth token (or username) from your Philips Hue Bridge. Read <a href="http://developers.meethue.com/gettingstarted.html" target="_blank">here</a> on how to do this.</p>
<p>This node looks for a Philips Hue Bridge in the local network.</p>
<p> The node has 2 outputs, the first one contains the IP address of the first discovered bridge and the second one the lights registered to that bridge (in JSON format).</p>
<p>To use the node you need to have obtained a valid auth token (or username) from your Philips Hue Bridge. Read <a href="http://developers.meethue.com/gettingstarted.html" target="_blank">here</a> on how to do this.</p>
</script>
<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('Discover',{
category: 'advanced-input', // the palette category
category: 'advanced-input',
color:"#EFEFEF",
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
defaults: {
name: {value:""},
username: {value:"", required:true}
},
inputs:1, // set the number of inputs - only 0 or 1
outputs:2, // set the number of outputs - 0 to n
icon: "huediscover.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name||this.topic||"Discover";
inputs:1,
outputs:2,
icon: "hue.png",
label: function() {
return this.name||"Discover";
},
labelStyle: function() { // sets the class to apply to the label
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});

View File

@@ -13,7 +13,6 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="HueNode">
@@ -57,23 +56,23 @@
<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('HueNode',{
category: 'advanced-input', // the palette category
category: 'advanced-input',
color:"#EFEFEF",
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
defaults: {
name: {value:""},
username: {value:"", required:true},
discovery_mode: {value: "", required:false},
lamp_id: {value:"", required:false},
color: {value:"EBF5FF"},
lamp_status:{}
},
inputs:1, // set the number of inputs - only 0 or 1
outputs:1, // set the number of outputs - 0 to n
icon: "huemanage.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name||this.topic||"HueNode";
inputs:1,
outputs:1,
icon: "hue.png",
label: function() {
return this.name||"HueNode";
},
labelStyle: function() { // sets the class to apply to the label
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});

View File

@@ -18,18 +18,13 @@
* limitations under the License.
**/
//Require node-hue-api
var hue = require("node-hue-api");
var HueApi = require("node-hue-api").HueApi;
// Require main module
var RED = require(process.env.NODE_RED_HOME+"/red/red");
//store the IP address of the Hue Gateway
var gw_ipaddress = "";
var username, lamp_status, lamp_id, color;
function hexToRgb(hex) {
@@ -41,84 +36,73 @@ function hexToRgb(hex) {
} : null;
}
// The main node definition - most things happen in here
function HueNode(n) {
// Create a RED node
RED.nodes.createNode(this,n);
var node = this;
//get parameters from user
this.username = n.username;
this.lamp_status = n.lamp_status;
this.lamp_id = n.lamp_id;
this.color = n.color;
var node = this;
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic;
var msg = {};
msg.topic = this.topic;
this.on("input", function(msg){
//set the lamp status
//first locate the Hue gateway:
hue.locateBridges(function(err, result) {
//set the lamp status
//first locate the Hue gateway:
hue.locateBridges(function(err, result) {
var msg2 = {};
msg2.topic = this.topic;
if (err) throw err;
//check for found bridges
if(result[0]!=null) {
//save the IP address of the 1st bridge found
this.gw_ipaddress = result[0].ipaddress;
var msg2 = {};
msg2.topic = this.topic;
if (err) throw err;
//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();
}
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();
}
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.off()).then(displayResult).fail(displayError).done();
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();
}
msg2.payload = 'Light with ID: '+node.lamp_id+ ' was set to '+status;
node.send(msg2);
}
else {
//bridge not found:
var msg = {};
msg.payload = "Bridge not found!";
node.send(msg);
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 {
//bridge not found:
var msg = {};
msg.payload = "Bridge not found!";
node.send(msg);
}
});
});
this.on("close", function() {
// Called when the node is shutdown - eg on redeploy.
@@ -137,9 +121,6 @@ var displayError = function(err) {
console.error(err);
};
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("HueNode",HueNode);
RED.nodes.registerType("HueNode",HueNode);

BIN
hardware/hue/icons/hue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB