spoolman.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. """Spoolman integration API routes."""
  2. import json
  3. import logging
  4. from typing import Literal
  5. from fastapi import APIRouter, Depends, HTTPException
  6. from pydantic import BaseModel
  7. from sqlalchemy import delete, select, text
  8. from sqlalchemy.ext.asyncio import AsyncSession
  9. from sqlalchemy.orm import selectinload
  10. from backend.app.api.routes._spoolman_helpers import _map_spoolman_spool
  11. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  12. from backend.app.core.database import get_db
  13. from backend.app.core.permissions import Permission
  14. from backend.app.models.printer import Printer
  15. from backend.app.models.settings import Settings
  16. from backend.app.models.spool_assignment import SpoolAssignment
  17. from backend.app.models.spoolman_k_profile import SpoolmanKProfile
  18. from backend.app.models.spoolman_slot_assignment import SpoolmanSlotAssignment
  19. from backend.app.models.user import User
  20. from backend.app.services.printer_manager import printer_manager
  21. from backend.app.services.spoolman import (
  22. SpoolmanClientError,
  23. SpoolmanNotFoundError,
  24. SpoolmanUnavailableError,
  25. close_spoolman_client,
  26. get_spoolman_client,
  27. init_spoolman_client,
  28. )
  29. from backend.app.utils.filament_ids import (
  30. GENERIC_FILAMENT_IDS,
  31. MATERIAL_TEMPS,
  32. normalize_slicer_filament,
  33. )
  34. logger = logging.getLogger(__name__)
  35. router = APIRouter(prefix="/spoolman", tags=["spoolman"])
  36. class SpoolmanStatus(BaseModel):
  37. """Spoolman connection status."""
  38. enabled: bool
  39. connected: bool
  40. url: str | None
  41. class SkippedSpool(BaseModel):
  42. """Information about a skipped spool during sync."""
  43. location: str
  44. reason: Literal["No RFID tag and no slot assignment"]
  45. filament_type: str | None = None
  46. color: str | None = None
  47. class SyncResult(BaseModel):
  48. """Result of a Spoolman sync operation."""
  49. success: bool
  50. synced_count: int
  51. skipped_count: int = 0
  52. skipped: list[SkippedSpool] = []
  53. errors: list[str]
  54. async def get_spoolman_settings(db: AsyncSession) -> dict:
  55. """Get Spoolman settings from database.
  56. Returns:
  57. Dict with keys: enabled, url, sync_mode, disable_weight_sync
  58. """
  59. settings = {
  60. "enabled": False,
  61. "url": "",
  62. "sync_mode": "auto",
  63. "disable_weight_sync": False,
  64. }
  65. result = await db.execute(select(Settings))
  66. for setting in result.scalars().all():
  67. if setting.key == "spoolman_enabled":
  68. settings["enabled"] = setting.value.lower() == "true"
  69. elif setting.key == "spoolman_url":
  70. settings["url"] = setting.value
  71. elif setting.key == "spoolman_sync_mode":
  72. settings["sync_mode"] = setting.value
  73. elif setting.key == "spoolman_disable_weight_sync":
  74. settings["disable_weight_sync"] = setting.value.lower() == "true"
  75. return settings
  76. @router.get("/status", response_model=SpoolmanStatus)
  77. async def get_spoolman_status(
  78. db: AsyncSession = Depends(get_db),
  79. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  80. ):
  81. """Get Spoolman integration status."""
  82. sm = await get_spoolman_settings(db)
  83. enabled, url = sm["enabled"], sm["url"]
  84. client = await get_spoolman_client()
  85. connected = False
  86. if client:
  87. connected = await client.health_check()
  88. return SpoolmanStatus(
  89. enabled=enabled,
  90. connected=connected,
  91. url=url if url else None,
  92. )
  93. @router.post("/connect")
  94. async def connect_spoolman(
  95. db: AsyncSession = Depends(get_db),
  96. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  97. ):
  98. """Connect to Spoolman server using configured URL."""
  99. sm = await get_spoolman_settings(db)
  100. enabled, url = sm["enabled"], sm["url"]
  101. if not enabled:
  102. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  103. if not url:
  104. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  105. try:
  106. client = await init_spoolman_client(url)
  107. connected = await client.health_check()
  108. if not connected:
  109. raise HTTPException(
  110. status_code=503,
  111. detail=f"Could not connect to Spoolman at {url}",
  112. )
  113. # Ensure the 'tag' extra field exists for RFID/UUID storage
  114. field_ok = await client.ensure_tag_extra_field()
  115. if not field_ok:
  116. logger.error("Spoolman tag extra field registration failed — NFC tag links may not persist")
  117. # Register slicer-preset extra fields (Spoolman rejects unknown extra keys).
  118. for field_name in ("bambu_slicer_filament", "bambu_slicer_filament_name"):
  119. if not await client.ensure_extra_field(field_name):
  120. logger.warning(
  121. "Spoolman extra field %r registration failed — spool slicer-preset edits will return 502",
  122. field_name,
  123. )
  124. return {"success": True, "message": f"Connected to Spoolman at {url}"}
  125. except ValueError as exc:
  126. logger.warning("Spoolman URL rejected: %s", exc)
  127. raise HTTPException(status_code=400, detail=str(exc)) from exc
  128. except Exception as e:
  129. logger.error("Failed to connect to Spoolman: %s", e)
  130. raise HTTPException(status_code=503, detail=str(e))
  131. @router.post("/disconnect")
  132. async def disconnect_spoolman(
  133. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  134. ):
  135. """Disconnect from Spoolman server."""
  136. await close_spoolman_client()
  137. return {"success": True, "message": "Disconnected from Spoolman"}
  138. @router.post("/sync/{printer_id}", response_model=SyncResult)
  139. async def sync_printer_ams(
  140. printer_id: int,
  141. db: AsyncSession = Depends(get_db),
  142. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_UPDATE),
  143. ):
  144. """Sync AMS data from a specific printer to Spoolman."""
  145. # Check if Spoolman is enabled and connected
  146. # disable_weight_sync is deprecated (#1119); weight comes from per-print tracking.
  147. sm = await get_spoolman_settings(db)
  148. enabled, url = sm["enabled"], sm["url"]
  149. if not enabled:
  150. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  151. client = await get_spoolman_client()
  152. if not client:
  153. # Try to connect
  154. if url:
  155. client = await init_spoolman_client(url)
  156. else:
  157. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  158. if not await client.health_check():
  159. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  160. # Get printer info
  161. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  162. printer = result.scalar_one_or_none()
  163. if not printer:
  164. raise HTTPException(status_code=404, detail="Printer not found")
  165. # Get current printer state with AMS data
  166. state = printer_manager.get_status(printer_id)
  167. if not state:
  168. raise HTTPException(status_code=404, detail="Printer not connected")
  169. if not state.raw_data:
  170. raise HTTPException(status_code=400, detail="No AMS data available")
  171. ams_data = state.raw_data.get("ams")
  172. if not ams_data:
  173. raise HTTPException(
  174. status_code=400,
  175. detail="No AMS data in printer state. Try triggering a slot re-read on the printer.",
  176. )
  177. # Sync each AMS tray to Spoolman
  178. synced = 0
  179. skipped: list[SkippedSpool] = []
  180. errors = []
  181. # Handle different AMS data structures
  182. # Traditional AMS: list of {"id": N, "tray": [...]} dicts
  183. # H2D/newer printers: dict with different structure
  184. ams_units = []
  185. if isinstance(ams_data, list):
  186. ams_units = ams_data
  187. elif isinstance(ams_data, dict):
  188. # H2D format: check for "ams" key containing list, or "tray" key directly
  189. if "ams" in ams_data and isinstance(ams_data["ams"], list):
  190. ams_units = ams_data["ams"]
  191. elif "tray" in ams_data:
  192. # Single AMS unit format - wrap in list
  193. ams_units = [{"id": 0, "tray": ams_data.get("tray", [])}]
  194. else:
  195. logger.info("AMS dict keys for debugging: %s", list(ams_data.keys()))
  196. if not ams_units:
  197. raise HTTPException(
  198. status_code=400,
  199. detail=(
  200. "AMS data format not supported. Keys: "
  201. f"{list(ams_data.keys()) if isinstance(ams_data, dict) else type(ams_data).__name__}"
  202. ),
  203. )
  204. # OPTIMIZATION: Fetch all spools once before processing trays
  205. # This eliminates redundant API calls (one per tray) when syncing multiple trays
  206. logger.debug("[Printer %s] Fetching spools cache for sync...", printer.name)
  207. try:
  208. cached_spools = await client.get_spools()
  209. logger.debug("[Printer %s] Cached %d spools for batch sync", printer.name, len(cached_spools))
  210. except Exception as e:
  211. logger.error("[Printer %s] Failed to fetch spools cache after retries: %s", printer.name, e)
  212. raise HTTPException(
  213. status_code=503,
  214. detail=f"Failed to connect to Spoolman after multiple retries: {str(e)}",
  215. )
  216. # Load inventory weights as fallback (when AMS MQTT data lacks remain values)
  217. inv_weights: dict[tuple[int, int], float] = {}
  218. try:
  219. assign_result = await db.execute(
  220. select(SpoolAssignment)
  221. .options(selectinload(SpoolAssignment.spool))
  222. .where(SpoolAssignment.printer_id == printer_id)
  223. )
  224. for assignment in assign_result.scalars().all():
  225. spool = assignment.spool
  226. if spool and spool.label_weight > 0:
  227. remaining = max(0.0, spool.label_weight - (spool.weight_used or 0))
  228. inv_weights[(assignment.ams_id, assignment.tray_id)] = remaining
  229. except Exception as e:
  230. logger.debug("Could not load inventory weights for printer %s: %s", printer_id, e)
  231. # Load existing Spoolman slot assignments for the no-RFID fallback path
  232. spoolman_slot_map: dict[tuple[int, int], int] = {}
  233. try:
  234. slot_result = await db.execute(
  235. select(SpoolmanSlotAssignment).where(SpoolmanSlotAssignment.printer_id == printer_id)
  236. )
  237. for slot in slot_result.scalars().all():
  238. spoolman_slot_map[(slot.ams_id, slot.tray_id)] = slot.spoolman_spool_id
  239. except Exception as e:
  240. logger.warning("Could not load Spoolman slot assignments for printer %s: %s", printer_id, e)
  241. slot_changes: list[tuple[int, int, int]] = [] # (ams_id, tray_id, spoolman_spool_id)
  242. empty_slots: list[tuple[int, int]] = [] # (ams_id, tray_id) now empty
  243. for ams_unit in ams_units:
  244. if not isinstance(ams_unit, dict):
  245. continue
  246. ams_id = int(ams_unit.get("id", 0))
  247. trays = ams_unit.get("tray", [])
  248. for tray_data in trays:
  249. if not isinstance(tray_data, dict):
  250. continue
  251. tray_id_raw = int(tray_data.get("id", 0))
  252. tray = client.parse_ams_tray(ams_id, tray_data)
  253. if not tray:
  254. empty_slots.append((ams_id, tray_id_raw))
  255. continue
  256. spool_tag = (
  257. tray.tray_uuid
  258. if tray.tray_uuid and tray.tray_uuid != "00000000000000000000000000000000"
  259. else tray.tag_uid
  260. )
  261. hint = spoolman_slot_map.get((ams_id, tray.tray_id)) if not spool_tag else None
  262. try:
  263. inv_remaining = inv_weights.get((ams_id, tray.tray_id))
  264. sync_result = await client.sync_ams_tray(
  265. tray,
  266. printer.name,
  267. # Per-print tracking owns weight updates (#1119); manual sync
  268. # only refreshes spool metadata + slot assignments here.
  269. disable_weight_sync=True,
  270. cached_spools=cached_spools,
  271. inventory_remaining=inv_remaining,
  272. spoolman_spool_id_hint=hint,
  273. )
  274. if sync_result:
  275. synced += 1
  276. if sync_result.get("id"):
  277. slot_changes.append((ams_id, tray.tray_id, sync_result["id"]))
  278. spool_exists = any(s.get("id") == sync_result["id"] for s in cached_spools)
  279. if not spool_exists:
  280. cached_spools.append(sync_result)
  281. logger.debug("Added newly created spool %s to cache", sync_result["id"])
  282. logger.info(
  283. "Synced %s from %s AMS %s tray %s", tray.tray_sub_brands, printer.name, ams_id, tray.tray_id
  284. )
  285. elif spool_tag:
  286. errors.append(f"Spool not found in Spoolman: AMS {ams_id}:{tray.tray_id}")
  287. elif not hint:
  288. skipped.append(
  289. SkippedSpool(
  290. location=f"AMS {ams_id} T{tray.tray_id}",
  291. reason="No RFID tag and no slot assignment",
  292. filament_type=tray.tray_type or None,
  293. color=tray.tray_color[:6] if tray.tray_color else None,
  294. )
  295. )
  296. except Exception as e:
  297. error_msg = f"Error syncing AMS {ams_id} tray {tray.tray_id}: {e}"
  298. logger.error(error_msg)
  299. errors.append(error_msg)
  300. # Persist slot assignment changes to the local table
  301. if slot_changes or empty_slots:
  302. try:
  303. for ams_id, tray_id, spool_id in slot_changes:
  304. await db.execute(
  305. text(
  306. "INSERT INTO spoolman_slot_assignments"
  307. " (printer_id, ams_id, tray_id, spoolman_spool_id)"
  308. " VALUES (:printer_id, :ams_id, :tray_id, :spool_id)"
  309. " ON CONFLICT(printer_id, ams_id, tray_id)"
  310. " DO UPDATE SET spoolman_spool_id = excluded.spoolman_spool_id"
  311. ),
  312. {"printer_id": printer_id, "ams_id": ams_id, "tray_id": tray_id, "spool_id": spool_id},
  313. )
  314. for ams_id, tray_id in empty_slots:
  315. await db.execute(
  316. delete(SpoolmanSlotAssignment).where(
  317. SpoolmanSlotAssignment.printer_id == printer_id,
  318. SpoolmanSlotAssignment.ams_id == ams_id,
  319. SpoolmanSlotAssignment.tray_id == tray_id,
  320. )
  321. )
  322. await db.commit()
  323. except Exception as e:
  324. await db.rollback()
  325. logger.error("Error persisting Spoolman slot assignments for printer %s: %s", printer_id, e)
  326. errors.append(f"Failed to persist slot assignments: {type(e).__name__}")
  327. return SyncResult(
  328. success=len(errors) == 0,
  329. synced_count=synced,
  330. skipped_count=len(skipped),
  331. skipped=skipped,
  332. errors=errors,
  333. )
  334. @router.post("/sync-all", response_model=SyncResult)
  335. async def sync_all_printers(
  336. db: AsyncSession = Depends(get_db),
  337. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_UPDATE),
  338. ):
  339. """Sync AMS data from all connected printers to Spoolman."""
  340. # Check if Spoolman is enabled
  341. # disable_weight_sync is deprecated (#1119); weight comes from per-print tracking.
  342. sm = await get_spoolman_settings(db)
  343. enabled, url = sm["enabled"], sm["url"]
  344. if not enabled:
  345. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  346. client = await get_spoolman_client()
  347. if not client:
  348. if url:
  349. client = await init_spoolman_client(url)
  350. else:
  351. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  352. if not await client.health_check():
  353. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  354. # Get all active printers
  355. result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  356. printers = result.scalars().all()
  357. total_synced = 0
  358. all_skipped: list[SkippedSpool] = []
  359. all_errors = []
  360. # OPTIMIZATION: Fetch all spools once before processing ALL printers/trays
  361. # This eliminates redundant API calls across all printers
  362. logger.debug("Fetching spools cache for sync-all operation...")
  363. try:
  364. cached_spools = await client.get_spools()
  365. logger.debug("Cached %d spools for batch sync across %d printers", len(cached_spools), len(printers))
  366. except Exception as e:
  367. logger.error("Failed to fetch spools cache after retries: %s", e)
  368. raise HTTPException(
  369. status_code=503,
  370. detail=f"Failed to connect to Spoolman after multiple retries: {str(e)}",
  371. )
  372. # Load inventory assignments for weight fallback (when AMS MQTT data lacks remain values)
  373. # Key: (printer_id, ams_id, tray_id) → remaining_weight in grams
  374. inventory_weights: dict[tuple[int, int, int], float] = {}
  375. try:
  376. assign_result = await db.execute(select(SpoolAssignment).options(selectinload(SpoolAssignment.spool)))
  377. for assignment in assign_result.scalars().all():
  378. spool = assignment.spool
  379. if spool and spool.label_weight > 0:
  380. remaining = max(0.0, spool.label_weight - (spool.weight_used or 0))
  381. inventory_weights[(assignment.printer_id, assignment.ams_id, assignment.tray_id)] = remaining
  382. except Exception as e:
  383. logger.debug("Could not load inventory assignments for weight fallback: %s", e)
  384. # Load all Spoolman slot assignments for the no-RFID fallback
  385. # Key: (printer_id, ams_id, tray_id) → spoolman_spool_id
  386. all_slot_map: dict[tuple[int, int, int], int] = {}
  387. try:
  388. slot_result = await db.execute(select(SpoolmanSlotAssignment))
  389. for slot in slot_result.scalars().all():
  390. all_slot_map[(slot.printer_id, slot.ams_id, slot.tray_id)] = slot.spoolman_spool_id
  391. except Exception as e:
  392. logger.warning("Could not load Spoolman slot assignments: %s", e)
  393. # Collect slot changes across all printers for a single DB write at the end
  394. all_slot_changes: list[tuple[int, int, int, int]] = [] # (printer_id, ams_id, tray_id, spool_id)
  395. all_empty_slots: list[tuple[int, int, int]] = [] # (printer_id, ams_id, tray_id)
  396. for printer in printers:
  397. state = printer_manager.get_status(printer.id)
  398. if not state or not state.raw_data:
  399. continue
  400. ams_data = state.raw_data.get("ams")
  401. if not ams_data:
  402. continue
  403. # Handle different AMS data structures
  404. # Traditional AMS: list of {"id": N, "tray": [...]} dicts
  405. # H2D/newer printers: dict with different structure
  406. ams_units = []
  407. if isinstance(ams_data, list):
  408. ams_units = ams_data
  409. elif isinstance(ams_data, dict):
  410. # H2D format: check for "ams" key containing list, or "tray" key directly
  411. if "ams" in ams_data and isinstance(ams_data["ams"], list):
  412. ams_units = ams_data["ams"]
  413. elif "tray" in ams_data:
  414. # Single AMS unit format - wrap in list
  415. ams_units = [{"id": 0, "tray": ams_data.get("tray", [])}]
  416. else:
  417. logger.debug("Printer %s AMS dict keys: %s", printer.name, list(ams_data.keys()))
  418. if not ams_units:
  419. logger.debug("Printer %s has no AMS units to sync (type: %s)", printer.name, type(ams_data).__name__)
  420. continue
  421. for ams_unit in ams_units:
  422. if not isinstance(ams_unit, dict):
  423. logger.debug("Skipping non-dict AMS unit: %s", type(ams_unit))
  424. continue
  425. ams_id = int(ams_unit.get("id", 0))
  426. trays = ams_unit.get("tray", [])
  427. for tray_data in trays:
  428. if not isinstance(tray_data, dict):
  429. continue
  430. tray_id_raw = int(tray_data.get("id", 0))
  431. tray = client.parse_ams_tray(ams_id, tray_data)
  432. if not tray:
  433. all_empty_slots.append((printer.id, ams_id, tray_id_raw))
  434. continue
  435. spool_tag = (
  436. tray.tray_uuid
  437. if tray.tray_uuid and tray.tray_uuid != "00000000000000000000000000000000"
  438. else tray.tag_uid
  439. )
  440. hint = all_slot_map.get((printer.id, ams_id, tray.tray_id)) if not spool_tag else None
  441. try:
  442. inv_remaining = inventory_weights.get((printer.id, ams_id, tray.tray_id))
  443. sync_result = await client.sync_ams_tray(
  444. tray,
  445. printer.name,
  446. # Per-print tracking owns weight updates (#1119); manual
  447. # sync-all only refreshes spool metadata + slot assignments.
  448. disable_weight_sync=True,
  449. cached_spools=cached_spools,
  450. inventory_remaining=inv_remaining,
  451. spoolman_spool_id_hint=hint,
  452. )
  453. if sync_result:
  454. total_synced += 1
  455. if sync_result.get("id"):
  456. all_slot_changes.append((printer.id, ams_id, tray.tray_id, sync_result["id"]))
  457. spool_exists = any(s.get("id") == sync_result["id"] for s in cached_spools)
  458. if not spool_exists:
  459. cached_spools.append(sync_result)
  460. logger.debug("Added newly created spool %s to cache", sync_result["id"])
  461. elif spool_tag:
  462. all_errors.append(f"Spool not found in Spoolman: {printer.name} AMS {ams_id}:{tray.tray_id}")
  463. elif not hint:
  464. all_skipped.append(
  465. SkippedSpool(
  466. location=f"{printer.name} AMS {ams_id} T{tray.tray_id}",
  467. reason="No RFID tag and no slot assignment",
  468. filament_type=tray.tray_type or None,
  469. color=tray.tray_color[:6] if tray.tray_color else None,
  470. )
  471. )
  472. except Exception as e:
  473. all_errors.append(f"{printer.name} AMS {ams_id}:{tray.tray_id}: {e}")
  474. # Persist slot assignment changes across all printers
  475. if all_slot_changes or all_empty_slots:
  476. try:
  477. for p_id, ams_id, tray_id, spool_id in all_slot_changes:
  478. await db.execute(
  479. text(
  480. "INSERT INTO spoolman_slot_assignments"
  481. " (printer_id, ams_id, tray_id, spoolman_spool_id)"
  482. " VALUES (:printer_id, :ams_id, :tray_id, :spool_id)"
  483. " ON CONFLICT(printer_id, ams_id, tray_id)"
  484. " DO UPDATE SET spoolman_spool_id = excluded.spoolman_spool_id"
  485. ),
  486. {"printer_id": p_id, "ams_id": ams_id, "tray_id": tray_id, "spool_id": spool_id},
  487. )
  488. for p_id, ams_id, tray_id in all_empty_slots:
  489. await db.execute(
  490. delete(SpoolmanSlotAssignment).where(
  491. SpoolmanSlotAssignment.printer_id == p_id,
  492. SpoolmanSlotAssignment.ams_id == ams_id,
  493. SpoolmanSlotAssignment.tray_id == tray_id,
  494. )
  495. )
  496. await db.commit()
  497. except Exception as e:
  498. await db.rollback()
  499. logger.error("Error persisting Spoolman slot assignments: %s", e)
  500. all_errors.append(f"Failed to persist slot assignments: {type(e).__name__}")
  501. return SyncResult(
  502. success=len(all_errors) == 0,
  503. synced_count=total_synced,
  504. skipped_count=len(all_skipped),
  505. skipped=all_skipped,
  506. errors=all_errors,
  507. )
  508. @router.get("/spools")
  509. async def get_spools(
  510. db: AsyncSession = Depends(get_db),
  511. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  512. ):
  513. """Get all spools from Spoolman."""
  514. sm = await get_spoolman_settings(db)
  515. enabled, url = sm["enabled"], sm["url"]
  516. if not enabled:
  517. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  518. client = await get_spoolman_client()
  519. if not client:
  520. if url:
  521. client = await init_spoolman_client(url)
  522. else:
  523. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  524. if not await client.health_check():
  525. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  526. spools = await client.get_spools()
  527. return {"spools": spools}
  528. @router.get("/filaments")
  529. async def get_filaments(
  530. db: AsyncSession = Depends(get_db),
  531. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  532. ):
  533. """Get all filaments from Spoolman."""
  534. sm = await get_spoolman_settings(db)
  535. enabled, url = sm["enabled"], sm["url"]
  536. if not enabled:
  537. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  538. client = await get_spoolman_client()
  539. if not client:
  540. if url:
  541. client = await init_spoolman_client(url)
  542. else:
  543. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  544. if not await client.health_check():
  545. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  546. filaments = await client.get_filaments()
  547. return {"filaments": filaments}
  548. class UnlinkedSpool(BaseModel):
  549. """A Spoolman spool that is not linked to any AMS tray."""
  550. id: int
  551. filament_name: str | None
  552. filament_vendor: str | None
  553. filament_material: str | None
  554. filament_color_hex: str | None
  555. remaining_weight: float | None
  556. location: str | None
  557. @router.get("/spools/unlinked", response_model=list[UnlinkedSpool])
  558. async def get_unlinked_spools(
  559. db: AsyncSession = Depends(get_db),
  560. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  561. ):
  562. """Get all Spoolman spools that don't have a tag (not linked to AMS)."""
  563. sm = await get_spoolman_settings(db)
  564. enabled, url = sm["enabled"], sm["url"]
  565. if not enabled:
  566. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  567. client = await get_spoolman_client()
  568. if not client:
  569. if url:
  570. client = await init_spoolman_client(url)
  571. else:
  572. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  573. if not await client.health_check():
  574. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  575. spools = await client.get_spools()
  576. unlinked = []
  577. for spool in spools:
  578. # Check if spool has a tag in extra field
  579. extra = spool.get("extra", {}) or {}
  580. tag = extra.get("tag", "")
  581. # Remove quotes if present (JSON encoded string) and check if empty
  582. clean_tag = tag.strip('"') if tag else ""
  583. if not clean_tag:
  584. filament = spool.get("filament", {}) or {}
  585. unlinked.append(
  586. UnlinkedSpool(
  587. id=spool["id"],
  588. filament_name=filament.get("name"),
  589. filament_vendor=(filament.get("vendor") or {}).get("name"),
  590. filament_material=filament.get("material"),
  591. filament_color_hex=filament.get("color_hex"),
  592. remaining_weight=spool.get("remaining_weight"),
  593. location=spool.get("location"),
  594. )
  595. )
  596. return unlinked
  597. @router.get("/spools/linked")
  598. async def get_linked_spools(
  599. db: AsyncSession = Depends(get_db),
  600. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  601. ):
  602. """Get a map of tag -> spool_id for all Spoolman spools that have a tag assigned."""
  603. sm = await get_spoolman_settings(db)
  604. enabled, url = sm["enabled"], sm["url"]
  605. if not enabled:
  606. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  607. client = await get_spoolman_client()
  608. if not client:
  609. if url:
  610. client = await init_spoolman_client(url)
  611. else:
  612. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  613. if not await client.health_check():
  614. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  615. spools = await client.get_spools()
  616. linked: dict[str, dict] = {}
  617. for spool in spools:
  618. # Check if spool has a tag in extra field
  619. extra = spool.get("extra", {}) or {}
  620. tag = extra.get("tag", "")
  621. if tag:
  622. # Remove quotes if present (JSON encoded string)
  623. clean_tag = tag.strip('"').upper()
  624. if clean_tag:
  625. filament = spool.get("filament") or {}
  626. linked[clean_tag] = {
  627. "id": spool["id"],
  628. "remaining_weight": spool.get("remaining_weight"),
  629. "filament_weight": filament.get("weight"),
  630. }
  631. return {"linked": linked}
  632. class LinkSpoolRequest(BaseModel):
  633. """Request to link a Spoolman spool to an AMS tag (tray_uuid or tag_uid)."""
  634. spool_tag: str | None = None
  635. tray_uuid: str | None = None
  636. tag_uid: str | None = None
  637. printer_id: int | None = None
  638. ams_id: int | None = None
  639. tray_id: int | None = None
  640. @router.post("/spools/{spool_id}/link")
  641. async def link_spool(
  642. spool_id: int,
  643. request: LinkSpoolRequest,
  644. db: AsyncSession = Depends(get_db),
  645. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_UPDATE),
  646. ):
  647. """Link a Spoolman spool to an AMS tag by setting Spoolman extra.tag."""
  648. sm = await get_spoolman_settings(db)
  649. enabled, url = sm["enabled"], sm["url"]
  650. if not enabled:
  651. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  652. client = await get_spoolman_client()
  653. if not client:
  654. if url:
  655. client = await init_spoolman_client(url)
  656. else:
  657. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  658. if not await client.health_check():
  659. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  660. # Resolve and validate spool tag (supports tray_uuid=32 hex and tag_uid=16 hex)
  661. spool_tag = (request.spool_tag or request.tray_uuid or request.tag_uid or "").strip()
  662. if not spool_tag:
  663. raise HTTPException(status_code=400, detail="Missing spool tag (tray_uuid or tag_uid)")
  664. if len(spool_tag) not in (16, 32):
  665. raise HTTPException(status_code=400, detail="Invalid spool tag format (must be 16 or 32 hex characters)")
  666. try:
  667. int(spool_tag, 16)
  668. except ValueError:
  669. raise HTTPException(status_code=400, detail="Invalid spool tag format (must be hex)")
  670. if set(spool_tag) == {"0"}:
  671. raise HTTPException(status_code=400, detail="Invalid spool tag format (all-zero tag is not linkable)")
  672. spool_tag = spool_tag.upper()
  673. # Validate printer context when provided, but do NOT write spool.location —
  674. # that field is user-managed in Spoolman. Slot assignment is stored locally.
  675. printer_context: tuple[int, int, int] | None = None
  676. if request.printer_id is not None and request.ams_id is not None and request.tray_id is not None:
  677. printer_result = await db.execute(select(Printer).where(Printer.id == request.printer_id))
  678. if not printer_result.scalar_one_or_none():
  679. raise HTTPException(status_code=404, detail="Printer not found")
  680. printer_context = (request.printer_id, request.ams_id, request.tray_id)
  681. try:
  682. await client.merge_spool_extra(spool_id, {"tag": json.dumps(spool_tag)})
  683. except SpoolmanNotFoundError:
  684. raise HTTPException(status_code=404, detail="Spool not found in Spoolman")
  685. except SpoolmanClientError:
  686. raise HTTPException(status_code=502, detail="Spoolman rejected the request")
  687. except SpoolmanUnavailableError:
  688. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  689. # Upsert slot assignment locally when printer context was supplied
  690. if printer_context:
  691. p_id, a_id, t_id = printer_context
  692. try:
  693. await db.execute(
  694. text(
  695. "INSERT INTO spoolman_slot_assignments"
  696. " (printer_id, ams_id, tray_id, spoolman_spool_id)"
  697. " VALUES (:printer_id, :ams_id, :tray_id, :spool_id)"
  698. " ON CONFLICT(printer_id, ams_id, tray_id)"
  699. " DO UPDATE SET spoolman_spool_id = excluded.spoolman_spool_id"
  700. ),
  701. {"printer_id": p_id, "ams_id": a_id, "tray_id": t_id, "spool_id": spool_id},
  702. )
  703. await db.commit()
  704. except Exception as e:
  705. await db.rollback()
  706. logger.error(
  707. "Linked spool %s in Spoolman but failed to persist local slot assignment "
  708. "(printer=%s ams=%s tray=%s): %s",
  709. spool_id,
  710. p_id,
  711. a_id,
  712. t_id,
  713. e,
  714. )
  715. raise HTTPException(
  716. status_code=500,
  717. detail=(
  718. "Spool linked in Spoolman but the local slot assignment could not be saved. "
  719. "Please re-open the link dialog to retry."
  720. ),
  721. ) from e
  722. logger.info("Linked Spoolman spool %s to tag %s", spool_id, spool_tag)
  723. # Auto-configure AMS slot via MQTT (best-effort; tag link and slot assignment already persisted)
  724. if printer_context:
  725. p_id, a_id, t_id = printer_context
  726. try:
  727. spool_data = await client.get_spool(spool_id)
  728. mapped = _map_spoolman_spool(spool_data)
  729. mqtt_client = printer_manager.get_client(p_id)
  730. if mqtt_client:
  731. tray_type = mapped.get("material") or ""
  732. brand = mapped.get("brand") or ""
  733. subtype = mapped.get("subtype") or ""
  734. if brand:
  735. tray_sub_brands = f"{brand} {tray_type} {subtype}".strip()
  736. elif subtype:
  737. tray_sub_brands = f"{tray_type} {subtype}".strip()
  738. else:
  739. tray_sub_brands = tray_type
  740. tray_color = (mapped.get("rgba") or "808080FF").upper()
  741. if len(tray_color) == 6:
  742. tray_color = tray_color + "FF"
  743. material_upper = tray_type.upper().strip()
  744. tray_info_idx = (
  745. GENERIC_FILAMENT_IDS.get(material_upper)
  746. or GENERIC_FILAMENT_IDS.get(material_upper.split("-")[0].split(" ")[0])
  747. or ""
  748. )
  749. setting_id = ""
  750. temp_defaults = MATERIAL_TEMPS.get(material_upper, (200, 240))
  751. temp_min = mapped.get("nozzle_temp_min") or temp_defaults[0]
  752. temp_max = temp_defaults[1]
  753. # Pull printer state via printer_manager (mqtt_client.printer_state
  754. # was a non-existent attribute — the hasattr check silently
  755. # returned None, defeating every state-based lookup below).
  756. state = printer_manager.get_status(p_id)
  757. nozzle_diameter = "0.4"
  758. if state and state.nozzles:
  759. nd = state.nozzles[0].nozzle_diameter
  760. if nd:
  761. nozzle_diameter = nd
  762. kp_result = await db.execute(
  763. select(SpoolmanKProfile).where(
  764. SpoolmanKProfile.spoolman_spool_id == spool_id,
  765. SpoolmanKProfile.printer_id == p_id,
  766. )
  767. )
  768. kp_rows = kp_result.scalars().all()
  769. slot_extruder = None
  770. if state and state.ams_extruder_map:
  771. if a_id == 255:
  772. slot_extruder = 1 - t_id
  773. else:
  774. slot_extruder = state.ams_extruder_map.get(str(a_id))
  775. # Prefer exact extruder match, fall back to extruder-agnostic kp
  776. # for the same nozzle. Hard-skip on extruder mismatch silently
  777. # dropped valid stored profiles when the AMS-extruder map
  778. # shifted since calibration.
  779. exact_kp = None
  780. fallback_kp = None
  781. for kp in kp_rows:
  782. if kp.nozzle_diameter != nozzle_diameter or kp.cali_idx is None:
  783. continue
  784. if slot_extruder is not None and kp.extruder is not None and kp.extruder == slot_extruder:
  785. exact_kp = kp
  786. break
  787. if fallback_kp is None:
  788. fallback_kp = kp
  789. matching_kp = exact_kp or fallback_kp
  790. # Resolve printer-side calibration entry by cali_idx — the
  791. # printer keys its calibration table by filament_id, not by
  792. # setting_id. Stored kp.setting_id alone isn't enough.
  793. printer_kp = None
  794. if matching_kp and state and state.kprofiles:
  795. for pkp in state.kprofiles:
  796. if pkp.slot_id == matching_kp.cali_idx and pkp.nozzle_diameter == nozzle_diameter:
  797. printer_kp = pkp
  798. break
  799. # Realign slot's filament context to the kp's calibration
  800. # context so ams_filament_setting and extrusion_cali_sel
  801. # reference the same preset; otherwise the printer drops the
  802. # cali_idx to default. PFUS-prefix cloud-user presets are
  803. # rejected by the slicer in tray_info_idx — skip realignment
  804. # in that case.
  805. effective_tray_info_idx = tray_info_idx
  806. effective_setting_id = setting_id
  807. if printer_kp and printer_kp.filament_id:
  808. if not printer_kp.filament_id.startswith("PFUS"):
  809. effective_tray_info_idx = printer_kp.filament_id
  810. if printer_kp.setting_id:
  811. effective_setting_id = printer_kp.setting_id
  812. elif matching_kp and matching_kp.setting_id:
  813. derived = normalize_slicer_filament(matching_kp.setting_id)[0]
  814. if derived and not derived.startswith("PFUS"):
  815. effective_tray_info_idx = derived
  816. effective_setting_id = matching_kp.setting_id
  817. if effective_tray_info_idx != tray_info_idx or effective_setting_id != setting_id:
  818. logger.info(
  819. "Spoolman link: realigning tray_info_idx %r → %r, setting_id %r → %r (kp_id=%s, source=%s)",
  820. tray_info_idx,
  821. effective_tray_info_idx,
  822. setting_id,
  823. effective_setting_id,
  824. matching_kp.id if matching_kp else None,
  825. "printer" if printer_kp else "stored",
  826. )
  827. mqtt_client.ams_set_filament_setting(
  828. ams_id=a_id,
  829. tray_id=t_id,
  830. tray_info_idx=effective_tray_info_idx,
  831. tray_type=tray_type,
  832. tray_sub_brands=tray_sub_brands,
  833. tray_color=tray_color,
  834. nozzle_temp_min=temp_min,
  835. nozzle_temp_max=temp_max,
  836. setting_id=effective_setting_id,
  837. )
  838. if matching_kp and matching_kp.cali_idx is not None:
  839. cali_filament_id = (
  840. printer_kp.filament_id if printer_kp and printer_kp.filament_id else None
  841. ) or effective_tray_info_idx
  842. mqtt_client.extrusion_cali_sel(
  843. ams_id=a_id,
  844. tray_id=t_id,
  845. cali_idx=matching_kp.cali_idx,
  846. filament_id=cali_filament_id,
  847. nozzle_diameter=nozzle_diameter,
  848. )
  849. logger.info(
  850. "Spoolman link: applied K-profile cali_idx=%d "
  851. "(kp_id=%d, filament_id=%s) for spool %d on printer %d AMS%d-T%d",
  852. matching_kp.cali_idx,
  853. matching_kp.id,
  854. cali_filament_id,
  855. spool_id,
  856. p_id,
  857. a_id,
  858. t_id,
  859. )
  860. else:
  861. from backend.app.api.routes.inventory import _find_tray_in_ams_data # noqa: PLC0415
  862. live_tray = None
  863. if state and state.raw_data:
  864. ams_raw = state.raw_data.get("ams", [])
  865. if isinstance(ams_raw, dict):
  866. ams_raw = ams_raw.get("ams", [])
  867. live_tray = _find_tray_in_ams_data(ams_raw, a_id, t_id)
  868. live_cali_idx = (live_tray or {}).get("cali_idx")
  869. if live_cali_idx is not None and live_cali_idx >= 0:
  870. mqtt_client.extrusion_cali_sel(
  871. ams_id=a_id,
  872. tray_id=t_id,
  873. cali_idx=live_cali_idx,
  874. filament_id=effective_tray_info_idx,
  875. nozzle_diameter=nozzle_diameter,
  876. )
  877. logger.info(
  878. "Auto-configured AMS slot ams=%d tray=%d after linking Spoolman spool %d on printer %d",
  879. a_id,
  880. t_id,
  881. spool_id,
  882. p_id,
  883. )
  884. except (SpoolmanNotFoundError, SpoolmanUnavailableError) as e:
  885. logger.warning(
  886. "Could not fetch Spoolman spool %d for MQTT configure after tag link: %s",
  887. spool_id,
  888. e,
  889. )
  890. except Exception:
  891. logger.exception(
  892. "Failed to auto-configure AMS slot after linking Spoolman spool %d (printer=%d ams=%d tray=%d)",
  893. spool_id,
  894. p_id,
  895. a_id,
  896. t_id,
  897. )
  898. return {"success": True, "message": f"Spool {spool_id} linked to AMS tag"}
  899. @router.post("/spools/{spool_id}/unlink")
  900. async def unlink_spool(
  901. spool_id: int,
  902. db: AsyncSession = Depends(get_db),
  903. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_UPDATE),
  904. ):
  905. """Unlink a Spoolman spool from AMS by clearing Spoolman extra.tag."""
  906. sm = await get_spoolman_settings(db)
  907. enabled, url = sm["enabled"], sm["url"]
  908. if not enabled:
  909. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  910. client = await get_spoolman_client()
  911. if not client:
  912. if url:
  913. client = await init_spoolman_client(url)
  914. else:
  915. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  916. if not await client.health_check():
  917. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  918. # Spoolman PATCHes the extra dict by MERGING with the existing keys —
  919. # popping "tag" from a copy of the dict and sending the rest doesn't
  920. # clear it; Spoolman keeps the old value because the key wasn't in the
  921. # payload. To actually clear a key we must explicitly send it as the
  922. # JSON-encoded empty string ('""'), which the read-side filters in
  923. # _map_spoolman_spool and get_linked_spools strip via .strip('"').
  924. #
  925. # merge_spool_extra acquires extra_lock(spool_id) internally — wrapping
  926. # this call in another `async with client.extra_lock(spool_id)` would
  927. # deadlock (asyncio.Lock is not reentrant).
  928. try:
  929. await client.merge_spool_extra(spool_id, {"tag": json.dumps("")})
  930. except SpoolmanNotFoundError:
  931. raise HTTPException(status_code=404, detail="Spool not found in Spoolman")
  932. except SpoolmanClientError:
  933. raise HTTPException(status_code=502, detail="Spoolman rejected the request")
  934. except SpoolmanUnavailableError:
  935. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  936. # Remove local slot assignment for this spool (all slots — a spool can only be in one at a time)
  937. try:
  938. await db.execute(delete(SpoolmanSlotAssignment).where(SpoolmanSlotAssignment.spoolman_spool_id == spool_id))
  939. await db.commit()
  940. except Exception:
  941. await db.rollback()
  942. logger.exception("DB error removing slot assignment for spool %s", spool_id)
  943. raise HTTPException(status_code=500, detail="Failed to remove local slot assignment")
  944. logger.info("Unlinked Spoolman spool %s", spool_id)
  945. return {"success": True, "message": f"Spool {spool_id} unlinked from AMS"}