Skip to content

Commit 67db238

Browse files
committed
tools: localhost: extract message generation
Extract websocket message generation to a dedicated function. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent a1a9e7e commit 67db238

1 file changed

Lines changed: 75 additions & 71 deletions

File tree

src/infuse_iot/tools/localhost.py

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
from typing import Any
1414

1515
from aiohttp import web
16+
from aiohttp.client_exceptions import WSMessageTypeError
1617
from aiohttp.web_request import BaseRequest
1718
from aiohttp.web_runner import GracefulExit
18-
from aiohttp.client_exceptions import WSMessageTypeError
1919

2020
import infuse_iot.epacket.interface as interface
2121
import infuse_iot.epacket.packet as packet
@@ -60,6 +60,78 @@ async def handle_index(self, _request):
6060

6161
return web.FileResponse(this_folder / "localhost" / "index.html")
6262

63+
def websocket_message(self) -> dict:
64+
self._data_lock.acquire(blocking=True)
65+
columns = [
66+
{
67+
"title": "Metadata",
68+
"headerHozAlign": "center",
69+
"frozen": True,
70+
"columns": [
71+
{
72+
"title": "Device",
73+
"field": "infuse_id",
74+
"headerHozAlign": "center",
75+
},
76+
{
77+
"title": "App ID",
78+
"field": "application",
79+
"headerHozAlign": "center",
80+
},
81+
{
82+
"title": "Network",
83+
"field": "network_id",
84+
"headerHozAlign": "center",
85+
},
86+
{
87+
"title": "Last Heard",
88+
"field": "time",
89+
"headerHozAlign": "center",
90+
},
91+
{
92+
"title": "Bluetooth",
93+
"headerHozAlign": "center",
94+
"columns": [
95+
{
96+
"title": "Address",
97+
"field": "bt_addr",
98+
"headerHozAlign": "center",
99+
},
100+
{
101+
"title": "RSSI (dBm)",
102+
"field": "bt_rssi",
103+
"headerVertical": "flip",
104+
"hozAlign": "right",
105+
},
106+
],
107+
},
108+
],
109+
}
110+
]
111+
# Put the announce TDFs first for clarity
112+
priorities = {"ANNOUNCE_V2": 0, "ANNOUNCE": 1}
113+
sorted_tdfs = sorted(self._columns, key=lambda x: priorities.get(x, 2))
114+
115+
for tdf_name in sorted_tdfs:
116+
columns.append(
117+
{
118+
"title": tdf_name,
119+
"field": tdf_name,
120+
"columns": self._columns[tdf_name],
121+
"headerHozAlign": "center",
122+
}
123+
)
124+
devices = sorted(self._data.keys())
125+
message = {
126+
"columns": columns,
127+
"rows": [self._data[d] for d in devices],
128+
"tdfs": sorted(list(self._columns.keys())),
129+
"apps": sorted(list(self._apps)),
130+
"networks": sorted(list(self._networks)),
131+
}
132+
self._data_lock.release()
133+
return message
134+
63135
async def websocket_handler(self, request: BaseRequest):
64136
ws = web.WebSocketResponse()
65137
await ws.prepare(request)
@@ -71,76 +143,8 @@ async def websocket_handler(self, request: BaseRequest):
71143
# Wait for request from browser (contents don't matter)
72144
_ = await ws.receive_str()
73145

74-
# Example data sent to the client
75-
self._data_lock.acquire(blocking=True)
76-
columns = [
77-
{
78-
"title": "Metadata",
79-
"headerHozAlign": "center",
80-
"frozen": True,
81-
"columns": [
82-
{
83-
"title": "Device",
84-
"field": "infuse_id",
85-
"headerHozAlign": "center",
86-
},
87-
{
88-
"title": "App ID",
89-
"field": "application",
90-
"headerHozAlign": "center",
91-
},
92-
{
93-
"title": "Network",
94-
"field": "network_id",
95-
"headerHozAlign": "center",
96-
},
97-
{
98-
"title": "Last Heard",
99-
"field": "time",
100-
"headerHozAlign": "center",
101-
},
102-
{
103-
"title": "Bluetooth",
104-
"headerHozAlign": "center",
105-
"columns": [
106-
{
107-
"title": "Address",
108-
"field": "bt_addr",
109-
"headerHozAlign": "center",
110-
},
111-
{
112-
"title": "RSSI (dBm)",
113-
"field": "bt_rssi",
114-
"headerVertical": "flip",
115-
"hozAlign": "right",
116-
},
117-
],
118-
},
119-
],
120-
}
121-
]
122-
# Put the announce TDFs first for clarity
123-
priorities = {"ANNOUNCE_V2": 0, "ANNOUNCE": 1}
124-
sorted_tdfs = sorted(self._columns, key=lambda x: priorities.get(x, 2))
125-
126-
for tdf_name in sorted_tdfs:
127-
columns.append(
128-
{
129-
"title": tdf_name,
130-
"field": tdf_name,
131-
"columns": self._columns[tdf_name],
132-
"headerHozAlign": "center",
133-
}
134-
)
135-
devices = sorted(self._data.keys())
136-
message = {
137-
"columns": columns,
138-
"rows": [self._data[d] for d in devices],
139-
"tdfs": sorted(list(self._columns.keys())),
140-
"apps": sorted(list(self._apps)),
141-
"networks": sorted(list(self._networks)),
142-
}
143-
self._data_lock.release()
146+
# Data sent to the client
147+
message = self.websocket_message()
144148

145149
await ws.send_json(message)
146150

0 commit comments

Comments
 (0)