UNPKG

node-red-contrib-mobius-flow-dali

Version:

Node-RED nodes to work with MOBiUSFlow and DALI devices

206 lines (205 loc) 7.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DaliLinker = void 0; class DaliLinker { hid; sid; serviceConn; constructor(hid, sid, mobius) { this.hid = hid; this.sid = sid; this.serviceConn = mobius; } /** * Fetch objects from service config * @param searchPid - The profile id of the desired object list * @param callback - Callback when complete */ fetchObjects(searchPid, callback) { const foundObjects = []; this.serviceConn.getServiceConfig(this.hid, this.sid, (err, config) => { if (!err) { config.value.objects.forEach(pidGroup => { if (pidGroup.pid === searchPid) { const objects = pidGroup.objects; objects.forEach(object => { foundObjects.push({ pid: pidGroup.pid, ins: object.ins, daliAddress: this.extractAddress(object) }); }); } }); setTimeout(() => { callback(null, foundObjects); }, 50); } else { callback(err, null); } }); } /** * Assign an Object a DALI shot address * @param pid - The profile ID of the target object * @param ins - The instance ID of the target object * @param addr - The DALI address to be assigned to the target object * @param callback - Callback when complete */ link(pid, ins, addr, callback) { this.updateDaliAddress(pid, ins, addr, callback); } /** * Remove current DALI short address from object (change to -1) * @param pid - The profile ID of the target object * @param ins - The instance ID of the target object * @param callback - Callback when complete */ unlink(pid, ins, callback) { this.updateDaliAddress(pid, ins, -1, callback); } /** * Remove current DALI short address from all objects * @param pid - The profile ID of the target object list * @param callback - Callback when complete */ unlinkAll(pid, callback) { this.serviceConn.getServiceConfig(this.hid, this.sid, (err, config) => { if (!err) { let error = null; config.value.objects.forEach(pidGroup => { if (pidGroup.pid === pid) { const objects = pidGroup.objects; let i; for (i = 0; i < objects.length; i++) { const object = objects[i]; const updatedRids = this.updateAddressResource(object.rids, -1); const newConfig = { ins: object.ins, pid: object.pid, uiname: object.uiname, rids: updatedRids }; this.serviceConn.updateObjectInServiceConfig(this.hid, this.sid, object.pid, object.ins, newConfig, (err1) => { if (err1) { error = err1; } }); this.serviceConn.writeResourceValue(`${this.hid}/${this.sid}/${object.pid}/${object.ins}/0A`, -1, 15, (err2, response) => { if (err2) { error = err2; } }); } } }); setTimeout(() => { if (!error) { callback(null); } else { callback(error); } }, 50); } else { callback(err); } }); } /** * Perform DALI short address update to a target object * @param pid - The profile ID of the target object * @param ins - The instance ID of the target object * @param newAddress - The new DALI short address to be assigned to target object * @param callback - Callback when complete */ updateDaliAddress(pid, ins, newAddress, callback) { this.serviceConn.getServiceConfig(this.hid, this.sid, (err, config) => { if (!err) { config.value.objects.forEach(pidGroup => { if (pidGroup.pid === pid) { const objects = pidGroup.objects; let i; for (i = 0; i < objects.length; i++) { if (objects[i].ins === ins) { const foundObject = objects[i]; const updatedRids = this.updateAddressResource(foundObject.rids, newAddress); this.serviceConn.updateObjectInServiceConfig(this.hid, this.sid, pid, foundObject.ins, { ins: ins, pid: pid, uiname: foundObject.uiname, rids: updatedRids }, (err1) => { if (err1) { callback(err1); } else { this.serviceConn.writeResourceValue(`${this.hid}/${this.sid}/${pid}/${ins}/0A`, newAddress, 15, (err2, response) => { if (err2) { callback(err2); } else { callback(null); } }); } }); i = objects.length; } } } }); } else { callback(err); } }); } /** * Update resources list with new DALI short address * @param rids - The current rids list * @param address - The new DALI short address */ updateAddressResource(rids, address) { let j; let resFound = false; for (j = 0; j < rids.length; j++) { if (rids[j].rid === '0A') { rids[j].settings.pv = address; resFound = true; } } if (!resFound) { rids.push({ rid: '0A', settings: { pv: address } }); } return rids; } /** * Extract the DALI short address from a given object * @param object - The object ot have its short address extracted */ extractAddress(object) { if (object.rids.length === 0) { return -1; } else { let i; let addressResourceFound = false; for (i = 0; i < object.rids.length; i++) { if (object.rids[i].rid === '0A') { addressResourceFound = true; return object.rids[i].settings.pv; } } if (!addressResourceFound) { return -1; } } } } exports.DaliLinker = DaliLinker;