File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # BenchMesh Serial Service - FastAPI mode
2+
3+ Two ways to run the service:
4+
5+ 1 ) Standalone SerialManager (legacy/debug)
6+ - poetry run python -m benchmesh_service.main --config ./config.yaml
7+
8+ 2 ) FastAPI application
9+ - ENV: BENCHMESH_CONFIG=./config.yaml
10+ - poetry run uvicorn benchmesh_service.api: app --host 0.0.0.0 --port 52892
11+
12+ API
13+ - GET /status -> { "devices_total": N, "connected": M, "disconnected": N-M }
14+
15+ Notes
16+ - The FastAPI app starts/stops SerialManager on app startup/shutdown.
17+ - Keep BENCHMESH_CONFIG environment variable pointing to your config.yaml.
18+ - To enable console DEBUG logs, adjust logger configuration if desired.
Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ pyserial = "^3.5"
1515pydantic = " ^1.8"
1616loguru = " ^0.5.3"
1717pyyaml = " ^6.0.3"
18+ fastapi = " ^0.111.0"
19+ uvicorn = {version = " ^0.30.0" , extras =[" standard" ]}
1820
1921[build-system ]
2022requires = [" poetry-core>=1.0.0" ]
Original file line number Diff line number Diff line change 1+ import os
2+ from fastapi import FastAPI
3+ from .serial_manager import SerialManager
4+ from .config import load_config
5+
6+ app = FastAPI (title = "BenchMesh Serial Service" , version = "0.1.0" )
7+
8+ _manager : SerialManager | None = None
9+
10+
11+ def _make_manager () -> SerialManager :
12+ cfg_path = os .getenv ("BENCHMESH_CONFIG" , "config.yaml" )
13+ cfg = load_config (cfg_path )
14+ return SerialManager (cfg .get ('devices' , []))
15+
16+
17+ @app .on_event ("startup" )
18+ def on_startup ():
19+ global _manager
20+ _manager = _make_manager ()
21+ _manager .start ()
22+
23+
24+ @app .on_event ("shutdown" )
25+ def on_shutdown ():
26+ global _manager
27+ if _manager :
28+ _manager .stop ()
29+ _manager = None
30+
31+
32+ @app .get ("/status" , summary = "Service status" , response_model = dict )
33+ def get_status ():
34+ global _manager
35+ if not _manager :
36+ return {"devices_total" : 0 , "connected" : 0 , "disconnected" : 0 }
37+ device_ids = [d .get ('id' ) for d in _manager .devices if d .get ('id' )]
38+ total = len (device_ids )
39+ connected = sum (1 for did in device_ids if _manager .connections .get (did ))
40+ return {"devices_total" : total , "connected" : connected , "disconnected" : total - connected }
You can’t perform that action at this time.
0 commit comments