From 008fda4d90bf9716ecff655a18324c7399e3399c Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sat, 23 Mar 2019 17:45:28 +0100 Subject: [PATCH] =?UTF-8?q?Introduce=20new=20ping=20mechanism=20(=E2=80=98?= =?UTF-8?q?basic=E2=80=99)=20and=20use=20it=20for=20Xiaomi.=20https://gith?= =?UTF-8?q?ub.com/Koenkk/zigbee2mqtt/issues/1248?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/extension/xiaomi.js | 2 +- lib/zigbee.js | 49 +++++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/extension/xiaomi.js b/lib/extension/xiaomi.js index 04c2835a..8aa9fa04 100644 --- a/lib/extension/xiaomi.js +++ b/lib/extension/xiaomi.js @@ -48,7 +48,7 @@ class Xiaomi { } ping(ieeeAddr) { - this.zigbee.ping(ieeeAddr); + this.zigbee.ping(ieeeAddr, 'error', null, 'basic'); } handleInterval() { diff --git a/lib/zigbee.js b/lib/zigbee.js index cbc6411b..f8a26514 100644 --- a/lib/zigbee.js +++ b/lib/zigbee.js @@ -275,26 +275,41 @@ class Zigbee { }); } - ping(ieeeAddr, errorLogLevel='error', cb) { - const device = this.shepherd._findDevByAddr(ieeeAddr); + ping(ieeeAddr, errorLogLevel='error', cb, mechanism='default') { + const callback = (error) => { + if (error) { + logger[errorLogLevel](`Failed to ping ${ieeeAddr}`); + } else { + logger.debug(`Successfully pinged ${ieeeAddr}`); + } - if (device) { - this.queue.push(ieeeAddr, (queueCallback) => { - logger.debug(`Ping ${ieeeAddr}`); - this.shepherd.controller.checkOnline(device, (error) => { - if (error) { - logger[errorLogLevel](`Failed to ping ${ieeeAddr}`); - } else { - logger.debug(`Successfully pinged ${ieeeAddr}`); - } + if (cb) { + cb(error); + } + }; - if (cb) { - cb(error); - } - - queueCallback(error); + if (mechanism === 'default') { + const device = this.shepherd._findDevByAddr(ieeeAddr); + if (device) { + logger.debug(`Ping ${ieeeAddr} (default)`); + this.queue.push(ieeeAddr, (queueCallback) => { + this.shepherd.controller.checkOnline(device, (error) => { + callback(error); + queueCallback(error); + }); }); - }); + } + } else if (mechanism === 'basic') { + const endpoint = this.getEndpoint(ieeeAddr, null); + if (endpoint) { + logger.debug(`Ping ${ieeeAddr} (basic)`); + this.queue.push(ieeeAddr, (queueCallback) => { + endpoint.foundation('genBasic', 'read', [{attrId: 0}], (error) => { + callback(error); + queueCallback(error); + }); + }); + } } }