From 06da8f5b9789acd3e3a76456cbebc669c53b1b56 Mon Sep 17 00:00:00 2001 From: Dennis Gunia Date: Sat, 3 Aug 2024 20:14:45 +0200 Subject: [PATCH] add stopWatcher() --- config-example.yaml | 6 ++++-- src/devicebase.js | 19 ++++++++++--------- src/modules/luminea_nx_4458.js | 4 ++++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/config-example.yaml b/config-example.yaml index b2b4d3d..b095b06 100644 --- a/config-example.yaml +++ b/config-example.yaml @@ -8,5 +8,7 @@ devices: - id: "sqy709956ply4inkx6ac87" key: "xxxxxxxxxxxxxxxx" topic: "tuya/device1" - refresh: 30 #refresh intervall in s - type: luminea_nx_4458 \ No newline at end of file + refresh: 30 + reconnect: 10 + type: luminea_nx_4458 +loglevel: "debug" diff --git a/src/devicebase.js b/src/devicebase.js index 38371fa..5e04740 100644 --- a/src/devicebase.js +++ b/src/devicebase.js @@ -37,7 +37,7 @@ class DeviceBase { this.logger.debug(`use topic (state) : ${this.topic_state}`) this.intervall_refresh = deviceconfig.refresh ? deviceconfig.refresh * 1000 : 10000 - this.reconnect_timout = deviceconfig.reconnect ? deviceconfig.reconnect * 1000 : 5000 + this.reconnect_timout = deviceconfig.reconnect ? deviceconfig.reconnect * 1000 : 5000 this.logger.debug(`refresh timer (ms) : ${this.intervall_refresh}`) this.logger.debug(`reconnect timer (ms) : ${this.reconnect_timout}`) @@ -53,6 +53,7 @@ class DeviceBase { key: this.devicekey, issueRefreshOnConnect: true, }) + this.device.device.ip = undefined this.reconnect() } catch (error) { @@ -63,7 +64,6 @@ class DeviceBase { this.device.on('connected', () => { this.connected = true this.logger.info(`Connected to tuya id: ${this.deviceid}, ip: ${this.device.device.ip}, prefix: ${this.topicname}`) - this.startWatcher() // subscribe to topic this.mqtt.subscribe(this.topic_set, (err) => { if (err) { @@ -74,10 +74,11 @@ class DeviceBase { }); this.logger.debug(`publish ${this.totopic_statepic_get}: "online"`) this.mqtt.publish(this.topic_state, "online") + this.startWatcher() }); this.device.on('disconnected', () => { - clearInterval(this.timer) + this.stopWatcher() this.logger.info(`Disconnected`) this.logger.debug(`publish ${this.topic_state}: "offline"`) this.mqtt.publish(this.topic_state, "offline") @@ -86,9 +87,7 @@ class DeviceBase { }); this.device.on('error', error => { - clearInterval(this.timer) - this.logger.debug(`publish ${this.topic_state}: "offline"`) - this.mqtt.publish(this.topic_state, "offline") + this.stopWatcher() setTimeout(() => this.reconnect(), this.reconnect_timout) }); } @@ -96,7 +95,9 @@ class DeviceBase { reconnect() { this.device.find().then(el => { if (el) { - this.device.connect() + this.device.connect().catch(el => { + this.logger.debug("Reconnect failed: device offline") + }) } else { this.logger.debug("Reconnect failed: device offline") setTimeout(() => this.reconnect(), this.reconnect_timout) @@ -109,10 +110,10 @@ class DeviceBase { disconnect() { this.connected = false - clearInterval(this.timer) + this.stopWatcher() this.device.disconnect() this.logger.info(`Disconnected for id: ${this.deviceid}`) - this.reconnect = () => {} // prevent reconnect on exit + this.reconnect = () => { } // prevent reconnect on exit } } diff --git a/src/modules/luminea_nx_4458.js b/src/modules/luminea_nx_4458.js index 6ec3536..64e02ce 100644 --- a/src/modules/luminea_nx_4458.js +++ b/src/modules/luminea_nx_4458.js @@ -57,6 +57,10 @@ class Lineplug extends DeviceBase { }) } + stopWatcher(){ + clearInterval(this.timer) + } + processData(data) { if (!data.dps) { this.logger.warn(`Received unexpected data from device`)