From e5608087b139202a5c85d9f3a1d23fad5dfc9e6f Mon Sep 17 00:00:00 2001 From: hogthrob Date: Sun, 28 Nov 2021 01:14:43 +0100 Subject: [PATCH] Fix for #160 and better serial error handling We just check for the existence of the supportsLED method and only then run the code which used to work in older zigbee-herdsman versions. In case of problems in connect we now properly stop the zigbee adapter before trying to restart it. This was a problem uncovered by #160 but not the root cause (this was the call to a no-longer-existing method of zigbee-herdsman. --- nodes/shepherd.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/nodes/shepherd.js b/nodes/shepherd.js index 7ee57bf..06d4843 100644 --- a/nodes/shepherd.js +++ b/nodes/shepherd.js @@ -619,18 +619,21 @@ module.exports = function (RED) { this.proxy.emit('ready'); this.status = 'connected'; this.proxy.emit('nodeStatus', {fill: 'green', shape: 'dot', text: 'connected'}); - this.herdsman.supportsLED().then(supportsLED => { - if (supportsLED === true) { - this.herdsman.setLED(this.led === 'enabled').then(() => { - this.debug(`setLED successfully set ${this.led}`); - }).catch(error => { - this.error(`setLED failed to set ${this.led} ${error.message}`); - }); - } else if (this.led === 'enabled') { - this.error('Setting LED not supported on this adapter. To avoid this message at startup, set CC2531 LED to \'disabled\' in controller node'); - } - }); - + + if (this.herdsman.supportsLED !== undefined) + { + this.herdsman.supportsLED().then(supportsLED => { + if (supportsLED === true) { + this.herdsman.setLED(this.led === 'enabled').then(() => { + this.debug(`setLED successfully set ${this.led}`); + }).catch(error => { + this.error(`setLED failed to set ${this.led} ${error.message}`); + }); + } else if (this.led === 'enabled') { + this.error('Setting LED not supported on this adapter. To avoid this message at startup, set CC2531 LED to \'disabled\' in controller node'); + } + }); + } this.checkOverdueInterval = setInterval(() => { this.checkOverdue(); }, 60 * 1000); @@ -653,9 +656,12 @@ module.exports = function (RED) { this.status = error.message; this.proxy.emit('nodeStatus', {fill: 'red', shape: 'ring', text: error.message + ', retrying'}); this.error(error.message); - setTimeout(() => { - this.connect(); - }, 10000); + + this.herdsman.finally().then( () => { + setTimeout(() => { + this.connect(); + }, 10000); + }); }).finally(() => { this.connecting = false; });