30 lines
829 B
Python
30 lines
829 B
Python
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel, field_validator
|
|
|
|
from services.leds import set_led
|
|
|
|
router = APIRouter(prefix="/api/drives", tags=["leds"])
|
|
|
|
|
|
class LedRequest(BaseModel):
|
|
state: str
|
|
|
|
@field_validator("state")
|
|
@classmethod
|
|
def validate_state(cls, v: str) -> str:
|
|
if v not in ("locate", "off"):
|
|
raise ValueError("state must be 'locate' or 'off'")
|
|
return v
|
|
|
|
|
|
@router.post("/{device}/led")
|
|
async def set_drive_led(device: str, body: LedRequest):
|
|
"""Set the locate LED for a drive."""
|
|
try:
|
|
result = await set_led(device, body.state)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except RuntimeError as e:
|
|
raise HTTPException(status_code=502, detail=str(e))
|
|
return result
|