ttb-node-hue
Version:
Nodes to handle Philips Hue for the Thingbox and TimeSquAir (and also stock Node-RED)
272 lines (252 loc) • 9.57 kB
HTML
<!--
Modified for the Thingbox
Digital Airways 2015
-->
<!--
Copyright 2015 Urbiworx
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="Hue Pull">
<div class="form-row">
<label for="node-input-serverid"><i class="fa fa-lightbulb-o"></i> <span data-i18n="hue.bridge"></span></label>
<input type="text" id="node-input-serverid" style="width: 250px;">
</input>
</div>
<div class="form-row">
<label for="node-input-deviceid"><i class="fa fa-lightbulb-o"></i> <span data-i18n="hue.bulb"></span></label>
<input type="text" id="node-input-deviceid" style="width: 250px;">
</input>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="hue.name"></span></label>
<input type="text" id="node-input-name" style="width: 250px;">
</input>
</div>
<div class="form-tips" id="node-warning" style="display: none"><b> Tip:</b> If no collection is set, ensure <b>msg.collection</b> will contain the collection name
</div>
</script>
<script type="text/x-red" data-template-name="Hue Set">
<div class="form-row">
<label for="node-input-serverid"><i class="fa fa-lightbulb-o"></i> <span data-i18n="hue.bridge"></span></label>
<input type="text" id="node-input-serverid" style="width: 250px;">
</input>
</div>
<div class="form-row">
<label for="node-input-deviceid"><i class="fa fa-lightbulb-o"></i> <span data-i18n="hue.bulb"></span></label>
<input type="text" id="node-input-deviceid" style="width: 250px;">
</input>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="hue.name"></span></label>
<input type="text" id="node-input-name" style="width: 250px;">
</input>
</div>
<div class="form-tips" id="node-warning" style="display: none"><b> Tip:</b> If no collection is set, ensure <b>msg.collection</b> will contain the collection name
</div>
</script>
<script type="text/x-red" data-help-name="Hue Pull">
<p>Returns the current state of the selected device<br/>Example:<br/>
<pre>
{
"state":{
"on":true,
"bri":1,
"hue":46353,
"sat":249,
"xy":[
0.1448,
0.0964
],
"alert":"none",
"effect":"none",
"colormode":"xy",
"reachable":true
},
"type":"Color light",
"name":"Color light 3",
"modelid":"LLC011",
"uniqueid":"...",
"swversion":"66009461",
"pointsymbol":{
"1":"none",
"2":"none",
"3":"none",
"4":"none",
"5":"none",
"6":"none",
"7":"none",
"8":"none"
}
}
</pre>
</p>
</script>
<script type="text/x-red" data-help-name="Hue Set">
<p>Use this node to manage Philips Hue bulbs</p>
<p>use intent open node to switch the light on, and intent Close to
switch off.</p>
<p>Use Intensity nodes to set parameters:</p>
<ul>
<li><b>intensity or brightness</b>:Sets the brightness, where value
from 0 to 255</li>
<li><b>hue</b>:Sets the hue, where value from 0 to 65535</li>
<li><b>saturation</b>:Sets the saturation value from 0 to 255</li>
<li><b>transitiontime</b>:Sets a transition time. Use 20 for some
seconds or 0 for immediate change.</li>
</ul>
<p>Use the color node to set the color of the bulb.</p>
<p>Note: this version is not able to understand the intents "less" and
"more".</p>
<h3>Advanced users</h3>
<p>You can also use a node function to adress the low level software.
Please see the documentation of the underlying API for options:
<a href="https://github.com/peter-murray/node-hue-api#lightstate-options" target="_blank">
API docs
</a>.<br/>
Example (function node):<br/>
<pre>
msg.payload={
"on":[true],
"transitiontime":[0],
"bri":[1],
"rgb":[75,75,255]
}
return msg;</pre>
</p>
</script>
<script type="text/javascript">
function generatePreparator(){
return function(){
var that = this;
var valueserver="";//$('#node-input-serverid').val();
var valuedevice="";//$('#node-input-deviceid').val();
$.ajax({
url: "philipshue/"+that.id+"/configuration",
method: "GET",
success: function(_conf){
_conf = JSON.parse(_conf);
valueserver = _conf.serverid;
valuedevice = _conf.deviceid;
$("#node-input-serverid").replaceWith('<select id="node-input-serverid" name="node-input-serverid" style="width: 250px;">');
$("#node-input-deviceid").replaceWith('<select id="node-input-deviceid" name="node-input-deviceid" style="width: 250px;">');
$('#node-input-serverid').find('option').remove();
$("#node-input-deviceid").change(function(){
if(!$("#node-input-name").val()){
$("#node-input-name").val($("#node-input-deviceid option:selected").text());
}
var _sid = $("#node-input-serverid").val();
var _did = $("#node-input-deviceid").val();
if(_sid && _did){
$.ajax({
url: "philipshue/"+that.id+"/server/"+_sid+"/device/"+_did+"/select",
method: "GET",
success: function(){},
error:console.error
});
}
});
var deviceRefresh=function(){
if ($('#node-input-serverid').val()==null){
return;
}
$('#node-input-deviceid').find('option').remove();
$.ajax({
url: "philipshue/devices/"+$('#node-input-serverid').val(),
success: function(data) {
var data=JSON.parse(data);
if (data.error===1){
$("#node-dialog-cancel").click();
$("<div>Please press the sync button on your hue and try again after.</div>").dialog();
return;
}
$.each( data.lights, function( key, value ) {
$('#node-input-deviceid').find('option').end().append('<option value="'+key+'">'+value.name+'</option>');
});
$('#node-input-deviceid').find('option').end().append('<option disabled>----------</option>');
$('#node-input-deviceid').find('option').end().append('<option value="g-0">All</option>');
$.each( data.groups, function( key, value ) {
$('#node-input-deviceid').find('option').end().append('<option value="g-'+key+'">'+value.name+'</option>');
});
$('#node-input-deviceid').val(valuedevice);
}
});
};
$("#node-input-serverid").change(deviceRefresh);
refresh(function(){
$('#node-input-serverid').val(valueserver);
deviceRefresh();
/*
$("#node-input-devicetype").change(function(){
$('#node-input-serverid').find('option').remove();
refresh();
});*/
});
function refresh(listener){
$.ajax({
url: "philipshue/server",
success: function(data) {
var data=JSON.parse(data);
for (var i=0;i<data.length;i++) {
var name = data[i].name;
if(!data[i].name)
name = "Bridge";
$('#node-input-serverid').find('option').end().append('<option value="'+data[i].id+'">'+name+'</option>');
}
if (typeof(listener)!=="undefined"){
listener();
}
}
});
};
},
error:console.error
});
}
};
RED.nodes.registerType('Hue Pull', {
category: 'Things',
color: "#ffffff",
defaults: {
name: {value: ""}
},
inputs: 1,
outputs: 2,
icon: "hue.png",
align:"left",
paletteLabel:"Hue Pull;",
label: function() {
return this.name||"Hue Pull";
},
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: generatePreparator()
});
RED.nodes.registerType('Hue Set', {
category: 'Things',
color: "#ffffff",
defaults: {
name: {value: ""}
},
inputs: 1,
outputs: 2,
icon: "hue.png",
align:"right",
paletteLabel:"Hue Set;",
label: function() {
return this.name||"";
},
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: generatePreparator()
});
</script>