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
This commit is contained in:
clockbrain
2019-05-21 21:04:14 +02:00
committed by Koen Kanters
parent dc028d5046
commit 22b64d09f2
+15 -2
View File
@@ -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([]);
});