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"
key: "xxxxxxxxxxxxxxxx"
topic: "tuya/device1"
refresh: 30 #refresh intervall in s
refresh: 30
reconnect: 10
type: luminea_nx_4458
loglevel: "debug"

View File

@@ -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
}
}

View File

@@ -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`)