add stopWatcher()
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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,7 +110,7 @@ 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
|
||||
|
||||
@@ -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`)
|
||||
|
||||
Reference in New Issue
Block a user