node-red-contrib-mobius-flow-dali
Version:
Node-RED nodes to work with MOBiUSFlow and DALI devices
125 lines (122 loc) • 5.37 kB
JavaScript
;
/**
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 nodeRedUtilities = require('@ia/node-red-utilities');
module.exports = (RED) => {
RED.nodes.registerType('dali emergency query command generator', function (config) {
RED.nodes.createNode(this, config);
const node = this;
const conNode = RED.nodes.getNode(config.daliConnection);
if (conNode !== null && conNode !== undefined) {
const daliMaster = conNode.connection;
const commandCode = parseInt(config.command, 10);
const commandName = daliCommon.daliLookup.commandLookup[commandCode];
let connectionStatus = false;
let statusText = '';
let addressListIndex = 0;
let addressList;
if (config.selectionMethod === 'ins') {
addressList = nodeRedUtilities.splitInstanceSelection(config.shortAddress);
}
else {
addressList = nodeRedUtilities.MSWordPrintingPageSelectionFormatDecode(config.shortAddress);
}
function updateStatus() {
if (connectionStatus) {
if (statusText === '') {
node.status({
fill: 'green',
shape: 'dot',
text: 'Connected to DALI'
});
}
else {
node.status({
fill: 'green',
shape: 'dot',
text: `${statusText}`
});
}
}
else {
node.status({
fill: 'yellow',
shape: 'dot',
text: 'DALI Not Connected!'
});
}
}
updateStatus();
function sendDaliCommand(addr) {
daliMaster.sendEmergencyDali({
topic: 'daliCommand',
payload: {
daliCircuitID: daliMaster.circuit.split('/')[1],
daliCommandCode: commandCode,
daliCommandName: commandName,
daliAddressingMode: 'short',
daliAddress: addr
}
});
}
node.on('input', () => {
const addr = addressList[addressListIndex];
if (config.selectionMethod === 'ins') {
daliMaster.serviceConn.readResource(`${daliMaster.circuit}/0259/${addr}/0A`, (err, response) => {
if (err) {
node.error('Mobius error fetching DALI address ' + JSON.stringify(err));
}
else {
sendDaliCommand(response.value.pv);
statusText = `Sent ${commandName} to instance ${addr}`;
updateStatus();
}
});
}
else {
sendDaliCommand(addr);
statusText = `Sent ${commandName} to address ${addr}`;
updateStatus();
}
addressListIndex += 1;
if (addressListIndex === addressList.length) {
addressListIndex = 0;
}
});
daliMaster.on('connectionStatusUpdate', (status) => {
connectionStatus = status;
updateStatus();
});
node.on('close', (done) => {
daliMaster.removeAllListeners('connectionStatusUpdate');
done();
});
}
else {
node.error('Connection to DALI master not specified');
}
});
};