sinricpro / python-sdk

python3 sdk for sinric pro.
https://sinric.pro
23 stars 9 forks source link

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason #20

Open neeraj-partha opened 4 years ago

neeraj-partha commented 4 years ago
│       │                                   │       │    └ [<Task pending coro=<SinricProSocket.receiveMessage() running at /home/pi/scripts/python-sdk/sinric/_sinricprosocket.py:58> c...
│       │                                   │       └ <function wait at 0x76233d68>
│       │                                   └ <module 'asyncio' from '/usr/lib/python3.7/asyncio/__init__.py'>
│       └ <built-in function get_event_loop>
└ <module 'asyncio' from '/usr/lib/python3.7/asyncio/__init__.py'>

File "/usr/lib/python3.7/asyncio/base_events.py", line 571, in run_until_complete self.run_forever() │ └ <function BaseEventLoop.run_forever at 0x7618a030> └ <_UnixSelectorEventLoop running=True closed=False debug=False> File "/usr/lib/python3.7/asyncio/base_events.py", line 539, in run_forever self._run_once() │ └ <function BaseEventLoop._run_once at 0x7618ad68> └ <_UnixSelectorEventLoop running=True closed=False debug=False> File "/usr/lib/python3.7/asyncio/base_events.py", line 1775, in _run_once handle._run() │ └ <function Handle._run at 0x76209df8> └ <Handle <TaskWakeupMethWrapper object at 0x75df89d0>()> File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run self._context.run(self._callback, *self._args) │ │ │ │ │ └ <member '_args' of 'Handle' objects> │ │ │ │ └ <Handle <TaskWakeupMethWrapper object at 0x75df89d0>()> │ │ │ └ <member '_callback' of 'Handle' objects> │ │ └ <Handle <TaskWakeupMethWrapper object at 0x75df89d0>()> │ └ <member '_context' of 'Handle' objects> └ <Handle <TaskWakeupMethWrapper object at 0x75df89d0>()>

File "/home/pi/scripts/python-sdk/sinric/_sinricprosocket.py", line 51, in receiveMessage message = await connection.recv() │ └ <function WebSocketCommonProtocol.recv at 0x7609b108> └ <websockets.client.WebSocketClientProtocol object at 0x765730f0>

File "/home/pi/.local/lib/python3.7/site-packages/websockets/protocol.py", line 509, in recv await self.ensure_open() │ └ <function WebSocketCommonProtocol.ensure_open at 0x7609b2b8> └ <websockets.client.WebSocketClientProtocol object at 0x765730f0> File "/home/pi/.local/lib/python3.7/site-packages/websockets/protocol.py", line 803, in ensure_open raise self.connection_closed_exc() │ └ <function WebSocketCommonProtocol.connection_closed_exc at 0x7609b270> └ <websockets.client.WebSocketClientProtocol object at 0x765730f0>

websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

neeraj-partha commented 4 years ago

can you please help me to add code to re-connect automatically when disconnected.

thegoliathgeek commented 4 years ago

Might maybe problem with ISP! Try in mobile hotspot once!

neeraj-partha commented 4 years ago

i'm using jio hotspot. It disconnects after a certain random amount of time. websocket connection is not consistent.

thegoliathgeek commented 4 years ago

I faced this error too. Try in different ISP.

neeraj-partha commented 4 years ago

Unfortunately I don't have access to any other isp. Can we somehow add code to reconnect when this situation occur? Please help. Thank you

neeraj-partha commented 4 years ago

Can I try removing ping interval and ping timeout as stated below

https://stackoverflow.com/questions/54101923/1006-connection-closed-abnormally-error-with-python-3-7-websockets

thegoliathgeek commented 4 years ago

Not an optional solution. If you remove ping. Maintaining a live connection will be lost.

neeraj-partha commented 4 years ago

How about catching this exception and reconnect?

DevMiser commented 4 years ago

Can this be caught by the DisconnectCallbackHandler? https://sinricpro.github.io/esp8266-esp32-sdk/class_sinric_pro_class.html#a654bb017ca55b448d55bb36e0346f38a I'm not sure if this works in python.
Can you tell me how to convert the code example (SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });) into python?

thegoliathgeek commented 4 years ago

@DevMiser sure I'll back to you on this.

DevMiser commented 4 years ago

I tried the following, but it did not work:

def onDisconnected(cb): if cb == 'None': print ("Disconnected from Sinric Pro")

callbacks = { 'powerState': onPowerState, 'Disconnected': onDisconnected }

thegoliathgeek commented 4 years ago

https://websockets.readthedocs.io/en/stable/intro.html?highlight=disconnect#producer

This will work. So I'll update this

DevMiser commented 4 years ago

Awesome. Thank you. It would be great to see this written into the Pro Switch example code.

DevMiser commented 3 years ago

I am now using this solution to reconnect automatically after being disconnected - using a while loop. When there is an error, it moves to the next lines in the code, which prints the time of the error, and then waits 20 minutes and runs the program again (by which time hopefully the ISP interruption or other cause of the error has been resolved). Here is the modification to the applicable portion of the code in the simple example:

if name == 'main': while True:
client = SinricPro(appKey,deviceIdArr,callbacks,enable_log=False, restore_states=True, secretKey=secretKey) udp_client = SinricProUdp(callbacks,deviceIdArr,enable_trace=False)
client.handle_all(udp_client) print ("Disconnected from SinricPro at: %s" % time.ctime()) sleep(1200) print ("Reconnection attempted at: %s" % time.ctime())

You may also need to add "import time" in a line before "from time import sleep" in the simple example.

buddydip commented 3 years ago

https://websockets.readthedocs.io/en/stable/intro.html?highlight=disconnect#producer

This will work. So I'll update this

Hi @imdhanush, Have you updated the SDK as I am facing the same issue while using Python SDK for which I have sent you an email also.

Thanks, Dipankar

buddydip commented 3 years ago

I have stopped using Sinric Pro and switched to OpenHAB because of the above issue. I felt OpenHAB much better in features and stability and more powerful as the local installation is also provided as a package which can be setup only by simple config (installed in Raspberry Pi) and can work even without internet connection in a local network

thegoliathgeek commented 3 years ago

@buddydip Sorry for the inconvenience. This existing python-sdk has many bugs. So am trying to rewrite the whole sdk. This might take some time. Thanks