Skip to content

Commit b7da37f

Browse files
feat(api): introduce FastAPI app that boots SerialManager and exposes GET /status
- Add benchmesh_service.api FastAPI module - Add dependencies (fastapi, uvicorn) to pyproject - README_FASTAPI with run instructions Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 362aed5 commit b7da37f

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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.

benchmesh-serial-service/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pyserial = "^3.5"
1515
pydantic = "^1.8"
1616
loguru = "^0.5.3"
1717
pyyaml = "^6.0.3"
18+
fastapi = "^0.111.0"
19+
uvicorn = {version = "^0.30.0", extras=["standard"]}
1820

1921
[build-system]
2022
requires = ["poetry-core>=1.0.0"]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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}

0 commit comments

Comments
 (0)