UNPKG

node-red-contrib-mobius-flow-dali

Version:

Node-RED nodes to work with MOBiUSFlow and DALI devices

267 lines (266 loc) 9.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CircuitEmergencyInterface = void 0; const events_1 = require("events"); const dali_linker_1 = require("./dali-linker"); class CircuitEmergencyInterface extends events_1.EventEmitter { circuit; daliMaster; devices = []; deviceIndex = 0; selectedObjectIns = '0001'; linker; constructor(conID, getNode) { super(); this.daliMaster = getNode(conID).connection; this.circuit = this.daliMaster.circuit.split('/')[1]; this.linker = new dali_linker_1.DaliLinker(this.daliMaster.circuit.split('/')[0], this.circuit, this.daliMaster.serviceConn); } /** * Send status update * @param message - Status update message */ updateStatus(message) { this.emit('emNotification', message); } /** * Send updated device list */ sendUpdatedDeviceList() { this.emit('interfaceEmergencyDevices', this.devices); } /** * Send updated object list */ sendUpdatedObjectList() { this.linker.fetchObjects('0259', (err, response) => { if (!err) { const objects = []; for (let i = 0; i < response.length; i++) { objects.push({ daliAddress: response[i].daliAddress, ins: response[i].ins, selected: (() => (response[i].ins === this.selectedObjectIns) ? true : false)() }); } this.emit('interfaceEmergencyObjects', objects); } else { this.updateStatus(err); } }); } /** * Update the device list * @param deviceList - The new device list */ updateDeviceList(deviceList) { this.devices = deviceList; for (let i = 0; i < this.devices.length; i++) { this.devices[i].selected = (i === this.deviceIndex) ? true : false; } this.linker.fetchObjects('0259', (err, response) => { if (!err) { const linkedAddresses = []; const instances = []; for (let i = 0; i < response.length; i++) { if (response[i].daliAddress !== -1) { linkedAddresses.push(response[i].daliAddress); instances.push(response[i].ins); } } for (let i = 0; i < this.devices.length; i++) { if (!this.devices[i].linked) { for (let j = 0; j < linkedAddresses.length; j++) { if (this.devices[i].daliAddress === linkedAddresses[j]) { this.devices[i].linked = true; this.devices[i].linkedTo = instances[j]; } } } } this.sendUpdatedDeviceList(); } else { this.updateStatus(err); this.sendUpdatedDeviceList(); } }); } /** * Select a device within the device list * @param addr - The DALI short address of the device to be selected */ selectDevice(addr) { for (let i = 0; i < this.devices.length; i++) { if (this.devices[i].daliAddress === addr) { this.devices[i].selected = true; this.deviceIndex = i; } else { this.devices[i].selected = false; } } this.sendUpdatedDeviceList(); this.notifyOfStateChange(addr); this.illuminateCurrent(); } /** * Select a new mobius luminaire object * @param ins - The ins of the mobius luminaire object to be selected (0001 - 003F) */ selectObject(ins) { this.selectedObjectIns = ins; this.sendUpdatedObjectList(); } /** * Send relevent notifications when a new device is selected * @param addr - DALI short address of new device */ notifyOfStateChange(addr) { let isLinked = false; for (let i = 0; i < this.devices.length; i++) { if (this.devices[i].daliAddress === addr) { isLinked = this.devices[i].linked; } } this.updateStatus(isLinked ? `Running function test on device ${addr} (already linked)` : `Running function test on device ${addr}`); this.sendUpdatedDeviceList(); } /** * Send a DALI command to start an emergency function test * @param address - The DALI address */ illuminate(address) { this.daliMaster.sendEmergencyDali({ topic: 'daliCommand', payload: { daliCircuitID: this.daliMaster.circuit.split('/')[1], daliCommandCode: 227, daliCommandName: 'Start Function Test', daliAddressingMode: 'short', daliAddress: address } }); } illuminateCurrent() { this.illuminate(this.devices[this.deviceIndex].daliAddress); this.notifyOfStateChange(this.devices[this.deviceIndex].daliAddress); } illuminateNext() { this.deviceIndex = this.deviceIndex + 1; if (this.deviceIndex >= this.devices.length) { this.deviceIndex = 0; } this.illuminate(this.devices[this.deviceIndex].daliAddress); this.notifyOfStateChange(this.devices[this.deviceIndex].daliAddress); } illuminatePrevious() { this.deviceIndex = this.deviceIndex - 1; if (this.deviceIndex < 0) { this.deviceIndex = this.devices.length; } this.illuminate(this.devices[this.deviceIndex].daliAddress); this.notifyOfStateChange(this.devices[this.deviceIndex].daliAddress); } /** * Link the currently selected luminaire device to the currently selected mobius object */ linkLuminaire() { if (this.devices.length >= 1) { for (let i = 0; i < this.devices.length; i++) { // Unlink any devices currently linked to the selected instance if (this.devices[i].linkedTo === this.selectedObjectIns) { this.devices[i].linked = false; this.devices[i].linkedTo = 'None'; } } if (!this.devices[this.deviceIndex].linked) { this.linker.link('0259', this.selectedObjectIns, this.devices[this.deviceIndex].daliAddress, (err) => { if (err) { this.updateStatus(`${err}`); } else { this.devices[this.deviceIndex].linked = true; this.devices[this.deviceIndex].linkedTo = this.selectedObjectIns; this.sendUpdatedDeviceList(); this.sendUpdatedObjectList(); this.updateStatus(`Linked device ${this.devices[this.deviceIndex].daliAddress} to object ${this.selectedObjectIns}`); } }); } else { this.updateStatus(`Device ${this.devices[this.deviceIndex].daliAddress} already linked`); } } else { this.updateStatus(`No devices to link`); } } /** * Unlink all luminaire objects */ unlinkAllMobObjects() { this.linker.unlinkAll('0259', (err) => { if (!err) { this.sendUpdatedObjectList(); } else { this.updateStatus(err); } }); for (let i = 0; i < this.devices.length; i++) { this.devices[i].linkedTo = 'None'; this.devices[i].linked = false; } this.sendUpdatedObjectList(); this.sendUpdatedDeviceList(); } /** * Unlink the selected luminaire object */ unlinkMobObject() { this.linker.unlink('0259', this.selectedObjectIns, (err) => { if (!err) { this.sendUpdatedObjectList(); } else { this.updateStatus(err); } }); for (let i = 0; i < this.devices.length; i++) { if (this.devices[i].linkedTo === this.selectedObjectIns) { this.devices[i].linkedTo = 'None'; this.devices[i].linked = false; } } this.sendUpdatedDeviceList(); } /** * Unlink a device / object pair as a result of a swap or delete process * @param addr - The DALI short address of the device within the pair to be unlinked */ unlinkOnSwap(addr) { let ins = ''; for (let i = 0; i < this.devices.length; i++) { if (this.devices[i].daliAddress === addr) { this.devices[i].linked = false; ins = this.devices[i].linkedTo; this.devices[i].linkedTo = 'None'; } } this.sendUpdatedDeviceList(); this.linker.unlink('0259', ins, (err) => { if (!err) { this.sendUpdatedObjectList(); } else { this.updateStatus(err); } }); } changeToThisCircuit() { this.sendUpdatedDeviceList(); this.sendUpdatedObjectList(); } } exports.CircuitEmergencyInterface = CircuitEmergencyInterface;