UNPKG

node-red-contrib-indigo

Version:
589 lines (512 loc) 22.2 kB
<!-- Indigo nodes for IBM's Node-Red https://github.com/pdmangel/node-red-contrib-indigo (c) 2017, Peter De Mangelaere <peter.demangelaere@gmail.com> 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="indigo-controller"> <div class="form-row"> <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-config-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-config-input-protocol"><i class="fa fa-unlock-alt"></i> Protocol</label> <select id="node-config-input-protocol" name="node-config-input-protocol" style="width: 250px;"> <option value="http">http</option> <!--<option value="https">https</option>--> </select> </div> <div class="form-row"> <label for="node-config-input-host"><i class="fa fa-globe"></i> Host</label> <input type="text" id="node-config-input-host"> </div> <div class="form-row"> <label for="node-config-input-port"><i class="icon-bookmark"></i> Port</label> <input type="text" id="node-config-input-port"> </div> <div class="form-row"> <label for="node-config-input-username"><i class="fa fa-user"></i> Username</label> <input type="text" id="node-config-input-username"> </div> <div class="form-row"> <label for="node-config-input-password"><i class="fa fa-user-secret"></i> Password</label> <input type="password" id="node-config-input-password"> </div> </script> <script type="text/x-red" data-template-name="indigo-in"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label> <input type="text" id="node-input-controller"> </div> <div class="form-row"> <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label> <select id="node-input-itemname" name="node-input-itemname" style="width: 250px;"> </select> </div> </script> <script type="text/x-red" data-template-name="indigo-get-device"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label> <input type="text" id="node-input-controller"> </div> <div class="form-row"> <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label> <select id="node-input-itemname" name="node-input-itemname" style="width: 250px;"> </select> </div> </script> <script type="text/x-red" data-template-name="indigo-get-variable"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label> <input type="text" id="node-input-controller"> </div> <div class="form-row"> <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label> <select id="node-input-itemname" name="node-input-itemname" style="width: 250px;"> </select> </div> </script> <script type="text/x-red" data-template-name="indigo-exec-action"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label> <input type="text" id="node-input-controller"> </div> <div class="form-row"> <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label> <select id="node-input-itemname" name="node-input-itemname" style="width: 250px;"> </select> </div> </script> <script type="text/x-red" data-template-name="indigo-set-device"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label> <input type="text" id="node-input-controller"> </div> <div class="form-row"> <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label> <select id="node-input-itemname" name="node-input-itemname" style="width: 250px;"> </select> </div> <div class="form-row"> <label for="node-input-key"><i class="fa fa-tag"></i> Key</label> <select id="node-input-key" name="node-input-key" style="width: 250px;"> <option value="isOn">On/Off State</option> <option value="brightness">Brightness</option> </select> </div> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Value (Uses msg.payload if blank)</label> <input type="text" id="node-input-value" placeholder=""> </div> </script> <script type="text/x-red" data-template-name="indigo-set-variable"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-input-controller"><i class="fa fa-globe"></i> Controller</label> <input type="text" id="node-input-controller"> </div> <div class="form-row"> <label for="node-input-itemname"><i class="fa fa-crosshairs"></i> ItemName</label> <select id="node-input-itemname" name="node-input-itemname" style="width: 250px;"> </select> </div> </script> <script type="text/x-red" data-help-name="indigo-controller"> <p>Configuration node for communication with an Indigo controller.</p> <p></p> <b>Configuration</b> <ul> <li><b>Name :</b> Specify a name for the configuration node</li> <li><b>Protocol :</b> "http" or "https"</li> <li><b>Host :</b> Specify the hostname or ip address</li> <li><b>Port :</b> (Optionally) Specify the ip port</li> <li><b>Username :</b> (Optionally) Specify the username to authenticate</li> <li><b>Password :</b> (Optionally) Specify the password to authenticate</li> </ul> </script> <script type="text/x-red" data-help-name="indigo-in"> <p>Listens to state changes of a selected Indigo Item.</p> <p></p> <b>Configuration</b> <ul> <li><b>Name :</b> Optionally specify a name</li> <li><b>Controller :</b> Select the Indigo controller</li> <li><b>Item :</b>Select the Item to monitor</li> </ul> <p></p> <b>Messages injected in NodeRED flows (2 channels)</b> <ul> <li>Channel 1 : <ul> <li><code>msg.item</code> : the item's itemname (not label)</li> <li><code>msg.topic</code> : "StateEvent"</li> <li><code>msg.payload</code> : the new state of the selected item</li> </ul> </li> </ul> </script> <script type="text/x-red" data-help-name="indigo-get-device"> <p>Gets an Indigo Item on an input message.</p> <p></p> <b>Configuration</b> <ul> <li><b>Name :</b> Optionally specify a name</li> <li><b>Controller :</b> Select the Indigo controller</li> <li><b>Item :</b> Optionally select the Item to address. If specified, it overrides the item specified in the incoming message.</li> </ul> <p></p> <b>Messages injected in NodeRED flows (1 channel)</b> The input message with addition of : <ul> <li><code>msg.payload</code> : the item object (name, label, state, ...)</li> <li><code>msg.payload_in</code> : copy of incoming message's payload</li> </ul> </script> <script type="text/x-red" data-help-name="indigo-get-variable"> <p>Gets an Indigo Item on an input message.</p> <p></p> <b>Configuration</b> <ul> <li><b>Name :</b> Optionally specify a name</li> <li><b>Controller :</b> Select the Indigo controller</li> <li><b>Item :</b> Optionally select the Item to address. If specified, it overrides the item specified in the incoming message.</li> </ul> <p></p> <b>Messages injected in NodeRED flows (1 channel)</b> The input message with addition of : <ul> <li><code>msg.payload</code> : the item object (name, label, state, ...)</li> <li><code>msg.payload_in</code> : copy of incoming message's payload</li> </ul> </script> <script type="text/x-red" data-help-name="indigo-exec-action"> <p>Executes an Indigo Action Group on an input message.</p> <p></p> <b>Configuration</b> <ul> <li><b>Name :</b> Optionally specify a name</li> <li><b>Controller :</b> Select the Indigo controller</li> <li><b>Item :</b> Optionally select the Item to address. If specified, it overrides the item specified in the incoming message.</li> </ul> <p></p> <b>Messages injected in NodeRED flows (1 channel)</b> The input message with addition of : <ul> <li><code>msg.payload</code> : the item object (name, label, state, ...)</li> <li><code>msg.payload_in</code> : copy of incoming message's payload</li> </ul> </script> <script type="text/javascript"> RED.nodes.registerType('indigo-controller', { category: 'config', defaults: { name: {value:"",required:true}, protocol: {value:"http",required:true}, host: {value:"localhost",required:true}, port: {value:8176, required:false /*, validate:RED.validators.number() */}, username: {value:"", required:false}, password: {value:"", required:false} }, label: function() { return this.name; } }); </script> <script type="text/javascript"> // some code chared by indigo-in and indigo-out function indigoEditPrepare(node, allowEmpty, itemType) { function updateIndigoItemSelection(cntrlConfig, itemName, itemType) { var itemSelectionEl = $('#node-input-itemname'); itemSelectionEl.children().remove(); if ( cntrlConfig ) { var config = { name: cntrlConfig.name, protocol: cntrlConfig.protocol, host: cntrlConfig.host, port: cntrlConfig.port, username: cntrlConfig.username, password: cntrlConfig.password } console.log("config = " + JSON.stringify(config)); $.getJSON("indigo/" + itemType + "s", config) .done( function(data) { itemSelectionEl.children().remove(); var items = data; items.sort(function(a,b) { if ( a.name < b.name ) return -1; else if ( a.name > b.name ) return 1; else return 0; }); if ( allowEmpty ) itemSelectionEl.append('<option value=""></option>'); items.forEach(function(item) { itemSelectionEl.append('<option>' + item.name + '</option>'); var it = itemSelectionEl.find('option').last(); it.attr('value', item.name); }); itemSelectionEl.val(itemName); }) .fail(function(e1, e2, e3) { console.log("error : " + JSON.stringify(e1)); }); } } updateIndigoItemSelection(RED.nodes.node(node.controller), node.itemname, itemType); $('#node-input-controller').change(function(ev) { updateIndigoItemSelection(RED.nodes.node($('#node-input-controller').val()), $('#node-input-').val() || node.itemname, itemType); }); } function indigoEditPrepareMQTT(node, allowEmpty, itemType) { function updateIndigoItemSelectionMQTT(cntrlConfig, itemId, itemType) { var itemSelectionEl = $('#node-input-itemid'); itemSelectionEl.children().remove(); $.getJSON("indigo/MQTT" + itemType + "s") .done( function(data) { itemSelectionEl.children().remove(); var items = data; items.sort(function(a,b) { if ( a.name < b.name ) return -1; else if ( a.name > b.name ) return 1; else return 0; }); if ( allowEmpty ) itemSelectionEl.append('<option value=""></option>'); items.forEach(function(item) { itemSelectionEl.append('<option value="' + item.id + '">' + item.name + '</option>'); var it = itemSelectionEl.find('option').last(); it.attr('value', item.id); }); itemSelectionEl.val(itemId); }) .fail(function(e1, e2, e3) { console.log("error : " + JSON.stringify(e1)); }); } updateIndigoItemSelectionMQTT(RED.nodes.node(node.controller), node.itemid, itemType); $('#node-input-controller').change(function(ev) { // debugger; updateIndigoItemSelectionMQTT(RED.nodes.node($('#node-input-controller').val()), $('#node-input-').val() || node.itemid, itemType); }); $('#node-input-itemid').change(function(ev) { if ($('#node-input-itemid option:selected').text() != "" ){ node.itemname = $('#node-input-itemid option:selected').text() } }); $('#node-input-state').autocomplete({ source: ['onOffState','brightnessLevel'] }) } </script> <script type="text/javascript"> RED.nodes.registerType('indigo-get-device',{ category: 'home automation', color: '#d0adff', defaults: { name: {value:""}, controller: {value:"", type:"indigo-controller", required:true}, itemname: {value:"", required:false}, }, inputs: 1, outputs: 1, icon: "node-red-contrib-indigo.png", label: function() { return(this.name||this.itemname||"indigo get device"); }, oneditprepare: function() { indigoEditPrepare(this, true,'device'); } }); </script> <script type="text/javascript"> RED.nodes.registerType('indigo-get-variable',{ category: 'home automation', color: '#d0adff', defaults: { name: {value:""}, controller: {value:"", type:"indigo-controller", required:true}, itemname: {value:"", required:false}, }, inputs: 1, outputs: 1, icon: "node-red-contrib-indigo.png", label: function() { return(this.name||this.itemname||"indigo get variable"); }, oneditprepare: function() { indigoEditPrepare(this, true,'variable'); } }); </script> <script type="text/javascript"> RED.nodes.registerType('indigo-exec-action',{ category: 'home automation', color: '#d0adff', defaults: { name: {value:""}, controller: {value:"", type:"indigo-controller", required:true}, itemname: {value:"", required:false}, }, inputs: 1, outputs: 1, icon: "node-red-contrib-indigo.png", label: function() { return(this.name||this.itemname||"indigo exec action"); }, oneditprepare: function() { indigoEditPrepare(this, true,'action'); } }); </script> <script type="text/javascript"> RED.nodes.registerType('indigo-set-device',{ category: 'home automation', color: '#d0adff', defaults: { name: {value:""}, controller: {value:"", type:"indigo-controller", required:true}, itemname: {value:"", required:false}, key: {value:"isOn"}, value: {value:""}, }, inputs: 1, outputs: 1, icon: "node-red-contrib-indigo.png", label: function() { return(this.name||this.itemname||"indigo set device"); }, oneditprepare: function() { indigoEditPrepare(this, true,'device'); } }); </script> <script type="text/javascript"> RED.nodes.registerType('indigo-set-variable',{ category: 'home automation', color: '#d0adff', defaults: { name: {value:""}, controller: {value:"", type:"indigo-controller", required:true}, itemname: {value:"", required:false}, }, inputs: 1, outputs: 1, icon: "node-red-contrib-indigo.png", label: function() { return(this.name||this.itemname||"indigo set variable"); }, oneditprepare: function() { indigoEditPrepare(this, true,'variable'); } }); </script> <script type="text/x-red" data-template-name="indigo mqtt in"> <div class="form-row"> <label for="node-input-broker"><i class="fa fa-globe"></i> <span >MQTT Broker</span></label> <input type="text" id="node-input-broker"> </div> <div class="form-row"> <label for="node-input-itemid"><i class="fa fa-crosshairs"></i> Item</label> <select id="node-input-itemid" name="node-input-itemid" style="width: 250px;"> </select> </div> <div class="form-row"> <label for="node-input-state"><i class="fa fa-crosshairs"></i> State</label> <input type="text" id="node-input-state" name="node-input-state" style="width: 250px;"> </div> <div class="form-row"> <label for="node-input-valuefilter"><i class="fa fa-crosshairs"></i> Value Filter</label> <input type="text" id="node-input-valuefilter" name="node-input-valuefilter" style="width: 250px;"> </div> <div class="form-row"> <label for="node-input-onlyonchange"><i class="fa fa-filter"></i> Only On Change</label> <input type="checkbox" id="node-input-onlyonchange" name="node-input-onlyonchange" style="width: 250px;"> </div> <div class="form-row"> <label for="node-input-ignoreretainedmessages"><i class="fa fa-filter"></i> Ignore Retained Messages</label> <input type="checkbox" id="node-input-ignoreretainedmessages" name="node-input-ignoreretainedmessages" style="width: 250px;"> </div> </script> <script type="text/x-red" data-help-name="indigo mqtt in"> <p>Connects to a MQTT broker and subscribes to messages from the specified Indigo device.</p> <h3>Outputs</h3> <dl class="message-properties"> <dt>payload <span class="property-type">object</span></dt> <dd>an object describing what has changed, and how.</dd> <dt>topic <span class="property-type">string</span></dt> <dd>the MQTT topic, uses / as a hierarchy separator.</dd> <dt>qos <span class="property-type">number</span> </dt> <dd>0, fire and forget - 1, at least once - 2, once and once only.</dd> <dt>retain <span class="property-type">boolean</span></dt> <dd>true indicates the message was retained and may be old.</dd> </dl> <h3>Details</h3> <p>This node uses a standard MQTT broker, which can be shared.</p> </script> <script type="text/javascript"> RED.nodes.registerType('indigo mqtt in',{ category: 'home automation', defaults: { name: {value:""}, topic: {value:"",required:false}, qos: {value: "2"}, broker: {type:"mqtt-broker", required:true}, controller: {value:"", type:"indigo-controller", required:false}, itemname: {value:""}, itemid: {value:-1, required:true}, state: {value:"",required:false}, onlyonchange: {value: false}, valuefilter:{value: ''}, ignoreretainedmessages: {value:true} }, color:"#d0adff", inputs:0, outputs:1, icon: "node-red-contrib-indigo.png", label: function() { return this.itemname||this.topic||"indigo device in"; }, labelStyle: function() { return this.name?"node_label_italic":""; }, oneditprepare: function() { indigoEditPrepareMQTT(this, true,'device'); } }); </script>