Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (70 loc) · 3.11 KB
/
Copy pathmain.py
File metadata and controls
93 lines (70 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
BeamNG Racing Dashboard – entry point.
Starts three concurrent asyncio tasks:
1. TelemetryReceiver – UDP socket, receives BeamNG OutGauge packets
2. UpdateController – rate-limited broadcast loop (20 FPS)
3. aiohttp Web Server – HTTP (static dashboard) + WebSocket (/ws)
Environment / configuration
----------------------------
All settings use sensible defaults; override via the constants below or via
environment variables in a later iteration.
Network layout
--------------
::
PC (this process)
├─ UDP 0.0.0.0:4444 ← BeamNG pushes OutGauge packets here
└─ TCP 0.0.0.0:8080 → browser connects, opens /ws for live data
"""
import asyncio
import logging
from aiohttp import web
from core.controller import UpdateController
from core.state import SharedState
from server.app import create_app
from telemetry.receiver import TelemetryReceiver
# ── Configuration ─────────────────────────────────────────────────────────────
UDP_HOST = "0.0.0.0"
UDP_PORT = 4444
WEB_HOST = "0.0.0.0"
WEB_PORT = 8080
TARGET_FPS = 20 # broadcast rate to browsers
# ── Logging ───────────────────────────────────────────────────────────────────
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(name)s – %(message)s",
)
logger = logging.getLogger(__name__)
# ── Main coroutine ────────────────────────────────────────────────────────────
async def main() -> None:
# 1. Shared State – single source of truth
shared_state = SharedState()
# 2. Update Controller – rate-limited broadcaster
controller = UpdateController(shared_state, target_fps=TARGET_FPS)
# 3. Telemetry Receiver – UDP input layer
receiver = TelemetryReceiver(host=UDP_HOST, port=UDP_PORT)
receiver.set_update_callback(shared_state.update)
await receiver.start()
logger.info("Telemetry receiver listening on UDP %s:%d", UDP_HOST, UDP_PORT)
# 4. Start broadcast loop as a background task
broadcast_task = asyncio.create_task(controller.broadcast_loop())
# 5. Web Server – HTTP + WebSocket
app = create_app(shared_state, controller)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, WEB_HOST, WEB_PORT)
await site.start()
logger.info("Dashboard available at http://%s:%d", WEB_HOST, WEB_PORT)
logger.info("WebSocket endpoint: ws://%s:%d/ws", WEB_HOST, WEB_PORT)
# Run until interrupted
try:
await asyncio.Event().wait()
except (KeyboardInterrupt, asyncio.CancelledError):
pass
finally:
controller.stop()
broadcast_task.cancel()
receiver.stop()
await runner.cleanup()
logger.info("Shutdown complete.")
if __name__ == "__main__":
asyncio.run(main())