From 31d21546b9cfc2c5da56be579f8feb8b349d601c Mon Sep 17 00:00:00 2001 From: clockbrain <40568549+clockbrain@users.noreply.github.com> Date: Mon, 20 May 2019 03:33:30 +1000 Subject: [PATCH] Simpler network map scan (#1543) * Initial iterative rewrite of network scan * Remove unused linkKey * Restore original log message * Cleaner array flattening * Fix various travis warnings --- lib/zigbee.js | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/zigbee.js b/lib/zigbee.js index 4fc0ca177..ddcc45511 100644 --- a/lib/zigbee.js +++ b/lib/zigbee.js @@ -267,16 +267,44 @@ class Zigbee { return this.shepherd.find(device.ieeeAddr, 1); } + getScanable() { + return this.getDevices().filter((d) => d.type != 'EndDevice'); + } + getGroup(ID) { return this.shepherd.getGroup(ID); } networkScan(callback) { logger.info('Starting network scan...'); - this.shepherd.lqiScan().then((result) => { + + const processResponse = function(parent, shepherd) { + return function(data) { + const linkSet = []; + return new Promise((resolve) => { + data.forEach(function(devinfo) { + const childDev = shepherd._findDevByAddr(devinfo.ieeeAddr); + devinfo.parent = parent; + devinfo.status = childDev ? childDev.status : 'offline'; + linkSet.push(devinfo); + }); + resolve(linkSet); + }); + }; + }; + + const allScans = this.getScanable().map((dev) => { + return this.shepherd.lqi(dev.ieeeAddr).then(processResponse(dev.ieeeAddr, this.shepherd)); + }, this); + Promise.all(allScans).then((linkSets) => { + const linkMap = [].concat(...linkSets); logger.info('Network scan completed'); - callback(result); - }); + callback(linkMap); + }) + .catch(function() { + logger.info('Network scan failed'); + callback([]); + }); } getEndpoint(ieeeAddr, ep) {