BLESession.notify: Send out notifications at once to avoid interruption

This commit is contained in:
MDE
2020-02-08 22:33:22 +01:00
committed by Shin'ichiro Kawasaki
parent ba8eba4800
commit 2c7a10f848

View File

@@ -95,20 +95,22 @@ class Session():
Notify BT/BLE device events to scratch. Notify BT/BLE device events to scratch.
""" """
logger.debug("start to notify") logger.debug("start to notify")
# flush notification queue # merge all notifications queued
notifications = []
while not self.notification_queue.empty(): while not self.notification_queue.empty():
method, params = self.notification_queue.get() 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 = { 'jsonrpc': "2.0", 'method': method }
jsonn['params'] = params jsonn['params'] = params
notification = json.dumps(jsonn) notification = json.dumps(jsonn)
logger.debug(f"notification: {notification}") 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): async def handle(self):
logger.debug("start session hanlder") logger.debug("start session hanlder")