TheAgentK / tuya-mqtt

Nodejs-Script to combine tuyaapi and openhab via mqtt
MIT License
173 stars 81 forks source link

Disconnected from device and never connects again ... #67

Closed thomasleitner closed 3 years ago

thomasleitner commented 3 years ago

Hi,

Today I've upgraded tuya-api to 6.1.4 and tuya-mqtt to 3.03 ... Everything works fine except: The connection to the device is closed after a random time after startup and never comes back up again. This is what it says in the logs:

2021-01-25T10:34:43.928Z tuya-mqtt:state MQTT DPS JSON: tuya/aiibot_air_purifier/dps/state -> {"2":5} 2021-01-25T10:34:43.929Z tuya-mqtt:state MQTT DPS2: tuya/aiibot_air_purifier/dps/2/state -> 5 2021-01-25T10:34:43.955Z tuya-mqtt:error Error: Error from socket at Socket.client.on.err (/home/fhem/tuya-mqtt/node_modules/tuyapi/index.js:388:30) at Socket.emit (events.js:193:13) at emitErrorNT (internal/streams/destroy.js:91:8) at emitErrorAndCloseNT (internal/streams/destroy.js:59:3) at processTicksAndRejections (internal/process/task_queues.js:81:17) 2021-01-25T10:34:43.958Z tuya-mqtt:tuyapi Disconnected from device Aiibot Air Purifier (10.10.1.42, xxxxxxx, yyyyyy) 2021-01-25T10:34:44.956Z tuya-mqtt:error Error connecting to device id xxxxxxx ....retry in 10 seconds.

I suppose that it would log something when it retries the connection, but it stays quiet, so I suppose it does not retry to establish the connection at all.

And the other question is why it disconnects anyway?

Note: When I restart the "node tuya-mqtt.js" process, the connection is established right away ...

Thanks // Tom

tsightler commented 3 years ago

Yes, 3.0.3 has a logic error in the reconnect code which keeps it from reconnecting sometimes. I've had this happen on one of my devices as well.

As far as what causes it to disconnect, I have no idea. I have 5 different Tuya devices and most maintain connection for weeks/months, but one disconnects semi-regularly, several times a week at least. It seems to happen if it roams between access points (it's fairly far away and almost perfectly equal distance from two different APs). Regardless, from a code perspective, there's not anything I can do about it other than reconnect since it's a socket error happening at very low level. I'll try to push 3.0.4 soon with the reconnect fix.

thomasleitner commented 3 years ago

Thanks for your reply ... will watch out for the 3.0.4 release ...

tsightler commented 3 years ago

3.0.4 has been released. I'm pretty sure it will address the issue so I'm going to close this for now but can re-open if you still have issues, just let me know. Honestly, I need to rework the connect/reconnect handler along with the error handler but just don't know if I'll have time so I'm hoping this is good enough as it seems to work for me in all cases.

thomasleitner commented 3 years ago

Thanks a lot ... I'll check it out and let you know here ....

cameleon77 commented 3 years ago

Hi, unfortunately the same issue happened to me with 3.0.4 version, gradually all devices become disconnected with the error:

2021-02-16T09:10:38.280Z tuya-mqtt:tuyapi Search for device id xxx
2021-02-16T09:10:38.280Z tuya-mqtt:tuyapi Found device id xxx
2021-02-16T09:10:39.164Z tuya-mqtt:error Error: Error from socket
    at Socket.<anonymous> (/home/node/tuya-mqtt/node_modules/tuyapi/index.js:387:30)
    at Socket.emit (node:events:376:20)
    at emitErrorNT (node:internal/streams/destroy:188:8)
    at emitErrorCloseNT (node:internal/streams/destroy:153:3)
    at processTicksAndRejections (node:internal/process/task_queues:80:21)
2021-02-16T09:10:39.165Z tuya-mqtt:tuyapi Disconnected from device xxx (yyyy, zzzz)

A restart of tuya-mqtt reconnects all devices successfully.

This happened 2nd time this month. Any chance to look into this?

tsightler commented 3 years ago

I would most likely need full logs to even have a shot as that snippet is not useful as none of that happens in tuya-mqtt. You can email logs to me if that's preferred, my email is the same username at gmail.