obico.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """API routes for Obico AI failure detection."""
  2. import logging
  3. from fastapi import APIRouter, HTTPException, Response
  4. from pydantic import BaseModel
  5. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  6. from backend.app.core.permissions import Permission
  7. from backend.app.models.user import User
  8. from backend.app.services.obico_detection import obico_detection_service, pop_frame
  9. logger = logging.getLogger(__name__)
  10. router = APIRouter(prefix="/obico", tags=["obico"])
  11. class TestConnectionRequest(BaseModel):
  12. url: str
  13. @router.get("/status")
  14. async def get_status(
  15. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  16. ):
  17. """Scheduler status, per-printer classification, and recent detection history."""
  18. settings = await obico_detection_service._load_settings()
  19. status = obico_detection_service.get_status(settings["sensitivity"])
  20. return {
  21. **status,
  22. "enabled": settings["enabled"],
  23. "ml_url": settings["ml_url"],
  24. "sensitivity": settings["sensitivity"],
  25. "action": settings["action"],
  26. "poll_interval": settings["poll_interval"],
  27. "external_url_configured": bool(settings["external_url"]),
  28. }
  29. @router.post("/test-connection")
  30. async def test_connection(
  31. req: TestConnectionRequest,
  32. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  33. ):
  34. """Ping the Obico ML API `/hc/` health endpoint. Returns ok + raw body."""
  35. if not req.url:
  36. return {"ok": False, "status_code": None, "body": None, "error": "URL is empty"}
  37. return await obico_detection_service.test_connection(req.url)
  38. @router.get("/cached-frame/{nonce}")
  39. async def cached_frame(nonce: str):
  40. """Serve a pre-captured JPEG to the Obico ML API.
  41. The detection loop captures a snapshot locally (where we control the timeout),
  42. stashes the bytes under a one-shot random nonce, then hands this URL to Obico's
  43. ML API. Obico's hardcoded 5s read timeout never races our snapshot pipeline.
  44. Unauthenticated: the unguessable 32-byte nonce is single-use and expires in
  45. seconds, so exposing this path doesn't widen the camera access surface.
  46. """
  47. data = await pop_frame(nonce)
  48. if data is None:
  49. raise HTTPException(status_code=404, detail="Frame not found or expired")
  50. return Response(
  51. content=data,
  52. media_type="image/jpeg",
  53. headers={"Cache-Control": "no-store"},
  54. )