camwall.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Read-only Cam Wall feed for token-authenticated kiosk displays (#2531).
  2. The Cam Wall inside the SPA runs on the ordinary printers API, behind a JWT. A
  3. wall pinned to a TV has no login, so it authenticates with a long-lived
  4. ``camwall``-scoped token carried in the URL — and a URL on a lobby screen is
  5. about as private as a sticky note.
  6. That is why this endpoint exists instead of letting a token through to
  7. ``GET /printers``: the printer list carries ``serial_number`` and
  8. ``ip_address`` (see ``schemas/printer.py``), and neither belongs on a screen in
  9. a shared room. What a wall tile actually draws is the whole payload here — a
  10. name, a connection flag, a state, a progress bar.
  11. Notably absent is the print filename. A token wall renders the compact status
  12. overlay, so the part being printed is never named to the room; the field simply
  13. isn't served rather than being served and then hidden client-side.
  14. """
  15. import logging
  16. from fastapi import APIRouter, Depends
  17. from sqlalchemy import select
  18. from sqlalchemy.ext.asyncio import AsyncSession
  19. from backend.app.core.auth import RequireCamWallTokenIfAuthEnabled
  20. from backend.app.core.database import get_db
  21. from backend.app.models.printer import Printer
  22. from backend.app.services.printer_manager import printer_manager
  23. _logger = logging.getLogger(__name__)
  24. router = APIRouter(prefix="/camwall", tags=["camwall"])
  25. @router.get("/printers")
  26. async def list_camwall_printers(
  27. _: None = RequireCamWallTokenIfAuthEnabled,
  28. db: AsyncSession = Depends(get_db),
  29. ) -> list[dict]:
  30. """Every printer plus the handful of status fields a Cam Wall tile draws.
  31. One call for the whole wall rather than one per printer: a kiosk polls this
  32. on a fixed interval with no WebSocket to invalidate it, and N+1 requests
  33. every few seconds is a poor trade for a screen nobody is interacting with.
  34. Ordered by name so tile positions stay put across polls — a wall that
  35. reshuffles itself is unusable to watch.
  36. """
  37. result = await db.execute(select(Printer).order_by(Printer.name))
  38. printers = list(result.scalars().all())
  39. payload: list[dict] = []
  40. for printer in printers:
  41. state = printer_manager.get_status(printer.id)
  42. entry: dict = {
  43. "id": printer.id,
  44. "name": printer.name,
  45. "camera_rotation": printer.camera_rotation or 0,
  46. # Mirrors get_printer_status(): no state object at all means the
  47. # printer was never connected this run; a state object still has
  48. # to be asked whether its link is currently up.
  49. "connected": bool(state and state.connected),
  50. "state": None,
  51. "progress": None,
  52. "remaining_time": None,
  53. "layer_num": None,
  54. "total_layers": None,
  55. # Codes only — enough for the client to run the same
  56. # filterKnownHMSErrors() it uses on the authenticated wall, so the
  57. # error chip means the same thing in both modes.
  58. "hms_errors": [],
  59. }
  60. if state is not None:
  61. entry.update(
  62. {
  63. "state": state.state,
  64. "progress": state.progress,
  65. "remaining_time": state.remaining_time,
  66. "layer_num": state.layer_num,
  67. "total_layers": state.total_layers,
  68. "hms_errors": [
  69. {
  70. "code": e.code,
  71. "attr": e.attr,
  72. "module": e.module,
  73. "severity": e.severity,
  74. "actions": e.actions or [],
  75. }
  76. for e in (state.hms_errors or [])
  77. ],
  78. }
  79. )
  80. payload.append(entry)
  81. return payload