mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-28 13:34:24 +00:00
Implement queue for Xiaomi router polling. https://github.com/Koenkk/zigbee2mqtt/issues/927
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
const utils = require('../util/utils');
|
||||
const interval = utils.secondsToMilliseconds(60);
|
||||
const Queue = require('queue');
|
||||
const logger = require('../util/logger');
|
||||
|
||||
/**
|
||||
* This extensions polls Xiaomi Zigbee routers to keep them awake.
|
||||
@@ -8,6 +10,15 @@ class RouterPollXiaomi {
|
||||
constructor(zigbee, mqtt, state, publishDeviceState) {
|
||||
this.zigbee = zigbee;
|
||||
this.timer = null;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
onZigbeeStarted() {
|
||||
@@ -27,15 +38,30 @@ class RouterPollXiaomi {
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.queue.stop();
|
||||
this.clearTimer();
|
||||
}
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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.zigbee.ping(d.ieeeAddr)); // Ping devices.
|
||||
.forEach((d) => this.ping(d.ieeeAddr)); // Ping devices.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user