Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 85 additions & 12 deletions daq/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@
POST /log/stop - stop manual CSV recording

All endpoints return JSON. Errors return {"error": "description"}.

/actuator, /fire, and /sequence/* share a single mutex and return 423
Locked if busy instead of queuing. /safe and /abort use the same
mutex but block until acquired so safety commands are never dropped.
"""

from __future__ import annotations

import asyncio
import threading
import time
from contextlib import contextmanager
from typing import Optional, Any

from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
Expand All @@ -65,6 +71,32 @@
_broadcaster = Broadcaster()


# Serializes API write handlers; always acquired before Engine._lock to avoid deadlocks.
_control_lock = threading.Lock()


class _ControlLockBusy(Exception):
"""Internal signal only: non-blocking acquire of _control_lock failed."""


@contextmanager
def _control_guard(*, blocking: bool):
"""Context manager for _control_lock.

blocking=False:
Raises _ControlLockBusy if contended (used by droppable routes:
/actuator, /fire, /sequence/*; callers return 423).
blocking=True:
Blocks until acquired (used by critical routes: /abort, /safe).
"""
if not _control_lock.acquire(blocking=blocking):
raise _ControlLockBusy()
try:
yield
finally:
_control_lock.release()


def set_engine(engine, logger=None) -> None:
"""
Injects runtime engine and logger instances at startup.
Expand Down Expand Up @@ -336,12 +368,18 @@ def post_actuator(cmd: ActuatorCommand):
The GUI should reflect the blocked state immediately per SR 3.2.4.

Body: {"name": "lox_main", "state": 1}

Returns 423 Locked instead of running at all if another guarded
command is being serviced.
"""
eng = _require_engine()
if cmd.state not in (0, 1):
raise HTTPException(status_code=422, detail="state must be 0 or 1")
try:
eng.write_actuator(cmd.name, cmd.state)
with _control_guard(blocking=False):
eng.write_actuator(cmd.name, cmd.state)
except _ControlLockBusy:
return Response(status_code=423)
except RuntimeError as exc:
raise HTTPException(status_code=409, detail=str(exc))
except KeyError as exc:
Expand All @@ -353,7 +391,8 @@ def post_actuator(cmd: ActuatorCommand):
def post_safe():
"""Immediately aborts active sequences and de-energizes all hardware outputs."""
eng = _require_engine()
eng.all_safe()
with _control_guard(blocking=True):
eng.all_safe()
return {"ok": True}


Expand All @@ -368,6 +407,9 @@ def post_fire():

Raises:
409: A sequence is already active, or this one was refused.

Returns 423 Locked instead of running at all if another guarded
command is being serviced.
"""
eng = _require_engine()
snap = eng.snapshot
Expand All @@ -377,7 +419,10 @@ def post_fire():
detail=f"Sequence '{snap.sequence_name}' already running"
)
try:
eng.fire()
with _control_guard(blocking=False):
eng.fire()
except _ControlLockBusy:
return Response(status_code=423)
except SequenceRefused as exc:
raise HTTPException(status_code=409, detail=str(exc))
return {"ok": True, "sequence": "fire"}
Expand All @@ -391,9 +436,13 @@ def post_abort():
Always succeeds. If abort.yaml is unrunnable the hardware is still
de-energised, so there is no failure to report to the caller - check
/events for whether the ordered closure or the fallback ran.

Always waits its turn on the guard shared with /actuator, /fire, and
/sequence/* and is never discarded b/c of aborts.
"""
eng = _require_engine()
eng.abort()
with _control_guard(blocking=True):
eng.abort()
return {"ok": True}


Expand All @@ -406,10 +455,16 @@ def post_sequence_start():
Unlike POST /fire, this does not reset position to 0 first. Refused
(409) if a sequence was aborted since - only POST /fire can restart
from there.

Returns 423 Locked instead of running at all if another guarded
command is being serviced.
"""
eng = _require_engine()
try:
return eng.start_sequence()
with _control_guard(blocking=False):
return eng.start_sequence()
except _ControlLockBusy:
return Response(status_code=423)
except (SequenceRefused, RuntimeError) as exc:
raise HTTPException(status_code=409, detail=str(exc))

Expand All @@ -419,10 +474,16 @@ def post_sequence_stop():
"""
Pauses the fire sequence in place. No abort.yaml runs,
hardware is left exactly where it sits, and CSV recording continues.

Returns 423 Locked instead of running at all if another guarded
command is being serviced.
"""
eng = _require_engine()
try:
return eng.stop_sequence()
with _control_guard(blocking=False):
return eng.stop_sequence()
except _ControlLockBusy:
return Response(status_code=423)
except RuntimeError as exc:
raise HTTPException(status_code=409, detail=str(exc))

Expand All @@ -431,13 +492,19 @@ def post_sequence_stop():
def post_sequence_time(req: SequenceTimeRequest):
"""
Seeks the fire sequence's clock. While stopped: display/rehearsal
only, no hardware effect. While running: forward-only fires every
action between the current position and the target, then keeps
only, no hardware effect. While running: forward-only fires every
action between the current position and the target, then keeps
ticking from there. Refused (409) for a backward seek while running.

Returns 423 Locked instead of running at all if another guarded
command is being serviced.
"""
eng = _require_engine()
try:
return eng.set_sequence_time(req.seconds)
with _control_guard(blocking=False):
return eng.set_sequence_time(req.seconds)
except _ControlLockBusy:
return Response(status_code=423)
except (SequenceRefused, RuntimeError) as exc:
raise HTTPException(status_code=409, detail=str(exc))

Expand All @@ -447,10 +514,16 @@ def post_sequence_step(req: SequenceStepRequest):
"""
Seeks the fire sequence's clock to a named step's start time. Same
stopped/running behavior as /sequence/time.

Returns 423 Locked instead of running at all if another guarded
command is being serviced.
"""
eng = _require_engine()
try:
return eng.jump_to_step(req.name)
with _control_guard(blocking=False):
return eng.jump_to_step(req.name)
except _ControlLockBusy:
return Response(status_code=423)
except (SequenceRefused, RuntimeError) as exc:
raise HTTPException(status_code=409, detail=str(exc))
except KeyError as exc:
Expand All @@ -471,7 +544,7 @@ def post_calibration(update: CalibrationUpdate):
Update a sensor calibration coefficient at runtime.

Changes take effect on the next processed scan batch.
Does not persist to calibration.json: must call /calibration/save for that.
Does not persist to calibration.json - call /calibration/save for that.

Body: {"tag": "pt0", "slope": 128.0, "intercept": -62.8}
"""
Expand Down
36 changes: 18 additions & 18 deletions daq/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,24 +527,24 @@ def write_actuator(self, name: str, state: int) -> None:
"Send ABORT first."
)

if self._is_stepper(name):
reading = self._device.actuator_states().get(name)
if reading is not None:
if reading.state == state:
self._log(
f"Manual: {name} already "
f"{'OPEN' if state else 'CLOSED'} - command ignored "
f"rather than re-running the step burst"
)
return
if reading.moving:
raise RuntimeError(
f"'{name}' is still executing a move - wait for it to "
f"finish, or send ABORT to reverse it now"
)

self._device.write_actuator(name, state)
self._log(f"Manual: {name} -> {'OPEN' if state else 'CLOSED'}")
if self._is_stepper(name):
reading = self._device.actuator_states().get(name)
if reading is not None:
if reading.state == state:
self._log(
f"Manual: {name} already "
f"{'OPEN' if state else 'CLOSED'} - command ignored "
f"rather than re-running the step burst"
)
return
if reading.moving:
raise RuntimeError(
f"'{name}' is still executing a move - wait for it to "
f"finish, or send ABORT to reverse it now"
)

self._device.write_actuator(name, state)
self._log(f"Manual: {name} -> {'OPEN' if state else 'CLOSED'}")

def _is_stepper(self, name: str) -> bool:
"""True if `name` is a pulse_stepper in this cart's manifest."""
Expand Down
Loading
Loading