-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e315010
commit 2104186
Showing
5 changed files
with
68 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class BrightnessMessage: | ||
uuid: str | ||
lat: float | ||
lon: float | ||
h3_id: str | ||
utc_iso: str | ||
utc_ns: int | ||
mpsas: float | ||
model_version: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import asyncio | ||
import json | ||
import logging | ||
from dataclasses import asdict | ||
|
||
from websockets import serve, broadcast | ||
|
||
from pc.config import * | ||
from pc.model import BrightnessMessage | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class WebSocketsHandler: | ||
clients = set() | ||
|
||
async def setup(self): | ||
async def register_client(websocket): | ||
log.info(f"registering {websocket}") | ||
self.clients.add(websocket) | ||
try: | ||
await websocket.wait_closed() | ||
finally: | ||
self.clients.remove(websocket) | ||
|
||
async with serve(register_client, WS_HOST, WS_PORT): | ||
await asyncio.Future() | ||
|
||
async def broadcast(self, message: BrightnessMessage): | ||
"""send the message to all websockets""" | ||
log.info(f"broadcasting to {len(self.clients)} clients") | ||
message_json = json.dumps(asdict(message)) | ||
broadcast(self.clients, message_json) |