Refactor ping queue to zigbee.js.

This commit is contained in:
Koen Kanters
2019-02-02 00:57:51 +01:00
parent 720addd906
commit 3963a8d26f
3 changed files with 32 additions and 67 deletions
+8 -29
View File
@@ -1,7 +1,6 @@
const logger = require('../util/logger');
const settings = require('../util/settings');
const utils = require('../util/utils');
const Queue = require('queue');
const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
// Some EndDevices should be pinged
@@ -22,15 +21,6 @@ class DeviceAvailabilityHandler {
this.timers = {};
this.pending = [];
this.state = {};
/**
* 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;
}
isPingable(device) {
@@ -66,25 +56,16 @@ class DeviceAvailabilityHandler {
this.pending.push(ieeeAddr);
this.queue.push((queueCallback) => {
this.zigbee.ping(ieeeAddr, (error) => {
if (error) {
logger.debug(`Failed to ping ${ieeeAddr}`);
} else {
logger.debug(`Successfully pinged ${ieeeAddr}`);
}
this.zigbee.ping(ieeeAddr, (error) => {
this.publishAvailability(ieeeAddr, !error);
this.publishAvailability(ieeeAddr, !error);
// Remove from pending jobs.
const index = this.pending.indexOf(ieeeAddr);
if (index !== -1) {
this.pending.splice(index, 1);
}
// Remove from pending jobs.
const index = this.pending.indexOf(ieeeAddr);
if (index !== -1) {
this.pending.splice(index, 1);
}
this.setTimer(ieeeAddr);
queueCallback();
});
this.setTimer(ieeeAddr);
});
}
@@ -99,8 +80,6 @@ class DeviceAvailabilityHandler {
}
stop() {
this.queue.stop();
this.zigbee.getDevices()
.filter((d) => d.type !== 'Coordinator')
.forEach((device) => this.publishAvailability(device.ieeeAddr, false));
+1 -23
View File
@@ -1,7 +1,5 @@
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.
@@ -10,15 +8,6 @@ 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() {
@@ -38,22 +27,11 @@ 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();
});
});
this.zigbee.ping(ieeeAddr);
}
handleInterval() {
+23 -15
View File
@@ -91,6 +91,8 @@ class Zigbee {
}
stop(callback) {
this.queue.stop();
this.shepherd.stop((error) => {
logger.info('zigbee-shepherd stopped');
callback(error);
@@ -174,21 +176,6 @@ class Zigbee {
}
}
ping(deviceID, callback) {
let friendlyName = 'unknown';
const device = this.shepherd._findDevByAddr(deviceID);
const ieeeAddr = device.ieeeAddr;
if (settings.getDevice(ieeeAddr)) {
friendlyName = settings.getDevice(ieeeAddr).friendly_name;
}
if (device) {
logger.debug(`Check online ${friendlyName} ${deviceID}`);
this.shepherd.controller.checkOnline(device, callback);
}
}
onMessage(message) {
if (this.messageHandler) {
this.messageHandler(message);
@@ -274,6 +261,27 @@ class Zigbee {
return endpoint;
}
ping(ieeeAddr, cb) {
const device = this.shepherd._findDevByAddr(ieeeAddr);
if (device) {
this.queue.push((queueCallback) => {
logger.debug(`Ping ${ieeeAddr}`);
this.shepherd.controller.checkOnline(device, (error) => {
if (error) {
logger.error(`Failed to ping ${ieeeAddr}`);
} else {
logger.debug(`Successfully pinged ${ieeeAddr}`);
}
cb(error);
});
setTimeout(() => queueCallback(), delay);
});
}
}
bind(ep, cluster, target=this.getCoordinator()) {
const log = `for ${ep.device.ieeeAddr} - ${cluster}`;