mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-07-20 10:31:15 +00:00
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:
+15
-2
@@ -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([]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user