UNPKG

node-red-contrib-iot-virtual-device

Version:

A set of nodes to help you create simulated IoT devices connected to IBM Watson IoT Platform.

133 lines (113 loc) 4.76 kB
<!-- Copyright 2014, 2015, 2016 IBM Corp. 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. --> <!--device function--> <script type="text/x-red" data-template-name="device function"> <div class="form-row"> <label for="node-input-name" style="width:130px;"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="device function" style="width:300px;"> </div> <div class="form-row"> <label for="node-input-deviceId" style="width:130px;"><i class="fa fa-street-view"></i> Device Id</label> <input type="text" id="node-input-deviceId" style="width:300px;"> </div> <div class="form-row" id = "node-input-schema-div"> <label for="node-input-schema" id="node-input-schemaLabel" style="width:130px;"><i class="fa fa-sitemap"></i> Device Schema</label> <input type="text" id="node-input-schema" style="width:300px;"> </div> <div class="form-row" style="margin-bottom: 0px;"> <label for="node-input-func"><i class="fa fa-wrench"></i> <span data-i18n="function.label.function"></span></label> <input type="hidden" id="node-input-func" autofocus="autofocus"> <input type="hidden" id="node-input-noerr"> </div> <div class="form-row node-text-editor-row"> <div style="height: 250px; min-height:150px;" class="node-text-editor" id="node-input-func-editor" ></div> </div> <div class="form-row"> <label for="node-input-outputs"><i class="fa fa-random"></i> <span data-i18n="function.label.outputs"></span></label> <input id="node-input-outputs" style="width: 60px;" value="1"> </div> <div class="form-tips"><b>Tip:</b> Leave the device Id blank if you want to set it by using <b>msg.payload.deviceId</b>.</div> </script> <script type="text/x-red" data-help-name="device function"> <p> Use this node to write code for customized actions that include reading/writing of virtual IoT device properties.<br> The node works just like the general node-RED function block but with additional access to virtual device properties.<br> <p> To enable reading and writing of property values, the input msg object must include the device ID (<code>msg.payload.deviceId</code>). </p> <p> To set/get a property value, just type the property name. For example: <code> actualTemp = 5; </code> or <code>var x = actualTemp*3; </code> </p> <p>For information about writing functions, see the <a target="_new" href="http://nodered.org/docs/writing-functions.html">Writing Functions</a> topic in the Node-RED online documentation.</p> </p> </script> <script type="text/javascript"> RED.nodes.registerType('device function',{ category: 'Virtual_IoT_Device', color:"#B277B1", defaults: { deviceId: {value:""}, schema: {type:"Device Schema", required: false}, name: { value:"" }, func: {value:"\nreturn msg;"}, outputs: {value:1}, noerr: {value:0,validate:function(v){ return ((!v) || (v === 0)) ? true : false; }} }, inputs:1, outputs:1, icon: "vdFunction.png", label: function() { return this.name || "device function"; }, labelStyle: function() { return this.name ? "node_label_italic" : ""; }, oneditprepare: function() { var that = this; $( "#node-input-outputs" ).spinner({ min:1 }); this.editor = RED.editor.createEditor({ id: 'node-input-func-editor', mode: 'ace/mode/javascript', value: $("#node-input-func").val() }); this.editor.focus(); }, oneditsave: function() { var annot = this.editor.getSession().getAnnotations(); this.noerr = 0; $("#node-input-noerr").val(0); for (var k=0; k < annot.length; k++) { if (annot[k].type === "error") { $("#node-input-noerr").val(annot.length); this.noerr = annot.length; } } $("#node-input-func").val(this.editor.getValue()); delete this.editor; }, oneditresize: function(size) { var rows = $("#dialog-form>div:not(.node-text-editor-row)"); var height = $("#dialog-form").height(); for (var i=0;i<rows.size();i++) { height -= $(rows[i]).outerHeight(true); } var editorRow = $("#dialog-form>div.node-text-editor-row"); height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom"))); $(".node-text-editor").css("height",height+"px"); this.editor.resize(); } }); </script>