spoolman.py 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. """Spoolman integration service for syncing AMS filament data."""
  2. import asyncio
  3. import logging
  4. import weakref
  5. from dataclasses import dataclass
  6. from datetime import datetime, timezone
  7. from typing import Literal
  8. import httpx
  9. logger = logging.getLogger(__name__)
  10. BAMBU_RFID_TAG_LENGTH = 32
  11. @dataclass
  12. class SpoolmanSpool:
  13. """Represents a spool in Spoolman."""
  14. id: int
  15. filament_id: int | None
  16. remaining_weight: float | None
  17. used_weight: float
  18. first_used: str | None
  19. last_used: str | None
  20. location: str | None
  21. lot_nr: str | None
  22. comment: str | None
  23. extra: dict | None # Contains tag_uid in extra.tag
  24. @dataclass
  25. class SpoolmanFilament:
  26. """Represents a filament type in Spoolman."""
  27. id: int
  28. name: str
  29. vendor_id: int | None
  30. material: str | None
  31. color_hex: str | None
  32. weight: float | None # Net weight in grams
  33. @dataclass
  34. class AMSTray:
  35. """Represents an AMS tray with filament data from Bambu printer."""
  36. ams_id: int # 0-3 for regular AMS, 128-135 for AMS-HT, 254+ for external spool
  37. tray_id: int # 0-3
  38. tray_type: str # PLA, PETG, ABS, etc.
  39. tray_sub_brands: str # Full name like "PLA Basic", "PETG HF"
  40. tray_color: str # Hex color like "FEC600FF"
  41. remain: int # Remaining percentage (0-100)
  42. tag_uid: str # RFID tag UID
  43. tray_uuid: str # Spool UUID
  44. tray_info_idx: str # Bambu filament preset ID like "GFA00"
  45. tray_weight: int # Spool weight in grams (usually 1000)
  46. class SpoolmanNotFoundError(Exception):
  47. """Raised when a spool ID does not exist in Spoolman (HTTP 404)."""
  48. class SpoolmanUnavailableError(Exception):
  49. """Raised when Spoolman is unreachable or returns a server/network error."""
  50. class SpoolmanClientError(Exception):
  51. """Raised when Spoolman returns a 4xx client error (not 404)."""
  52. def __init__(self, message: str, status_code: int, response_text: str = "") -> None:
  53. super().__init__(message)
  54. self.status_code = status_code
  55. self.response_text = response_text
  56. def _filament_subtype_part(name: str, material: str) -> str:
  57. """Return the subtype portion of a filament name, lowercased.
  58. Mirrors the read-side derivation in
  59. ``backend/app/api/routes/_spoolman_helpers.py::_map_spoolman_spool``:
  60. if the filament name starts with the material prefix (e.g. ``"PLA Glow"``
  61. when material is ``"PLA"``), strip it; otherwise return the name as-is.
  62. Used by ``find_or_create_filament`` so that an existing filament saved by
  63. the AMS-sync path with name ``"Glow"`` still matches a user-driven edit
  64. that composes ``"PLA Glow"`` (#1357).
  65. """
  66. s = (name or "").strip()
  67. m = (material or "").strip()
  68. if m and s.upper().startswith(m.upper() + " "):
  69. return s[len(m) + 1 :].strip().lower()
  70. return s.lower()
  71. class SpoolmanClient:
  72. """Client for interacting with Spoolman API."""
  73. def __init__(self, base_url: str):
  74. """Initialize the Spoolman client."""
  75. self.base_url = base_url.rstrip("/")
  76. self.api_url = f"{self.base_url}/api/v1"
  77. self._client: httpx.AsyncClient | None = None
  78. self._connected = False
  79. # Per-spool locks for atomic read-modify-write in merge_spool_extra.
  80. # WeakValueDictionary: locks are GC'd once no coroutine holds a reference.
  81. self._extra_locks: weakref.WeakValueDictionary[int, asyncio.Lock] = weakref.WeakValueDictionary()
  82. async def _get_client(self) -> httpx.AsyncClient:
  83. """Get or create the HTTP client with connection pooling limits."""
  84. if self._client is None:
  85. self._client = httpx.AsyncClient(
  86. timeout=httpx.Timeout(connect=5.0, read=10.0, write=5.0, pool=5.0),
  87. follow_redirects=False,
  88. verify=True,
  89. limits=httpx.Limits(
  90. max_keepalive_connections=5,
  91. max_connections=10,
  92. keepalive_expiry=30.0,
  93. ),
  94. )
  95. return self._client
  96. async def close(self):
  97. """Close the HTTP client."""
  98. if self._client:
  99. await self._client.aclose()
  100. self._client = None
  101. async def health_check(self) -> bool:
  102. """Check if Spoolman server is reachable; returns True if healthy."""
  103. try:
  104. client = await self._get_client()
  105. response = await client.get(f"{self.api_url}/health")
  106. self._connected = response.status_code == 200
  107. return self._connected
  108. except Exception as e:
  109. logger.warning(
  110. "Spoolman health check failed (url=%s, type=%s): %s",
  111. self.api_url,
  112. type(e).__name__,
  113. e,
  114. )
  115. self._connected = False
  116. return False
  117. @property
  118. def is_connected(self) -> bool:
  119. """Check if client is connected to Spoolman."""
  120. return self._connected
  121. async def get_spools(self) -> list[dict]:
  122. """Fetch all spools from Spoolman with up to 3 retries on connection errors."""
  123. max_attempts = 3
  124. retry_delay = 0.5 # 500ms
  125. for attempt in range(1, max_attempts + 1):
  126. try:
  127. client = await self._get_client()
  128. response = await client.get(f"{self.api_url}/spool")
  129. response.raise_for_status()
  130. spools = response.json()
  131. if attempt > 1:
  132. logger.info("Successfully fetched %d spools on attempt %d", len(spools), attempt)
  133. return spools
  134. except (httpx.ReadError, httpx.RemoteProtocolError, httpx.ConnectError) as e:
  135. # Connection-related errors - close and recreate client for next attempt
  136. if attempt < max_attempts:
  137. logger.warning(
  138. "Connection error getting spools (attempt %d/%d): %s. Recreating client and retrying in %dms...",
  139. attempt,
  140. max_attempts,
  141. e,
  142. int(retry_delay * 1000),
  143. )
  144. # Close the stale client and recreate it
  145. await self.close()
  146. await asyncio.sleep(retry_delay)
  147. else:
  148. logger.error("Failed to get spools from Spoolman after %d attempts: %s", max_attempts, e)
  149. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  150. except Exception as e:
  151. # Other errors (HTTP errors, JSON decode errors, etc.)
  152. if attempt < max_attempts:
  153. logger.warning(
  154. "Failed to get spools from Spoolman (attempt %d/%d): %s. Retrying in %dms...",
  155. attempt,
  156. max_attempts,
  157. e,
  158. int(retry_delay * 1000),
  159. )
  160. await asyncio.sleep(retry_delay)
  161. else:
  162. logger.error("Failed to get spools from Spoolman after %d attempts: %s", max_attempts, e)
  163. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  164. async def _get_with_retry(self, path: str, params: dict | None = None) -> list[dict]:
  165. """GET a Spoolman JSON list endpoint with up to 3 retries on connection errors."""
  166. max_attempts = 3
  167. retry_delay = 0.5
  168. url = f"{self.api_url}/{path.lstrip('/')}"
  169. for attempt in range(1, max_attempts + 1):
  170. try:
  171. client = await self._get_client()
  172. response = await client.get(url, params=params or None)
  173. response.raise_for_status()
  174. return response.json()
  175. except (httpx.ReadError, httpx.RemoteProtocolError, httpx.ConnectError) as e:
  176. if attempt < max_attempts:
  177. logger.warning(
  178. "Connection error fetching %s (attempt %d/%d): %s. Recreating client and retrying in %dms...",
  179. path,
  180. attempt,
  181. max_attempts,
  182. e,
  183. int(retry_delay * 1000),
  184. )
  185. await self.close()
  186. await asyncio.sleep(retry_delay)
  187. else:
  188. logger.error("Failed to fetch %s from Spoolman after %d attempts: %s", path, max_attempts, e)
  189. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  190. except Exception as e:
  191. if attempt < max_attempts:
  192. logger.warning(
  193. "Failed to fetch %s from Spoolman (attempt %d/%d): %s. Retrying in %dms...",
  194. path,
  195. attempt,
  196. max_attempts,
  197. e,
  198. int(retry_delay * 1000),
  199. )
  200. await asyncio.sleep(retry_delay)
  201. else:
  202. logger.error("Failed to fetch %s from Spoolman after %d attempts: %s", path, max_attempts, e)
  203. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  204. async def get_filaments(self) -> list[dict]:
  205. """Fetch all internal filaments from Spoolman."""
  206. try:
  207. client = await self._get_client()
  208. response = await client.get(f"{self.api_url}/filament")
  209. response.raise_for_status()
  210. return response.json()
  211. except Exception as e:
  212. logger.error("Failed to get filaments from Spoolman: %s", e)
  213. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  214. async def get_filament(self, filament_id: int) -> dict:
  215. """Fetch a single filament by ID from Spoolman."""
  216. if filament_id <= 0:
  217. raise ValueError(f"Invalid filament_id: {filament_id}")
  218. response = await self._request_filament("GET", filament_id, operation="get_filament")
  219. return response.json()
  220. async def get_external_filaments(self) -> list[dict]:
  221. """Fetch external/library filaments from Spoolman."""
  222. try:
  223. client = await self._get_client()
  224. response = await client.get(f"{self.api_url}/external/filament")
  225. response.raise_for_status()
  226. return response.json()
  227. except Exception as e:
  228. logger.error("Failed to get external filaments from Spoolman: %s", e)
  229. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  230. async def get_vendors(self) -> list[dict]:
  231. """Fetch all vendors from Spoolman."""
  232. try:
  233. client = await self._get_client()
  234. response = await client.get(f"{self.api_url}/vendor")
  235. response.raise_for_status()
  236. return response.json()
  237. except Exception as e:
  238. logger.error("Failed to get vendors from Spoolman: %s", e)
  239. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  240. async def create_vendor(self, name: str) -> dict:
  241. """Create a new vendor in Spoolman."""
  242. try:
  243. client = await self._get_client()
  244. response = await client.post(f"{self.api_url}/vendor", json={"name": name})
  245. if 400 <= response.status_code < 500:
  246. raise SpoolmanClientError(
  247. f"Spoolman rejected vendor creation (HTTP {response.status_code})",
  248. response.status_code,
  249. )
  250. response.raise_for_status()
  251. return response.json()
  252. except SpoolmanClientError:
  253. raise
  254. except Exception as e:
  255. logger.error("Failed to create vendor in Spoolman: %s", e)
  256. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  257. def _get_material_density(self, material: str | None) -> float:
  258. """Return typical density (g/cm³) for the given filament material; defaults to PLA (1.24)."""
  259. # Typical densities for common filament materials
  260. densities = {
  261. "PLA": 1.24,
  262. "PLA-CF": 1.29,
  263. "PLA-S": 1.24,
  264. "PETG": 1.27,
  265. "ABS": 1.04,
  266. "ASA": 1.07,
  267. "TPU": 1.21,
  268. "PA": 1.14, # Nylon
  269. "PA-CF": 1.20,
  270. "PC": 1.20,
  271. "PVA": 1.23,
  272. "HIPS": 1.04,
  273. "PP": 0.90,
  274. "PET": 1.38,
  275. }
  276. if material:
  277. # Try exact match first, then uppercase
  278. mat_upper = material.upper()
  279. for key, density in densities.items():
  280. if key.upper() == mat_upper or mat_upper.startswith(key.upper()):
  281. return density
  282. return 1.24 # Default to PLA density
  283. async def create_filament(
  284. self,
  285. name: str,
  286. vendor_id: int | None = None,
  287. material: str | None = None,
  288. color_hex: str | None = None,
  289. color_name: str | None = None,
  290. weight: float | None = None,
  291. diameter: float = 1.75,
  292. density: float | None = None,
  293. ) -> dict:
  294. """Create a new filament in Spoolman."""
  295. if not name or not name.strip():
  296. raise ValueError("Filament name is required")
  297. if density is None:
  298. density = self._get_material_density(material)
  299. data: dict = {
  300. "name": name.strip(),
  301. "diameter": diameter,
  302. "density": density,
  303. }
  304. if vendor_id:
  305. data["vendor_id"] = vendor_id
  306. if material:
  307. data["material"] = material
  308. if color_hex:
  309. # Strip alpha channel if present (RRGGBBAA -> RRGGBB)
  310. color_hex = color_hex[:6] if len(color_hex) >= 6 else color_hex
  311. data["color_hex"] = color_hex
  312. if color_name:
  313. data["color_name"] = color_name
  314. if weight:
  315. data["weight"] = weight
  316. logger.debug("Creating filament in Spoolman: %s", data)
  317. try:
  318. client = await self._get_client()
  319. response = await client.post(f"{self.api_url}/filament", json=data)
  320. if 400 <= response.status_code < 500:
  321. raise SpoolmanClientError(
  322. f"Spoolman rejected filament creation (HTTP {response.status_code})",
  323. response.status_code,
  324. )
  325. response.raise_for_status()
  326. return response.json()
  327. except SpoolmanClientError:
  328. raise
  329. except Exception as e:
  330. logger.error("Failed to create filament in Spoolman: %s", e)
  331. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  332. async def patch_filament(self, filament_id: int, data: dict) -> dict:
  333. """PATCH a filament entry in Spoolman (e.g. update name or spool_weight)."""
  334. if filament_id <= 0:
  335. raise ValueError(f"Invalid filament_id: {filament_id}")
  336. response = await self._request_filament("PATCH", filament_id, json_body=data, operation="patch_filament")
  337. return response.json()
  338. async def create_spool(
  339. self,
  340. filament_id: int,
  341. remaining_weight: float | None = None,
  342. location: str | None = None,
  343. lot_nr: str | None = None,
  344. comment: str | None = None,
  345. extra: dict | None = None,
  346. ) -> dict:
  347. """Create a new spool in Spoolman."""
  348. data: dict = {"filament_id": filament_id}
  349. if remaining_weight is not None:
  350. data["remaining_weight"] = remaining_weight
  351. if location:
  352. data["location"] = location
  353. if lot_nr:
  354. data["lot_nr"] = lot_nr
  355. if comment:
  356. data["comment"] = comment
  357. if extra:
  358. data["extra"] = extra
  359. logger.debug("Creating spool in Spoolman: %s", data)
  360. try:
  361. client = await self._get_client()
  362. response = await client.post(f"{self.api_url}/spool", json=data)
  363. if response.status_code == 404:
  364. raise SpoolmanNotFoundError(f"Filament {filament_id} not found in Spoolman")
  365. if 400 <= response.status_code < 500:
  366. raise SpoolmanClientError(
  367. f"Spoolman rejected spool creation (HTTP {response.status_code})",
  368. response.status_code,
  369. )
  370. response.raise_for_status()
  371. result = response.json()
  372. logger.info("Created spool %s in Spoolman", result.get("id"))
  373. return result
  374. except (SpoolmanNotFoundError, SpoolmanClientError):
  375. raise
  376. except Exception as e:
  377. logger.error("Failed to create spool in Spoolman: %s", e)
  378. raise SpoolmanUnavailableError("Cannot reach Spoolman") from e
  379. async def update_spool(
  380. self,
  381. spool_id: int,
  382. remaining_weight: float | None = None,
  383. location: str | None = None,
  384. clear_location: bool = False,
  385. extra: dict | None = None,
  386. ) -> dict:
  387. """Update an existing spool in Spoolman, always setting last_used."""
  388. data: dict = {}
  389. if remaining_weight is not None:
  390. data["remaining_weight"] = remaining_weight
  391. if clear_location:
  392. data["location"] = None
  393. elif location:
  394. data["location"] = location
  395. if extra:
  396. data["extra"] = extra
  397. data["last_used"] = datetime.now(timezone.utc).isoformat()
  398. response = await self._request_spool("PATCH", spool_id, json_body=data, operation="update")
  399. return response.json()
  400. async def _request_spool(
  401. self,
  402. method: Literal["GET", "PATCH", "DELETE"],
  403. spool_id: int,
  404. *,
  405. json_body: dict | None = None,
  406. operation: str,
  407. ) -> httpx.Response:
  408. """Perform a spool-scoped HTTP request, translating 404 and errors to named exceptions."""
  409. try:
  410. client = await self._get_client()
  411. response = await client.request(
  412. method,
  413. f"{self.api_url}/spool/{spool_id}",
  414. json=json_body,
  415. )
  416. if response.status_code == 404:
  417. raise SpoolmanNotFoundError(f"Spool {spool_id} not found in Spoolman")
  418. response.raise_for_status()
  419. return response
  420. except SpoolmanNotFoundError:
  421. raise
  422. except httpx.HTTPStatusError as e:
  423. if 400 <= e.response.status_code < 500:
  424. logger.warning(
  425. "Spoolman returned %d for %s spool %s",
  426. e.response.status_code,
  427. operation,
  428. spool_id,
  429. )
  430. raise SpoolmanClientError(
  431. f"Spoolman rejected {operation} for spool {spool_id} (HTTP {e.response.status_code})",
  432. e.response.status_code,
  433. e.response.text[:500],
  434. ) from e
  435. else:
  436. logger.error("Failed to %s spool %s in Spoolman: %s", operation, spool_id, e)
  437. raise SpoolmanUnavailableError(f"Failed to {operation} spool {spool_id}") from e
  438. except Exception as e:
  439. logger.error("Failed to %s spool %s in Spoolman: %s", operation, spool_id, e)
  440. raise SpoolmanUnavailableError(f"Failed to {operation} spool {spool_id}") from e
  441. async def _request_filament(
  442. self,
  443. method: Literal["GET", "PATCH"],
  444. filament_id: int,
  445. *,
  446. json_body: dict | None = None,
  447. operation: str,
  448. ) -> httpx.Response:
  449. """Perform a filament-scoped HTTP request, translating 404 and errors to named exceptions."""
  450. try:
  451. client = await self._get_client()
  452. response = await client.request(
  453. method,
  454. f"{self.api_url}/filament/{filament_id}",
  455. json=json_body,
  456. )
  457. if response.status_code == 404:
  458. raise SpoolmanNotFoundError(f"Filament {filament_id} not found in Spoolman")
  459. response.raise_for_status()
  460. return response
  461. except SpoolmanNotFoundError:
  462. raise
  463. except httpx.HTTPStatusError as e:
  464. if 400 <= e.response.status_code < 500:
  465. logger.warning(
  466. "Spoolman returned %d for %s filament %s",
  467. e.response.status_code,
  468. operation,
  469. filament_id,
  470. )
  471. raise SpoolmanClientError(
  472. f"Spoolman rejected {operation} for filament {filament_id} (HTTP {e.response.status_code})",
  473. e.response.status_code,
  474. e.response.text[:500],
  475. ) from e
  476. else:
  477. logger.error("Failed to %s filament %s in Spoolman: %s", operation, filament_id, e)
  478. raise SpoolmanUnavailableError(f"Failed to {operation} filament {filament_id}") from e
  479. except Exception as e:
  480. logger.error("Failed to %s filament %s in Spoolman: %s", operation, filament_id, e)
  481. raise SpoolmanUnavailableError(f"Failed to {operation} filament {filament_id}") from e
  482. async def get_spool(self, spool_id: int) -> dict:
  483. """Fetch a single spool by ID from Spoolman."""
  484. response = await self._request_spool("GET", spool_id, operation="get")
  485. return response.json()
  486. async def get_all_spools(self, allow_archived: bool = False) -> list[dict]:
  487. """Fetch all spools from Spoolman with retry, optionally including archived ones."""
  488. params: dict = {}
  489. if allow_archived:
  490. params["allow_archived"] = "true"
  491. return await self._get_with_retry("/spool", params=params or None)
  492. async def delete_spool(self, spool_id: int) -> None:
  493. """Delete a spool from Spoolman."""
  494. await self._request_spool("DELETE", spool_id, operation="delete")
  495. async def is_filament_shared(self, filament_id: int, exclude_spool_id: int) -> bool:
  496. """True if any spool other than ``exclude_spool_id`` is linked to ``filament_id``.
  497. Used by the spool-edit path to decide between PATCHing the existing
  498. filament in place (singleton) and falling back to find_or_create
  499. (shared — re-linking the spool is the only safe option). Includes
  500. archived spools so a shared link doesn't suddenly look singleton just
  501. because the sibling spool was archived.
  502. """
  503. spools = await self.get_all_spools(allow_archived=True)
  504. for s in spools:
  505. if s.get("id") == exclude_spool_id:
  506. continue
  507. if ((s.get("filament") or {}).get("id")) == filament_id:
  508. return True
  509. return False
  510. async def set_spool_archived(self, spool_id: int, archived: bool) -> dict:
  511. """Archive or restore a spool in Spoolman."""
  512. response = await self._request_spool(
  513. "PATCH",
  514. spool_id,
  515. json_body={"archived": archived},
  516. operation="archive/restore",
  517. )
  518. return response.json()
  519. async def reset_spool_usage(self, spool_id: int) -> dict:
  520. """Reset a spool's used_weight to 0 in Spoolman.
  521. Used by the per-spool / bulk "Reset usage to 0" actions on the
  522. Inventory page so the Total Consumed stat can be cleared without
  523. touching the rest of the spool's data.
  524. """
  525. response = await self._request_spool(
  526. "PATCH",
  527. spool_id,
  528. json_body={"used_weight": 0},
  529. operation="reset-usage",
  530. )
  531. return response.json()
  532. async def update_spool_full(
  533. self,
  534. spool_id: int,
  535. *,
  536. filament_id: int | None = None,
  537. remaining_weight: float | None = None,
  538. comment: str | None = None,
  539. price: float | None = None,
  540. location: str | None = None,
  541. clear_location: bool = False,
  542. extra: dict | None = None,
  543. spool_weight: float | None = None,
  544. clear_spool_weight: bool = False,
  545. ) -> dict:
  546. """Update a spool with full field support; unlike update_spool, does not auto-set last_used."""
  547. data: dict = {}
  548. if filament_id is not None:
  549. data["filament_id"] = filament_id
  550. if remaining_weight is not None:
  551. data["remaining_weight"] = remaining_weight
  552. if comment is not None:
  553. data["comment"] = comment if comment else None
  554. if price is not None:
  555. data["price"] = price
  556. if clear_location:
  557. data["location"] = None
  558. elif location is not None:
  559. data["location"] = location
  560. if extra is not None:
  561. data["extra"] = extra
  562. if clear_spool_weight:
  563. data["spool_weight"] = None
  564. elif spool_weight is not None:
  565. data["spool_weight"] = spool_weight
  566. response = await self._request_spool("PATCH", spool_id, json_body=data, operation="update")
  567. return response.json()
  568. def extra_lock(self, spool_id: int) -> asyncio.Lock:
  569. """Return (creating if needed) the per-spool asyncio.Lock used by merge_spool_extra."""
  570. lock = self._extra_locks.get(spool_id)
  571. if lock is None:
  572. lock = asyncio.Lock()
  573. self._extra_locks[spool_id] = lock
  574. return lock
  575. async def merge_spool_extra(self, spool_id: int, new_fields: dict) -> dict:
  576. """Fetch the spool's extra dict, merge new_fields into it, then PATCH back — serialised per spool."""
  577. async with self.extra_lock(spool_id):
  578. current = await self.get_spool(spool_id) # raises on error
  579. current_extra: dict = current.get("extra") or {}
  580. merged = {**current_extra, **new_fields}
  581. return await self.update_spool_full(spool_id=spool_id, extra=merged)
  582. async def find_or_create_vendor(self, name: str) -> int:
  583. """Return the Spoolman vendor ID for the given name, creating the vendor if absent."""
  584. vendors = await self.get_vendors()
  585. name_lower = name.strip().lower()
  586. for vendor in vendors:
  587. if vendor.get("name", "").strip().lower() == name_lower:
  588. return vendor["id"]
  589. created = await self.create_vendor(name.strip())
  590. vendor_id = created.get("id")
  591. if not vendor_id:
  592. raise SpoolmanUnavailableError(f"Spoolman returned vendor without id field: {list(created.keys())}")
  593. return vendor_id
  594. async def find_or_create_filament(
  595. self,
  596. material: str,
  597. subtype: str,
  598. brand: str | None,
  599. color_hex: str,
  600. label_weight: int,
  601. color_name: str | None = None,
  602. ) -> int:
  603. """Return the filament ID matching material/name/brand/color, creating it if absent."""
  604. name = f"{material} {subtype}".strip() if subtype else material
  605. color = color_hex[:6].upper() if len(color_hex) >= 6 else color_hex.upper()
  606. vendor_id: int | None = None
  607. if brand:
  608. vendor_id = await self.find_or_create_vendor(brand)
  609. # Normalised match keys (case-insensitive). Computed once outside the
  610. # loop so the inner comparison stays simple.
  611. composed_subtype = _filament_subtype_part(name, material)
  612. material_norm = material.upper()
  613. brand_norm = (brand or "").strip().lower()
  614. filaments = await self.get_filaments()
  615. for f in filaments:
  616. f_material = (f.get("material") or "").upper()
  617. f_color = (f.get("color_hex") or "").upper()[:6]
  618. f_vendor = f.get("vendor") or {}
  619. f_vendor_name = (f_vendor.get("name") or "").strip().lower()
  620. material_match = f_material == material_norm
  621. # Match on the subtype portion of the filament name. AMS-sync
  622. # auto-create (the underscore-prefixed `_find_or_create_filament`
  623. # used during MQTT tray import) stores the filament as just
  624. # ``tray.tray_sub_brands`` — e.g. ``"Glow"`` — while the
  625. # user-driven edit path here composes ``"<material> <subtype>"``
  626. # — ``"PLA Glow"``. The old literal equality `f_name == name`
  627. # failed to bridge the two shapes, so every edit fell through to
  628. # `create_filament`, leaving a trail of duplicate filaments AND
  629. # leaving the spool either still pointed at the old filament
  630. # whose `color_name` never got patched, or pointed at a new
  631. # filament with the colour while the inventory list kept
  632. # showing the synth fallback from the old one (#1357).
  633. f_subtype_part = _filament_subtype_part(f.get("name") or "", material)
  634. name_match = f_subtype_part == composed_subtype
  635. color_match = f_color == color
  636. vendor_match = (not brand) or f_vendor_name == brand_norm
  637. if material_match and name_match and color_match and vendor_match:
  638. # color_name is intentionally not part of the match key and
  639. # is no longer patched onto the filament here: Spoolman 0.23.1
  640. # has no `color_name` field on Filament (#1357 — confirmed
  641. # against the FilamentUpdateParameters schema). The earlier
  642. # #1319 fix tried to patch it and Spoolman silently dropped
  643. # the key, which is exactly why the user's edit looked "not
  644. # saved". The route now persists color_name via
  645. # spool.extra.bambu_color_name (see _map_spoolman_spool for
  646. # the read side); find_or_create_filament's only job is to
  647. # resolve the right filament_id for the spool link.
  648. return f["id"]
  649. # color_name omitted: Spoolman has no such field on Filament (#1357);
  650. # the user's color_name lands in spool.extra.bambu_color_name via the
  651. # route after find_or_create_filament returns the new id.
  652. filament = await self.create_filament(
  653. name=name,
  654. vendor_id=vendor_id,
  655. material=material,
  656. color_hex=color,
  657. weight=float(label_weight),
  658. )
  659. filament_id = filament.get("id")
  660. if not filament_id:
  661. raise SpoolmanUnavailableError(f"Spoolman returned filament without id field: {list(filament.keys())}")
  662. return filament_id
  663. async def use_spool(self, spool_id: int, used_weight: float) -> dict:
  664. """Record filament usage for a spool via the Spoolman /use endpoint."""
  665. try:
  666. client = await self._get_client()
  667. response = await client.put(
  668. f"{self.api_url}/spool/{spool_id}/use",
  669. json={"use_weight": used_weight},
  670. )
  671. if response.status_code == 404:
  672. raise SpoolmanNotFoundError(f"Spool {spool_id} not found in Spoolman")
  673. if 400 <= response.status_code < 500:
  674. raise SpoolmanClientError(
  675. f"Spoolman rejected use_spool for spool {spool_id} (HTTP {response.status_code})",
  676. response.status_code,
  677. )
  678. response.raise_for_status()
  679. return response.json()
  680. except (SpoolmanNotFoundError, SpoolmanClientError):
  681. raise
  682. except Exception as e:
  683. logger.error("Failed to record spool usage in Spoolman: %s", e)
  684. raise SpoolmanUnavailableError(f"Failed to record usage for spool {spool_id}") from e
  685. async def find_spool_by_tag(self, tag_uid: str, cached_spools: list[dict] | None = None) -> dict | None:
  686. """Return the spool matching the given RFID tag UID, or None if not found."""
  687. # Use cached spools if provided, otherwise fetch from API
  688. spools = cached_spools if cached_spools is not None else await self.get_spools()
  689. # Normalize tag_uid for comparison (uppercase, strip quotes)
  690. search_tag = tag_uid.strip('"').upper()
  691. for spool in spools:
  692. extra = spool.get("extra", {})
  693. if extra:
  694. stored_tag = extra.get("tag", "")
  695. # Normalize stored tag (strip quotes, uppercase)
  696. if stored_tag:
  697. normalized_tag = stored_tag.strip('"').upper()
  698. if normalized_tag == search_tag:
  699. logger.debug("Found spool %s matching tag %s", spool["id"], tag_uid)
  700. return spool
  701. return None
  702. def _find_spool_by_location(self, location: str, cached_spools: list[dict] | None) -> dict | None:
  703. """Return the spool at the exact location string, or None; fallback when RFID is unavailable."""
  704. if not cached_spools:
  705. return None
  706. for spool in cached_spools:
  707. if spool.get("location") == location:
  708. return spool
  709. return None
  710. async def find_spools_by_location_prefix(
  711. self, location_prefix: str, cached_spools: list[dict] | None = None
  712. ) -> list[dict]:
  713. """Return all spools whose location starts with location_prefix."""
  714. # Use cached spools if provided, otherwise fetch from API
  715. spools = cached_spools if cached_spools is not None else await self.get_spools()
  716. matching = []
  717. for spool in spools:
  718. location = spool.get("location", "")
  719. if location and location.startswith(location_prefix):
  720. matching.append(spool)
  721. return matching
  722. async def clear_location_for_removed_spools(
  723. self,
  724. printer_name: str,
  725. current_tray_uuids: set[str],
  726. cached_spools: list[dict] | None = None,
  727. synced_spool_ids: set[int] | None = None,
  728. ) -> int:
  729. """Clear location for Bambu Lab spools at this printer whose tray_uuid is no longer in the AMS."""
  730. location_prefix = f"{printer_name} - "
  731. spools_at_printer = await self.find_spools_by_location_prefix(location_prefix, cached_spools=cached_spools)
  732. cleared_count = 0
  733. for spool in spools_at_printer:
  734. spool_id = spool.get("id")
  735. # Skip spools that were just synced (matched by location or tag)
  736. if synced_spool_ids and spool_id in synced_spool_ids:
  737. continue
  738. # Get the tray_uuid (stored as "tag" in extra field)
  739. extra = spool.get("extra", {}) or {}
  740. stored_tag = extra.get("tag", "")
  741. if stored_tag:
  742. # Normalize: strip quotes and uppercase
  743. spool_uuid = stored_tag.strip('"').upper()
  744. else:
  745. spool_uuid = ""
  746. # Only clear location for Bambu Lab spools (those with a stored 32-character RFID tag).
  747. if len(spool_uuid) != BAMBU_RFID_TAG_LENGTH:
  748. continue
  749. # If this spool's UUID is not in the current AMS, clear its location
  750. if spool_uuid not in current_tray_uuids:
  751. logger.info(
  752. f"Clearing location for spool {spool_id} "
  753. f"(was: {spool.get('location')}, uuid: {spool_uuid[:16] if spool_uuid else 'none'}...)"
  754. )
  755. result = await self.update_spool(spool_id=spool_id, clear_location=True)
  756. if result:
  757. cleared_count += 1
  758. return cleared_count
  759. async def ensure_bambu_vendor(self) -> int | None:
  760. """Return the Bambu Lab vendor ID in Spoolman, creating the vendor if absent."""
  761. vendors = await self.get_vendors()
  762. for vendor in vendors:
  763. if vendor.get("name", "").lower() == "bambu lab":
  764. return vendor["id"]
  765. # Create Bambu Lab vendor if not exists
  766. vendor = await self.create_vendor("Bambu Lab")
  767. return vendor["id"] if vendor else None
  768. async def ensure_tag_extra_field(self) -> bool:
  769. """Register the 'tag' extra field in Spoolman if not present; returns True on success."""
  770. return await self.ensure_extra_field("tag")
  771. async def ensure_extra_field(self, name: str, field_type: str = "text") -> bool:
  772. """Register a custom extra field in Spoolman if not present.
  773. Spoolman rejects PATCH requests that include unknown extra-dict keys
  774. with HTTP 400 ('Unknown extra field <name>.'), so any custom field
  775. Bambuddy persists alongside spools needs to be pre-registered.
  776. Idempotent — returns True if the field already exists.
  777. """
  778. try:
  779. client = await self._get_client()
  780. # Check if field already exists
  781. response = await client.get(f"{self.api_url}/field/spool/{name}")
  782. if response.status_code == 200:
  783. logger.debug("Spoolman extra field %r already exists", name)
  784. return True
  785. # Field doesn't exist - create it
  786. field_data = {
  787. "name": name,
  788. "field_type": field_type,
  789. "default_value": None,
  790. }
  791. response = await client.post(f"{self.api_url}/field/spool/{name}", json=field_data)
  792. if response.status_code in (200, 201):
  793. logger.info("Created Spoolman extra field %r", name)
  794. return True
  795. logger.warning(
  796. "Failed to create Spoolman extra field %r: %s - %s",
  797. name,
  798. response.status_code,
  799. response.text,
  800. )
  801. return False
  802. except Exception as e:
  803. logger.warning("Failed to ensure Spoolman extra field %r exists: %s", name, e)
  804. return False
  805. def parse_ams_tray(self, ams_id: int, tray_data: dict) -> AMSTray | None:
  806. """Parse raw MQTT tray data into an AMSTray; returns None for empty or invalid trays."""
  807. # Skip empty trays - check for valid tray_type
  808. tray_type = tray_data.get("tray_type", "")
  809. if not tray_type or tray_type.strip() == "":
  810. return None
  811. # Need valid color to create filament
  812. tray_color = tray_data.get("tray_color", "")
  813. if not tray_color or tray_color.strip() == "":
  814. logger.debug("Skipping tray with empty color")
  815. return None
  816. # Handle transparent/natural filament (RRGGBBAA with alpha=00)
  817. # Replace with cream color that represents how natural PLA actually looks
  818. if tray_color == "00000000":
  819. tray_color = "F5E6D3FF" # Light cream/natural color
  820. # Get sub_brands, falling back to tray_type
  821. tray_sub_brands = tray_data.get("tray_sub_brands", "")
  822. if not tray_sub_brands or tray_sub_brands.strip() == "":
  823. tray_sub_brands = tray_type
  824. # Get tag_uid and tray_uuid, filtering out empty/invalid values
  825. tag_uid = tray_data.get("tag_uid", "")
  826. if tag_uid in ("", "0000000000000000"):
  827. tag_uid = ""
  828. tray_uuid = tray_data.get("tray_uuid", "")
  829. if tray_uuid in ("", "00000000000000000000000000000000"):
  830. tray_uuid = ""
  831. # Get tray_info_idx (Bambu filament preset ID like "GFA00")
  832. tray_info_idx = tray_data.get("tray_info_idx", "") or ""
  833. # Get remaining percentage (-1 means unknown/not read by AMS)
  834. remain = int(tray_data.get("remain", -1))
  835. return AMSTray(
  836. ams_id=ams_id,
  837. tray_id=int(tray_data.get("id", 0)),
  838. tray_type=tray_type.strip(),
  839. tray_sub_brands=tray_sub_brands.strip(),
  840. tray_color=tray_color,
  841. remain=remain,
  842. tag_uid=tag_uid,
  843. tray_uuid=tray_uuid,
  844. tray_info_idx=tray_info_idx.strip(),
  845. tray_weight=int(tray_data.get("tray_weight", 1000)),
  846. )
  847. def convert_ams_slot_to_location(self, ams_id: int, tray_id: int) -> str:
  848. """Return a human-readable location string (e.g. "AMS A1") for the given AMS slot."""
  849. if ams_id >= 254:
  850. return "External Spool"
  851. if 128 <= ams_id <= 135:
  852. # AMS-HT units use IDs 128-135
  853. ht_letter = chr(ord("A") + (ams_id - 128))
  854. return f"AMS-HT {ht_letter}{tray_id + 1}"
  855. ams_letter = chr(ord("A") + ams_id)
  856. return f"AMS {ams_letter}{tray_id + 1}"
  857. def is_bambu_lab_spool(self, tray_uuid: str, tag_uid: str = "", tray_info_idx: str = "") -> bool:
  858. """Return True if tray_uuid or tag_uid identifies a Bambu Lab spool; tray_info_idx is ignored."""
  859. # Check tray_uuid (preferred - consistent across printer models)
  860. if tray_uuid:
  861. uuid = tray_uuid.strip()
  862. if len(uuid) == 32 and uuid != "00000000000000000000000000000000":
  863. try:
  864. int(uuid, 16)
  865. return True
  866. except ValueError:
  867. pass
  868. # Fallback: check tag_uid (RFID tag - varies between printer readers)
  869. # Bambu Lab RFID tags are 16 hex characters (8 bytes)
  870. if tag_uid:
  871. tag = tag_uid.strip()
  872. if len(tag) == 16 and tag != "0000000000000000":
  873. try:
  874. int(tag, 16)
  875. logger.debug("Identified Bambu Lab spool via tag_uid fallback: %s", tag)
  876. return True
  877. except ValueError:
  878. pass
  879. return False
  880. def calculate_remaining_weight(self, remain_percent: int, spool_weight: int) -> float:
  881. """Return remaining filament weight in grams given a percentage and total spool weight."""
  882. return (remain_percent / 100.0) * spool_weight
  883. async def sync_ams_tray(
  884. self,
  885. tray: AMSTray,
  886. printer_name: str,
  887. disable_weight_sync: bool = False,
  888. cached_spools: list[dict] | None = None,
  889. inventory_remaining: float | None = None,
  890. spoolman_spool_id_hint: int | None = None,
  891. ) -> dict | None:
  892. """Sync one AMS tray to Spoolman; creates the spool on first sight, updates weight otherwise."""
  893. logger.debug(
  894. f"Processing {printer_name} AMS {tray.ams_id} tray {tray.tray_id}: "
  895. f"type={tray.tray_type}, idx={tray.tray_info_idx or 'none'}, "
  896. f"uuid={tray.tray_uuid[:16] if tray.tray_uuid else 'none'}, "
  897. f"tag={tray.tag_uid[:8] if tray.tag_uid else 'none'}..."
  898. )
  899. # Determine which identifier to use for Spoolman (prefer tray_uuid, fallback to tag_uid)
  900. # Zero-filled values mean the AMS hasn't read the RFID tag — treat as no tag
  901. zero_uuid = "00000000000000000000000000000000"
  902. zero_tag = "0000000000000000"
  903. spool_tag = None
  904. if tray.tray_uuid and tray.tray_uuid != zero_uuid:
  905. spool_tag = tray.tray_uuid
  906. elif tray.tag_uid and tray.tag_uid != zero_tag:
  907. spool_tag = tray.tag_uid
  908. # Calculate remaining weight
  909. # Primary: AMS MQTT data (remain percentage + tray_weight)
  910. # Fallback: Built-in inventory tracked weight (when firmware sends invalid remain/tray_weight)
  911. if tray.remain >= 0 and tray.tray_weight > 0:
  912. remaining = self.calculate_remaining_weight(tray.remain, tray.tray_weight)
  913. elif inventory_remaining is not None:
  914. remaining = inventory_remaining
  915. logger.debug(
  916. "Using inventory weight fallback for %s AMS %s tray %s: %.1fg",
  917. printer_name,
  918. tray.ams_id,
  919. tray.tray_id,
  920. remaining,
  921. )
  922. else:
  923. remaining = None
  924. if spool_tag:
  925. # Primary path: match by RFID tag
  926. existing = await self.find_spool_by_tag(spool_tag, cached_spools=cached_spools)
  927. if existing:
  928. logger.info("Updating existing spool %s for tag %s...", existing["id"], spool_tag[:16])
  929. return await self.update_spool(
  930. spool_id=existing["id"],
  931. remaining_weight=None if disable_weight_sync else remaining,
  932. )
  933. # Spool not found by tag - auto-create it
  934. logger.info("Creating new spool in Spoolman for %s (tag: %s...)", tray.tray_sub_brands, spool_tag[:16])
  935. if self.is_bambu_lab_spool(tray.tray_uuid, tray.tag_uid, tray.tray_info_idx):
  936. filament = await self._find_or_create_filament(tray)
  937. filament_id = filament["id"] if filament else None
  938. else:
  939. # Non-BL spool with custom RFID: use generic vendor lookup
  940. brand = tray.tray_sub_brands if tray.tray_sub_brands != tray.tray_type else None
  941. try:
  942. filament_id = await self.find_or_create_filament(
  943. material=tray.tray_type,
  944. subtype="",
  945. brand=brand,
  946. color_hex=tray.tray_color[:6],
  947. label_weight=tray.tray_weight,
  948. )
  949. except (SpoolmanNotFoundError, SpoolmanUnavailableError, SpoolmanClientError):
  950. logger.warning("Could not find or create filament for non-BL spool %s", tray.tray_sub_brands)
  951. return None
  952. if not filament_id:
  953. logger.error("Failed to find or create filament for %s", tray.tray_sub_brands)
  954. return None
  955. import json
  956. return await self.create_spool(
  957. filament_id=filament_id,
  958. remaining_weight=remaining,
  959. comment="Created by Bambuddy",
  960. extra={"tag": json.dumps(spool_tag)},
  961. )
  962. # No-RFID fallback: use the spool ID resolved from the local slot-assignment table.
  963. # Never create new spools without a tag to avoid duplicates.
  964. if spoolman_spool_id_hint is not None:
  965. existing = next((s for s in (cached_spools or []) if s.get("id") == spoolman_spool_id_hint), None)
  966. if existing is None:
  967. try:
  968. existing = await self.get_spool(spoolman_spool_id_hint)
  969. except (SpoolmanNotFoundError, SpoolmanUnavailableError):
  970. existing = None
  971. if existing:
  972. logger.info(
  973. "Updating spool %s by slot-assignment hint (no RFID tag available)",
  974. existing["id"],
  975. )
  976. return await self.update_spool(
  977. spool_id=existing["id"],
  978. remaining_weight=None if disable_weight_sync else remaining,
  979. )
  980. logger.info(
  981. "%s AMS %s tray %s — skipping (no RFID tag and no slot-assignment hint)",
  982. printer_name,
  983. tray.ams_id,
  984. tray.tray_id,
  985. )
  986. return None
  987. async def _find_or_create_filament(self, tray: AMSTray) -> dict | None:
  988. """Return a Bambu Lab filament matching the tray's material/color, creating it if absent."""
  989. bambu_vendor_id = await self.ensure_bambu_vendor()
  990. color_hex = tray.tray_color[:6] # Strip alpha channel
  991. material_upper = tray.tray_type.upper()
  992. color_upper = color_hex.upper()
  993. # Search internal filaments - only match Bambu Lab vendor
  994. filaments = await self.get_filaments()
  995. for filament in filaments:
  996. fil_vendor_id = filament.get("vendor_id") or filament.get("vendor", {}).get("id")
  997. if fil_vendor_id != bambu_vendor_id:
  998. continue
  999. fil_material = filament.get("material") or ""
  1000. fil_color = filament.get("color_hex") or ""
  1001. if fil_material.upper() == material_upper and fil_color.upper() == color_upper:
  1002. return filament
  1003. # Search external filaments (SpoolmanDB) — restrict to Bambu Lab only.
  1004. # The /api/v1/external/filament endpoint returns the full multi-vendor catalog
  1005. # with no server-side filter, so without a manufacturer check the first PLA/black
  1006. # hit is typically 3DJAKE or 3DXTECH, not Bambu Lab.
  1007. external = await self.get_external_filaments()
  1008. sub_brand = (tray.tray_sub_brands or "").strip().lower()
  1009. bambu_candidates = []
  1010. for filament in external:
  1011. manufacturer = (filament.get("manufacturer") or "").strip().lower()
  1012. ext_id = (filament.get("id") or "").strip().lower()
  1013. if manufacturer != "bambu lab" and not ext_id.startswith("bambulab_"):
  1014. continue
  1015. fil_material = filament.get("material") or ""
  1016. fil_color = filament.get("color_hex") or ""
  1017. if fil_material.upper() == material_upper and fil_color.upper() == color_upper:
  1018. bambu_candidates.append(filament)
  1019. if bambu_candidates:
  1020. # Prefer the entry whose `name` matches the AMS `tray_sub_brands`
  1021. # (e.g. "PLA Basic", "Support for PLA/PETG Black") so the more specific
  1022. # variant wins over a generic "Black" entry when both are present.
  1023. chosen = next(
  1024. (f for f in bambu_candidates if (f.get("name") or "").strip().lower() == sub_brand),
  1025. bambu_candidates[0],
  1026. )
  1027. return await self._create_filament_from_external(chosen, tray)
  1028. # Not found in either source - create a new Bambu Lab filament from scratch.
  1029. return await self.create_filament(
  1030. name=tray.tray_sub_brands or tray.tray_type,
  1031. vendor_id=bambu_vendor_id,
  1032. material=tray.tray_type,
  1033. color_hex=color_hex,
  1034. weight=tray.tray_weight,
  1035. )
  1036. async def _create_filament_from_external(self, external: dict, tray: AMSTray) -> dict | None:
  1037. """Create an internal Spoolman filament from an external library entry."""
  1038. vendor_id = await self.ensure_bambu_vendor()
  1039. return await self.create_filament(
  1040. name=external.get("name", tray.tray_sub_brands),
  1041. vendor_id=vendor_id,
  1042. material=external.get("material", tray.tray_type),
  1043. color_hex=external.get("color_hex", tray.tray_color[:6]),
  1044. weight=external.get("weight", tray.tray_weight),
  1045. density=external.get("density"),
  1046. )
  1047. # Global client instance (initialized when settings are loaded)
  1048. _spoolman_client: SpoolmanClient | None = None
  1049. async def get_spoolman_client() -> SpoolmanClient | None:
  1050. """Return the global SpoolmanClient, or None if not configured."""
  1051. return _spoolman_client
  1052. async def init_spoolman_client(url: str) -> SpoolmanClient:
  1053. """Initialise (or reinitialise) the global SpoolmanClient; raises ValueError if url fails SSRF guard."""
  1054. from backend.app.api.routes._spoolman_helpers import assert_safe_spoolman_url
  1055. assert_safe_spoolman_url(url)
  1056. global _spoolman_client
  1057. if _spoolman_client:
  1058. await _spoolman_client.close()
  1059. _spoolman_client = SpoolmanClient(url)
  1060. return _spoolman_client
  1061. async def close_spoolman_client():
  1062. """Close the global Spoolman client."""
  1063. global _spoolman_client
  1064. if _spoolman_client:
  1065. await _spoolman_client.close()
  1066. _spoolman_client = None