Files
pont-bascule-connector/app/serial_bridge.py.old
matthieu fce468cf76 feat(api): FastAPI bridge for pont bascule via serial + Tailscale access
- Add FastAPI service exposing /health, /last, /send/esclave, /send/dsd, /send/custom
- Implement SerialBridge aligned with legacy Tkinter behavior (open delay 2s, post-write 0.5s, read in_waiting once)
- Enforce single in-flight serial request (non-blocking lock, returns 409 BUSY)
- Add environment-based serial configuration (.env + systemd EnvironmentFile)
- Document installation, systemd service, and Tailscale usage (direct IP and tailscale serve)
2026-01-09 15:34:47 +00:00

27 lines
654 B
Python

import threading
from dataclasses import dataclass
def hex2b(s: str) -> bytes:
return bytes(int(x, 16) for x in s.split())
@dataclass
class SerialConfig:
port: str
baudrate: int = 9600
class SerialBridge:
def __init__(self, cfg: SerialConfig):
self.cfg = cfg
self._lock = threading.Lock()
def busy(self):
return self._lock.locked()
def send_and_read_once(self, payload: bytes):
if not self._lock.acquire(blocking=False):
return {"busy": True, "error": "BUSY"}
try:
return {"ok": True, "request": payload.hex()}
finally:
self._lock.release()