From 2c7a10f848895d73cd2c6bebfca1f3ba4df957c9 Mon Sep 17 00:00:00 2001 From: MDE <59321583+mdevel1@users.noreply.github.com> Date: Sat, 8 Feb 2020 22:33:22 +0100 Subject: [PATCH] BLESession.notify: Send out notifications at once to avoid interruption --- scratch_link.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/scratch_link.py b/scratch_link.py index 189f844..f3fe620 100755 --- a/scratch_link.py +++ b/scratch_link.py @@ -95,20 +95,22 @@ class Session(): Notify BT/BLE device events to scratch. """ logger.debug("start to notify") - # flush notification queue + # merge all notifications queued + notifications = [] while not self.notification_queue.empty(): method, params = self.notification_queue.get() - self._send_notification(method, params) + notifications.append(self._build_notification(method, params)) + # send merged notifications + future = asyncio.run_coroutine_threadsafe( + self.websocket.send('\n'.join(notifications)), self.loop) + result = future.result() - def _send_notification(self, method, params): + def _build_notification(self, method, params): jsonn = { 'jsonrpc': "2.0", 'method': method } jsonn['params'] = params notification = json.dumps(jsonn) logger.debug(f"notification: {notification}") - - future = asyncio.run_coroutine_threadsafe( - self.websocket.send(notification), self.loop) - result = future.result() + return notification async def handle(self): logger.debug("start session hanlder")