UNPKG

node-red-contrib-mobius-flow-dali

Version:

Node-RED nodes to work with MOBiUSFlow and DALI devices

290 lines (287 loc) 13.4 kB
"use strict"; /** Copyright (C) 2020, IAConnects Technology Limited All Rights Reserved ALL INFORMATION CONTAINED HEREIN IS, AND REMAINS THE PROPERTY OF IACONNECTS TECHNOLOGY LIMITED AND ITS SUPPLIERS, IF ANY. THE INTELLECTUAL AND TECHNICAL CONCEPTS CONTAINED HEREIN ARE PROPRIETARY TO IACONNECTS TECHNOLOGY LIMITED AND ITS SUPPLIERS AND ARE PROTECTED BY TRADE SECRET OR COPYRIGHT LAW. DISSEMINATION OF THIS INFORMATION OR REPRODUCTION OF THIS MATERIAL IS STRICTLY FORBIDDEN UNLESS PRIOR WRITTEN PERMISSION IS OBTAINED FROM IACONNECTS TECHNOLOGY LIMITED. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **/ Object.defineProperty(exports, "__esModule", { value: true }); const daliCommon = require('@ia/dali-common'); const collection = require('../dali-collection-class'); module.exports = (RED) => { RED.nodes.registerType('dali collection', function (config) { RED.nodes.createNode(this, config); const node = this; let groupingLoop; let groupingIntervalLoop; const shortAddressList = new collection.ShortAddressList(); const groupAddressList = new collection.GroupAddressList(); const masterAddressList = new collection.MasterAddressList(); const ungroupedAddressList = new collection.UngroupedAddressList(); collection.populateCollectionLists(config.groups, config.selectionMethod, RED.nodes.getNode, shortAddressList, groupAddressList, masterAddressList, ungroupedAddressList); function sendGroupingCommand(currentShort, addr) { currentShort.value.masterConnection.master.sendNormalDali({ topic: 'daliCommand', payload: { daliCircuitID: currentShort.value.masterConnection.master.circuit.split('/')[1], daliCommandCode: 96 + currentShort.value.group, daliCommandName: daliCommon.daliLookup.commandLookup[96 + currentShort.value.group], daliAddressingMode: 'short', daliAddress: addr, } }); } /** * Adds dali devices to respective groups when called * A grouping command is sent every 1.5 seconds until all devices have been grouped * Grouping commands are sent to the respective dali master * @return {NodeJS.Timeout} - The grouping interval loop is returned to allow it to be * cleared on node closure */ function regroup() { clearInterval(groupingLoop); node.status({ fill: 'green', shape: 'dot', text: `Grouping in progress...`, }); const shortAddrs = shortAddressList.shortAddressIterator(); let currentShort = shortAddrs.next(); const loop = setInterval(() => { if (config.selectionMethod === 'ins') { currentShort.value.masterConnection.mobius.readResource(`${currentShort.value.masterConnection.master.circuit}/0258/${currentShort.value.addr}/0A`, (err, response) => { if (err) { node.error('Mobius error fetching DALI address ' + JSON.stringify(err)); } else { sendGroupingCommand(currentShort, response.value.pv); } }); } else { sendGroupingCommand(currentShort, currentShort.value.addr); } setTimeout(() => { currentShort = shortAddrs.next(); if (currentShort.done) { clearInterval(loop); node.status({ fill: 'green', shape: 'dot', text: `Grouping complete`, }); } }, 100); }, 1500); return loop; } // Send lighting level commands to whole dali groups function sendToCollection(payload) { const groups = groupAddressList.groupsIterator(); let currentGroup = groups.next(); while (!currentGroup.done) { const outMsg = { topic: 'daliCommand', payload: { daliCircuitID: currentGroup.value.masterConnection.master.circuit.split('/')[1], daliCommandCode: 999, daliCommandName: 'Direct Power Control', daliAddress: currentGroup.value.groupNum, daliAddressingMode: 'group', level: payload.level } }; if (payload.daliCommandCode !== undefined) { outMsg.payload.daliCommandCode = payload.daliCommandCode; } currentGroup.value.masterConnection.master.sendNormalDali(outMsg); currentGroup = groups.next(); if (currentGroup.done) { sendUngrouped(payload); } } } function sendSingleLightingCommand(currentUngrouped, payload, addr) { const outMsg = { topic: 'daliCommand', payload: { daliCircuitID: currentUngrouped.value.masterConnection.master.circuit.split('/')[1], daliCommandCode: 999, daliCommandName: 'Direct Power Control', daliAddress: addr, daliAddressingMode: 'short', level: payload.level } }; if (payload.daliCommandCode !== undefined) { outMsg.payload.daliCommandCode = payload.daliCommandCode; } currentUngrouped.value.masterConnection.master.sendNormalDali(outMsg); } function sendUngrouped(payload) { const ungrouped = ungroupedAddressList.ungroupedAddressIterator(); let currentUngrouped; const loop = setInterval(() => { currentUngrouped = ungrouped.next(); if (currentUngrouped.done) { clearInterval(loop); } else { if (config.selectionMethod === 'ins') { currentUngrouped.value.masterConnection.mobius.readResource(`${currentUngrouped.value.masterConnection.master.circuit}/0258/${currentUngrouped.value.addr}/0A`, (err, response) => { if (err) { node.error('Mobius error fetching DALI address ' + JSON.stringify(err)); node.status({ fill: 'yellow', shape: 'dot', text: `Instance not found`, }); } else { sendSingleLightingCommand(currentUngrouped, payload, response.value.pv); } }); } else { sendSingleLightingCommand(currentUngrouped, payload, currentUngrouped.value.addr); } } }, 50); node.status({ fill: 'green', shape: 'dot', text: `Sent ${payload.level} to DALI collection`, }); } node.on('input', (msg) => { sendToCollection(msg.payload); }); // Run grouping process on interval if (config.autoGrouping) { let groupingInterval = 5 * 60 * 1000; if (config.timeUnits === 'seconds') { groupingInterval = config.time * 1000; } else if (config.timeUnits === 'minutes') { groupingInterval = config.time * 60 * 1000; } else if (config.timeUnits === 'hours') { groupingInterval = config.time * 60 * 60 * 1000; } groupingIntervalLoop = setInterval(function () { groupingLoop = regroup(); }, groupingInterval); } // Calculate error checking interval let errInterval = 5 * 60 * 1000; if (config.errtimeUnits === 'seconds') { errInterval = config.errtime * 1000; } else if (config.errtimeUnits === 'minutes') { errInterval = config.errtime * 60 * 1000; } else if (config.errtimeUnits === 'hours') { errInterval = config.errtime * 60 * 60 * 1000; } let errorCheckLoop; let failures; const errorCheckInterval = setInterval(() => { failures = { controlGearOkTotal: 0, lampFailureTotal: 0, limitErrorTotal: 0, powerFailureTotal: 0 }; const shortAddrs = shortAddressList.shortAddressIterator(); if (config.selectionMethod === 'ins') { errorCheckLoop = setInterval(() => { fetchErrByInst(shortAddrs); }, 150); } else { errorCheckLoop = setInterval(() => { fetchErrByAddr(shortAddrs); }, 150); } }, errInterval); function fetchErrByAddr(shortAddrs) { const currentShort = shortAddrs.next(); if (currentShort.done) { sendFailureStatus(); clearInterval(errorCheckLoop); } else { const hidSid = currentShort.value.masterConnection.master.circuit; const discParams = { uri: hidSid, pids: ['0258'], rid: '0A', limit: 1, func: `return $PV === ${currentShort.value.addr};` }; currentShort.value.masterConnection.mobius.discover(discParams, (err, response) => { if (err) { node.error(JSON.stringify(err)); } else { const ins = response.value[0].ins; readErrResource(currentShort, `${hidSid}/0258/${ins}/43`, 'controlGearOkTotal', true); readErrResource(currentShort, `${hidSid}/0258/${ins}/44`, 'lampFailureTotal', false); readErrResource(currentShort, `${hidSid}/0258/${ins}/46`, 'limitErrorTotal', false); readErrResource(currentShort, `${hidSid}/0258/${ins}/4A`, 'powerFailureTotal', false); } }); } } function fetchErrByInst(shortAddrs) { const currentShort = shortAddrs.next(); if (currentShort.done) { sendFailureStatus(); clearInterval(errorCheckLoop); } else { const ins = '000000'.substr(0, 4 - currentShort.value.addr.length) + currentShort.value.addr; const hidSid = currentShort.value.masterConnection.master.circuit; readErrResource(currentShort, `${hidSid}/0258/${ins}/43`, 'controlGearOkTotal', true); readErrResource(currentShort, `${hidSid}/0258/${ins}/44`, 'lampFailureTotal', false); readErrResource(currentShort, `${hidSid}/0258/${ins}/46`, 'limitErrorTotal', false); readErrResource(currentShort, `${hidSid}/0258/${ins}/4A`, 'powerFailureTotal', false); } } function readErrResource(currentShort, uri, key, markOfFailed) { currentShort.value.masterConnection.mobius.readResource(uri, (err, response) => { if (err) { node.error(JSON.stringify(err)); } else { if (markOfFailed) { failures[key] += (!response.value.pv) ? 1 : 0; } else { failures[key] += (response.value.pv) ? 1 : 0; } } }); } function sendFailureStatus() { node.send({ topic: 'luminaireFailure', payload: failures }); } node.on('close', (done) => { clearInterval(groupingLoop); clearInterval(groupingIntervalLoop); clearInterval(errorCheckInterval); clearInterval(errorCheckLoop); done(); }); }); };