Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 78 additions & 70 deletions src/infuse_iot/tools/localhost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions src/infuse_iot/tools/localhost/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ <h1>Infuse-IoT TDF Viewer</h1>
let current_num_columns = 0;
let currentApps = [];
let currentNetworks = []
let dataRequestId = 0;

const arraysEqual = (a, b) =>
a.length === b.length &&
Expand Down Expand Up @@ -246,12 +247,22 @@ <h1>Infuse-IoT TDF Viewer</h1>
}
};

function dataUpdateRequest() {
ws.send("dataUpdateRequest");
}

ws.onopen = function () {
console.log("WebSocket connected");
// Initial data request
dataUpdateRequest();
// Setup data request interval
dataRequestId = setInterval(dataUpdateRequest, 1000);
};

ws.onerror = function (error) {
console.error("WebSocket error:", error);
// Cancel data requests
clearInterval(dataRequestId);
};
</script>
</body>
Expand Down