diff --git a/Dockerfile b/Dockerfile index bde770e..e5f16af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/backend/psn_server.py b/backend/psn_server.py index 60847be..55e7b26 100644 --- a/backend/psn_server.py +++ b/backend/psn_server.py @@ -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)