spoolman.py 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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 set_spool_archived(self, spool_id: int, archived: bool) -> dict:
  496. """Archive or restore a spool in Spoolman."""
  497. response = await self._request_spool(
  498. "PATCH",
  499. spool_id,
  500. json_body={"archived": archived},
  501. operation="archive/restore",
  502. )
  503. return response.json()
  504. async def reset_spool_usage(self, spool_id: int) -> dict:
  505. """Reset a spool's used_weight to 0 in Spoolman.
  506. Used by the per-spool / bulk "Reset usage to 0" actions on the
  507. Inventory page so the Total Consumed stat can be cleared without
  508. touching the rest of the spool's data.
  509. """
  510. response = await self._request_spool(
  511. "PATCH",
  512. spool_id,
  513. json_body={"used_weight": 0},
  514. operation="reset-usage",
  515. )
  516. return response.json()
  517. async def update_spool_full(
  518. self,
  519. spool_id: int,
  520. *,
  521. filament_id: int | None = None,
  522. remaining_weight: float | None = None,
  523. comment: str | None = None,
  524. price: float | None = None,
  525. location: str | None = None,
  526. clear_location: bool = False,
  527. extra: dict | None = None,
  528. spool_weight: float | None = None,
  529. clear_spool_weight: bool = False,
  530. ) -> dict:
  531. """Update a spool with full field support; unlike update_spool, does not auto-set last_used."""
  532. data: dict = {}
  533. if filament_id is not None:
  534. data["filament_id"] = filament_id
  535. if remaining_weight is not None:
  536. data["remaining_weight"] = remaining_weight
  537. if comment is not None:
  538. data["comment"] = comment if comment else None
  539. if price is not None:
  540. data["price"] = price
  541. if clear_location:
  542. data["location"] = None
  543. elif location is not None:
  544. data["location"] = location
  545. if extra is not None:
  546. data["extra"] = extra
  547. if clear_spool_weight:
  548. data["spool_weight"] = None
  549. elif spool_weight is not None:
  550. data["spool_weight"] = spool_weight
  551. response = await self._request_spool("PATCH", spool_id, json_body=data, operation="update")
  552. return response.json()
  553. def extra_lock(self, spool_id: int) -> asyncio.Lock:
  554. """Return (creating if needed) the per-spool asyncio.Lock used by merge_spool_extra."""
  555. lock = self._extra_locks.get(spool_id)
  556. if lock is None:
  557. lock = asyncio.Lock()
  558. self._extra_locks[spool_id] = lock
  559. return lock
  560. async def merge_spool_extra(self, spool_id: int, new_fields: dict) -> dict:
  561. """Fetch the spool's extra dict, merge new_fields into it, then PATCH back — serialised per spool."""
  562. async with self.extra_lock(spool_id):
  563. current = await self.get_spool(spool_id) # raises on error
  564. current_extra: dict = current.get("extra") or {}
  565. merged = {**current_extra, **new_fields}
  566. return await self.update_spool_full(spool_id=spool_id, extra=merged)
  567. async def find_or_create_vendor(self, name: str) -> int:
  568. """Return the Spoolman vendor ID for the given name, creating the vendor if absent."""
  569. vendors = await self.get_vendors()
  570. name_lower = name.strip().lower()
  571. for vendor in vendors:
  572. if vendor.get("name", "").strip().lower() == name_lower:
  573. return vendor["id"]
  574. created = await self.create_vendor(name.strip())
  575. vendor_id = created.get("id")
  576. if not vendor_id:
  577. raise SpoolmanUnavailableError(f"Spoolman returned vendor without id field: {list(created.keys())}")
  578. return vendor_id
  579. async def find_or_create_filament(
  580. self,
  581. material: str,
  582. subtype: str,
  583. brand: str | None,
  584. color_hex: str,
  585. label_weight: int,
  586. color_name: str | None = None,
  587. ) -> int:
  588. """Return the filament ID matching material/name/brand/color, creating it if absent."""
  589. name = f"{material} {subtype}".strip() if subtype else material
  590. color = color_hex[:6].upper() if len(color_hex) >= 6 else color_hex.upper()
  591. vendor_id: int | None = None
  592. if brand:
  593. vendor_id = await self.find_or_create_vendor(brand)
  594. # Normalised match keys (case-insensitive). Computed once outside the
  595. # loop so the inner comparison stays simple.
  596. composed_subtype = _filament_subtype_part(name, material)
  597. material_norm = material.upper()
  598. brand_norm = (brand or "").strip().lower()
  599. filaments = await self.get_filaments()
  600. for f in filaments:
  601. f_material = (f.get("material") or "").upper()
  602. f_color = (f.get("color_hex") or "").upper()[:6]
  603. f_vendor = f.get("vendor") or {}
  604. f_vendor_name = (f_vendor.get("name") or "").strip().lower()
  605. material_match = f_material == material_norm
  606. # Match on the subtype portion of the filament name. AMS-sync
  607. # auto-create (the underscore-prefixed `_find_or_create_filament`
  608. # used during MQTT tray import) stores the filament as just
  609. # ``tray.tray_sub_brands`` — e.g. ``"Glow"`` — while the
  610. # user-driven edit path here composes ``"<material> <subtype>"``
  611. # — ``"PLA Glow"``. The old literal equality `f_name == name`
  612. # failed to bridge the two shapes, so every edit fell through to
  613. # `create_filament`, leaving a trail of duplicate filaments AND
  614. # leaving the spool either still pointed at the old filament
  615. # whose `color_name` never got patched, or pointed at a new
  616. # filament with the colour while the inventory list kept
  617. # showing the synth fallback from the old one (#1357).
  618. f_subtype_part = _filament_subtype_part(f.get("name") or "", material)
  619. name_match = f_subtype_part == composed_subtype
  620. color_match = f_color == color
  621. vendor_match = (not brand) or f_vendor_name == brand_norm
  622. if material_match and name_match and color_match and vendor_match:
  623. # color_name is intentionally not part of the match key and
  624. # is no longer patched onto the filament here: Spoolman 0.23.1
  625. # has no `color_name` field on Filament (#1357 — confirmed
  626. # against the FilamentUpdateParameters schema). The earlier
  627. # #1319 fix tried to patch it and Spoolman silently dropped
  628. # the key, which is exactly why the user's edit looked "not
  629. # saved". The route now persists color_name via
  630. # spool.extra.bambu_color_name (see _map_spoolman_spool for
  631. # the read side); find_or_create_filament's only job is to
  632. # resolve the right filament_id for the spool link.
  633. return f["id"]
  634. # color_name omitted: Spoolman has no such field on Filament (#1357);
  635. # the user's color_name lands in spool.extra.bambu_color_name via the
  636. # route after find_or_create_filament returns the new id.
  637. filament = await self.create_filament(
  638. name=name,
  639. vendor_id=vendor_id,
  640. material=material,
  641. color_hex=color,
  642. weight=float(label_weight),
  643. )
  644. filament_id = filament.get("id")
  645. if not filament_id:
  646. raise SpoolmanUnavailableError(f"Spoolman returned filament without id field: {list(filament.keys())}")
  647. return filament_id
  648. async def use_spool(self, spool_id: int, used_weight: float) -> dict:
  649. """Record filament usage for a spool via the Spoolman /use endpoint."""
  650. try:
  651. client = await self._get_client()
  652. response = await client.put(
  653. f"{self.api_url}/spool/{spool_id}/use",
  654. json={"use_weight": used_weight},
  655. )
  656. if response.status_code == 404:
  657. raise SpoolmanNotFoundError(f"Spool {spool_id} not found in Spoolman")
  658. if 400 <= response.status_code < 500:
  659. raise SpoolmanClientError(
  660. f"Spoolman rejected use_spool for spool {spool_id} (HTTP {response.status_code})",
  661. response.status_code,
  662. )
  663. response.raise_for_status()
  664. return response.json()
  665. except (SpoolmanNotFoundError, SpoolmanClientError):
  666. raise
  667. except Exception as e:
  668. logger.error("Failed to record spool usage in Spoolman: %s", e)
  669. raise SpoolmanUnavailableError(f"Failed to record usage for spool {spool_id}") from e
  670. async def find_spool_by_tag(self, tag_uid: str, cached_spools: list[dict] | None = None) -> dict | None:
  671. """Return the spool matching the given RFID tag UID, or None if not found."""
  672. # Use cached spools if provided, otherwise fetch from API
  673. spools = cached_spools if cached_spools is not None else await self.get_spools()
  674. # Normalize tag_uid for comparison (uppercase, strip quotes)
  675. search_tag = tag_uid.strip('"').upper()
  676. for spool in spools:
  677. extra = spool.get("extra", {})
  678. if extra:
  679. stored_tag = extra.get("tag", "")
  680. # Normalize stored tag (strip quotes, uppercase)
  681. if stored_tag:
  682. normalized_tag = stored_tag.strip('"').upper()
  683. if normalized_tag == search_tag:
  684. logger.debug("Found spool %s matching tag %s", spool["id"], tag_uid)
  685. return spool
  686. return None
  687. def _find_spool_by_location(self, location: str, cached_spools: list[dict] | None) -> dict | None:
  688. """Return the spool at the exact location string, or None; fallback when RFID is unavailable."""
  689. if not cached_spools:
  690. return None
  691. for spool in cached_spools:
  692. if spool.get("location") == location:
  693. return spool
  694. return None
  695. async def find_spools_by_location_prefix(
  696. self, location_prefix: str, cached_spools: list[dict] | None = None
  697. ) -> list[dict]:
  698. """Return all spools whose location starts with location_prefix."""
  699. # Use cached spools if provided, otherwise fetch from API
  700. spools = cached_spools if cached_spools is not None else await self.get_spools()
  701. matching = []
  702. for spool in spools:
  703. location = spool.get("location", "")
  704. if location and location.startswith(location_prefix):
  705. matching.append(spool)
  706. return matching
  707. async def clear_location_for_removed_spools(
  708. self,
  709. printer_name: str,
  710. current_tray_uuids: set[str],
  711. cached_spools: list[dict] | None = None,
  712. synced_spool_ids: set[int] | None = None,
  713. ) -> int:
  714. """Clear location for Bambu Lab spools at this printer whose tray_uuid is no longer in the AMS."""
  715. location_prefix = f"{printer_name} - "
  716. spools_at_printer = await self.find_spools_by_location_prefix(location_prefix, cached_spools=cached_spools)
  717. cleared_count = 0
  718. for spool in spools_at_printer:
  719. spool_id = spool.get("id")
  720. # Skip spools that were just synced (matched by location or tag)
  721. if synced_spool_ids and spool_id in synced_spool_ids:
  722. continue
  723. # Get the tray_uuid (stored as "tag" in extra field)
  724. extra = spool.get("extra", {}) or {}
  725. stored_tag = extra.get("tag", "")
  726. if stored_tag:
  727. # Normalize: strip quotes and uppercase
  728. spool_uuid = stored_tag.strip('"').upper()
  729. else:
  730. spool_uuid = ""
  731. # Only clear location for Bambu Lab spools (those with a stored 32-character RFID tag).
  732. if len(spool_uuid) != BAMBU_RFID_TAG_LENGTH:
  733. continue
  734. # If this spool's UUID is not in the current AMS, clear its location
  735. if spool_uuid not in current_tray_uuids:
  736. logger.info(
  737. f"Clearing location for spool {spool_id} "
  738. f"(was: {spool.get('location')}, uuid: {spool_uuid[:16] if spool_uuid else 'none'}...)"
  739. )
  740. result = await self.update_spool(spool_id=spool_id, clear_location=True)
  741. if result:
  742. cleared_count += 1
  743. return cleared_count
  744. async def ensure_bambu_vendor(self) -> int | None:
  745. """Return the Bambu Lab vendor ID in Spoolman, creating the vendor if absent."""
  746. vendors = await self.get_vendors()
  747. for vendor in vendors:
  748. if vendor.get("name", "").lower() == "bambu lab":
  749. return vendor["id"]
  750. # Create Bambu Lab vendor if not exists
  751. vendor = await self.create_vendor("Bambu Lab")
  752. return vendor["id"] if vendor else None
  753. async def ensure_tag_extra_field(self) -> bool:
  754. """Register the 'tag' extra field in Spoolman if not present; returns True on success."""
  755. return await self.ensure_extra_field("tag")
  756. async def ensure_extra_field(self, name: str, field_type: str = "text") -> bool:
  757. """Register a custom extra field in Spoolman if not present.
  758. Spoolman rejects PATCH requests that include unknown extra-dict keys
  759. with HTTP 400 ('Unknown extra field <name>.'), so any custom field
  760. Bambuddy persists alongside spools needs to be pre-registered.
  761. Idempotent — returns True if the field already exists.
  762. """
  763. try:
  764. client = await self._get_client()
  765. # Check if field already exists
  766. response = await client.get(f"{self.api_url}/field/spool/{name}")
  767. if response.status_code == 200:
  768. logger.debug("Spoolman extra field %r already exists", name)
  769. return True
  770. # Field doesn't exist - create it
  771. field_data = {
  772. "name": name,
  773. "field_type": field_type,
  774. "default_value": None,
  775. }
  776. response = await client.post(f"{self.api_url}/field/spool/{name}", json=field_data)
  777. if response.status_code in (200, 201):
  778. logger.info("Created Spoolman extra field %r", name)
  779. return True
  780. logger.warning(
  781. "Failed to create Spoolman extra field %r: %s - %s",
  782. name,
  783. response.status_code,
  784. response.text,
  785. )
  786. return False
  787. except Exception as e:
  788. logger.warning("Failed to ensure Spoolman extra field %r exists: %s", name, e)
  789. return False
  790. def parse_ams_tray(self, ams_id: int, tray_data: dict) -> AMSTray | None:
  791. """Parse raw MQTT tray data into an AMSTray; returns None for empty or invalid trays."""
  792. # Skip empty trays - check for valid tray_type
  793. tray_type = tray_data.get("tray_type", "")
  794. if not tray_type or tray_type.strip() == "":
  795. return None
  796. # Need valid color to create filament
  797. tray_color = tray_data.get("tray_color", "")
  798. if not tray_color or tray_color.strip() == "":
  799. logger.debug("Skipping tray with empty color")
  800. return None
  801. # Handle transparent/natural filament (RRGGBBAA with alpha=00)
  802. # Replace with cream color that represents how natural PLA actually looks
  803. if tray_color == "00000000":
  804. tray_color = "F5E6D3FF" # Light cream/natural color
  805. # Get sub_brands, falling back to tray_type
  806. tray_sub_brands = tray_data.get("tray_sub_brands", "")
  807. if not tray_sub_brands or tray_sub_brands.strip() == "":
  808. tray_sub_brands = tray_type
  809. # Get tag_uid and tray_uuid, filtering out empty/invalid values
  810. tag_uid = tray_data.get("tag_uid", "")
  811. if tag_uid in ("", "0000000000000000"):
  812. tag_uid = ""
  813. tray_uuid = tray_data.get("tray_uuid", "")
  814. if tray_uuid in ("", "00000000000000000000000000000000"):
  815. tray_uuid = ""
  816. # Get tray_info_idx (Bambu filament preset ID like "GFA00")
  817. tray_info_idx = tray_data.get("tray_info_idx", "") or ""
  818. # Get remaining percentage (-1 means unknown/not read by AMS)
  819. remain = int(tray_data.get("remain", -1))
  820. return AMSTray(
  821. ams_id=ams_id,
  822. tray_id=int(tray_data.get("id", 0)),
  823. tray_type=tray_type.strip(),
  824. tray_sub_brands=tray_sub_brands.strip(),
  825. tray_color=tray_color,
  826. remain=remain,
  827. tag_uid=tag_uid,
  828. tray_uuid=tray_uuid,
  829. tray_info_idx=tray_info_idx.strip(),
  830. tray_weight=int(tray_data.get("tray_weight", 1000)),
  831. )
  832. def convert_ams_slot_to_location(self, ams_id: int, tray_id: int) -> str:
  833. """Return a human-readable location string (e.g. "AMS A1") for the given AMS slot."""
  834. if ams_id >= 254:
  835. return "External Spool"
  836. if 128 <= ams_id <= 135:
  837. # AMS-HT units use IDs 128-135
  838. ht_letter = chr(ord("A") + (ams_id - 128))
  839. return f"AMS-HT {ht_letter}{tray_id + 1}"
  840. ams_letter = chr(ord("A") + ams_id)
  841. return f"AMS {ams_letter}{tray_id + 1}"
  842. def is_bambu_lab_spool(self, tray_uuid: str, tag_uid: str = "", tray_info_idx: str = "") -> bool:
  843. """Return True if tray_uuid or tag_uid identifies a Bambu Lab spool; tray_info_idx is ignored."""
  844. # Check tray_uuid (preferred - consistent across printer models)
  845. if tray_uuid:
  846. uuid = tray_uuid.strip()
  847. if len(uuid) == 32 and uuid != "00000000000000000000000000000000":
  848. try:
  849. int(uuid, 16)
  850. return True
  851. except ValueError:
  852. pass
  853. # Fallback: check tag_uid (RFID tag - varies between printer readers)
  854. # Bambu Lab RFID tags are 16 hex characters (8 bytes)
  855. if tag_uid:
  856. tag = tag_uid.strip()
  857. if len(tag) == 16 and tag != "0000000000000000":
  858. try:
  859. int(tag, 16)
  860. logger.debug("Identified Bambu Lab spool via tag_uid fallback: %s", tag)
  861. return True
  862. except ValueError:
  863. pass
  864. return False
  865. def calculate_remaining_weight(self, remain_percent: int, spool_weight: int) -> float:
  866. """Return remaining filament weight in grams given a percentage and total spool weight."""
  867. return (remain_percent / 100.0) * spool_weight
  868. async def sync_ams_tray(
  869. self,
  870. tray: AMSTray,
  871. printer_name: str,
  872. disable_weight_sync: bool = False,
  873. cached_spools: list[dict] | None = None,
  874. inventory_remaining: float | None = None,
  875. spoolman_spool_id_hint: int | None = None,
  876. ) -> dict | None:
  877. """Sync one AMS tray to Spoolman; creates the spool on first sight, updates weight otherwise."""
  878. logger.debug(
  879. f"Processing {printer_name} AMS {tray.ams_id} tray {tray.tray_id}: "
  880. f"type={tray.tray_type}, idx={tray.tray_info_idx or 'none'}, "
  881. f"uuid={tray.tray_uuid[:16] if tray.tray_uuid else 'none'}, "
  882. f"tag={tray.tag_uid[:8] if tray.tag_uid else 'none'}..."
  883. )
  884. # Determine which identifier to use for Spoolman (prefer tray_uuid, fallback to tag_uid)
  885. # Zero-filled values mean the AMS hasn't read the RFID tag — treat as no tag
  886. zero_uuid = "00000000000000000000000000000000"
  887. zero_tag = "0000000000000000"
  888. spool_tag = None
  889. if tray.tray_uuid and tray.tray_uuid != zero_uuid:
  890. spool_tag = tray.tray_uuid
  891. elif tray.tag_uid and tray.tag_uid != zero_tag:
  892. spool_tag = tray.tag_uid
  893. # Calculate remaining weight
  894. # Primary: AMS MQTT data (remain percentage + tray_weight)
  895. # Fallback: Built-in inventory tracked weight (when firmware sends invalid remain/tray_weight)
  896. if tray.remain >= 0 and tray.tray_weight > 0:
  897. remaining = self.calculate_remaining_weight(tray.remain, tray.tray_weight)
  898. elif inventory_remaining is not None:
  899. remaining = inventory_remaining
  900. logger.debug(
  901. "Using inventory weight fallback for %s AMS %s tray %s: %.1fg",
  902. printer_name,
  903. tray.ams_id,
  904. tray.tray_id,
  905. remaining,
  906. )
  907. else:
  908. remaining = None
  909. if spool_tag:
  910. # Primary path: match by RFID tag
  911. existing = await self.find_spool_by_tag(spool_tag, cached_spools=cached_spools)
  912. if existing:
  913. logger.info("Updating existing spool %s for tag %s...", existing["id"], spool_tag[:16])
  914. return await self.update_spool(
  915. spool_id=existing["id"],
  916. remaining_weight=None if disable_weight_sync else remaining,
  917. )
  918. # Spool not found by tag - auto-create it
  919. logger.info("Creating new spool in Spoolman for %s (tag: %s...)", tray.tray_sub_brands, spool_tag[:16])
  920. if self.is_bambu_lab_spool(tray.tray_uuid, tray.tag_uid, tray.tray_info_idx):
  921. filament = await self._find_or_create_filament(tray)
  922. filament_id = filament["id"] if filament else None
  923. else:
  924. # Non-BL spool with custom RFID: use generic vendor lookup
  925. brand = tray.tray_sub_brands if tray.tray_sub_brands != tray.tray_type else None
  926. try:
  927. filament_id = await self.find_or_create_filament(
  928. material=tray.tray_type,
  929. subtype="",
  930. brand=brand,
  931. color_hex=tray.tray_color[:6],
  932. label_weight=tray.tray_weight,
  933. )
  934. except (SpoolmanNotFoundError, SpoolmanUnavailableError, SpoolmanClientError):
  935. logger.warning("Could not find or create filament for non-BL spool %s", tray.tray_sub_brands)
  936. return None
  937. if not filament_id:
  938. logger.error("Failed to find or create filament for %s", tray.tray_sub_brands)
  939. return None
  940. import json
  941. return await self.create_spool(
  942. filament_id=filament_id,
  943. remaining_weight=remaining,
  944. comment="Created by Bambuddy",
  945. extra={"tag": json.dumps(spool_tag)},
  946. )
  947. # No-RFID fallback: use the spool ID resolved from the local slot-assignment table.
  948. # Never create new spools without a tag to avoid duplicates.
  949. if spoolman_spool_id_hint is not None:
  950. existing = next((s for s in (cached_spools or []) if s.get("id") == spoolman_spool_id_hint), None)
  951. if existing is None:
  952. try:
  953. existing = await self.get_spool(spoolman_spool_id_hint)
  954. except (SpoolmanNotFoundError, SpoolmanUnavailableError):
  955. existing = None
  956. if existing:
  957. logger.info(
  958. "Updating spool %s by slot-assignment hint (no RFID tag available)",
  959. existing["id"],
  960. )
  961. return await self.update_spool(
  962. spool_id=existing["id"],
  963. remaining_weight=None if disable_weight_sync else remaining,
  964. )
  965. logger.info(
  966. "%s AMS %s tray %s — skipping (no RFID tag and no slot-assignment hint)",
  967. printer_name,
  968. tray.ams_id,
  969. tray.tray_id,
  970. )
  971. return None
  972. async def _find_or_create_filament(self, tray: AMSTray) -> dict | None:
  973. """Return a Bambu Lab filament matching the tray's material/color, creating it if absent."""
  974. bambu_vendor_id = await self.ensure_bambu_vendor()
  975. color_hex = tray.tray_color[:6] # Strip alpha channel
  976. material_upper = tray.tray_type.upper()
  977. color_upper = color_hex.upper()
  978. # Search internal filaments - only match Bambu Lab vendor
  979. filaments = await self.get_filaments()
  980. for filament in filaments:
  981. fil_vendor_id = filament.get("vendor_id") or filament.get("vendor", {}).get("id")
  982. if fil_vendor_id != bambu_vendor_id:
  983. continue
  984. fil_material = filament.get("material") or ""
  985. fil_color = filament.get("color_hex") or ""
  986. if fil_material.upper() == material_upper and fil_color.upper() == color_upper:
  987. return filament
  988. # Search external filaments (SpoolmanDB) — restrict to Bambu Lab only.
  989. # The /api/v1/external/filament endpoint returns the full multi-vendor catalog
  990. # with no server-side filter, so without a manufacturer check the first PLA/black
  991. # hit is typically 3DJAKE or 3DXTECH, not Bambu Lab.
  992. external = await self.get_external_filaments()
  993. sub_brand = (tray.tray_sub_brands or "").strip().lower()
  994. bambu_candidates = []
  995. for filament in external:
  996. manufacturer = (filament.get("manufacturer") or "").strip().lower()
  997. ext_id = (filament.get("id") or "").strip().lower()
  998. if manufacturer != "bambu lab" and not ext_id.startswith("bambulab_"):
  999. continue
  1000. fil_material = filament.get("material") or ""
  1001. fil_color = filament.get("color_hex") or ""
  1002. if fil_material.upper() == material_upper and fil_color.upper() == color_upper:
  1003. bambu_candidates.append(filament)
  1004. if bambu_candidates:
  1005. # Prefer the entry whose `name` matches the AMS `tray_sub_brands`
  1006. # (e.g. "PLA Basic", "Support for PLA/PETG Black") so the more specific
  1007. # variant wins over a generic "Black" entry when both are present.
  1008. chosen = next(
  1009. (f for f in bambu_candidates if (f.get("name") or "").strip().lower() == sub_brand),
  1010. bambu_candidates[0],
  1011. )
  1012. return await self._create_filament_from_external(chosen, tray)
  1013. # Not found in either source - create a new Bambu Lab filament from scratch.
  1014. return await self.create_filament(
  1015. name=tray.tray_sub_brands or tray.tray_type,
  1016. vendor_id=bambu_vendor_id,
  1017. material=tray.tray_type,
  1018. color_hex=color_hex,
  1019. weight=tray.tray_weight,
  1020. )
  1021. async def _create_filament_from_external(self, external: dict, tray: AMSTray) -> dict | None:
  1022. """Create an internal Spoolman filament from an external library entry."""
  1023. vendor_id = await self.ensure_bambu_vendor()
  1024. return await self.create_filament(
  1025. name=external.get("name", tray.tray_sub_brands),
  1026. vendor_id=vendor_id,
  1027. material=external.get("material", tray.tray_type),
  1028. color_hex=external.get("color_hex", tray.tray_color[:6]),
  1029. weight=external.get("weight", tray.tray_weight),
  1030. density=external.get("density"),
  1031. )
  1032. # Global client instance (initialized when settings are loaded)
  1033. _spoolman_client: SpoolmanClient | None = None
  1034. async def get_spoolman_client() -> SpoolmanClient | None:
  1035. """Return the global SpoolmanClient, or None if not configured."""
  1036. return _spoolman_client
  1037. async def init_spoolman_client(url: str) -> SpoolmanClient:
  1038. """Initialise (or reinitialise) the global SpoolmanClient; raises ValueError if url fails SSRF guard."""
  1039. from backend.app.api.routes._spoolman_helpers import assert_safe_spoolman_url
  1040. assert_safe_spoolman_url(url)
  1041. global _spoolman_client
  1042. if _spoolman_client:
  1043. await _spoolman_client.close()
  1044. _spoolman_client = SpoolmanClient(url)
  1045. return _spoolman_client
  1046. async def close_spoolman_client():
  1047. """Close the global Spoolman client."""
  1048. global _spoolman_client
  1049. if _spoolman_client:
  1050. await _spoolman_client.close()
  1051. _spoolman_client = None