spoolman.py 46 KB

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