added philips_hue node

This commit is contained in:
Charalampos Doukas 2013-11-05 16:32:42 +01:00
parent 28e82c1351
commit 5747dfe25d
3 changed files with 233 additions and 0 deletions

View File

@ -0,0 +1,79 @@
<!--
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="HueNode">
<div class="form-row">
<label for="node-input-topic"><i class="icon-tasks"></i>Hue App 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> Set Discovery Mode ON:</label>
<input type="checkbox" value="1" id="node-input-discovery_mode" placeholder="discovery_mode">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i>Lamp ID:</label>
<input type="text" id="node-input-lamp_id" placeholder="lamp_id">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i>Lamp Status:</label>
<select id="node-input-lamp_status" placeholder="lamp_status">
<option value="AUTO">AUTO</option>
<option value="ON">ON</option>
<option value="OFF">OFF</option>
</select>
</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="HueNode">
<p>This node implements some basic functionality for accessing and managing a Philips Hue wireless Lamp system.</p>
<p>You can enable the discovery mode to get the IP address of the Hue bridge and a list with the connected Lamps.</p>
<p>You can enter the ID (1, 2, ...) of a Lamp and turn it ON or OFF. </p><p>By setting the status to AUTO, you can set the ON/OFF status as a message payload (e.g., msg.payload="ON") on the node input. Please note, in case you use both, the status selection overides the msg.payload!</p><p>Also, if you pass something like msg.payload="ALERT" the Lamp will flash once.</p>
</script>
<!-- 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
color:"#7B7B79",
defaults: { // defines the editable properties of the node
name: {value:""}, // along with default values.
username: {value:"", required:true},
discovery_mode: {value: "", required:false},
lamp_id: {value:"", required:false},
lamp_status:{}
},
inputs:1, // set the number of inputs - only 0 or 1
outputs:1, // 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||"HueNode";
},
labelStyle: function() { // sets the class to apply to the label
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -0,0 +1,154 @@
/**
* philips_hue.js
* Basic functionality for accessing and contolling a Philips Hue wireless Lamp
* Allows for bridge/gateway and light scanning, as well as Light ON/OFF/ALERT status update
* 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 lights_discovered = "";
// The main node definition - most things happen in here
function HueNode(n) {
// Create a RED node
RED.nodes.createNode(this,n);
var node = this;
var username, discovery_mode, lamp_status, lamp_id;
//get parameters from user
this.username = n.username;
this.discovery_mode = n.discovery_mode;
this.lamp_status = n.lamp_status;
this.lamp_id = n.lamp_id;
// 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){
//check if users has selected discovery mode, provide output:
if(this.discovery_mode==1) {
//start with detecting the IP address of the Hue gateway in the local network:
hue.locateBridges(function(err, result) {
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;
msg2.payload = this.gw_ipaddress;
node.send(msg2);
//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:node.topic, payload: { ipaddress:this.gw_ipaddress, lights:lights_discovered} };
node.send(msg2);
});
});
}
else {
//set the lamp status
//first locate the Hue gateway:
hue.locateBridges(function(err, result) {
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;
//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";
if(status=="ALERT") {
api.setLightState(node.lamp_id, state.alert()).then(displayResult).fail(displayError).done();
}
else if(status=="ON") {
api.setLightState(node.lamp_id, state.on()).then(displayResult).fail(displayError).done();
}
else {
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);
});
}
});
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("HueNode",HueNode);

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB