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([]); });