2018-10-02 12:15:12 -07:00
|
|
|
const utils = require('../util/utils');
|
|
|
|
const interval = utils.secondsToMilliseconds(60);
|
2019-01-26 13:19:20 -07:00
|
|
|
const Queue = require('queue');
|
|
|
|
const logger = require('../util/logger');
|
2018-10-02 12:15:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This extensions polls Xiaomi Zigbee routers to keep them awake.
|
|
|
|
*/
|
|
|
|
class RouterPollXiaomi {
|
2018-11-16 12:23:11 -07:00
|
|
|
constructor(zigbee, mqtt, state, publishDeviceState) {
|
2018-10-02 12:15:12 -07:00
|
|
|
this.zigbee = zigbee;
|
|
|
|
this.timer = null;
|
2019-01-26 13:19:20 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup command queue.
|
|
|
|
* The command queue ensures that only 1 command is executed at a time.
|
|
|
|
* This is to avoid DDoSiNg of the coordinator.
|
|
|
|
*/
|
|
|
|
this.queue = new Queue();
|
|
|
|
this.queue.concurrency = 1;
|
|
|
|
this.queue.autostart = true;
|
2018-11-16 12:23:11 -07:00
|
|
|
}
|
2018-10-02 12:15:12 -07:00
|
|
|
|
2018-11-16 12:23:11 -07:00
|
|
|
onZigbeeStarted() {
|
2018-10-02 12:15:12 -07:00
|
|
|
this.startTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
startTimer() {
|
|
|
|
this.clearTimer();
|
|
|
|
this.timer = setInterval(() => this.handleInterval(), interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimer() {
|
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
2019-01-26 13:19:20 -07:00
|
|
|
this.queue.stop();
|
2018-10-02 12:15:12 -07:00
|
|
|
this.clearTimer();
|
|
|
|
}
|
|
|
|
|
2019-01-26 13:19:20 -07:00
|
|
|
ping(ieeeAddr) {
|
|
|
|
this.queue.push((queueCallback) => {
|
|
|
|
this.zigbee.ping(ieeeAddr, (error) => {
|
|
|
|
if (error) {
|
|
|
|
logger.debug(`Failed to ping ${ieeeAddr}`);
|
|
|
|
} else {
|
|
|
|
logger.debug(`Successfully pinged ${ieeeAddr}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
queueCallback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-02 12:15:12 -07:00
|
|
|
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
|
2019-01-26 13:19:20 -07:00
|
|
|
.forEach((d) => this.ping(d.ieeeAddr)); // Ping devices.
|
2018-10-02 12:15:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RouterPollXiaomi;
|