Skip to content

Commit

Permalink
Add node to read current device state
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkow committed Oct 21, 2018
1 parent e41dfef commit 5114165
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
78 changes: 76 additions & 2 deletions tahoma.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script type="text/javascript">

RED.nodes.registerType('tahoma',{
category: 'function',
color: '#E2D96E',
Expand All @@ -9,7 +8,8 @@
device: {value: ""}
},
inputs:1,
outputs:1,
outputs:1,
align: 'right',
icon: "bridge-dash.png",
label: function() {
return this.name || "Tahoma Controllable Element";
Expand Down Expand Up @@ -50,6 +50,61 @@
});
};

$("#node-input-tahomabox").on('change', deviceRefresh);
deviceRefresh()
}
});

RED.nodes.registerType('tahoma-read',{
category: 'function',
color: '#E2D96E',
defaults: {
tahomabox: {value:"", type:"tahoma-config"},
name: {value: ""},
device: {value: ""}
},
inputs: 1,
outputs: 1,
icon: "bridge-dash.png",
label: function() {
return this.name || "Tahoma Readable Element";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function generateEditForm() {
var valuedevice = $('#node-input-device').val();

$("#node-input-device").replaceWith('<select id="node-input-device" name="node-input-device" style="width: 250px;">');

var deviceRefresh = function () {
var tahomaBoxValue = $("#node-input-tahomabox").val();

if(tahomaBoxValue.length <= 0 || tahomaBoxValue === "_ADD_") {
$("#node-input-device").empty().prop('disabled', true)
return;
}

$("#node-input-device").empty().prop('disabled', false)

$.ajax({
url: "tahomasomfy/getSetup/"+ $("#node-input-tahomabox").val(),
success: function(data) {
if(data.setup === undefined || data.setup.devices === undefined || data.setup.devices.length === 0) {
$("#node-input-device").empty().html('<option value="_NO_DEVICE_" selected="selected">No devices found.</option>');
} else {
var html = "";

$.each( data.setup.devices, function( key, value ) {
html += '<option value="'+ value.deviceURL +'">['+ value.uiClass +'] - '+value.label+'</option>';
});

$("#node-input-device").empty().html(html).val(valuedevice);
}
}
});
};

$("#node-input-tahomabox").on('change', deviceRefresh);
deviceRefresh()
}
Expand Down Expand Up @@ -86,6 +141,25 @@
<p>Execute actions on your Somfy Tahoma controllable devices.</p>
</script>

<script type="text/x-red" data-template-name="tahoma-read">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name">
</div>
<div class="form-row">
<label for="node-input-tahomabox"><i class="icon-tag"></i> Tahoma Box</label>
<input type="text" id="node-input-tahomabox">
</div>
<div class="form-row">
<label for="node-input-device"><i class="icon-tag"></i> Device</label>
<input type="text" id="node-input-device">
</div>
</script>

<script type="text/x-red" data-help-name="tahoma-read">
<p>Reads the current position/value of a given Tahoma controllable device.</p>
</script>

<script type="text/x-red" data-template-name="tahoma-config">
<div class="form-row">
<label for="node-config-input-username"><i class="icon-bookmark"></i> Username:</label>
Expand Down
23 changes: 23 additions & 0 deletions tahoma.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ module.exports = function(RED) {
}
RED.nodes.registerType("tahoma", TahomaNode);

function TahomaNodeRead(config) {
RED.nodes.createNode(this, config);

this.device = config.device;
this.tahomabox = config.tahomabox;

var node = this;
var configNode = RED.nodes.getNode(node.tahomabox);

node.on('input', function(msg) {
tahomalink.login(configNode.username, configNode.password)
.then(function() {
tahomalink.getDeviceState(node.device)
.then(function(data) {
console.log(data);
msg.payload = data;
node.send(msg);
});
});
});
}
RED.nodes.registerType('tahoma-read', TahomaNodeRead);

function TahomaConfigNode(n) {
RED.nodes.createNode(this,n);
this.username = n.username;
Expand Down

0 comments on commit 5114165

Please sign in to comment.