add stopWatcher()

This commit is contained in:
Dennis Gunia
2024-08-03 20:14:45 +02:00
parent 291e1364e0
commit 06da8f5b97
3 changed files with 18 additions and 11 deletions

View File

@@ -8,5 +8,7 @@ devices:
- id: "sqy709956ply4inkx6ac87" - id: "sqy709956ply4inkx6ac87"
key: "xxxxxxxxxxxxxxxx" key: "xxxxxxxxxxxxxxxx"
topic: "tuya/device1" topic: "tuya/device1"
refresh: 30 #refresh intervall in s refresh: 30
reconnect: 10
type: luminea_nx_4458 type: luminea_nx_4458
loglevel: "debug"

View File

@@ -53,6 +53,7 @@ class DeviceBase {
key: this.devicekey, key: this.devicekey,
issueRefreshOnConnect: true, issueRefreshOnConnect: true,
}) })
this.device.device.ip = undefined
this.reconnect() this.reconnect()
} catch (error) { } catch (error) {
@@ -63,7 +64,6 @@ class DeviceBase {
this.device.on('connected', () => { this.device.on('connected', () => {
this.connected = true this.connected = true
this.logger.info(`Connected to tuya id: ${this.deviceid}, ip: ${this.device.device.ip}, prefix: ${this.topicname}`) this.logger.info(`Connected to tuya id: ${this.deviceid}, ip: ${this.device.device.ip}, prefix: ${this.topicname}`)
this.startWatcher()
// subscribe to topic // subscribe to topic
this.mqtt.subscribe(this.topic_set, (err) => { this.mqtt.subscribe(this.topic_set, (err) => {
if (err) { if (err) {
@@ -74,10 +74,11 @@ class DeviceBase {
}); });
this.logger.debug(`publish ${this.totopic_statepic_get}: "online"`) this.logger.debug(`publish ${this.totopic_statepic_get}: "online"`)
this.mqtt.publish(this.topic_state, "online") this.mqtt.publish(this.topic_state, "online")
this.startWatcher()
}); });
this.device.on('disconnected', () => { this.device.on('disconnected', () => {
clearInterval(this.timer) this.stopWatcher()
this.logger.info(`Disconnected`) this.logger.info(`Disconnected`)
this.logger.debug(`publish ${this.topic_state}: "offline"`) this.logger.debug(`publish ${this.topic_state}: "offline"`)
this.mqtt.publish(this.topic_state, "offline") this.mqtt.publish(this.topic_state, "offline")
@@ -86,9 +87,7 @@ class DeviceBase {
}); });
this.device.on('error', error => { this.device.on('error', error => {
clearInterval(this.timer) this.stopWatcher()
this.logger.debug(`publish ${this.topic_state}: "offline"`)
this.mqtt.publish(this.topic_state, "offline")
setTimeout(() => this.reconnect(), this.reconnect_timout) setTimeout(() => this.reconnect(), this.reconnect_timout)
}); });
} }
@@ -96,7 +95,9 @@ class DeviceBase {
reconnect() { reconnect() {
this.device.find().then(el => { this.device.find().then(el => {
if (el) { if (el) {
this.device.connect() this.device.connect().catch(el => {
this.logger.debug("Reconnect failed: device offline")
})
} else { } else {
this.logger.debug("Reconnect failed: device offline") this.logger.debug("Reconnect failed: device offline")
setTimeout(() => this.reconnect(), this.reconnect_timout) setTimeout(() => this.reconnect(), this.reconnect_timout)
@@ -109,10 +110,10 @@ class DeviceBase {
disconnect() { disconnect() {
this.connected = false this.connected = false
clearInterval(this.timer) this.stopWatcher()
this.device.disconnect() this.device.disconnect()
this.logger.info(`Disconnected for id: ${this.deviceid}`) this.logger.info(`Disconnected for id: ${this.deviceid}`)
this.reconnect = () => {} // prevent reconnect on exit this.reconnect = () => { } // prevent reconnect on exit
} }
} }

View File

@@ -57,6 +57,10 @@ class Lineplug extends DeviceBase {
}) })
} }
stopWatcher(){
clearInterval(this.timer)
}
processData(data) { processData(data) {
if (!data.dps) { if (!data.dps) {
this.logger.warn(`Received unexpected data from device`) this.logger.warn(`Received unexpected data from device`)