From 22b64d09f28cc9069d2d48402f3f2cd353bbb746 Mon Sep 17 00:00:00 2001 From: clockbrain <40568549+clockbrain@users.noreply.github.com> Date: Wed, 22 May 2019 05:04:14 +1000 Subject: [PATCH] Network map (#1546) * Initial iterative rewrite of network scan * Remove unused linkKey * Restore original log message * Cleaner array flattening * Fix various travis warnings * Add a timeout so partial map can be generated * Fix further eslint issues * Add a timeout so partial map can be generated * Fix further eslint issues --- lib/zigbee.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/zigbee.js b/lib/zigbee.js index 6e4b99d3..eece6cb3 100644 --- a/lib/zigbee.js +++ b/lib/zigbee.js @@ -292,6 +292,18 @@ class Zigbee { networkScan(callback) { logger.info('Starting network scan...'); + Promise.delay = function(t, val) { + return new Promise((resolve) => { + setTimeout(resolve.bind(null, val), t); + }); + }; + + Promise.raceAll = function(promises, timeoutTime, timeoutVal) { + return Promise.all(promises.map((p) => { + return Promise.race([p, Promise.delay(timeoutTime, timeoutVal)]); + })); + }; + const processResponse = function(parent, shepherd) { return function(data) { const linkSet = []; @@ -310,12 +322,13 @@ class Zigbee { const allScans = this.getScanable().map((dev) => { return this.shepherd.lqi(dev.ieeeAddr).then(processResponse(dev.ieeeAddr, this.shepherd)); }, this); - Promise.all(allScans).then((linkSets) => { + // Collect all lqi scans but timeout after 2 seconds if any fail + Promise.raceAll(allScans, 2000, []).then((linkSets) => { const linkMap = [].concat(...linkSets); logger.info('Network scan completed'); callback(linkMap); }) - .catch(function() { + .catch(function(result) { logger.info('Network scan failed'); callback([]); });