diff --git a/src/infuse_iot/tools/localhost.py b/src/infuse_iot/tools/localhost.py index 3d3e32e..68aad5f 100644 --- a/src/infuse_iot/tools/localhost.py +++ b/src/infuse_iot/tools/localhost.py @@ -13,6 +13,7 @@ from typing import Any from aiohttp import web +from aiohttp.client_exceptions import WSMessageTypeError from aiohttp.web_request import BaseRequest from aiohttp.web_runner import GracefulExit @@ -59,88 +60,95 @@ async def handle_index(self, _request): return web.FileResponse(this_folder / "localhost" / "index.html") - async def websocket_handler(self, request: BaseRequest): - ws = web.WebSocketResponse() - await ws.prepare(request) - - Console.log_info(f"Websocket client connected ({request.remote})") - - try: - while True: - # Example data sent to the client - self._data_lock.acquire(blocking=True) - columns = [ + def websocket_message(self) -> dict: + self._data_lock.acquire(blocking=True) + columns = [ + { + "title": "Metadata", + "headerHozAlign": "center", + "frozen": True, + "columns": [ + { + "title": "Device", + "field": "infuse_id", + "headerHozAlign": "center", + }, { - "title": "Metadata", + "title": "App ID", + "field": "application", + "headerHozAlign": "center", + }, + { + "title": "Network", + "field": "network_id", + "headerHozAlign": "center", + }, + { + "title": "Last Heard", + "field": "time", + "headerHozAlign": "center", + }, + { + "title": "Bluetooth", "headerHozAlign": "center", - "frozen": True, "columns": [ { - "title": "Device", - "field": "infuse_id", + "title": "Address", + "field": "bt_addr", "headerHozAlign": "center", }, { - "title": "App ID", - "field": "application", - "headerHozAlign": "center", - }, - { - "title": "Network", - "field": "network_id", - "headerHozAlign": "center", - }, - { - "title": "Last Heard", - "field": "time", - "headerHozAlign": "center", - }, - { - "title": "Bluetooth", - "headerHozAlign": "center", - "columns": [ - { - "title": "Address", - "field": "bt_addr", - "headerHozAlign": "center", - }, - { - "title": "RSSI (dBm)", - "field": "bt_rssi", - "headerVertical": "flip", - "hozAlign": "right", - }, - ], + "title": "RSSI (dBm)", + "field": "bt_rssi", + "headerVertical": "flip", + "hozAlign": "right", }, ], - } - ] - # Put the announce TDFs first for clarity - priorities = {"ANNOUNCE_V2": 0, "ANNOUNCE": 1} - sorted_tdfs = sorted(self._columns, key=lambda x: priorities.get(x, 2)) - - for tdf_name in sorted_tdfs: - columns.append( - { - "title": tdf_name, - "field": tdf_name, - "columns": self._columns[tdf_name], - "headerHozAlign": "center", - } - ) - devices = sorted(self._data.keys()) - message = { - "columns": columns, - "rows": [self._data[d] for d in devices], - "tdfs": sorted(list(self._columns.keys())), - "apps": sorted(list(self._apps)), - "networks": sorted(list(self._networks)), + }, + ], + } + ] + # Put the announce TDFs first for clarity + priorities = {"ANNOUNCE_V2": 0, "ANNOUNCE": 1} + sorted_tdfs = sorted(self._columns, key=lambda x: priorities.get(x, 2)) + + for tdf_name in sorted_tdfs: + columns.append( + { + "title": tdf_name, + "field": tdf_name, + "columns": self._columns[tdf_name], + "headerHozAlign": "center", } - self._data_lock.release() + ) + devices = sorted(self._data.keys()) + message = { + "columns": columns, + "rows": [self._data[d] for d in devices], + "tdfs": sorted(list(self._columns.keys())), + "apps": sorted(list(self._apps)), + "networks": sorted(list(self._networks)), + } + self._data_lock.release() + return message + + async def websocket_handler(self, request: BaseRequest): + ws = web.WebSocketResponse() + await ws.prepare(request) + + Console.log_info(f"Websocket client connected ({request.remote})") + + try: + while True: + # Wait for request from browser (contents don't matter) + _ = await ws.receive_str() + + # Data sent to the client + message = self.websocket_message() await ws.send_json(message) - await asyncio.sleep(1) - except (asyncio.CancelledError, ConnectionResetError): + + except (asyncio.CancelledError, ConnectionResetError, WSMessageTypeError): pass finally: await ws.close() diff --git a/src/infuse_iot/tools/localhost/index.html b/src/infuse_iot/tools/localhost/index.html index 2055f9a..15ca515 100644 --- a/src/infuse_iot/tools/localhost/index.html +++ b/src/infuse_iot/tools/localhost/index.html @@ -143,6 +143,7 @@