Session.close: Call close handler on websocket close

When scratch closes websocket connection, bluepy-scratch-link does not
close Bluetooth connection cleanly. This causes connection failure on
reconnect.

To close Bluetooth connection, catch ConnectionClosedError of websocket
and call close handler of the session.

Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
This commit is contained in:
Shin'ichiro Kawasaki
2020-07-25 10:18:37 +09:00
parent dfe97777fd
commit dd194ae1c2

View File

@@ -120,10 +120,22 @@ class Session():
await self.recv_request()
await asyncio.sleep(0.1)
while True:
if await self.recv_request():
try:
if await self.recv_request():
break
await self._send_notifications()
logger.debug("in handle loop")
except websockets.ConnectionClosedError as e:
logger.info("scratch closed session")
logger.debug(e)
self.close()
break
await self._send_notifications()
logger.debug("in handle loop")
def close(self):
"""
Default handler called at session end.
"""
return
class BTSession(Session):
"""Manage a session for Bluetooth device"""