spoolman.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. """Spoolman integration service for syncing AMS filament data."""
  2. import logging
  3. from dataclasses import dataclass
  4. from datetime import UTC, datetime
  5. import httpx
  6. logger = logging.getLogger(__name__)
  7. @dataclass
  8. class SpoolmanSpool:
  9. """Represents a spool in Spoolman."""
  10. id: int
  11. filament_id: int | None
  12. remaining_weight: float | None
  13. used_weight: float
  14. first_used: str | None
  15. last_used: str | None
  16. location: str | None
  17. lot_nr: str | None
  18. comment: str | None
  19. extra: dict | None # Contains tag_uid in extra.tag
  20. @dataclass
  21. class SpoolmanFilament:
  22. """Represents a filament type in Spoolman."""
  23. id: int
  24. name: str
  25. vendor_id: int | None
  26. material: str | None
  27. color_hex: str | None
  28. weight: float | None # Net weight in grams
  29. @dataclass
  30. class AMSTray:
  31. """Represents an AMS tray with filament data from Bambu printer."""
  32. ams_id: int # 0-3 for regular AMS, 128-135 for external spool
  33. tray_id: int # 0-3
  34. tray_type: str # PLA, PETG, ABS, etc.
  35. tray_sub_brands: str # Full name like "PLA Basic", "PETG HF"
  36. tray_color: str # Hex color like "FEC600FF"
  37. remain: int # Remaining percentage (0-100)
  38. tag_uid: str # RFID tag UID
  39. tray_uuid: str # Spool UUID
  40. tray_info_idx: str # Bambu filament preset ID like "GFA00"
  41. tray_weight: int # Spool weight in grams (usually 1000)
  42. class SpoolmanClient:
  43. """Client for interacting with Spoolman API."""
  44. def __init__(self, base_url: str):
  45. """Initialize the Spoolman client.
  46. Args:
  47. base_url: The base URL of the Spoolman server (e.g., http://localhost:7912)
  48. """
  49. self.base_url = base_url.rstrip("/")
  50. self.api_url = f"{self.base_url}/api/v1"
  51. self._client: httpx.AsyncClient | None = None
  52. self._connected = False
  53. async def _get_client(self) -> httpx.AsyncClient:
  54. """Get or create the HTTP client."""
  55. if self._client is None:
  56. self._client = httpx.AsyncClient(timeout=10.0)
  57. return self._client
  58. async def close(self):
  59. """Close the HTTP client."""
  60. if self._client:
  61. await self._client.aclose()
  62. self._client = None
  63. async def health_check(self) -> bool:
  64. """Check if Spoolman server is reachable.
  65. Returns:
  66. True if server is healthy, False otherwise.
  67. """
  68. try:
  69. client = await self._get_client()
  70. response = await client.get(f"{self.api_url}/health")
  71. self._connected = response.status_code == 200
  72. return self._connected
  73. except Exception as e:
  74. logger.warning(f"Spoolman health check failed: {e}")
  75. self._connected = False
  76. return False
  77. @property
  78. def is_connected(self) -> bool:
  79. """Check if client is connected to Spoolman."""
  80. return self._connected
  81. async def get_spools(self) -> list[dict]:
  82. """Get all spools from Spoolman.
  83. Returns:
  84. List of spool dictionaries.
  85. """
  86. try:
  87. client = await self._get_client()
  88. response = await client.get(f"{self.api_url}/spool")
  89. response.raise_for_status()
  90. return response.json()
  91. except Exception as e:
  92. logger.error(f"Failed to get spools from Spoolman: {e}")
  93. return []
  94. async def get_filaments(self) -> list[dict]:
  95. """Get all internal filaments from Spoolman.
  96. Returns:
  97. List of filament dictionaries.
  98. """
  99. try:
  100. client = await self._get_client()
  101. response = await client.get(f"{self.api_url}/filament")
  102. response.raise_for_status()
  103. return response.json()
  104. except Exception as e:
  105. logger.error(f"Failed to get filaments from Spoolman: {e}")
  106. return []
  107. async def get_external_filaments(self) -> list[dict]:
  108. """Get external/library filaments from Spoolman.
  109. Returns:
  110. List of external filament dictionaries.
  111. """
  112. try:
  113. client = await self._get_client()
  114. response = await client.get(f"{self.api_url}/external/filament")
  115. response.raise_for_status()
  116. return response.json()
  117. except Exception as e:
  118. logger.error(f"Failed to get external filaments from Spoolman: {e}")
  119. return []
  120. async def get_vendors(self) -> list[dict]:
  121. """Get all vendors from Spoolman.
  122. Returns:
  123. List of vendor dictionaries.
  124. """
  125. try:
  126. client = await self._get_client()
  127. response = await client.get(f"{self.api_url}/vendor")
  128. response.raise_for_status()
  129. return response.json()
  130. except Exception as e:
  131. logger.error(f"Failed to get vendors from Spoolman: {e}")
  132. return []
  133. async def create_vendor(self, name: str) -> dict | None:
  134. """Create a new vendor in Spoolman.
  135. Args:
  136. name: Vendor name (e.g., "Bambu Lab")
  137. Returns:
  138. Created vendor dictionary or None on failure.
  139. """
  140. try:
  141. client = await self._get_client()
  142. response = await client.post(f"{self.api_url}/vendor", json={"name": name})
  143. response.raise_for_status()
  144. return response.json()
  145. except Exception as e:
  146. logger.error(f"Failed to create vendor in Spoolman: {e}")
  147. return None
  148. def _get_material_density(self, material: str | None) -> float:
  149. """Get typical density for a filament material type.
  150. Args:
  151. material: Material type (PLA, PETG, ABS, etc.)
  152. Returns:
  153. Density in g/cm³
  154. """
  155. # Typical densities for common filament materials
  156. densities = {
  157. "PLA": 1.24,
  158. "PLA-CF": 1.29,
  159. "PLA-S": 1.24,
  160. "PETG": 1.27,
  161. "ABS": 1.04,
  162. "ASA": 1.07,
  163. "TPU": 1.21,
  164. "PA": 1.14, # Nylon
  165. "PA-CF": 1.20,
  166. "PC": 1.20,
  167. "PVA": 1.23,
  168. "HIPS": 1.04,
  169. "PP": 0.90,
  170. "PET": 1.38,
  171. }
  172. if material:
  173. # Try exact match first, then uppercase
  174. mat_upper = material.upper()
  175. for key, density in densities.items():
  176. if key.upper() == mat_upper or mat_upper.startswith(key.upper()):
  177. return density
  178. return 1.24 # Default to PLA density
  179. async def create_filament(
  180. self,
  181. name: str,
  182. vendor_id: int | None = None,
  183. material: str | None = None,
  184. color_hex: str | None = None,
  185. weight: float | None = None,
  186. diameter: float = 1.75,
  187. density: float | None = None,
  188. ) -> dict | None:
  189. """Create a new filament in Spoolman.
  190. Args:
  191. name: Filament name
  192. vendor_id: Vendor ID
  193. material: Material type (PLA, PETG, etc.)
  194. color_hex: Color in hex format (without #)
  195. weight: Net weight in grams
  196. diameter: Filament diameter in mm (default 1.75)
  197. density: Filament density in g/cm³ (auto-calculated if not provided)
  198. Returns:
  199. Created filament dictionary or None on failure.
  200. """
  201. # Validate required fields
  202. if not name or not name.strip():
  203. logger.error("Cannot create filament: name is required")
  204. return None
  205. try:
  206. # Calculate density from material if not provided
  207. if density is None:
  208. density = self._get_material_density(material)
  209. data = {
  210. "name": name.strip(),
  211. "diameter": diameter,
  212. "density": density,
  213. }
  214. if vendor_id:
  215. data["vendor_id"] = vendor_id
  216. if material:
  217. data["material"] = material
  218. if color_hex:
  219. # Strip alpha channel if present (RRGGBBAA -> RRGGBB)
  220. color_hex = color_hex[:6] if len(color_hex) >= 6 else color_hex
  221. data["color_hex"] = color_hex
  222. if weight:
  223. data["weight"] = weight
  224. logger.debug(f"Creating filament in Spoolman: {data}")
  225. client = await self._get_client()
  226. response = await client.post(f"{self.api_url}/filament", json=data)
  227. response.raise_for_status()
  228. return response.json()
  229. except httpx.HTTPStatusError as e:
  230. logger.error(f"Failed to create filament in Spoolman: {e}, response: {e.response.text}")
  231. return None
  232. except Exception as e:
  233. logger.error(f"Failed to create filament in Spoolman: {e}")
  234. return None
  235. async def create_spool(
  236. self,
  237. filament_id: int,
  238. remaining_weight: float | None = None,
  239. location: str | None = None,
  240. lot_nr: str | None = None,
  241. comment: str | None = None,
  242. extra: dict | None = None,
  243. ) -> dict | None:
  244. """Create a new spool in Spoolman.
  245. Args:
  246. filament_id: ID of the filament type
  247. remaining_weight: Remaining weight in grams
  248. location: Physical location description
  249. lot_nr: Lot/batch number
  250. comment: Optional comment
  251. extra: Extra fields (e.g., {"tag": "RFID_TAG_UID"})
  252. Returns:
  253. Created spool dictionary or None on failure.
  254. """
  255. try:
  256. data = {"filament_id": filament_id}
  257. if remaining_weight is not None:
  258. data["remaining_weight"] = remaining_weight
  259. if location:
  260. data["location"] = location
  261. if lot_nr:
  262. data["lot_nr"] = lot_nr
  263. if comment:
  264. data["comment"] = comment
  265. if extra:
  266. data["extra"] = extra
  267. logger.debug(f"Creating spool in Spoolman: {data}")
  268. client = await self._get_client()
  269. response = await client.post(f"{self.api_url}/spool", json=data)
  270. response.raise_for_status()
  271. result = response.json()
  272. logger.info(f"Created spool {result.get('id')} in Spoolman")
  273. return result
  274. except httpx.HTTPStatusError as e:
  275. logger.error(f"Failed to create spool in Spoolman: {e}, response: {e.response.text}")
  276. return None
  277. except Exception as e:
  278. logger.error(f"Failed to create spool in Spoolman: {e}")
  279. return None
  280. async def update_spool(
  281. self,
  282. spool_id: int,
  283. remaining_weight: float | None = None,
  284. location: str | None = None,
  285. clear_location: bool = False,
  286. extra: dict | None = None,
  287. ) -> dict | None:
  288. """Update an existing spool in Spoolman.
  289. Args:
  290. spool_id: ID of the spool to update
  291. remaining_weight: New remaining weight in grams
  292. location: New location (ignored if clear_location is True)
  293. clear_location: If True, clears the location field
  294. extra: Extra fields to update
  295. Returns:
  296. Updated spool dictionary or None on failure.
  297. """
  298. try:
  299. data = {}
  300. if remaining_weight is not None:
  301. data["remaining_weight"] = remaining_weight
  302. if clear_location:
  303. data["location"] = None
  304. elif location:
  305. data["location"] = location
  306. if extra:
  307. data["extra"] = extra
  308. # Always update last_used
  309. data["last_used"] = datetime.now(UTC).isoformat()
  310. client = await self._get_client()
  311. response = await client.patch(f"{self.api_url}/spool/{spool_id}", json=data)
  312. response.raise_for_status()
  313. return response.json()
  314. except Exception as e:
  315. logger.error(f"Failed to update spool in Spoolman: {e}")
  316. return None
  317. async def use_spool(self, spool_id: int, used_weight: float) -> dict | None:
  318. """Record filament usage for a spool.
  319. Args:
  320. spool_id: ID of the spool
  321. used_weight: Amount of filament used in grams
  322. Returns:
  323. Updated spool dictionary or None on failure.
  324. """
  325. try:
  326. client = await self._get_client()
  327. response = await client.put(
  328. f"{self.api_url}/spool/{spool_id}/use",
  329. json={"use_weight": used_weight},
  330. )
  331. response.raise_for_status()
  332. return response.json()
  333. except Exception as e:
  334. logger.error(f"Failed to record spool usage in Spoolman: {e}")
  335. return None
  336. async def find_spool_by_tag(self, tag_uid: str) -> dict | None:
  337. """Find a spool by its RFID tag UID.
  338. Args:
  339. tag_uid: The RFID tag UID to search for
  340. Returns:
  341. Spool dictionary or None if not found.
  342. """
  343. spools = await self.get_spools()
  344. # Normalize tag_uid for comparison (uppercase, strip quotes)
  345. search_tag = tag_uid.strip('"').upper()
  346. for spool in spools:
  347. extra = spool.get("extra", {})
  348. if extra:
  349. stored_tag = extra.get("tag", "")
  350. # Normalize stored tag (strip quotes, uppercase)
  351. if stored_tag:
  352. normalized_tag = stored_tag.strip('"').upper()
  353. if normalized_tag == search_tag:
  354. logger.debug(f"Found spool {spool['id']} matching tag {tag_uid}")
  355. return spool
  356. return None
  357. async def find_spools_by_location_prefix(self, location_prefix: str) -> list[dict]:
  358. """Find all spools with locations starting with a given prefix.
  359. Args:
  360. location_prefix: The location prefix to search for (e.g., "PrinterName - ")
  361. Returns:
  362. List of spool dictionaries with matching locations.
  363. """
  364. spools = await self.get_spools()
  365. matching = []
  366. for spool in spools:
  367. location = spool.get("location", "")
  368. if location and location.startswith(location_prefix):
  369. matching.append(spool)
  370. return matching
  371. async def clear_location_for_removed_spools(
  372. self,
  373. printer_name: str,
  374. current_tray_uuids: set[str],
  375. ) -> int:
  376. """Clear location for spools that are no longer in the AMS.
  377. When a spool is removed from the AMS, its location should be cleared
  378. in Spoolman. This method finds all spools with locations for this printer
  379. and clears the location for any that are not in the current_tray_uuids set.
  380. Args:
  381. printer_name: The printer name used as location prefix
  382. current_tray_uuids: Set of tray_uuids currently in the AMS
  383. Returns:
  384. Number of spools whose location was cleared.
  385. """
  386. location_prefix = f"{printer_name} - "
  387. spools_at_printer = await self.find_spools_by_location_prefix(location_prefix)
  388. cleared_count = 0
  389. for spool in spools_at_printer:
  390. # Get the tray_uuid (stored as "tag" in extra field)
  391. extra = spool.get("extra", {}) or {}
  392. stored_tag = extra.get("tag", "")
  393. if stored_tag:
  394. # Normalize: strip quotes and uppercase
  395. spool_uuid = stored_tag.strip('"').upper()
  396. else:
  397. spool_uuid = ""
  398. # If this spool's UUID is not in the current AMS, clear its location
  399. if spool_uuid not in current_tray_uuids:
  400. logger.info(
  401. f"Clearing location for spool {spool['id']} "
  402. f"(was: {spool.get('location')}, uuid: {spool_uuid[:16] if spool_uuid else 'none'}...)"
  403. )
  404. result = await self.update_spool(spool_id=spool["id"], clear_location=True)
  405. if result:
  406. cleared_count += 1
  407. return cleared_count
  408. async def ensure_bambu_vendor(self) -> int | None:
  409. """Ensure Bambu Lab vendor exists and return its ID.
  410. Returns:
  411. Vendor ID or None on failure.
  412. """
  413. vendors = await self.get_vendors()
  414. for vendor in vendors:
  415. if vendor.get("name", "").lower() == "bambu lab":
  416. return vendor["id"]
  417. # Create Bambu Lab vendor if not exists
  418. vendor = await self.create_vendor("Bambu Lab")
  419. return vendor["id"] if vendor else None
  420. def parse_ams_tray(self, ams_id: int, tray_data: dict) -> AMSTray | None:
  421. """Parse AMS tray data into AMSTray object.
  422. Args:
  423. ams_id: The AMS unit ID (0-3 for regular, 128-135 for external)
  424. tray_data: Raw tray data from MQTT
  425. Returns:
  426. AMSTray object or None if tray is empty or invalid.
  427. """
  428. # Skip empty trays - check for valid tray_type
  429. tray_type = tray_data.get("tray_type", "")
  430. if not tray_type or tray_type.strip() == "":
  431. return None
  432. # Need valid color to create filament
  433. tray_color = tray_data.get("tray_color", "")
  434. if not tray_color or tray_color in ("", "00000000"):
  435. logger.debug(f"Skipping tray with invalid color: {tray_color}")
  436. return None
  437. # Get sub_brands, falling back to tray_type
  438. tray_sub_brands = tray_data.get("tray_sub_brands", "")
  439. if not tray_sub_brands or tray_sub_brands.strip() == "":
  440. tray_sub_brands = tray_type
  441. # Get tag_uid and tray_uuid, filtering out empty/invalid values
  442. tag_uid = tray_data.get("tag_uid", "")
  443. if tag_uid in ("", "0000000000000000"):
  444. tag_uid = ""
  445. tray_uuid = tray_data.get("tray_uuid", "")
  446. if tray_uuid in ("", "00000000000000000000000000000000"):
  447. tray_uuid = ""
  448. # Get tray_info_idx (Bambu filament preset ID like "GFA00")
  449. tray_info_idx = tray_data.get("tray_info_idx", "") or ""
  450. # Get remaining percentage, ensure non-negative
  451. remain = max(0, int(tray_data.get("remain", 0)))
  452. return AMSTray(
  453. ams_id=ams_id,
  454. tray_id=int(tray_data.get("id", 0)),
  455. tray_type=tray_type.strip(),
  456. tray_sub_brands=tray_sub_brands.strip(),
  457. tray_color=tray_color,
  458. remain=remain,
  459. tag_uid=tag_uid,
  460. tray_uuid=tray_uuid,
  461. tray_info_idx=tray_info_idx.strip(),
  462. tray_weight=int(tray_data.get("tray_weight", 1000)),
  463. )
  464. def convert_ams_slot_to_location(self, ams_id: int, tray_id: int) -> str:
  465. """Convert AMS ID and tray ID to human-readable location.
  466. Args:
  467. ams_id: AMS unit ID (0-3 for regular AMS, 128-135 for external)
  468. tray_id: Tray ID within the AMS (0-3)
  469. Returns:
  470. Location string like "AMS A1", "AMS B2", "External"
  471. """
  472. if ams_id >= 128:
  473. return "External Spool"
  474. ams_letter = chr(ord("A") + ams_id)
  475. return f"AMS {ams_letter}{tray_id + 1}"
  476. def is_bambu_lab_spool(self, tray_uuid: str, tag_uid: str = "", tray_info_idx: str = "") -> bool:
  477. """Check if a tray has a valid Bambu Lab spool.
  478. Bambu Lab spools can be identified by:
  479. 1. tray_uuid: 32-character hex string (preferred, consistent across printers)
  480. 2. tag_uid: 16-character hex string (RFID tag, varies between readers)
  481. 3. tray_info_idx: Bambu filament preset ID like "GFA00" (most reliable)
  482. Non-Bambu Lab spools (SpoolEase, third-party) won't have these identifiers.
  483. Args:
  484. tray_uuid: The tray UUID to check (32 hex chars)
  485. tag_uid: The RFID tag UID to check as fallback (16 hex chars)
  486. tray_info_idx: Bambu filament preset ID like "GFA00", "GFB00"
  487. Returns:
  488. True if the spool has valid Bambu Lab identifiers, False otherwise.
  489. """
  490. # Check tray_info_idx first - Bambu filament preset IDs like "GFA00", "GFB00", etc.
  491. # This is the most reliable indicator as it's set when the spool is recognized
  492. if tray_info_idx:
  493. idx = tray_info_idx.strip()
  494. # Bambu Lab preset IDs start with "GF" followed by letter and digits
  495. # e.g., GFA00, GFB00, GFL00, GFN00, GFG00, GFS00, GFU00
  496. if idx and len(idx) >= 3 and idx.startswith("GF"):
  497. logger.debug(f"Identified Bambu Lab spool via tray_info_idx: {idx}")
  498. return True
  499. # Check tray_uuid (preferred - consistent across printer models)
  500. if tray_uuid:
  501. uuid = tray_uuid.strip()
  502. if len(uuid) == 32 and uuid != "00000000000000000000000000000000":
  503. try:
  504. int(uuid, 16)
  505. return True
  506. except ValueError:
  507. pass
  508. # Fallback: check tag_uid (RFID tag - varies between printer readers)
  509. # Bambu Lab RFID tags are 16 hex characters (8 bytes)
  510. if tag_uid:
  511. tag = tag_uid.strip()
  512. if len(tag) == 16 and tag != "0000000000000000":
  513. try:
  514. int(tag, 16)
  515. logger.debug(f"Identified Bambu Lab spool via tag_uid fallback: {tag}")
  516. return True
  517. except ValueError:
  518. pass
  519. return False
  520. def calculate_remaining_weight(self, remain_percent: int, spool_weight: int) -> float:
  521. """Calculate remaining weight from percentage.
  522. Args:
  523. remain_percent: Remaining percentage (0-100)
  524. spool_weight: Total spool weight in grams
  525. Returns:
  526. Remaining weight in grams.
  527. """
  528. return (remain_percent / 100.0) * spool_weight
  529. async def sync_ams_tray(self, tray: AMSTray, printer_name: str) -> dict | None:
  530. """Sync a single AMS tray to Spoolman.
  531. Only syncs trays with valid Bambu Lab tray_uuid (32 hex characters).
  532. Non-Bambu Lab spools (SpoolEase/third-party) are skipped.
  533. Uses tray_uuid for matching, as it's consistent across all printer models
  534. (unlike tag_uid which varies between X1C/H2D readers).
  535. Args:
  536. tray: The AMSTray to sync
  537. printer_name: Name of the printer for location
  538. Returns:
  539. Synced spool dictionary or None if skipped or failed.
  540. """
  541. logger.debug(
  542. f"Processing {printer_name} AMS {tray.ams_id} tray {tray.tray_id}: "
  543. f"type={tray.tray_type}, idx={tray.tray_info_idx or 'none'}, "
  544. f"uuid={tray.tray_uuid[:16] if tray.tray_uuid else 'none'}, "
  545. f"tag={tray.tag_uid[:8] if tray.tag_uid else 'none'}..."
  546. )
  547. # Only sync trays with valid Bambu Lab identifiers
  548. if not self.is_bambu_lab_spool(tray.tray_uuid, tray.tag_uid, tray.tray_info_idx):
  549. if tray.tray_uuid or tray.tag_uid or tray.tray_info_idx:
  550. logger.info(
  551. f"Skipping non-Bambu Lab spool: {printer_name} AMS {tray.ams_id} tray {tray.tray_id} "
  552. f"(tray_info_idx={tray.tray_info_idx}, tray_uuid={tray.tray_uuid}, tag_uid={tray.tag_uid})"
  553. )
  554. else:
  555. logger.debug(f"Skipping tray without RFID tag: AMS {tray.ams_id} tray {tray.tray_id}")
  556. return None
  557. # Determine which identifier to use for Spoolman (prefer tray_uuid, fallback to tag_uid)
  558. spool_tag = (
  559. tray.tray_uuid if tray.tray_uuid and tray.tray_uuid != "00000000000000000000000000000000" else tray.tag_uid
  560. )
  561. # If no unique identifier available, we can't sync even if it's a Bambu Lab spool
  562. if not spool_tag:
  563. logger.warning(
  564. f"Bambu Lab spool detected but no unique identifier for Spoolman: "
  565. f"{printer_name} AMS {tray.ams_id} tray {tray.tray_id} (tray_info_idx={tray.tray_info_idx})"
  566. )
  567. return None
  568. # Calculate remaining weight
  569. remaining = self.calculate_remaining_weight(tray.remain, tray.tray_weight)
  570. location = f"{printer_name} - {self.convert_ams_slot_to_location(tray.ams_id, tray.tray_id)}"
  571. # Find existing spool by tag (tray_uuid or tag_uid, stored as "tag" in Spoolman)
  572. existing = await self.find_spool_by_tag(spool_tag)
  573. if existing:
  574. # Update existing spool
  575. logger.info(f"Updating existing spool {existing['id']} for tag {spool_tag[:16]}...")
  576. return await self.update_spool(
  577. spool_id=existing["id"],
  578. remaining_weight=remaining,
  579. location=location,
  580. )
  581. # Spool not found - auto-create it
  582. logger.info(f"Creating new spool in Spoolman for {tray.tray_sub_brands} (tag: {spool_tag[:16]}...)")
  583. # First find or create the filament type
  584. filament = await self._find_or_create_filament(tray)
  585. if not filament:
  586. logger.error(f"Failed to find or create filament for {tray.tray_sub_brands}")
  587. return None
  588. # Create the spool with identifier stored as "tag" in extra field
  589. # Note: Spoolman extra field values must be valid JSON, so we encode the string
  590. import json
  591. return await self.create_spool(
  592. filament_id=filament["id"],
  593. remaining_weight=remaining,
  594. location=location,
  595. comment="Created by Bambuddy",
  596. extra={"tag": json.dumps(spool_tag)},
  597. )
  598. async def _find_or_create_filament(self, tray: AMSTray) -> dict | None:
  599. """Find existing filament or create new one.
  600. Only matches Bambu Lab vendor filaments since this is called for
  601. Bambu Lab spools. Third-party filaments (like 3DJAKE) are ignored
  602. to prevent incorrect matching by color alone.
  603. Args:
  604. tray: The AMSTray containing filament info
  605. Returns:
  606. Filament dictionary or None on failure.
  607. """
  608. # Get Bambu Lab vendor ID for filtering
  609. bambu_vendor_id = await self.ensure_bambu_vendor()
  610. color_hex = tray.tray_color[:6] # Strip alpha channel
  611. # Search internal filaments - only match Bambu Lab vendor
  612. filaments = await self.get_filaments()
  613. for filament in filaments:
  614. # Only match filaments from Bambu Lab vendor
  615. fil_vendor_id = filament.get("vendor_id") or filament.get("vendor", {}).get("id")
  616. if fil_vendor_id != bambu_vendor_id:
  617. continue
  618. # Match by material and color (handle None values)
  619. fil_material = filament.get("material") or ""
  620. fil_color = filament.get("color_hex") or ""
  621. if fil_material.upper() == tray.tray_type.upper() and fil_color.upper() == color_hex.upper():
  622. return filament
  623. # Search external filaments (Bambu library)
  624. external = await self.get_external_filaments()
  625. for filament in external:
  626. fil_material = filament.get("material") or ""
  627. fil_color = filament.get("color_hex") or ""
  628. if fil_material.upper() == tray.tray_type.upper() and fil_color.upper() == color_hex.upper():
  629. # Found in external library - need to create internal copy
  630. return await self._create_filament_from_external(filament, tray)
  631. # Not found - create new Bambu Lab filament
  632. return await self.create_filament(
  633. name=tray.tray_sub_brands or tray.tray_type,
  634. vendor_id=bambu_vendor_id,
  635. material=tray.tray_type,
  636. color_hex=color_hex,
  637. weight=tray.tray_weight,
  638. )
  639. async def _create_filament_from_external(self, external: dict, tray: AMSTray) -> dict | None:
  640. """Create internal filament from external library entry.
  641. Args:
  642. external: External filament dictionary
  643. tray: The AMSTray for additional info
  644. Returns:
  645. Created filament dictionary or None on failure.
  646. """
  647. vendor_id = await self.ensure_bambu_vendor()
  648. return await self.create_filament(
  649. name=external.get("name", tray.tray_sub_brands),
  650. vendor_id=vendor_id,
  651. material=external.get("material", tray.tray_type),
  652. color_hex=external.get("color_hex", tray.tray_color[:6]),
  653. weight=external.get("weight", tray.tray_weight),
  654. )
  655. # Global client instance (initialized when settings are loaded)
  656. _spoolman_client: SpoolmanClient | None = None
  657. async def get_spoolman_client() -> SpoolmanClient | None:
  658. """Get the global Spoolman client instance.
  659. Returns:
  660. SpoolmanClient instance or None if not configured.
  661. """
  662. return _spoolman_client
  663. async def init_spoolman_client(url: str) -> SpoolmanClient:
  664. """Initialize the global Spoolman client.
  665. Args:
  666. url: Spoolman server URL
  667. Returns:
  668. Initialized SpoolmanClient instance.
  669. """
  670. global _spoolman_client
  671. if _spoolman_client:
  672. await _spoolman_client.close()
  673. _spoolman_client = SpoolmanClient(url)
  674. return _spoolman_client
  675. async def close_spoolman_client():
  676. """Close the global Spoolman client."""
  677. global _spoolman_client
  678. if _spoolman_client:
  679. await _spoolman_client.close()
  680. _spoolman_client = None