UNPKG

node-red-contrib-msl

Version:

A node-red module to simplify the access of msl4 channel data.

125 lines (115 loc) 6.34 kB
<script type="text/javascript"> RED.nodes.registerType('msl-channel-config',{ category: 'config', defaults: { device: {value: "", type:"msl-device-config"}, name: {value:""}, channelType: {value: "null"}, selectedDevice: {value: ""}, subdevice: {value: ""}, channels: {value: ""} }, label: function() { if(this.name) return this.name; else if (this.channels && this.channels.length) return this.channels.flatMap(channel => channel.channelname).join(); else return 'MSL Kanal'; }, oneditprepare: function() { let node = this; let typeEl = $("#input-channelType"); let subdeviceEl = $("#input-subdevice"); let channelEl = $("#input-channel"); if(node.channels === undefined) { node.channels = []; } // console.log(node.selectedDevice, node.subdevice, node.channels); // Prepopulate input fields if neccessary node.channelType = typeEl.val(); if(node.selectedDevice) { node.selectedDevice.devices.forEach(subdevice => subdeviceEl.html(subdeviceEl.html() + `<option value="${subdevice.devicename}">${subdevice.devicename}</option>`)); if(node.subdevice) { subdeviceEl.val(node.subdevice.devicename).change(); node.subdevice.channels.forEach(channel => channelEl.html(channelEl.html() + `<option value="${channel.channelname}">${channel.channelname}</option>`)) if(node.channels && node.channels.length) { channelEl.val(node.channels[0].channelname).change(); } } } $("#node-config-input-device").on("change", function() { let config = RED.nodes.node($(this).val()); // console.log('config: ', config, 'node: ', node); if(config && config.device && config.device !== node.selectedDevice) { node.selectedDevice = config.device; subdeviceEl.html(""); node.selectedDevice.devices.forEach(subdevice => subdeviceEl.html(subdeviceEl.html() + `<option value="${subdevice.devicename}">${subdevice.devicename}</option>`)); subdeviceEl.change(); } }); typeEl.on("change", function() { node.channelType = typeEl.val(); subdeviceEl.val(subdeviceEl.val()).change(); }); subdeviceEl.on("change", function() { node.subdevice = node.selectedDevice.devices.find(subdevice => subdevice.devicename === $(this).val()); channelEl.html(""); // console.log(node.channelType); let channeltypes; if(node.channelType === "null") channeltypes = [1,2,3,4,5,6,7,8,9,7777,10,20,21,30,40,41,50,51,52,9998,9999]; else if (node.channelType === "input") channeltypes = [1,2,4,6,10,20,51,52,9998,9999]; else if (node.channelType === "output") channeltypes = [3,5,7,9,21,51,52]; let channels = node.subdevice.channels.filter(channel => channeltypes.includes(parseInt(channel.channeltype))); channels.forEach(channel => channelEl.html(channelEl.html() + `<option value="${channel.channelname}">${channel.channelname}</option>`)); }) channelEl.on("change", function() { // console.log($(this).val()); // console.log("setting channels: ", node.subdevice.channels.filter(channel => $(this).val().includes(channel.channelname))); node.channels = node.subdevice.channels.filter(channel => $(this).val().includes(channel.channelname)); }) } }); </script> <script type="text/html" data-template-name="msl-channel-config"> <div class="form-row"> <label for="node-config-input-device">Gerät</label> <input type="text" id="node-config-input-device" placeholder="Gerät"> </div> <div class="form-row"> <label for="node-config-input-name">Name</label> <input type="text" id="node-config-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="input-channelType">Kanaltyp</label> <select id="input-channelType"> <option value="input">Eingang</option> <option value="output">Ausgang</option> <option value="null" selected>Alle</option> </select> </div> <div class="form-row"> <label for="sub-device">Verb. Gerät</label> <select id="input-subdevice"> <option value="" selected disabled hidden>Gerät auswählen</option> </select> </div> <div class="form-row"> <label for="channel">Kanäle</label> <select id="input-channel" multiple title="Mehrere Kanäle mit STRG+Klick auswählen"> <option value="" selected disabled hidden>Kanal auswählen</option> </select> </div> </script> <script type="text/x-red" data-help-name="msl-channel-config"> <h3>MSL4 Kanal-Konfigurationsnode</h3> <p>Konfigurationsnode zur Zuordnung eines MSL4-Kanals</p> <p> Dieses Konfigurationsnode dient der Zuordnung von Kanälen eines MSL4 zu einem Flow-Node. Damit Kanäle ausgesucht werden könn, muss zunächst über das Auswahlfeld "Gerät" ein Geräte-Konfigurationsnode hinzugefügt werden. Haben Sie ein Gerät zugeordnet können Sie über das "Name"-Eingabgefeld einen beliebigen Namen für dieses Konfigurationsnode festlegen. Lassen Sie dieses Feld frei, übernimmt dieses Node als Namen die Namen der zugeordneten Kanäle. Über das "Kanaltyp"-Auswahlfeld kann ein Kanaltyp (Eingang, Ausgang oder beides) festgelegt werden. Über das "Verb. Gerät"-Auswahlfeld kann entweder das MSL-Gerät selbst oder ein an dieses MSL angeschlossenes Gerät ausgewählt werden. Über das "Kanäle"-Auswahlfeld lassen sich Kanäle zuordnen. <b>Mit STRG+Klick lassen sich in diesem Feld mehrere Kanäle auswählen.</b> </p> </script>