feat: add osc scaffolding

This commit is contained in:
2025-01-10 15:33:19 +01:00
parent 724a423ce8
commit 6e3a97da5f
2 changed files with 20 additions and 3 deletions

View File

@@ -31,7 +31,7 @@ RUN cp /psn-py/psn$(python3-config --extension-suffix) /output
FROM alpine:3.20 AS final
RUN apk add --no-cache python3 py3-aiohttp
RUN apk add --no-cache python3 py3-aiohttp py3-python-osc
COPY --from=backend /output /backend/psn.so

View File

@@ -7,12 +7,15 @@ import weakref
from dataclasses import dataclass
import psn
from pythonosc.osc_server import AsyncIOOSCUDPServer
from pythonosc.dispatcher import Dispatcher
from aiohttp import web, WSCloseCode
PSN_DEFAULT_UDP_PORT = 56565
PSN_DEFAULT_UDP_MCAST_ADDRESS = "236.10.10.10"
PORT = 8000
WEB_SERVER_PORT = 8000
IP = "0.0.0.0"
OSC_SERVER_PORT = 6969
NUM_TRACKERS = 3
@@ -129,6 +132,19 @@ async def background_tasks(app: web.Application):
await app["broadcast_psn_data"]
def filter_handler(address, *args) -> None:
logging.debug(f"{address}: {args}")
async def receive_osc_data(app):
dispatcher = Dispatcher()
dispatcher.map("/Tracker*", filter_handler)
server = AsyncIOOSCUDPServer((IP, OSC_SERVER_PORT), dispatcher, asyncio.get_event_loop())
transport, protocol = await server.create_serve_endpoint()
app["osc_transport"] = transport
yield
await transport.close()
def create_app():
app = web.Application()
app.router.add_get("/", handle_root)
@@ -145,6 +161,7 @@ def create_app():
app.on_shutdown.append(on_shutdown)
app.cleanup_ctx.append(background_tasks)
app.cleanup_ctx.append(receive_osc_data)
return app
@@ -152,4 +169,4 @@ def create_app():
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
app = create_app()
web.run_app(app, host=IP, port=PORT)
web.run_app(app, host=IP, port=WEB_SERVER_PORT)