From e015d9c2836a5ae3affb8c50b7ed09cbe43d37f8 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Sat, 20 Jun 2020 13:31:31 +0900 Subject: [PATCH] Revert "BLESession.notify: Send out notifications at once to avoid interruption" This reverts commit 2c7a10f848895d73cd2c6bebfca1f3ba4df957c9. It turned out that string join with '\n' is not the proper way to pack multiple JSON messages to Scratch Lego Boost extension. --- scratch_link.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/scratch_link.py b/scratch_link.py index f3fe620..189f844 100755 --- a/scratch_link.py +++ b/scratch_link.py @@ -95,22 +95,20 @@ class Session(): Notify BT/BLE device events to scratch. """ logger.debug("start to notify") - # merge all notifications queued - notifications = [] + # flush notification queue while not self.notification_queue.empty(): method, params = self.notification_queue.get() - 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() + self._send_notification(method, params) - def _build_notification(self, method, params): + def _send_notification(self, method, params): jsonn = { 'jsonrpc': "2.0", 'method': method } jsonn['params'] = params notification = json.dumps(jsonn) logger.debug(f"notification: {notification}") - return notification + + future = asyncio.run_coroutine_threadsafe( + self.websocket.send(notification), self.loop) + result = future.result() async def handle(self): logger.debug("start session hanlder")