martenjacobs / py-otgw-mqtt

Python OTGW MQTT bridge
MIT License
9 stars 16 forks source link

Improve handling unreliable connections #2

Closed martenjacobs closed 5 years ago

martenjacobs commented 6 years ago

As requested by @Jodur in the discussion on PR #1, handling of disconnects on less reliable connections should be improved

martenjacobs commented 6 years ago

quote @jodur in #1:

After getting a better overview now, one important thing must also be added to the library if we want to run this software as a "never forget" service. exception handling for following situations:

power cycle (on->off->on) of OTGW should be possible without terminating the service temporary communication breaks (TCP -> Wifi problem, disconnection) For the Serial implementation this is probably not necessary, due the nature of serial com, but for the tcp implementation this is a must! For the above i would normally implement a state machine with states disconnected, connected and communication interrupted In the disconnected state i would do, the open method, after success => State connected if not retry to open the connection after a delay, the read statements in State connected. If a failure is detected=> communication interrupted state, do some error handling (closing sockets for example) in State communication interrupted and then go back to state disconnected. I think this should be implemented in the worker Thread?

martenjacobs commented 6 years ago

To make sure the service does not fail when the connection is lost, I think we could define some Exceptions (something like a ConnectionLostException springs to mind) and raise them whenever read fails due to a broken connection. Then the worker could call close and then open to reopen the connection. If this fails, wait 10 seconds and try again. Does that sound good to you @jodur?

jodur commented 6 years ago

Marten sounds good!

martenjacobs commented 6 years ago

Hi @jodur,

Could you do some testing on the tcp branch? I've made some changes to improve support for connection dropping, but I'm unable to test them. If it works, I'll merge it into master

jodur commented 6 years ago

@martenjacobs , Thx for the improvements, i will be able to test it this weekend!

I notice on first sight of the code that no exception handeling is defined for the open method?

In case the code starts and the client is not available, it should als try to reconnect!

jodur commented 6 years ago

@martenjacobs ,

It took sometime longer, had no time this weekend but i have created a pull request for the code i tested. I have added als the reconnection on the open method.

andriej commented 5 years ago

Any updates?

martenjacobs commented 5 years ago

@andriej please see #1 and #3