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")