spoolman.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. """Spoolman integration API routes."""
  2. import json
  3. import logging
  4. from fastapi import APIRouter, Depends, HTTPException
  5. from pydantic import BaseModel
  6. from sqlalchemy import select
  7. from sqlalchemy.ext.asyncio import AsyncSession
  8. from sqlalchemy.orm import selectinload
  9. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  10. from backend.app.core.database import get_db
  11. from backend.app.core.permissions import Permission
  12. from backend.app.models.printer import Printer
  13. from backend.app.models.settings import Settings
  14. from backend.app.models.spool_assignment import SpoolAssignment
  15. from backend.app.models.user import User
  16. from backend.app.services.printer_manager import printer_manager
  17. from backend.app.services.spoolman import (
  18. close_spoolman_client,
  19. get_spoolman_client,
  20. init_spoolman_client,
  21. )
  22. logger = logging.getLogger(__name__)
  23. router = APIRouter(prefix="/spoolman", tags=["spoolman"])
  24. class SpoolmanStatus(BaseModel):
  25. """Spoolman connection status."""
  26. enabled: bool
  27. connected: bool
  28. url: str | None
  29. class SkippedSpool(BaseModel):
  30. """Information about a skipped spool during sync."""
  31. location: str # e.g., "AMS A1" or "External Spool"
  32. reason: str # e.g., "Not a Bambu Lab spool", "Empty tray"
  33. filament_type: str | None = None # e.g., "PLA", "PETG"
  34. color: str | None = None # Hex color
  35. class SyncResult(BaseModel):
  36. """Result of a Spoolman sync operation."""
  37. success: bool
  38. synced_count: int
  39. skipped_count: int = 0
  40. skipped: list[SkippedSpool] = []
  41. errors: list[str]
  42. async def get_spoolman_settings(db: AsyncSession) -> dict:
  43. """Get Spoolman settings from database.
  44. Returns:
  45. Dict with keys: enabled, url, sync_mode, disable_weight_sync
  46. """
  47. settings = {
  48. "enabled": False,
  49. "url": "",
  50. "sync_mode": "auto",
  51. "disable_weight_sync": False,
  52. }
  53. result = await db.execute(select(Settings))
  54. for setting in result.scalars().all():
  55. if setting.key == "spoolman_enabled":
  56. settings["enabled"] = setting.value.lower() == "true"
  57. elif setting.key == "spoolman_url":
  58. settings["url"] = setting.value
  59. elif setting.key == "spoolman_sync_mode":
  60. settings["sync_mode"] = setting.value
  61. elif setting.key == "spoolman_disable_weight_sync":
  62. settings["disable_weight_sync"] = setting.value.lower() == "true"
  63. return settings
  64. @router.get("/status", response_model=SpoolmanStatus)
  65. async def get_spoolman_status(
  66. db: AsyncSession = Depends(get_db),
  67. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  68. ):
  69. """Get Spoolman integration status."""
  70. sm = await get_spoolman_settings(db)
  71. enabled, url = sm["enabled"], sm["url"]
  72. client = await get_spoolman_client()
  73. connected = False
  74. if client:
  75. connected = await client.health_check()
  76. return SpoolmanStatus(
  77. enabled=enabled,
  78. connected=connected,
  79. url=url if url else None,
  80. )
  81. @router.post("/connect")
  82. async def connect_spoolman(
  83. db: AsyncSession = Depends(get_db),
  84. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  85. ):
  86. """Connect to Spoolman server using configured URL."""
  87. sm = await get_spoolman_settings(db)
  88. enabled, url = sm["enabled"], sm["url"]
  89. if not enabled:
  90. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  91. if not url:
  92. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  93. try:
  94. client = await init_spoolman_client(url)
  95. connected = await client.health_check()
  96. if not connected:
  97. raise HTTPException(
  98. status_code=503,
  99. detail=f"Could not connect to Spoolman at {url}",
  100. )
  101. # Ensure the 'tag' extra field exists for RFID/UUID storage
  102. await client.ensure_tag_extra_field()
  103. return {"success": True, "message": f"Connected to Spoolman at {url}"}
  104. except Exception as e:
  105. logger.error("Failed to connect to Spoolman: %s", e)
  106. raise HTTPException(status_code=503, detail=str(e))
  107. @router.post("/disconnect")
  108. async def disconnect_spoolman(
  109. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  110. ):
  111. """Disconnect from Spoolman server."""
  112. await close_spoolman_client()
  113. return {"success": True, "message": "Disconnected from Spoolman"}
  114. @router.post("/sync/{printer_id}", response_model=SyncResult)
  115. async def sync_printer_ams(
  116. printer_id: int,
  117. db: AsyncSession = Depends(get_db),
  118. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_UPDATE),
  119. ):
  120. """Sync AMS data from a specific printer to Spoolman."""
  121. # Check if Spoolman is enabled and connected
  122. sm = await get_spoolman_settings(db)
  123. enabled, url, disable_weight_sync = sm["enabled"], sm["url"], sm["disable_weight_sync"]
  124. if not enabled:
  125. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  126. client = await get_spoolman_client()
  127. if not client:
  128. # Try to connect
  129. if url:
  130. client = await init_spoolman_client(url)
  131. else:
  132. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  133. if not await client.health_check():
  134. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  135. # Get printer info
  136. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  137. printer = result.scalar_one_or_none()
  138. if not printer:
  139. raise HTTPException(status_code=404, detail="Printer not found")
  140. # Get current printer state with AMS data
  141. state = printer_manager.get_status(printer_id)
  142. if not state:
  143. raise HTTPException(status_code=404, detail="Printer not connected")
  144. if not state.raw_data:
  145. raise HTTPException(status_code=400, detail="No AMS data available")
  146. ams_data = state.raw_data.get("ams")
  147. if not ams_data:
  148. raise HTTPException(
  149. status_code=400,
  150. detail="No AMS data in printer state. Try triggering a slot re-read on the printer.",
  151. )
  152. # Sync each AMS tray to Spoolman
  153. synced = 0
  154. skipped: list[SkippedSpool] = []
  155. errors = []
  156. # Track tray UUIDs currently in the AMS (for clearing removed spools)
  157. current_tray_uuids: set[str] = set()
  158. # Handle different AMS data structures
  159. # Traditional AMS: list of {"id": N, "tray": [...]} dicts
  160. # H2D/newer printers: dict with different structure
  161. ams_units = []
  162. if isinstance(ams_data, list):
  163. ams_units = ams_data
  164. elif isinstance(ams_data, dict):
  165. # H2D format: check for "ams" key containing list, or "tray" key directly
  166. if "ams" in ams_data and isinstance(ams_data["ams"], list):
  167. ams_units = ams_data["ams"]
  168. elif "tray" in ams_data:
  169. # Single AMS unit format - wrap in list
  170. ams_units = [{"id": 0, "tray": ams_data.get("tray", [])}]
  171. else:
  172. logger.info("AMS dict keys for debugging: %s", list(ams_data.keys()))
  173. if not ams_units:
  174. raise HTTPException(
  175. status_code=400,
  176. detail=f"AMS data format not supported. Keys: {list(ams_data.keys()) if isinstance(ams_data, dict) else type(ams_data).__name__}",
  177. )
  178. # OPTIMIZATION: Fetch all spools once before processing trays
  179. # This eliminates redundant API calls (one per tray) when syncing multiple trays
  180. logger.debug("[Printer %s] Fetching spools cache for sync...", printer.name)
  181. try:
  182. cached_spools = await client.get_spools()
  183. logger.debug("[Printer %s] Cached %d spools for batch sync", printer.name, len(cached_spools))
  184. except Exception as e:
  185. logger.error("[Printer %s] Failed to fetch spools cache after retries: %s", printer.name, e)
  186. raise HTTPException(
  187. status_code=503,
  188. detail=f"Failed to connect to Spoolman after multiple retries: {str(e)}",
  189. )
  190. # Load inventory weights as fallback (when AMS MQTT data lacks remain values)
  191. inv_weights: dict[tuple[int, int], float] = {}
  192. try:
  193. assign_result = await db.execute(
  194. select(SpoolAssignment)
  195. .options(selectinload(SpoolAssignment.spool))
  196. .where(SpoolAssignment.printer_id == printer_id)
  197. )
  198. for assignment in assign_result.scalars().all():
  199. spool = assignment.spool
  200. if spool and spool.label_weight > 0:
  201. remaining = max(0.0, spool.label_weight - (spool.weight_used or 0))
  202. inv_weights[(assignment.ams_id, assignment.tray_id)] = remaining
  203. except Exception as e:
  204. logger.debug("Could not load inventory weights for printer %s: %s", printer_id, e)
  205. for ams_unit in ams_units:
  206. if not isinstance(ams_unit, dict):
  207. continue
  208. ams_id = int(ams_unit.get("id", 0))
  209. trays = ams_unit.get("tray", [])
  210. for tray_data in trays:
  211. if not isinstance(tray_data, dict):
  212. continue
  213. tray = client.parse_ams_tray(ams_id, tray_data)
  214. if not tray:
  215. continue # Empty tray - nothing to sync
  216. # Build location string for reporting
  217. location = client.convert_ams_slot_to_location(ams_id, tray.tray_id)
  218. # Skip non-Bambu Lab spools (SpoolEase/third-party) - track as skipped
  219. if not client.is_bambu_lab_spool(tray.tray_uuid, tray.tag_uid, tray.tray_info_idx):
  220. skipped.append(
  221. SkippedSpool(
  222. location=location,
  223. reason="Non-Bambu Lab spool (no RFID tag)",
  224. filament_type=tray.tray_type if tray.tray_type else None,
  225. color=tray.tray_color[:6] if tray.tray_color else None,
  226. )
  227. )
  228. continue
  229. # Track this spool tag as currently present in the AMS (prefer tray_uuid, fallback to tag_uid)
  230. spool_tag = (
  231. tray.tray_uuid
  232. if tray.tray_uuid and tray.tray_uuid != "00000000000000000000000000000000"
  233. else tray.tag_uid
  234. )
  235. if spool_tag:
  236. current_tray_uuids.add(spool_tag.upper())
  237. try:
  238. inv_remaining = inv_weights.get((ams_id, tray.tray_id))
  239. sync_result = await client.sync_ams_tray(
  240. tray,
  241. printer.name,
  242. disable_weight_sync=disable_weight_sync,
  243. cached_spools=cached_spools,
  244. inventory_remaining=inv_remaining,
  245. )
  246. if sync_result:
  247. synced += 1
  248. # Add newly created spool to cache
  249. if sync_result.get("id"):
  250. spool_exists = any(s.get("id") == sync_result["id"] for s in cached_spools)
  251. if not spool_exists:
  252. cached_spools.append(sync_result)
  253. logger.debug("Added newly created spool %s to cache", sync_result["id"])
  254. logger.info(
  255. "Synced %s from %s AMS %s tray %s", tray.tray_sub_brands, printer.name, ams_id, tray.tray_id
  256. )
  257. else:
  258. # Bambu Lab spool that wasn't synced (not found in Spoolman)
  259. errors.append(f"Spool not found in Spoolman: AMS {ams_id}:{tray.tray_id}")
  260. except Exception as e:
  261. error_msg = f"Error syncing AMS {ams_id} tray {tray.tray_id}: {e}"
  262. logger.error(error_msg)
  263. errors.append(error_msg)
  264. # Clear location for spools that were removed from this printer's AMS
  265. try:
  266. cleared = await client.clear_location_for_removed_spools(
  267. printer.name, current_tray_uuids, cached_spools=cached_spools
  268. )
  269. if cleared > 0:
  270. logger.info("Cleared location for %s spools removed from %s", cleared, printer.name)
  271. except Exception as e:
  272. logger.error("Error clearing locations for removed spools: %s", e)
  273. return SyncResult(
  274. success=len(errors) == 0,
  275. synced_count=synced,
  276. skipped_count=len(skipped),
  277. skipped=skipped,
  278. errors=errors,
  279. )
  280. @router.post("/sync-all", response_model=SyncResult)
  281. async def sync_all_printers(
  282. db: AsyncSession = Depends(get_db),
  283. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_UPDATE),
  284. ):
  285. """Sync AMS data from all connected printers to Spoolman."""
  286. # Check if Spoolman is enabled
  287. sm = await get_spoolman_settings(db)
  288. enabled, url, disable_weight_sync = sm["enabled"], sm["url"], sm["disable_weight_sync"]
  289. if not enabled:
  290. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  291. client = await get_spoolman_client()
  292. if not client:
  293. if url:
  294. client = await init_spoolman_client(url)
  295. else:
  296. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  297. if not await client.health_check():
  298. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  299. # Get all active printers
  300. result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  301. printers = result.scalars().all()
  302. total_synced = 0
  303. all_skipped: list[SkippedSpool] = []
  304. all_errors = []
  305. # Track tray UUIDs per printer (for clearing removed spools)
  306. printer_tray_uuids: dict[str, set[str]] = {}
  307. # Track synced spool IDs per printer (for location-based cleanup when no UUIDs available)
  308. printer_synced_ids: dict[str, set[int]] = {}
  309. # OPTIMIZATION: Fetch all spools once before processing ALL printers/trays
  310. # This eliminates redundant API calls across all printers
  311. logger.debug("Fetching spools cache for sync-all operation...")
  312. try:
  313. cached_spools = await client.get_spools()
  314. logger.debug("Cached %d spools for batch sync across %d printers", len(cached_spools), len(printers))
  315. except Exception as e:
  316. logger.error("Failed to fetch spools cache after retries: %s", e)
  317. raise HTTPException(
  318. status_code=503,
  319. detail=f"Failed to connect to Spoolman after multiple retries: {str(e)}",
  320. )
  321. # Load inventory assignments for weight fallback (when AMS MQTT data lacks remain values)
  322. # Key: (printer_id, ams_id, tray_id) → remaining_weight in grams
  323. inventory_weights: dict[tuple[int, int, int], float] = {}
  324. try:
  325. assign_result = await db.execute(select(SpoolAssignment).options(selectinload(SpoolAssignment.spool)))
  326. for assignment in assign_result.scalars().all():
  327. spool = assignment.spool
  328. if spool and spool.label_weight > 0:
  329. remaining = max(0.0, spool.label_weight - (spool.weight_used or 0))
  330. inventory_weights[(assignment.printer_id, assignment.ams_id, assignment.tray_id)] = remaining
  331. except Exception as e:
  332. logger.debug("Could not load inventory assignments for weight fallback: %s", e)
  333. for printer in printers:
  334. state = printer_manager.get_status(printer.id)
  335. if not state or not state.raw_data:
  336. continue
  337. ams_data = state.raw_data.get("ams")
  338. if not ams_data:
  339. continue
  340. # Initialize tracking sets for this printer
  341. printer_tray_uuids[printer.name] = set()
  342. printer_synced_ids[printer.name] = set()
  343. # Handle different AMS data structures
  344. # Traditional AMS: list of {"id": N, "tray": [...]} dicts
  345. # H2D/newer printers: dict with different structure
  346. ams_units = []
  347. if isinstance(ams_data, list):
  348. ams_units = ams_data
  349. elif isinstance(ams_data, dict):
  350. # H2D format: check for "ams" key containing list, or "tray" key directly
  351. if "ams" in ams_data and isinstance(ams_data["ams"], list):
  352. ams_units = ams_data["ams"]
  353. elif "tray" in ams_data:
  354. # Single AMS unit format - wrap in list
  355. ams_units = [{"id": 0, "tray": ams_data.get("tray", [])}]
  356. else:
  357. logger.debug("Printer %s AMS dict keys: %s", printer.name, list(ams_data.keys()))
  358. if not ams_units:
  359. logger.debug("Printer %s has no AMS units to sync (type: %s)", printer.name, type(ams_data).__name__)
  360. continue
  361. for ams_unit in ams_units:
  362. if not isinstance(ams_unit, dict):
  363. logger.debug("Skipping non-dict AMS unit: %s", type(ams_unit))
  364. continue
  365. ams_id = int(ams_unit.get("id", 0))
  366. trays = ams_unit.get("tray", [])
  367. for tray_data in trays:
  368. if not isinstance(tray_data, dict):
  369. continue
  370. tray = client.parse_ams_tray(ams_id, tray_data)
  371. if not tray:
  372. continue
  373. # Build location string for reporting
  374. location = f"{printer.name} - {client.convert_ams_slot_to_location(ams_id, tray.tray_id)}"
  375. # Skip non-Bambu Lab spools (SpoolEase/third-party) - track as skipped
  376. if not client.is_bambu_lab_spool(tray.tray_uuid, tray.tag_uid, tray.tray_info_idx):
  377. all_skipped.append(
  378. SkippedSpool(
  379. location=location,
  380. reason="Non-Bambu Lab spool (no RFID tag)",
  381. filament_type=tray.tray_type if tray.tray_type else None,
  382. color=tray.tray_color[:6] if tray.tray_color else None,
  383. )
  384. )
  385. continue
  386. # Track this spool tag as currently present in the AMS (prefer tray_uuid, fallback to tag_uid)
  387. spool_tag = (
  388. tray.tray_uuid
  389. if tray.tray_uuid and tray.tray_uuid != "00000000000000000000000000000000"
  390. else tray.tag_uid
  391. )
  392. if spool_tag:
  393. printer_tray_uuids[printer.name].add(spool_tag.upper())
  394. try:
  395. # Look up inventory weight as fallback when AMS data is invalid
  396. inv_remaining = inventory_weights.get((printer.id, ams_id, tray.tray_id))
  397. sync_result = await client.sync_ams_tray(
  398. tray,
  399. printer.name,
  400. disable_weight_sync=disable_weight_sync,
  401. cached_spools=cached_spools,
  402. inventory_remaining=inv_remaining,
  403. )
  404. if sync_result:
  405. total_synced += 1
  406. # Track synced spool ID for cleanup
  407. if sync_result.get("id"):
  408. printer_synced_ids[printer.name].add(sync_result["id"])
  409. # Add newly created spool to cache
  410. spool_exists = any(s.get("id") == sync_result["id"] for s in cached_spools)
  411. if not spool_exists:
  412. cached_spools.append(sync_result)
  413. logger.debug("Added newly created spool %s to cache", sync_result["id"])
  414. except Exception as e:
  415. all_errors.append(f"{printer.name} AMS {ams_id}:{tray.tray_id}: {e}")
  416. # Clear location for spools that were removed from each printer's AMS
  417. for printer_name, current_tray_uuids in printer_tray_uuids.items():
  418. try:
  419. cleared = await client.clear_location_for_removed_spools(
  420. printer_name,
  421. current_tray_uuids,
  422. cached_spools=cached_spools,
  423. synced_spool_ids=printer_synced_ids.get(printer_name, set()),
  424. )
  425. if cleared > 0:
  426. logger.info("Cleared location for %s spools removed from %s", cleared, printer_name)
  427. except Exception as e:
  428. logger.error("Error clearing locations for %s: %s", printer_name, e)
  429. return SyncResult(
  430. success=len(all_errors) == 0,
  431. synced_count=total_synced,
  432. skipped_count=len(all_skipped),
  433. skipped=all_skipped,
  434. errors=all_errors,
  435. )
  436. @router.get("/spools")
  437. async def get_spools(
  438. db: AsyncSession = Depends(get_db),
  439. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  440. ):
  441. """Get all spools from Spoolman."""
  442. sm = await get_spoolman_settings(db)
  443. enabled, url = sm["enabled"], sm["url"]
  444. if not enabled:
  445. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  446. client = await get_spoolman_client()
  447. if not client:
  448. if url:
  449. client = await init_spoolman_client(url)
  450. else:
  451. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  452. if not await client.health_check():
  453. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  454. spools = await client.get_spools()
  455. return {"spools": spools}
  456. @router.get("/filaments")
  457. async def get_filaments(
  458. db: AsyncSession = Depends(get_db),
  459. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  460. ):
  461. """Get all filaments from Spoolman."""
  462. sm = await get_spoolman_settings(db)
  463. enabled, url = sm["enabled"], sm["url"]
  464. if not enabled:
  465. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  466. client = await get_spoolman_client()
  467. if not client:
  468. if url:
  469. client = await init_spoolman_client(url)
  470. else:
  471. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  472. if not await client.health_check():
  473. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  474. filaments = await client.get_filaments()
  475. return {"filaments": filaments}
  476. class UnlinkedSpool(BaseModel):
  477. """A Spoolman spool that is not linked to any AMS tray."""
  478. id: int
  479. filament_name: str | None
  480. filament_material: str | None
  481. filament_color_hex: str | None
  482. remaining_weight: float | None
  483. location: str | None
  484. @router.get("/spools/unlinked", response_model=list[UnlinkedSpool])
  485. async def get_unlinked_spools(
  486. db: AsyncSession = Depends(get_db),
  487. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  488. ):
  489. """Get all Spoolman spools that don't have a tag (not linked to AMS)."""
  490. sm = await get_spoolman_settings(db)
  491. enabled, url = sm["enabled"], sm["url"]
  492. if not enabled:
  493. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  494. client = await get_spoolman_client()
  495. if not client:
  496. if url:
  497. client = await init_spoolman_client(url)
  498. else:
  499. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  500. if not await client.health_check():
  501. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  502. spools = await client.get_spools()
  503. unlinked = []
  504. for spool in spools:
  505. # Check if spool has a tag in extra field
  506. extra = spool.get("extra", {}) or {}
  507. tag = extra.get("tag", "")
  508. # Remove quotes if present (JSON encoded string) and check if empty
  509. clean_tag = tag.strip('"') if tag else ""
  510. if not clean_tag:
  511. filament = spool.get("filament", {}) or {}
  512. unlinked.append(
  513. UnlinkedSpool(
  514. id=spool["id"],
  515. filament_name=filament.get("name"),
  516. filament_material=filament.get("material"),
  517. filament_color_hex=filament.get("color_hex"),
  518. remaining_weight=spool.get("remaining_weight"),
  519. location=spool.get("location"),
  520. )
  521. )
  522. return unlinked
  523. @router.get("/spools/linked")
  524. async def get_linked_spools(
  525. db: AsyncSession = Depends(get_db),
  526. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_READ),
  527. ):
  528. """Get a map of tag -> spool_id for all Spoolman spools that have a tag assigned."""
  529. sm = await get_spoolman_settings(db)
  530. enabled, url = sm["enabled"], sm["url"]
  531. if not enabled:
  532. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  533. client = await get_spoolman_client()
  534. if not client:
  535. if url:
  536. client = await init_spoolman_client(url)
  537. else:
  538. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  539. if not await client.health_check():
  540. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  541. spools = await client.get_spools()
  542. linked: dict[str, dict] = {}
  543. for spool in spools:
  544. # Check if spool has a tag in extra field
  545. extra = spool.get("extra", {}) or {}
  546. tag = extra.get("tag", "")
  547. if tag:
  548. # Remove quotes if present (JSON encoded string)
  549. clean_tag = tag.strip('"').upper()
  550. if clean_tag:
  551. filament = spool.get("filament") or {}
  552. linked[clean_tag] = {
  553. "id": spool["id"],
  554. "remaining_weight": spool.get("remaining_weight"),
  555. "filament_weight": filament.get("weight"),
  556. }
  557. return {"linked": linked}
  558. class LinkSpoolRequest(BaseModel):
  559. """Request to link a Spoolman spool to an AMS tag (tray_uuid or tag_uid)."""
  560. spool_tag: str | None = None
  561. tray_uuid: str | None = None
  562. tag_uid: str | None = None
  563. printer_id: int | None = None
  564. ams_id: int | None = None
  565. tray_id: int | None = None
  566. @router.post("/spools/{spool_id}/link")
  567. async def link_spool(
  568. spool_id: int,
  569. request: LinkSpoolRequest,
  570. db: AsyncSession = Depends(get_db),
  571. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_UPDATE),
  572. ):
  573. """Link a Spoolman spool to an AMS tag by setting Spoolman extra.tag."""
  574. sm = await get_spoolman_settings(db)
  575. enabled, url = sm["enabled"], sm["url"]
  576. if not enabled:
  577. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  578. client = await get_spoolman_client()
  579. if not client:
  580. if url:
  581. client = await init_spoolman_client(url)
  582. else:
  583. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  584. if not await client.health_check():
  585. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  586. # Resolve and validate spool tag (supports tray_uuid=32 hex and tag_uid=16 hex)
  587. spool_tag = (request.spool_tag or request.tray_uuid or request.tag_uid or "").strip()
  588. if not spool_tag:
  589. raise HTTPException(status_code=400, detail="Missing spool tag (tray_uuid or tag_uid)")
  590. if len(spool_tag) not in (16, 32):
  591. raise HTTPException(status_code=400, detail="Invalid spool tag format (must be 16 or 32 hex characters)")
  592. try:
  593. int(spool_tag, 16)
  594. except ValueError:
  595. raise HTTPException(status_code=400, detail="Invalid spool tag format (must be hex)")
  596. if set(spool_tag) == {"0"}:
  597. raise HTTPException(status_code=400, detail="Invalid spool tag format (all-zero tag is not linkable)")
  598. spool_tag = spool_tag.upper()
  599. # Build location like: "{Printer Name} - {AMS Name} {Slot Number}"
  600. location: str | None = None
  601. if request.printer_id is not None and request.ams_id is not None and request.tray_id is not None:
  602. printer_result = await db.execute(select(Printer).where(Printer.id == request.printer_id))
  603. printer = printer_result.scalar_one_or_none()
  604. if not printer:
  605. raise HTTPException(status_code=404, detail="Printer not found")
  606. location = f"{printer.name} - {client.convert_ams_slot_to_location(request.ams_id, request.tray_id)}"
  607. # Update spool with tag
  608. # Note: Spoolman extra field values must be valid JSON, so we encode the string
  609. result = await client.update_spool(
  610. spool_id=spool_id,
  611. location=location,
  612. extra={"tag": json.dumps(spool_tag)},
  613. )
  614. if result:
  615. logger.info("Linked Spoolman spool %s to tag %s", spool_id, spool_tag)
  616. return {"success": True, "message": f"Spool {spool_id} linked to AMS tag"}
  617. else:
  618. raise HTTPException(status_code=500, detail="Failed to update spool")
  619. @router.post("/spools/{spool_id}/unlink")
  620. async def unlink_spool(
  621. spool_id: int,
  622. db: AsyncSession = Depends(get_db),
  623. _: User | None = RequirePermissionIfAuthEnabled(Permission.FILAMENTS_UPDATE),
  624. ):
  625. """Unlink a Spoolman spool from AMS by clearing Spoolman extra.tag."""
  626. sm = await get_spoolman_settings(db)
  627. enabled, url = sm["enabled"], sm["url"]
  628. if not enabled:
  629. raise HTTPException(status_code=400, detail="Spoolman integration is not enabled")
  630. client = await get_spoolman_client()
  631. if not client:
  632. if url:
  633. client = await init_spoolman_client(url)
  634. else:
  635. raise HTTPException(status_code=400, detail="Spoolman URL is not configured")
  636. if not await client.health_check():
  637. raise HTTPException(status_code=503, detail="Spoolman is not reachable")
  638. result = await client.update_spool(
  639. spool_id=spool_id,
  640. clear_location=True,
  641. extra={"tag": json.dumps("")},
  642. )
  643. if result:
  644. logger.info("Unlinked Spoolman spool %s", spool_id)
  645. return {"success": True, "message": f"Spool {spool_id} unlinked from AMS"}
  646. else:
  647. raise HTTPException(status_code=500, detail="Failed to update spool")