sprint-4: Runtime server base — tag_store + historian + alarm engine + API FastAPI
Arquitectura asincronica completa para correr 24/7 a bordo del buque.
vmssailor/runtime/server/tag_store.py
- TagStore in-memory con pub/sub asyncio.Queue
- register_tag/register_many con valores iniciales
- TagValue dataclass: value + quality + timestamp + raw_value
- subscribe()/unsubscribe() para fan-out
- stats() con breakdown por Quality
vmssailor/runtime/server/historian.py
- Historian DuckDB embebido (in-memory o archivo)
- Reader loop suscrito al tag_store + buffer + flush periodico (1s)
- query(tag_id, since, until, limit) para series temporales
- Soporta valores numericos y boolean separadamente
vmssailor/runtime/server/alarm_engine.py
- Suscriptor al tag_store que evalua AlarmConfig por update
- Operators: >, >=, <, <=, ==, !=
- Hysteresis correcta: aplica al SALIR de alarma, no a entrar
- Delay configurable (persistencia minima antes de disparar)
- Estados: ACTIVE -> ACK (con user) -> CLEARED
- ack(alarm_id, user) reconoce sin clear
vmssailor/runtime/server/drivers.py
- SimulatorDriver: produce valores sinteticos creibles por UnitSI
- Tick configurable (default 0.5s)
- Respeta range_normal_min/max del tag para mantenerse en rango
- Permite probar UI/API sin hardware ni Modbus real
vmssailor/runtime/server/runtime_app.py
- RuntimeApp dataclass ensambla todos los servicios
- build_runtime(project) construye listo para correr
- start()/stop() async lifecycle ordenado
vmssailor/runtime/server/api.py
- FastAPI app con lifespan que arranca/detiene el runtime
- GET /health, /project
- GET /tags, /tags/{id}, /tags/{id}/history
- GET /alarms, POST /alarms/{id}/ack
- WebSocket /ws/realtime con snapshot inicial + push + heartbeat
runtime_server_main.py
- Entry point con argparse: --vmsproj, --host, --port, --db
- Sin --vmsproj usa proyecto demo Sprint 0 (genera simulator vivo)
- Lanza con uvicorn
Tests (tests/runtime/, 16 nuevos, total 142/142):
- test_tag_store: register, update, subscribe, unsubscribe, stats
- test_historian: roundtrip query, stats
- test_alarm_engine: fire when below, hysteresis clears, ack
- test_api: health, project, tags listing, history, alarms via httpx.ASGITransport
Para correr el servidor en vivo:
uv run python runtime_server_main.py --verbose
Luego en otro shell:
curl http://127.0.0.1:8765/health
curl http://127.0.0.1:8765/tags | jq .
Dependencias agregadas:
- fastapi >=0.110
- uvicorn[standard] >=0.27
- websockets >=12.0
- duckdb >=0.10
- pymodbus >=3.5 (Sprint 5)
- python-can >=4.3 (Sprint 5)
- httpx >=0.27 (testing + cliente HTTP)
142/142 pytest verde, ruff clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+49
-5
@@ -1,16 +1,60 @@
|
||||
"""Entry point del servicio Runtime (stub Sprint 0).
|
||||
"""Entry point del Runtime servidor.
|
||||
|
||||
Sprint 4 lo reemplaza con `vmssailor.runtime.server.service:main`.
|
||||
Uso:
|
||||
uv run vms-runtime-server --vmsproj path/to/project.vmsproj
|
||||
uv run vms-runtime-server (genera proyecto demo y arranca)
|
||||
|
||||
Sin --vmsproj genera el proyecto demo de Sprint 0 y arranca el servidor
|
||||
con simulator-driver para que puedas probar el cliente y la API.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main() -> int:
|
||||
print("VMS-Sailor Runtime servidor — Sprint 4 trae el servicio Windows.")
|
||||
print("En Sprint 0 solo existe el modelo de datos core.")
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="vms-runtime-server")
|
||||
parser.add_argument(
|
||||
"--vmsproj",
|
||||
type=Path,
|
||||
default=None,
|
||||
help="Ruta al .vmsproj a cargar. Default: genera proyecto demo Sprint 0.",
|
||||
)
|
||||
parser.add_argument("--host", default="127.0.0.1")
|
||||
parser.add_argument("--port", type=int, default=8765)
|
||||
parser.add_argument("--db", type=Path, default=None, help="Historian DuckDB (default: in-memory)")
|
||||
parser.add_argument("--verbose", action="store_true")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
from vmssailor.shared.logging_setup import setup_logging
|
||||
|
||||
setup_logging(verbose=args.verbose)
|
||||
log = logging.getLogger("vms-runtime-server")
|
||||
|
||||
if args.vmsproj:
|
||||
from vmssailor.core.persistence import load_project
|
||||
|
||||
project = load_project(args.vmsproj)
|
||||
log.info("Loaded project from %s", args.vmsproj)
|
||||
else:
|
||||
from vmssailor.tools.generate_test_project import build_demo_project
|
||||
|
||||
project = build_demo_project()
|
||||
log.info("No --vmsproj — using built-in demo project")
|
||||
|
||||
from vmssailor.runtime.server.api import create_app
|
||||
from vmssailor.runtime.server.runtime_app import build_runtime
|
||||
|
||||
runtime = build_runtime(project, historian_db=args.db)
|
||||
app = create_app(runtime)
|
||||
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host=args.host, port=args.port, log_level="info" if args.verbose else "warning")
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user