Added 2 Philips Hue nodes, hue_discover and hue_manage

This commit is contained in:
Charalampos Doukas
2013-11-11 15:04:32 +01:00
parent 85797eb7fc
commit b35ff010e2
6 changed files with 375 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<!--
Copyright 2013 Charalampos Doukas.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="Discover">
<div class="form-row">
<label for="node-input-username"><i class="icon-tag"></i> Username:</label>
<input type="text" id="node-input-username" placeholder="username">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<!-- 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>
</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
color:"#EFEFEF",
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
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: "philipshue.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name||this.topic||"Discover";
},
labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":"";
}
});
</script>

View File

@@ -0,0 +1,105 @@
/**
* philips_hue.js
* Basic functionality for accessing a Philips Hue wireless Lamp
* Allows for bridge/gateway detection and light scanning.
* Requires node-hue-api https://github.com/peter-murray/node-hue-api
* Copyright 2013 Charalampos Doukas - @BuildingIoT
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* 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;
// The main node definition - most things happen in here
function HueNodeDiscovery(n) {
// Create a RED node
RED.nodes.createNode(this,n);
var node = this;
//get username from user input
this.username = n.username;
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic;
this.on("input", function(msg){
//start with detecting the IP address of the Hue gateway in the local network:
hue.locateBridges(function(err, result) {
var msg = {};
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;
msg.payload = this.gw_ipaddress;
//get light info:
var api = new HueApi(this.gw_ipaddress, node.username);
api.lights(function(err, lights) {
var msg2 = {};
if (err) throw err;
var lights_discovered = JSON.stringify(lights, null, 2);
msg2.topic = "Lights";
msg2.payload = lights_discovered;
node.send([msg, 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.
// Allows ports to be closed, connections dropped etc.
// eg: this.client.disconnect();
});
}
//hue debugging on the output:
var displayResult = function(result) {
console.log(result);
};
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("Discover",HueNodeDiscovery);

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB