Skip to content

Commit

Permalink
Always ping xiaomi devices through ‘basic’ mechanism. #1248
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Mar 24, 2019
1 parent 69d04d0 commit 40596ba
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
14 changes: 8 additions & 6 deletions lib/extension/deviceAvailability.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DeviceAvailability {
return true;
}

return device.type === 'Router' && (device.powerSource && device.powerSource !== 'Battery');
return utils.isRouter(device) && !utils.isBatteryPowered(device);
}

getAllPingableDevices() {
Expand All @@ -53,12 +53,13 @@ class DeviceAvailability {
.forEach((device) => this.publishAvailability(device.ieeeAddr, true));

// Start timers for all devices
this.getAllPingableDevices().forEach((device) => this.setTimer(device.ieeeAddr));
this.getAllPingableDevices().forEach((device) => this.setTimer(device));
}

handleInterval(ieeeAddr) {
handleInterval(device) {
// Check if a job is already pending.
// This avoids overflowing of the queue in case the queue is not able to catch-up with the jobs being added.
const ieeeAddr = device.ieeeAddr;
if (this.pending.includes(ieeeAddr)) {
logger.debug(`Skipping ping for ${ieeeAddr} becuase job is already in queue`);
return;
Expand All @@ -68,6 +69,7 @@ class DeviceAvailability {

// When a device is already unavailable, log the ping failed on 'debug' instead of 'error'.
const errorLogLevel = this.state.hasOwnProperty(ieeeAddr) && !this.state[ieeeAddr] ? 'debug' : 'error';
const mechanism = utils.isXiaomiDevice(device) ? 'basic' : 'default';

this.zigbee.ping(ieeeAddr, errorLogLevel, (error) => {
this.publishAvailability(ieeeAddr, !error);
Expand All @@ -78,8 +80,8 @@ class DeviceAvailability {
this.pending.splice(index, 1);
}

this.setTimer(ieeeAddr);
});
this.setTimer(device);
}, mechanism);
}

setTimer(ieeeAddr) {
Expand Down Expand Up @@ -148,7 +150,7 @@ class DeviceAvailability {
this.publishAvailability(device.ieeeAddr, true);
}

this.setTimer(device.ieeeAddr);
this.setTimer(device);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions lib/extension/xiaomi.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ class Xiaomi {

handleInterval() {
this.zigbee.getAllClients()
.filter((d) => utils.isXiaomiDevice(d)) // Filter Xiaomi devices
.filter((d) => d.type === 'Router') // Filter routers
.filter((d) => d.powerSource && d.powerSource !== 'Battery') // Remove battery powered devices
.forEach((d) => this.ping(d.ieeeAddr)); // Ping devices.
.filter((d) => utils.isXiaomiDevice(d) && utils.isRouter(d) && !utils.isBatteryPowered(d))
.forEach((d) => this.ping(d.ieeeAddr));
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ module.exports = {
isXiaomiDevice: (device) => device.modelId !== 'lumi.router' && xiaomiManufacturerID.includes(device.manufId),
isIkeaTradfriDevice: (device) => ikeaTradfriManufacturerID.includes(device.manufId),
isRouter: (device) => device.type === 'Router',
isBatteryPowered: (device) => device.powerSource && device.powerSource === 'Battery',
isNumeric: (string) => /^\d+$/.test(string),
toLocalISOString: (dDate) => toLocalISOString(dDate),
getPostfixes: () => postfixes,
Expand Down

0 comments on commit 40596ba

Please sign in to comment.