Skip to content

Commit

Permalink
Refactor lqi scan (zigbeer#23)
Browse files Browse the repository at this point in the history
* Use a composite key for lqiScan link deduplication

Composite key of concatenated parent and device ieee address means deduplication happens for the links rather than for the devices.

* Tidy lqiScan and include debug.

* Add network map link before recursing into child router
  • Loading branch information
clockbrain authored and Koenkk committed May 14, 2019
1 parent 566cba2 commit 52d54dd
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/shepherd.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ ZShepherd.prototype.lqi = function (ieeeAddr, callback) {
}).then(function (rsp) { // { srcaddr, status, neighbortableentries, startindex, neighborlqilistcount, neighborlqilist }
if (rsp.status === 0) // success
return _.map(rsp.neighborlqilist, function (neighbor) {
return { ieeeAddr: neighbor.extAddr, nwkAddr: neighbor.nwkAddr, lqi: neighbor.lqi };
return { ieeeAddr: neighbor.extAddr, nwkAddr: neighbor.nwkAddr, lqi: neighbor.lqi, depth: neighbor.depth };
});
}).nodeify(callback);
};
Expand All @@ -479,24 +479,32 @@ ZShepherd.prototype.remove = function (ieeeAddr, cfg, callback) {
ZShepherd.prototype.lqiScan = function (ieeeAddr) {
var info = this.info();
var self = this;
const noDuplicate = {};
const visited = new Set();
const linkMap = {};

const processResponse = function(parent){
return function(data){
var chain = Q();
data.forEach(function (devinfo) {
const ieeeAddr = devinfo.ieeeAddr;
if (ieeeAddr == "0x0000000000000000") return;
let dev = self._findDevByAddr(ieeeAddr);
const childIeeeAddr = devinfo.ieeeAddr;
if (childIeeeAddr == "0x0000000000000000") return;
let childDev = self._findDevByAddr(childIeeeAddr);
devinfo.parent = parent;
devinfo.status = dev ? dev.status : "offline";
const dedupKey = parent + '|' + ieeeAddr;
if (dev && dev.type == "Router" && !noDuplicate[dedupKey]) {
devinfo.status = childDev ? childDev.status : "offline";
const linkKey = parent + '|' + childIeeeAddr
if (!linkMap[linkKey]) {
linkMap[linkKey] = devinfo;
} else {
debug.shepherd('Ignoring duplicate key %s.', linkKey);
}
if (childDev && childDev.type == "Router" && !visited.has(childIeeeAddr)) {
visited.add(childIeeeAddr);
chain = chain.then(function () {
return self.lqi(ieeeAddr).then(processResponse(ieeeAddr));
return self.lqi(childIeeeAddr).then(processResponse(childIeeeAddr));
});
} else {
debug.shepherd('LQI scan skipping %s from parent %s', childIeeeAddr, parent);
}
noDuplicate[dedupKey] = devinfo;
});
return chain;
}
Expand All @@ -510,10 +518,10 @@ ZShepherd.prototype.lqiScan = function (ieeeAddr) {
.timeout(5000)
.then(processResponse(ieeeAddr))
.then(function(){
return Object.values(noDuplicate);
return Object.values(linkMap);
})
.catch(function(){
return Object.values(noDuplicate);
return Object.values(linkMap);
});
};

Expand Down

0 comments on commit 52d54dd

Please sign in to comment.