spoolbuddy.py 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. """SpoolBuddy device management API routes."""
  2. import asyncio
  3. import contextlib
  4. import json
  5. import logging
  6. import time
  7. from datetime import datetime, timedelta, timezone
  8. from urllib.parse import urlparse
  9. import httpx
  10. from fastapi import APIRouter, Depends, HTTPException
  11. from sqlalchemy import select
  12. from sqlalchemy.ext.asyncio import AsyncSession
  13. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  14. from backend.app.core.database import get_db
  15. from backend.app.core.permissions import Permission
  16. from backend.app.core.websocket import ws_manager
  17. from backend.app.models.spoolbuddy_device import SpoolBuddyDevice
  18. from backend.app.models.user import User
  19. from backend.app.schemas.spoolbuddy import (
  20. CalibrationResponse,
  21. DeviceRegisterRequest,
  22. DeviceResponse,
  23. DiagnosticResultRequest,
  24. DisplaySettingsRequest,
  25. HeartbeatRequest,
  26. HeartbeatResponse,
  27. ScaleReadingRequest,
  28. SetCalibrationFactorRequest,
  29. SetTareRequest,
  30. SystemCommandRequest,
  31. SystemCommandResultRequest,
  32. SystemConfigRequest,
  33. TagRemovedRequest,
  34. TagScannedRequest,
  35. UpdateSpoolWeightRequest,
  36. UpdateStatusRequest,
  37. WriteTagRequest,
  38. WriteTagResultRequest,
  39. )
  40. from backend.app.services.spool_tag_matcher import get_spool_by_tag
  41. from backend.app.services.spoolman import SpoolmanClientError, SpoolmanNotFoundError, SpoolmanUnavailableError
  42. logger = logging.getLogger(__name__)
  43. router = APIRouter(prefix="/spoolbuddy", tags=["spoolbuddy"])
  44. OFFLINE_THRESHOLD_SECONDS = 30
  45. ONLINE_BROADCAST_INTERVAL_SECONDS = 10
  46. _SSRF_WARN_THROTTLE_SECONDS = 60
  47. _spoolbuddy_online_last_broadcast: dict[str, float] = {}
  48. _ssrf_warn_last_broadcast: dict[str, float] = {}
  49. _diagnostic_results: dict[tuple[str, str], dict] = {}
  50. @contextlib.asynccontextmanager
  51. async def _translate_spoolbuddy_errors():
  52. """Translate Spoolman typed exceptions to HTTP for SpoolBuddy endpoints."""
  53. try:
  54. yield
  55. except SpoolmanNotFoundError as exc:
  56. raise HTTPException(status_code=404, detail="Spool not found in Spoolman") from exc
  57. except SpoolmanClientError as exc:
  58. raise HTTPException(status_code=502, detail="Spoolman rejected the request") from exc
  59. except SpoolmanUnavailableError as exc:
  60. raise HTTPException(status_code=503, detail="Spoolman server is not reachable") from exc
  61. async def _get_spoolman_client_or_none(db: AsyncSession):
  62. """Return a SpoolmanClient if Spoolman is enabled with a safe URL, else None."""
  63. from backend.app.api.routes._spoolman_helpers import assert_safe_spoolman_url
  64. from backend.app.models.settings import Settings
  65. from backend.app.services.spoolman import get_spoolman_client, init_spoolman_client
  66. settings_result = await db.execute(select(Settings))
  67. settings_dict = {s.key: s.value for s in settings_result.scalars().all()}
  68. spoolman_url = settings_dict.get("spoolman_url", "").strip()
  69. spoolman_enabled = settings_dict.get("spoolman_enabled", "false").lower() == "true" and bool(spoolman_url)
  70. if not spoolman_enabled:
  71. return None
  72. # SSRF guard: reject dangerous schemes, cloud-metadata IPs (169.254.169.254, 100.100.100.200,
  73. # fd00:ec2::254), multicast and unspecified addresses — loopback and RFC-1918 ranges are
  74. # intentionally permitted (Spoolman commonly runs on the same host or home LAN).
  75. try:
  76. assert_safe_spoolman_url(spoolman_url)
  77. except ValueError as exc:
  78. logger.warning(
  79. "Spoolman integration disabled: URL %r rejected by SSRF guard: %s",
  80. spoolman_url,
  81. exc,
  82. )
  83. now = time.monotonic()
  84. if now - _ssrf_warn_last_broadcast.get(spoolman_url, 0) > _SSRF_WARN_THROTTLE_SECONDS:
  85. _ssrf_warn_last_broadcast[spoolman_url] = now
  86. await ws_manager.broadcast(
  87. {
  88. "type": "spoolman_ssrf_blocked",
  89. "detail": "Spoolman URL was rejected by the SSRF guard",
  90. }
  91. )
  92. return None
  93. client = await get_spoolman_client()
  94. if not client or client.base_url != spoolman_url.rstrip("/"):
  95. try:
  96. client = await init_spoolman_client(spoolman_url)
  97. except ValueError as exc:
  98. logger.warning(
  99. "Spoolman integration disabled: URL %r rejected on re-initialisation: %s",
  100. spoolman_url,
  101. exc,
  102. )
  103. return None
  104. return client
  105. def _is_online(device: SpoolBuddyDevice) -> bool:
  106. if not device.last_seen:
  107. return False
  108. return (
  109. datetime.now(timezone.utc) - device.last_seen.replace(tzinfo=timezone.utc)
  110. ).total_seconds() < OFFLINE_THRESHOLD_SECONDS
  111. def _device_to_response(device: SpoolBuddyDevice) -> DeviceResponse:
  112. return DeviceResponse(
  113. id=device.id,
  114. device_id=device.device_id,
  115. hostname=device.hostname,
  116. ip_address=device.ip_address,
  117. firmware_version=device.firmware_version,
  118. has_nfc=device.has_nfc,
  119. has_scale=device.has_scale,
  120. tare_offset=device.tare_offset,
  121. calibration_factor=device.calibration_factor,
  122. nfc_reader_type=device.nfc_reader_type,
  123. nfc_connection=device.nfc_connection,
  124. backend_url=device.backend_url,
  125. display_brightness=device.display_brightness,
  126. display_blank_timeout=device.display_blank_timeout,
  127. has_backlight=device.has_backlight,
  128. last_calibrated_at=device.last_calibrated_at,
  129. last_seen=device.last_seen,
  130. pending_command=device.pending_command,
  131. nfc_ok=device.nfc_ok,
  132. scale_ok=device.scale_ok,
  133. uptime_s=device.uptime_s,
  134. update_status=device.update_status,
  135. update_message=device.update_message,
  136. system_stats=json.loads(device.system_stats) if device.system_stats else None,
  137. online=_is_online(device),
  138. created_at=device.created_at,
  139. updated_at=device.updated_at,
  140. )
  141. def _should_broadcast_online(device_id: str, force: bool = False) -> bool:
  142. if force:
  143. _spoolbuddy_online_last_broadcast[device_id] = time.time()
  144. return True
  145. now_ts = time.time()
  146. last_ts = _spoolbuddy_online_last_broadcast.get(device_id, 0.0)
  147. if now_ts - last_ts >= ONLINE_BROADCAST_INTERVAL_SECONDS:
  148. _spoolbuddy_online_last_broadcast[device_id] = now_ts
  149. return True
  150. return False
  151. # --- Device endpoints ---
  152. @router.post("/devices/register", response_model=DeviceResponse)
  153. async def register_device(
  154. req: DeviceRegisterRequest,
  155. db: AsyncSession = Depends(get_db),
  156. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  157. ):
  158. """Register or re-register a SpoolBuddy device."""
  159. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == req.device_id))
  160. device = result.scalar_one_or_none()
  161. now = datetime.now(timezone.utc)
  162. if device:
  163. device.hostname = req.hostname
  164. device.ip_address = req.ip_address
  165. device.firmware_version = req.firmware_version
  166. device.has_nfc = req.has_nfc
  167. device.has_scale = req.has_scale
  168. device.nfc_reader_type = req.nfc_reader_type
  169. device.nfc_connection = req.nfc_connection
  170. if req.backend_url:
  171. device.backend_url = req.backend_url
  172. device.has_backlight = req.has_backlight
  173. device.last_seen = now
  174. # Clear stale update status on re-registration (daemon restarted after update)
  175. if device.update_status in ("pending", "updating", "complete", "error"):
  176. device.update_status = None
  177. device.update_message = None
  178. logger.info("SpoolBuddy device re-registered: %s (%s)", req.device_id, req.hostname)
  179. else:
  180. device = SpoolBuddyDevice(
  181. device_id=req.device_id,
  182. hostname=req.hostname,
  183. ip_address=req.ip_address,
  184. firmware_version=req.firmware_version,
  185. has_nfc=req.has_nfc,
  186. has_scale=req.has_scale,
  187. tare_offset=req.tare_offset,
  188. calibration_factor=req.calibration_factor,
  189. nfc_reader_type=req.nfc_reader_type,
  190. nfc_connection=req.nfc_connection,
  191. has_backlight=req.has_backlight,
  192. backend_url=req.backend_url,
  193. last_seen=now,
  194. )
  195. db.add(device)
  196. logger.info("SpoolBuddy device registered: %s (%s)", req.device_id, req.hostname)
  197. await db.commit()
  198. await db.refresh(device)
  199. _spoolbuddy_online_last_broadcast[device.device_id] = time.time()
  200. await ws_manager.broadcast(
  201. {
  202. "type": "spoolbuddy_online",
  203. "device_id": device.device_id,
  204. "hostname": device.hostname,
  205. }
  206. )
  207. response = _device_to_response(device)
  208. # Include SSH public key so the daemon can auto-deploy it
  209. try:
  210. from backend.app.services.spoolbuddy_ssh import get_public_key
  211. response.ssh_public_key = await get_public_key()
  212. except Exception as exc:
  213. logger.warning("Could not attach SSH public key to heartbeat response: %s", exc)
  214. return response
  215. @router.get("/devices", response_model=list[DeviceResponse])
  216. async def list_devices(
  217. db: AsyncSession = Depends(get_db),
  218. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  219. ):
  220. """List all registered SpoolBuddy devices."""
  221. result = await db.execute(select(SpoolBuddyDevice).order_by(SpoolBuddyDevice.hostname))
  222. devices = list(result.scalars().all())
  223. return [_device_to_response(d) for d in devices]
  224. @router.delete("/devices/{device_id}")
  225. async def unregister_device(
  226. device_id: str,
  227. db: AsyncSession = Depends(get_db),
  228. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_DELETE),
  229. ):
  230. """Unregister a SpoolBuddy device. The daemon can re-register via heartbeat later."""
  231. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  232. device = result.scalar_one_or_none()
  233. if not device:
  234. raise HTTPException(status_code=404, detail="Device not registered")
  235. await db.delete(device)
  236. await db.commit()
  237. _spoolbuddy_online_last_broadcast.pop(device_id, None)
  238. logger.info("SpoolBuddy device unregistered: %s (%s)", device_id, device.hostname)
  239. await ws_manager.broadcast({"type": "spoolbuddy_unregistered", "device_id": device_id})
  240. return {"status": "deleted", "device_id": device_id}
  241. @router.post("/devices/{device_id}/heartbeat", response_model=HeartbeatResponse)
  242. async def device_heartbeat(
  243. device_id: str,
  244. req: HeartbeatRequest,
  245. db: AsyncSession = Depends(get_db),
  246. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  247. ):
  248. """Daemon heartbeat — updates status and returns pending commands."""
  249. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  250. device = result.scalar_one_or_none()
  251. if not device:
  252. raise HTTPException(status_code=404, detail="Device not registered")
  253. was_offline = not _is_online(device)
  254. now = datetime.now(timezone.utc)
  255. device.last_seen = now
  256. device.nfc_ok = req.nfc_ok
  257. device.scale_ok = req.scale_ok
  258. device.uptime_s = req.uptime_s
  259. if req.firmware_version:
  260. device.firmware_version = req.firmware_version
  261. if req.ip_address:
  262. device.ip_address = req.ip_address
  263. if req.nfc_reader_type:
  264. device.nfc_reader_type = req.nfc_reader_type
  265. if req.nfc_connection:
  266. device.nfc_connection = req.nfc_connection
  267. if req.backend_url:
  268. device.backend_url = req.backend_url
  269. if req.system_stats is not None:
  270. device.system_stats = json.dumps(req.system_stats)
  271. # Return and clear pending command
  272. pending = device.pending_command
  273. pending_write = None
  274. pending_system = None
  275. if pending == "write_tag" and device.pending_write_payload:
  276. # Parse the stored JSON payload to include in response
  277. try:
  278. pending_write = json.loads(device.pending_write_payload)
  279. except (json.JSONDecodeError, TypeError):
  280. pending_write = None
  281. # Don't clear write_tag command — it gets cleared by write-result
  282. elif pending == "apply_system_config" and device.pending_system_payload:
  283. try:
  284. pending_system = json.loads(device.pending_system_payload)
  285. except (json.JSONDecodeError, TypeError):
  286. pending_system = None
  287. # Don't clear config command — it gets cleared by daemon command-result callback
  288. elif pending and pending.startswith("run_") and pending.endswith("_diag"):
  289. # Don't clear diagnostic commands — they get cleared by the device reporting results
  290. pass
  291. else:
  292. device.pending_command = None
  293. await db.commit()
  294. # Emit online presence on offline->online transitions immediately, and
  295. # periodically while online so newly connected UIs can bootstrap state.
  296. if _should_broadcast_online(device.device_id, force=was_offline):
  297. await ws_manager.broadcast(
  298. {
  299. "type": "spoolbuddy_online",
  300. "device_id": device.device_id,
  301. "hostname": device.hostname,
  302. }
  303. )
  304. if was_offline:
  305. logger.info("SpoolBuddy device back online: %s", device.device_id)
  306. # Include current SSH public key so the daemon can re-deploy it whenever
  307. # Bambuddy's keypair rotates (data dir wiped, container recreated, etc.) —
  308. # otherwise SSH updates fail until the daemon restarts.
  309. ssh_public_key: str | None = None
  310. try:
  311. from backend.app.services.spoolbuddy_ssh import get_public_key
  312. ssh_public_key = await get_public_key()
  313. except Exception:
  314. pass
  315. return HeartbeatResponse(
  316. pending_command=pending,
  317. pending_write_payload=pending_write,
  318. pending_system_payload=pending_system,
  319. tare_offset=device.tare_offset,
  320. calibration_factor=device.calibration_factor,
  321. display_brightness=device.display_brightness,
  322. display_blank_timeout=device.display_blank_timeout,
  323. ssh_public_key=ssh_public_key,
  324. )
  325. # --- NFC endpoints ---
  326. @router.post("/nfc/tag-scanned")
  327. async def nfc_tag_scanned(
  328. req: TagScannedRequest,
  329. db: AsyncSession = Depends(get_db),
  330. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  331. ):
  332. """RPi reports NFC tag detected — lookup spool and broadcast.
  333. Routes the lookup to the inventory backend Bambuddy is configured for:
  334. Spoolman exclusively when ``spoolman_enabled`` is true, local DB
  335. exclusively otherwise. The previous implementation always tried local
  336. first and only consulted Spoolman as a fallback on local-DB miss, which
  337. meant a stale local copy of a tag would silently win over the
  338. authoritative Spoolman row, and deleting the local copy was the only way
  339. to surface the Spoolman match. Operators expect the SpoolBuddy lookup to
  340. follow the inventory mode they selected in Bambuddy settings.
  341. """
  342. from backend.app.api.routes._spoolman_helpers import _map_spoolman_spool
  343. # _get_spoolman_client_or_none returns a usable client when spoolman_enabled
  344. # is true (and the URL passes the SSRF guard), None otherwise — so its
  345. # return value doubles as the mode discriminator.
  346. client = await _get_spoolman_client_or_none(db)
  347. if client is not None:
  348. # Spoolman mode — exclusive lookup, no local-DB fallback.
  349. try:
  350. cached_spools = await client.get_spools()
  351. sm_spool: dict | None = None
  352. if req.tray_uuid:
  353. sm_spool = await client.find_spool_by_tag(req.tray_uuid, cached_spools=cached_spools)
  354. if sm_spool is None and req.tag_uid:
  355. sm_spool = await client.find_spool_by_tag(req.tag_uid, cached_spools=cached_spools)
  356. if sm_spool is not None:
  357. mapped = _map_spoolman_spool(sm_spool)
  358. await ws_manager.broadcast(
  359. {
  360. "type": "spoolbuddy_tag_matched",
  361. "device_id": req.device_id,
  362. "tag_uid": req.tag_uid,
  363. "tray_uuid": req.tray_uuid,
  364. "spool": {
  365. "id": mapped["id"],
  366. "material": mapped["material"],
  367. "subtype": mapped["subtype"],
  368. "color_name": mapped["color_name"],
  369. "rgba": mapped["rgba"],
  370. "brand": mapped["brand"],
  371. "label_weight": mapped["label_weight"],
  372. "core_weight": mapped["core_weight"],
  373. "weight_used": mapped["weight_used"],
  374. },
  375. }
  376. )
  377. logger.info("SpoolBuddy tag matched (Spoolman): %s -> spool %d", req.tag_uid, mapped["id"])
  378. return {"status": "ok", "matched": True, "spool_id": mapped["id"]}
  379. except ValueError as exc:
  380. logger.error(
  381. "Spoolman returned malformed spool data during tag lookup for %s: %s",
  382. req.tag_uid,
  383. exc,
  384. )
  385. return {"status": "ok", "matched": False, "spool_id": None}
  386. except (httpx.RequestError, httpx.HTTPStatusError, SpoolmanUnavailableError):
  387. logger.warning(
  388. "Spoolman unreachable during tag lookup for %s",
  389. req.tag_uid,
  390. )
  391. # Broadcast a diagnostic event so the UI can surface "Spoolman down" to the user.
  392. # Use a distinct type from spoolbuddy_unknown_tag — Spoolman outage != unregistered spool.
  393. await ws_manager.broadcast(
  394. {
  395. "type": "spoolman_unavailable",
  396. "device_id": req.device_id,
  397. "context": "nfc_tag_scanned",
  398. }
  399. )
  400. return {"status": "ok", "matched": False, "spool_id": None}
  401. except Exception as exc:
  402. logger.error(
  403. "Spoolman tag lookup failed unexpectedly for %s: %s",
  404. req.tag_uid,
  405. exc,
  406. )
  407. # Broadcast a distinct error event so operators can distinguish
  408. # "unexpected backend error" from "unregistered tag".
  409. await ws_manager.broadcast(
  410. {
  411. "type": "spoolbuddy_lookup_error",
  412. "device_id": req.device_id,
  413. }
  414. )
  415. # Same silent-return policy: an unexpected error must not break device operation
  416. # or trigger spurious duplicate-registration flows in the UI.
  417. return {"status": "ok", "matched": False, "spool_id": None}
  418. else:
  419. # Local mode — exclusive lookup, no Spoolman fallback.
  420. spool = await get_spool_by_tag(db, req.tag_uid, req.tray_uuid or "")
  421. if spool:
  422. await ws_manager.broadcast(
  423. {
  424. "type": "spoolbuddy_tag_matched",
  425. "device_id": req.device_id,
  426. "tag_uid": req.tag_uid,
  427. "tray_uuid": req.tray_uuid,
  428. "spool": {
  429. "id": spool.id,
  430. "material": spool.material,
  431. "subtype": spool.subtype,
  432. "color_name": spool.color_name,
  433. "rgba": spool.rgba,
  434. "brand": spool.brand,
  435. "label_weight": spool.label_weight,
  436. "core_weight": spool.core_weight,
  437. "weight_used": spool.weight_used,
  438. },
  439. }
  440. )
  441. logger.info("SpoolBuddy tag matched (local): %s -> spool %d", req.tag_uid, spool.id)
  442. return {"status": "ok", "matched": True, "spool_id": spool.id}
  443. await ws_manager.broadcast(
  444. {
  445. "type": "spoolbuddy_unknown_tag",
  446. "device_id": req.device_id,
  447. "tag_uid": req.tag_uid,
  448. "tray_uuid": req.tray_uuid,
  449. "sak": req.sak,
  450. "tag_type": req.tag_type,
  451. }
  452. )
  453. logger.info(
  454. "SpoolBuddy unknown tag: uid=%s (len=%d), tray_uuid=%s (len=%d), type=%s, sak=%s",
  455. req.tag_uid,
  456. len(req.tag_uid or ""),
  457. req.tray_uuid,
  458. len(req.tray_uuid or ""),
  459. req.tag_type,
  460. req.sak,
  461. )
  462. return {"status": "ok", "matched": False, "spool_id": None}
  463. @router.post("/nfc/tag-removed")
  464. async def nfc_tag_removed(
  465. req: TagRemovedRequest,
  466. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  467. ):
  468. """RPi reports NFC tag removed — broadcast event."""
  469. await ws_manager.broadcast(
  470. {
  471. "type": "spoolbuddy_tag_removed",
  472. "device_id": req.device_id,
  473. "tag_uid": req.tag_uid,
  474. }
  475. )
  476. return {"status": "ok"}
  477. @router.post("/nfc/write-tag")
  478. async def nfc_write_tag(
  479. req: WriteTagRequest,
  480. db: AsyncSession = Depends(get_db),
  481. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  482. ):
  483. """Queue an NFC tag write command for a SpoolBuddy device."""
  484. from backend.app.models.spool import Spool
  485. from backend.app.services.opentag3d import encode_opentag3d, encode_opentag3d_from_mapped
  486. # Find the device first (required regardless of spool source)
  487. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == req.device_id))
  488. device = result.scalar_one_or_none()
  489. if not device:
  490. raise HTTPException(status_code=404, detail="Device not registered")
  491. # Try local DB first
  492. result = await db.execute(select(Spool).where(Spool.id == req.spool_id))
  493. spool = result.scalar_one_or_none()
  494. nfc_warnings: list[str] = []
  495. if spool:
  496. ndef_data = encode_opentag3d(spool)
  497. data_origin = "local"
  498. else:
  499. # Local DB miss — fall back to Spoolman when enabled
  500. from backend.app.api.routes._spoolman_helpers import _map_spoolman_spool
  501. sm_client = await _get_spoolman_client_or_none(db)
  502. if sm_client is None:
  503. raise HTTPException(status_code=404, detail="Spool not found")
  504. async with _translate_spoolbuddy_errors():
  505. sm_spool = await sm_client.get_spool(req.spool_id)
  506. try:
  507. mapped = _map_spoolman_spool(sm_spool)
  508. except ValueError as exc:
  509. logger.warning("Spoolman returned invalid spool for write-tag: %s", exc)
  510. raise HTTPException(status_code=502, detail="Spoolman returned malformed spool data")
  511. if not mapped.get("material"):
  512. raise HTTPException(
  513. status_code=400,
  514. detail="Spoolman spool has no material set — cannot encode NFC tag",
  515. )
  516. ndef_data = encode_opentag3d_from_mapped(mapped)
  517. data_origin = "spoolman"
  518. # Warn when fields that drive NFC content are absent in Spoolman.
  519. # color_name specifically must check the raw filament field, not the
  520. # mapped value — _map_spoolman_spool falls back to the filament's
  521. # subtype when color_name is unset (so LinkSpoolModal stops showing
  522. # "Unknown color"), but the NFC tag should still warn when Spoolman
  523. # has no genuine color_name on file. Without this, the fallback
  524. # silently masks a real missing-data condition.
  525. raw_filament: dict = sm_spool.get("filament") or {}
  526. if not raw_filament.get("color_name"):
  527. nfc_warnings.append("color_name not set in Spoolman — tag encodes empty color name")
  528. if not mapped.get("nozzle_temp_min"):
  529. nfc_warnings.append("nozzle_temp_min not set in Spoolman — tag encodes 0 °C")
  530. if not mapped.get("subtype"):
  531. nfc_warnings.append("subtype not set in Spoolman — tag encodes empty subtype")
  532. if not mapped.get("brand"):
  533. nfc_warnings.append("brand/vendor not set in Spoolman — tag encodes empty brand")
  534. if not mapped.get("rgba"):
  535. nfc_warnings.append("rgba not set in Spoolman — tag encodes default colour")
  536. if not mapped.get("label_weight"):
  537. nfc_warnings.append("label_weight not set in Spoolman — tag encodes 0 g")
  538. if nfc_warnings:
  539. logger.warning(
  540. "NFC encode for Spoolman spool %d has incomplete data: %s",
  541. req.spool_id,
  542. "; ".join(nfc_warnings),
  543. )
  544. # Store write payload and set pending command
  545. device.pending_write_payload = json.dumps(
  546. {
  547. "spool_id": req.spool_id,
  548. "ndef_data_hex": ndef_data.hex(),
  549. "data_origin": data_origin,
  550. }
  551. )
  552. device.pending_command = "write_tag"
  553. await db.commit()
  554. logger.info(
  555. "Write tag queued for device %s, spool %d (%s, %d bytes)",
  556. req.device_id,
  557. req.spool_id,
  558. data_origin,
  559. len(ndef_data),
  560. )
  561. result: dict = {"status": "queued"}
  562. if nfc_warnings:
  563. result["warnings"] = nfc_warnings
  564. return result
  565. @router.post("/nfc/write-result")
  566. async def nfc_write_result(
  567. req: WriteTagResultRequest,
  568. db: AsyncSession = Depends(get_db),
  569. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  570. ):
  571. """Handle NFC tag write result from SpoolBuddy daemon."""
  572. # Find the device
  573. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == req.device_id))
  574. device = result.scalar_one_or_none()
  575. if not device:
  576. raise HTTPException(status_code=404, detail="Device not registered")
  577. # Capture data_origin before clearing the payload
  578. try:
  579. payload_dict = json.loads(device.pending_write_payload or "{}")
  580. except (json.JSONDecodeError, TypeError):
  581. payload_dict = {}
  582. logger.warning("Malformed pending_write_payload for device %s — treating as local", req.device_id)
  583. data_origin = payload_dict.get("data_origin", "local")
  584. device.pending_command = None
  585. device.pending_write_payload = None
  586. if req.success:
  587. if data_origin == "spoolman":
  588. # Update Spoolman extra.tag with the written NFC UID using a safe merge
  589. # (fetches current extra first to avoid overwriting other custom fields).
  590. sm_client = await _get_spoolman_client_or_none(db)
  591. if sm_client is None:
  592. logger.warning("Spoolman not configured; cannot persist tag link for spool %d", req.spool_id)
  593. await db.commit()
  594. await ws_manager.broadcast(
  595. {
  596. "type": "spoolbuddy_tag_link_failed",
  597. "device_id": req.device_id,
  598. "spool_id": req.spool_id,
  599. "tag_uid": req.tag_uid,
  600. "message": "Spoolman not configured",
  601. }
  602. )
  603. raise HTTPException(
  604. status_code=502,
  605. detail="Tag written to NFC but Spoolman is not configured; link not persisted",
  606. )
  607. _tag_link_ok = False
  608. try:
  609. tag_value = json.dumps(req.tag_uid.upper())
  610. # Tag uniqueness: a single physical NFC UID must map to at most
  611. # one Spoolman spool, otherwise find_spool_by_tag returns
  612. # whichever spool comes first in the cached list (usually the
  613. # older one) and the dashboard shows the wrong spool when the
  614. # tag is scanned. Before binding the new owner, clear the tag
  615. # from any other spool that currently has it. Best-effort:
  616. # cleanup failure does not block the write itself, but the
  617. # warning surfaces in logs so a stale duplicate can be tracked
  618. # down manually.
  619. try:
  620. cached_spools = await sm_client.get_spools()
  621. duplicate = await sm_client.find_spool_by_tag(req.tag_uid, cached_spools=cached_spools)
  622. if duplicate is not None and duplicate.get("id") != req.spool_id:
  623. await sm_client.merge_spool_extra(int(duplicate["id"]), {"tag": ""})
  624. logger.info(
  625. "Spoolman: cleared tag %s from previous holder spool %d before binding to spool %d",
  626. req.tag_uid,
  627. duplicate["id"],
  628. req.spool_id,
  629. )
  630. except (SpoolmanNotFoundError, SpoolmanUnavailableError, SpoolmanClientError) as cleanup_exc:
  631. logger.warning(
  632. "Spoolman: failed to clear duplicate tag %s before binding to spool %d (proceeding anyway): %s",
  633. req.tag_uid,
  634. req.spool_id,
  635. cleanup_exc,
  636. )
  637. except Exception:
  638. logger.exception(
  639. "Spoolman: unexpected error clearing duplicate tag %s before binding to spool %d (proceeding anyway)",
  640. req.tag_uid,
  641. req.spool_id,
  642. )
  643. await sm_client.merge_spool_extra(req.spool_id, {"tag": tag_value})
  644. logger.info(
  645. "Spoolman tag written and linked: spool %d -> tag %s",
  646. req.spool_id,
  647. req.tag_uid,
  648. )
  649. _tag_link_ok = True
  650. except (SpoolmanNotFoundError, SpoolmanUnavailableError, SpoolmanClientError) as exc:
  651. logger.error(
  652. "Spoolman error during tag write-back for spool %d (type=%s, status=%s): %s",
  653. req.spool_id,
  654. type(exc).__name__,
  655. getattr(exc, "status_code", "N/A"),
  656. exc,
  657. )
  658. # fall through to broadcast + raise 502 below
  659. except Exception:
  660. logger.exception(
  661. "Unexpected error during Spoolman tag write-back for spool %d",
  662. req.spool_id,
  663. )
  664. # fall through to broadcast + raise 502 below
  665. await db.commit()
  666. if _tag_link_ok:
  667. await ws_manager.broadcast(
  668. {
  669. "type": "spoolbuddy_tag_written",
  670. "device_id": req.device_id,
  671. "spool_id": req.spool_id,
  672. "tag_uid": req.tag_uid,
  673. }
  674. )
  675. else:
  676. await ws_manager.broadcast(
  677. {
  678. "type": "spoolbuddy_tag_link_failed",
  679. "device_id": req.device_id,
  680. "spool_id": req.spool_id,
  681. "tag_uid": req.tag_uid,
  682. # Generic message — full exception (may contain internal URLs/hostnames)
  683. # is logged server-side only to prevent information leakage via WebSocket.
  684. "message": "Spoolman link failed",
  685. }
  686. )
  687. raise HTTPException(
  688. status_code=502,
  689. detail="Tag written to NFC but Spoolman link failed",
  690. )
  691. else:
  692. # Link the tag to the local DB spool
  693. from backend.app.models.spool import Spool
  694. result = await db.execute(select(Spool).where(Spool.id == req.spool_id))
  695. spool = result.scalar_one_or_none()
  696. if spool is None:
  697. logger.warning(
  698. "NFC tag written for spool %d but it no longer exists in local DB; tag is orphaned",
  699. req.spool_id,
  700. )
  701. await db.commit()
  702. await ws_manager.broadcast(
  703. {
  704. "type": "spoolbuddy_tag_link_failed",
  705. "device_id": req.device_id,
  706. "spool_id": req.spool_id,
  707. "message": "Spool not found",
  708. }
  709. )
  710. return {"status": "ok", "linked": False, "message": "Spool not found"}
  711. spool.tag_uid = req.tag_uid.upper()
  712. spool.tag_type = "ntag"
  713. spool.data_origin = "opentag3d"
  714. spool.encode_time = datetime.now(timezone.utc)
  715. logger.info("Tag written and linked: spool %d -> tag %s", spool.id, req.tag_uid)
  716. await db.commit()
  717. await ws_manager.broadcast(
  718. {
  719. "type": "spoolbuddy_tag_written",
  720. "device_id": req.device_id,
  721. "spool_id": req.spool_id,
  722. "tag_uid": req.tag_uid,
  723. }
  724. )
  725. else:
  726. await db.commit()
  727. await ws_manager.broadcast(
  728. {
  729. "type": "spoolbuddy_tag_write_failed",
  730. "device_id": req.device_id,
  731. "spool_id": req.spool_id,
  732. "message": req.message,
  733. }
  734. )
  735. logger.warning("Tag write failed for device %s: %s", req.device_id, req.message)
  736. return {"status": "ok"}
  737. @router.post("/devices/{device_id}/cancel-write")
  738. async def cancel_write(
  739. device_id: str,
  740. db: AsyncSession = Depends(get_db),
  741. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  742. ):
  743. """Cancel a pending write-tag command."""
  744. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  745. device = result.scalar_one_or_none()
  746. if not device:
  747. raise HTTPException(status_code=404, detail="Device not registered")
  748. if device.pending_command == "write_tag":
  749. device.pending_command = None
  750. device.pending_write_payload = None
  751. await db.commit()
  752. logger.info("Write tag cancelled for device %s", device_id)
  753. return {"status": "ok"}
  754. # --- Scale endpoints ---
  755. @router.post("/scale/reading")
  756. async def scale_reading(
  757. req: ScaleReadingRequest,
  758. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  759. ):
  760. """RPi reports scale weight — broadcast to all clients."""
  761. await ws_manager.broadcast(
  762. {
  763. "type": "spoolbuddy_weight",
  764. "device_id": req.device_id,
  765. "weight_grams": req.weight_grams,
  766. "stable": req.stable,
  767. "raw_adc": req.raw_adc,
  768. }
  769. )
  770. return {"status": "ok"}
  771. @router.post("/scale/update-spool-weight")
  772. async def update_spool_weight(
  773. req: UpdateSpoolWeightRequest,
  774. db: AsyncSession = Depends(get_db),
  775. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  776. ):
  777. """Update spool's used weight from scale reading."""
  778. from backend.app.api.routes._spoolman_helpers import _safe_float
  779. from backend.app.models.spool import Spool
  780. # Try local DB first — local spool IDs must not be forwarded to Spoolman.
  781. db_result = await db.execute(select(Spool).where(Spool.id == req.spool_id))
  782. spool = db_result.scalar_one_or_none()
  783. if spool:
  784. net_filament = max(0, req.weight_grams - spool.core_weight)
  785. spool.weight_used = max(0, spool.label_weight - net_filament)
  786. spool.last_scale_weight = req.weight_grams
  787. spool.last_weighed_at = datetime.now(timezone.utc)
  788. await db.commit()
  789. logger.info(
  790. "SpoolBuddy updated spool %d weight: %.1fg on scale, %.1fg used",
  791. spool.id,
  792. req.weight_grams,
  793. spool.weight_used,
  794. )
  795. return {"status": "ok", "weight_used": spool.weight_used}
  796. # Local miss — fall back to Spoolman when enabled.
  797. sm_client = await _get_spoolman_client_or_none(db)
  798. if sm_client is None:
  799. raise HTTPException(status_code=404, detail="Spool not found")
  800. async with _translate_spoolbuddy_errors():
  801. sm_spool = await sm_client.get_spool(req.spool_id)
  802. filament = sm_spool.get("filament") or {}
  803. spool_tare = sm_spool.get("spool_weight")
  804. raw_tare = spool_tare if spool_tare is not None else filament.get("spool_weight")
  805. spool_weight_warning: str | None = None
  806. if raw_tare is None:
  807. logger.warning(
  808. "Spoolman spool %d has no spool_weight set; using 250g fallback for tare",
  809. req.spool_id,
  810. )
  811. spool_weight_warning = (
  812. "spool_weight_not_set: Spoolman filament has no spool_weight configured; weight estimate uses 250g fallback"
  813. )
  814. core_weight = _safe_float(raw_tare, 250.0)
  815. label_weight = _safe_float(filament.get("weight"), 1000.0)
  816. remaining_weight = max(0.0, req.weight_grams - core_weight)
  817. async with _translate_spoolbuddy_errors():
  818. await sm_client.update_spool(spool_id=req.spool_id, remaining_weight=remaining_weight)
  819. weight_used = max(0.0, label_weight - remaining_weight)
  820. logger.info(
  821. "SpoolBuddy updated Spoolman spool %d: %.1fg on scale, core=%.1fg → %.1fg remaining",
  822. req.spool_id,
  823. req.weight_grams,
  824. core_weight,
  825. remaining_weight,
  826. )
  827. result: dict = {"status": "ok", "weight_used": weight_used}
  828. if spool_weight_warning:
  829. result["warnings"] = [spool_weight_warning]
  830. return result
  831. # --- Calibration endpoints ---
  832. @router.post("/devices/{device_id}/calibration/tare")
  833. async def tare_scale(
  834. device_id: str,
  835. db: AsyncSession = Depends(get_db),
  836. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  837. ):
  838. """Set pending tare command for the device to pick up."""
  839. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  840. device = result.scalar_one_or_none()
  841. if not device:
  842. raise HTTPException(status_code=404, detail="Device not registered")
  843. device.pending_command = "tare"
  844. await db.commit()
  845. return {"status": "ok", "message": "Tare command queued"}
  846. @router.post("/devices/{device_id}/calibration/set-tare")
  847. async def set_tare_offset(
  848. device_id: str,
  849. req: SetTareRequest,
  850. db: AsyncSession = Depends(get_db),
  851. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  852. ):
  853. """Store tare offset reported by the daemon after executing a tare."""
  854. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  855. device = result.scalar_one_or_none()
  856. if not device:
  857. raise HTTPException(status_code=404, detail="Device not registered")
  858. device.tare_offset = req.tare_offset
  859. device.last_calibrated_at = datetime.now(timezone.utc)
  860. await db.commit()
  861. logger.info("SpoolBuddy %s tare offset set to %d", device_id, req.tare_offset)
  862. return CalibrationResponse(
  863. tare_offset=device.tare_offset,
  864. calibration_factor=device.calibration_factor,
  865. )
  866. @router.post("/devices/{device_id}/calibration/set-factor")
  867. async def set_calibration_factor(
  868. device_id: str,
  869. req: SetCalibrationFactorRequest,
  870. db: AsyncSession = Depends(get_db),
  871. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  872. ):
  873. """Calculate and store calibration factor from a known weight."""
  874. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  875. device = result.scalar_one_or_none()
  876. if not device:
  877. raise HTTPException(status_code=404, detail="Device not registered")
  878. tare = req.tare_raw_adc if req.tare_raw_adc is not None else device.tare_offset
  879. raw_delta = req.raw_adc - tare
  880. if raw_delta == 0:
  881. raise HTTPException(status_code=400, detail="Raw ADC value equals tare offset — place weight on scale")
  882. device.calibration_factor = req.known_weight_grams / raw_delta
  883. if req.tare_raw_adc is not None:
  884. device.tare_offset = tare
  885. device.last_calibrated_at = datetime.now(timezone.utc)
  886. await db.commit()
  887. logger.info(
  888. "SpoolBuddy %s calibration factor set to %.6f (known=%.1fg, raw=%d, tare=%d)",
  889. device_id,
  890. device.calibration_factor,
  891. req.known_weight_grams,
  892. req.raw_adc,
  893. tare,
  894. )
  895. return CalibrationResponse(
  896. tare_offset=device.tare_offset,
  897. calibration_factor=device.calibration_factor,
  898. )
  899. @router.get("/devices/{device_id}/calibration", response_model=CalibrationResponse)
  900. async def get_calibration(
  901. device_id: str,
  902. db: AsyncSession = Depends(get_db),
  903. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  904. ):
  905. """Get current calibration values for a device."""
  906. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  907. device = result.scalar_one_or_none()
  908. if not device:
  909. raise HTTPException(status_code=404, detail="Device not registered")
  910. return CalibrationResponse(
  911. tare_offset=device.tare_offset,
  912. calibration_factor=device.calibration_factor,
  913. )
  914. # --- Display settings ---
  915. @router.get("/devices/{device_id}/display")
  916. async def get_display_settings(
  917. device_id: str,
  918. db: AsyncSession = Depends(get_db),
  919. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  920. ):
  921. """Read current display brightness and screen blank timeout for a device.
  922. Used by the SpoolBuddy kiosk idle watchdog on autostart to configure
  923. swayidle with the same timeout the user picked in the UI, without having
  924. to wait for the daemon heartbeat to arrive first.
  925. """
  926. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  927. device = result.scalar_one_or_none()
  928. if not device:
  929. raise HTTPException(status_code=404, detail="Device not registered")
  930. return {
  931. "brightness": device.display_brightness,
  932. "blank_timeout": device.display_blank_timeout,
  933. }
  934. @router.put("/devices/{device_id}/display")
  935. async def update_display_settings(
  936. device_id: str,
  937. req: DisplaySettingsRequest,
  938. db: AsyncSession = Depends(get_db),
  939. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  940. ):
  941. """Update display brightness and screen blank timeout for a device."""
  942. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  943. device = result.scalar_one_or_none()
  944. if not device:
  945. raise HTTPException(status_code=404, detail="Device not registered")
  946. device.display_brightness = req.brightness
  947. device.display_blank_timeout = req.blank_timeout
  948. await db.commit()
  949. logger.info(
  950. "SpoolBuddy %s display updated: brightness=%d%%, blank_timeout=%ds",
  951. device_id,
  952. req.brightness,
  953. req.blank_timeout,
  954. )
  955. return {"status": "ok", "brightness": req.brightness, "blank_timeout": req.blank_timeout}
  956. @router.post("/devices/{device_id}/system/config")
  957. async def queue_system_config_update(
  958. device_id: str,
  959. req: SystemConfigRequest,
  960. db: AsyncSession = Depends(get_db),
  961. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  962. ):
  963. """Queue update of SpoolBuddy .env config on the device."""
  964. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  965. device = result.scalar_one_or_none()
  966. if not device:
  967. raise HTTPException(status_code=404, detail="Device not registered")
  968. parsed = urlparse(req.backend_url.strip())
  969. if parsed.scheme not in ("http", "https") or not parsed.netloc:
  970. raise HTTPException(
  971. status_code=400,
  972. detail="backend_url must be a full URL with scheme, e.g. http://192.168.1.100:5000 or http://bambuddy.local",
  973. )
  974. payload = {
  975. "backend_url": req.backend_url.strip(),
  976. }
  977. if req.api_key is not None and req.api_key.strip():
  978. payload["api_key"] = req.api_key.strip()
  979. device.pending_system_payload = json.dumps(payload)
  980. device.pending_command = "apply_system_config"
  981. await db.commit()
  982. logger.info("Queued system config update for device %s", device_id)
  983. return {"status": "queued", "message": "System config update queued"}
  984. VALID_SYSTEM_COMMANDS = {"reboot", "shutdown", "restart_daemon", "restart_browser"}
  985. @router.post("/devices/{device_id}/system/command")
  986. async def queue_system_command(
  987. device_id: str,
  988. req: SystemCommandRequest,
  989. db: AsyncSession = Depends(get_db),
  990. # Aligns with the rest of the kiosk-scoped device routes (calibration,
  991. # display, cancel-write, command-result — all INVENTORY_UPDATE). The
  992. # previous SETTINGS_UPDATE gate locked operators out of the QuickMenu's
  993. # Restart-Daemon / Restart-Browser / Reboot / Shutdown buttons even
  994. # though they had access to every other operation on the same device.
  995. # Reboot and shutdown remain recoverable via physical access — the
  996. # operator already has the kiosk in front of them.
  997. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  998. ):
  999. """Queue a system command (reboot, shutdown, restart_daemon, restart_browser) for the SpoolBuddy device."""
  1000. if req.command not in VALID_SYSTEM_COMMANDS:
  1001. raise HTTPException(
  1002. status_code=400,
  1003. detail=f"Invalid command. Must be one of: {', '.join(sorted(VALID_SYSTEM_COMMANDS))}",
  1004. )
  1005. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  1006. device = result.scalar_one_or_none()
  1007. if not device:
  1008. raise HTTPException(status_code=404, detail="Device not registered")
  1009. if not _is_online(device):
  1010. raise HTTPException(status_code=409, detail="Device is offline")
  1011. device.pending_command = req.command
  1012. await db.commit()
  1013. logger.info("System command queued for device %s: %s", device_id, req.command)
  1014. return {"status": "queued", "command": req.command}
  1015. @router.post("/devices/{device_id}/system/command-result")
  1016. async def system_command_result(
  1017. device_id: str,
  1018. req: SystemCommandResultRequest,
  1019. db: AsyncSession = Depends(get_db),
  1020. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  1021. ):
  1022. """Receive completion status for queued system command from daemon."""
  1023. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  1024. device = result.scalar_one_or_none()
  1025. if not device:
  1026. raise HTTPException(status_code=404, detail="Device not registered")
  1027. if not device.pending_command:
  1028. logger.info("System command result from %s with no pending command: %s", device_id, req.command)
  1029. return {"status": "ok", "message": "No pending command"}
  1030. if req.command != device.pending_command:
  1031. raise HTTPException(
  1032. status_code=409,
  1033. detail=f"Command mismatch: pending '{device.pending_command}', got '{req.command}'",
  1034. )
  1035. if req.command == "apply_system_config":
  1036. device.pending_system_payload = None
  1037. device.pending_command = None
  1038. await db.commit()
  1039. logger.info(
  1040. "System command result from %s: %s success=%s message=%s",
  1041. device_id,
  1042. req.command,
  1043. req.success,
  1044. req.message,
  1045. )
  1046. return {"status": "ok"}
  1047. # --- Diagnostics ---
  1048. @router.post("/diagnostics/{device_id}/run")
  1049. async def queue_diagnostic(
  1050. device_id: str,
  1051. diagnostic: str,
  1052. db: AsyncSession = Depends(get_db),
  1053. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  1054. ):
  1055. """Queue a hardware diagnostic to run on the SpoolBuddy device.
  1056. Args:
  1057. device_id: The device ID
  1058. diagnostic: 'scale' or 'nfc' to select which diagnostic to run
  1059. Returns:
  1060. Status message indicating diagnostic was queued
  1061. """
  1062. if diagnostic not in ("scale", "nfc", "read_tag"):
  1063. raise HTTPException(status_code=400, detail="Unknown diagnostic. Must be 'scale', 'nfc', or 'read_tag'")
  1064. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  1065. device = result.scalar_one_or_none()
  1066. if not device:
  1067. raise HTTPException(status_code=404, detail="Device not registered")
  1068. device.pending_command = f"run_{diagnostic}_diag"
  1069. _diagnostic_results.pop((device_id, diagnostic), None)
  1070. await db.commit()
  1071. logger.info("Diagnostic queued for device %s: %s", device_id, diagnostic)
  1072. return {"status": "queued", "diagnostic": diagnostic, "message": f"Diagnostic '{diagnostic}' queued for device"}
  1073. @router.get("/diagnostics/{device_id}/result")
  1074. async def get_diagnostic_result(
  1075. device_id: str,
  1076. diagnostic: str,
  1077. db: AsyncSession = Depends(get_db),
  1078. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  1079. ):
  1080. """Get the latest diagnostic result for a device.
  1081. Args:
  1082. device_id: The device ID
  1083. diagnostic: 'scale' or 'nfc'
  1084. Returns:
  1085. Diagnostic result or 404 if not found
  1086. """
  1087. if diagnostic not in ("scale", "nfc", "read_tag"):
  1088. raise HTTPException(status_code=400, detail="Unknown diagnostic. Must be 'scale', 'nfc', or 'read_tag'")
  1089. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  1090. device = result.scalar_one_or_none()
  1091. if not device:
  1092. raise HTTPException(status_code=404, detail="Device not registered")
  1093. diag_result = _diagnostic_results.get((device_id, diagnostic))
  1094. if not diag_result:
  1095. raise HTTPException(status_code=404, detail=f"No {diagnostic} diagnostic results available yet")
  1096. return diag_result
  1097. @router.post("/diagnostics/{device_id}/result")
  1098. async def report_diagnostic_result(
  1099. device_id: str,
  1100. req: DiagnosticResultRequest,
  1101. db: AsyncSession = Depends(get_db),
  1102. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  1103. ):
  1104. """Report diagnostic result from SpoolBuddy device."""
  1105. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  1106. device = result.scalar_one_or_none()
  1107. if not device:
  1108. raise HTTPException(status_code=404, detail="Device not registered")
  1109. if req.diagnostic not in ("nfc", "scale", "read_tag"):
  1110. raise HTTPException(status_code=400, detail="Unknown diagnostic. Must be 'scale', 'nfc', or 'read_tag'")
  1111. _diagnostic_results[(device_id, req.diagnostic)] = {
  1112. "diagnostic": req.diagnostic,
  1113. "success": req.success,
  1114. "output": req.output,
  1115. "exit_code": req.exit_code,
  1116. }
  1117. device.pending_command = None
  1118. await db.commit()
  1119. logger.info("Diagnostic result received for device %s: %s (success=%s)", device_id, req.diagnostic, req.success)
  1120. return {"status": "ok", "message": "Diagnostic result recorded"}
  1121. # --- Update check ---
  1122. @router.get("/devices/{device_id}/update-check")
  1123. async def check_daemon_update(
  1124. device_id: str,
  1125. db: AsyncSession = Depends(get_db),
  1126. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_READ),
  1127. ):
  1128. """Check if the SpoolBuddy daemon needs updating to match the Bambuddy backend version."""
  1129. from backend.app.api.routes.updates import is_newer_version
  1130. from backend.app.core.config import APP_VERSION
  1131. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  1132. device = result.scalar_one_or_none()
  1133. if not device:
  1134. raise HTTPException(status_code=404, detail="Device not registered")
  1135. current = device.firmware_version or "0.0.0"
  1136. return {
  1137. "current_version": current,
  1138. "latest_version": APP_VERSION,
  1139. "update_available": is_newer_version(APP_VERSION, current),
  1140. }
  1141. @router.post("/devices/{device_id}/update")
  1142. async def trigger_daemon_update(
  1143. device_id: str,
  1144. req: dict | None = None,
  1145. db: AsyncSession = Depends(get_db),
  1146. # Aligns with the rest of the kiosk-scoped device routes (calibration,
  1147. # display, cancel-write, system/command — all INVENTORY_UPDATE).
  1148. # SETTINGS_UPDATE is on the API-key deny-list, which blocks the Update
  1149. # button from the kiosk's own Settings page even when the operator has
  1150. # physical access. Update only acts on the device the operator already
  1151. # controls (git fetch + pip install + systemctl restart on that one
  1152. # host) — same blast radius as the restart_daemon command.
  1153. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  1154. ):
  1155. """Trigger a SpoolBuddy update over SSH.
  1156. Bambuddy SSHes into the device, pulls the matching branch, installs deps,
  1157. and restarts the daemon. Progress is broadcast via WebSocket.
  1158. """
  1159. from backend.app.services.spoolbuddy_ssh import perform_ssh_update
  1160. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  1161. device = result.scalar_one_or_none()
  1162. if not device:
  1163. raise HTTPException(status_code=404, detail="Device not registered")
  1164. if not _is_online(device):
  1165. raise HTTPException(status_code=409, detail="Device is offline")
  1166. if device.update_status == "updating":
  1167. return {"status": "already_updating", "message": "Update already in progress"}
  1168. device.update_status = "pending"
  1169. device.update_message = "Starting SSH update..."
  1170. await db.commit()
  1171. logger.info("SpoolBuddy %s: SSH update triggered (ip=%s)", device_id, device.ip_address)
  1172. await ws_manager.broadcast(
  1173. {
  1174. "type": "spoolbuddy_update",
  1175. "device_id": device_id,
  1176. "update_status": "pending",
  1177. }
  1178. )
  1179. # Run the SSH update in the background — hold reference to prevent GC cancellation
  1180. _ssh_update_task = asyncio.create_task(perform_ssh_update(device_id, device.ip_address))
  1181. _ssh_update_task.add_done_callback(
  1182. lambda t: (
  1183. logger.error(
  1184. "SSH update task for device %s ended unexpectedly (cancelled=%s)",
  1185. device_id,
  1186. t.cancelled(),
  1187. )
  1188. if (t.cancelled() or t.exception() is not None)
  1189. else None
  1190. )
  1191. )
  1192. return {"status": "ok", "message": "SSH update started"}
  1193. @router.get("/ssh/public-key")
  1194. async def get_ssh_public_key(
  1195. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  1196. ):
  1197. """Return the SSH public key for SpoolBuddy pairing."""
  1198. from backend.app.services.spoolbuddy_ssh import get_public_key
  1199. try:
  1200. key = await get_public_key()
  1201. return {"public_key": key}
  1202. except Exception as e:
  1203. logger.error("Failed to get SSH public key: %s", e)
  1204. raise HTTPException(status_code=500, detail="Failed to retrieve SSH public key") from e
  1205. @router.post("/devices/{device_id}/update-status")
  1206. async def report_update_status(
  1207. device_id: str,
  1208. req: UpdateStatusRequest,
  1209. db: AsyncSession = Depends(get_db),
  1210. _: User | None = RequirePermissionIfAuthEnabled(Permission.INVENTORY_UPDATE),
  1211. ):
  1212. """Daemon reports update progress back to the backend."""
  1213. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.device_id == device_id))
  1214. device = result.scalar_one_or_none()
  1215. if not device:
  1216. raise HTTPException(status_code=404, detail="Device not registered")
  1217. device.update_status = req.status
  1218. device.update_message = req.message
  1219. # Only "complete" clears pending_command here. "error" leaves it set so the user can retry
  1220. # via the UI. The SSH service's own _update_progress clears on both "complete" and "error"
  1221. # because it owns the full update lifecycle end-to-end.
  1222. if req.status == "complete":
  1223. device.pending_command = None
  1224. await db.commit()
  1225. logger.info("SpoolBuddy %s: update status=%s msg=%s", device_id, req.status, req.message)
  1226. await ws_manager.broadcast(
  1227. {
  1228. "type": "spoolbuddy_update",
  1229. "device_id": device_id,
  1230. "update_status": req.status,
  1231. "update_message": req.message,
  1232. }
  1233. )
  1234. return {"status": "ok"}
  1235. # --- Background watchdog ---
  1236. async def spoolbuddy_watchdog():
  1237. """Check for devices that have gone offline (no heartbeat for 30s).
  1238. Called periodically from the main app's background task loop.
  1239. """
  1240. from backend.app.core.database import async_session
  1241. async with async_session() as db:
  1242. result = await db.execute(select(SpoolBuddyDevice).where(SpoolBuddyDevice.last_seen.isnot(None)))
  1243. devices = list(result.scalars().all())
  1244. threshold = datetime.now(timezone.utc) - timedelta(seconds=OFFLINE_THRESHOLD_SECONDS)
  1245. for device in devices:
  1246. last_seen = device.last_seen.replace(tzinfo=timezone.utc) if device.last_seen else None
  1247. if last_seen and last_seen < threshold:
  1248. # Only broadcast once — clear last_seen after marking offline
  1249. await ws_manager.broadcast(
  1250. {
  1251. "type": "spoolbuddy_offline",
  1252. "device_id": device.device_id,
  1253. }
  1254. )
  1255. device.last_seen = None
  1256. logger.info("SpoolBuddy device offline: %s", device.device_id)
  1257. await db.commit()