print_scheduler.py 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. """Print scheduler service - processes the print queue."""
  2. import asyncio
  3. import json
  4. import logging
  5. import zipfile
  6. from datetime import datetime
  7. from pathlib import Path
  8. import defusedxml.ElementTree as ET
  9. from sqlalchemy import func, select
  10. from sqlalchemy.ext.asyncio import AsyncSession
  11. from backend.app.core.config import settings
  12. from backend.app.core.database import async_session
  13. from backend.app.models.archive import PrintArchive
  14. from backend.app.models.library import LibraryFile
  15. from backend.app.models.print_queue import PrintQueueItem
  16. from backend.app.models.printer import Printer
  17. from backend.app.models.smart_plug import SmartPlug
  18. from backend.app.services.bambu_ftp import delete_file_async, get_ftp_retry_settings, upload_file_async, with_ftp_retry
  19. from backend.app.services.notification_service import notification_service
  20. from backend.app.services.printer_manager import printer_manager
  21. from backend.app.services.smart_plug_manager import smart_plug_manager
  22. from backend.app.utils.printer_models import normalize_printer_model
  23. from backend.app.utils.threemf_tools import extract_nozzle_mapping_from_3mf
  24. logger = logging.getLogger(__name__)
  25. class PrintScheduler:
  26. """Background scheduler that processes the print queue."""
  27. def __init__(self):
  28. self._running = False
  29. self._check_interval = 30 # seconds
  30. self._power_on_wait_time = 180 # seconds to wait for printer after power on (3 min)
  31. self._power_on_check_interval = 10 # seconds between connection checks
  32. async def run(self):
  33. """Main loop - check queue every interval."""
  34. self._running = True
  35. logger.info("Print scheduler started")
  36. while self._running:
  37. try:
  38. await self.check_queue()
  39. except Exception as e:
  40. logger.error("Scheduler error: %s", e)
  41. await asyncio.sleep(self._check_interval)
  42. def stop(self):
  43. """Stop the scheduler."""
  44. self._running = False
  45. logger.info("Print scheduler stopped")
  46. async def check_queue(self):
  47. """Check for prints ready to start."""
  48. async with async_session() as db:
  49. # Get all pending items, ordered by printer and position
  50. result = await db.execute(
  51. select(PrintQueueItem)
  52. .where(PrintQueueItem.status == "pending")
  53. .order_by(PrintQueueItem.printer_id, PrintQueueItem.position)
  54. )
  55. items = list(result.scalars().all())
  56. if not items:
  57. return
  58. logger.info(
  59. "Queue check: found %d pending items: %s",
  60. len(items),
  61. [(i.id, i.printer_id, i.archive_id, i.library_file_id) for i in items],
  62. )
  63. # Track busy printers to avoid assigning multiple items to same printer
  64. busy_printers: set[int] = set()
  65. for item in items:
  66. # Check scheduled time first (scheduled_time is stored in UTC from ISO string)
  67. if item.scheduled_time and item.scheduled_time > datetime.utcnow():
  68. continue
  69. # Skip items that require manual start
  70. if item.manual_start:
  71. continue
  72. if item.printer_id:
  73. # Specific printer assignment (existing behavior)
  74. if item.printer_id in busy_printers:
  75. continue
  76. # Check if printer is idle
  77. printer_idle = self._is_printer_idle(item.printer_id)
  78. printer_connected = printer_manager.is_connected(item.printer_id)
  79. # If printer not connected, try to power on via smart plug
  80. if not printer_connected:
  81. plug = await self._get_smart_plug(db, item.printer_id)
  82. if plug and plug.auto_on and plug.enabled:
  83. logger.info("Printer %s offline, attempting to power on via smart plug", item.printer_id)
  84. powered_on = await self._power_on_and_wait(plug, item.printer_id, db)
  85. if powered_on:
  86. printer_connected = True
  87. printer_idle = self._is_printer_idle(item.printer_id)
  88. else:
  89. logger.warning("Could not power on printer %s via smart plug", item.printer_id)
  90. busy_printers.add(item.printer_id)
  91. continue
  92. else:
  93. # No plug or auto_on disabled
  94. busy_printers.add(item.printer_id)
  95. continue
  96. # Check if printer is idle (busy with another print)
  97. if not printer_idle:
  98. busy_printers.add(item.printer_id)
  99. continue
  100. # Check condition (previous print success)
  101. if item.require_previous_success:
  102. if not await self._check_previous_success(db, item):
  103. item.status = "skipped"
  104. item.error_message = "Previous print failed or was aborted"
  105. item.completed_at = datetime.now()
  106. await db.commit()
  107. logger.info("Skipped queue item %s - previous print failed", item.id)
  108. # Send notification
  109. job_name = await self._get_job_name(db, item)
  110. printer = await self._get_printer(db, item.printer_id)
  111. await notification_service.on_queue_job_skipped(
  112. job_name=job_name,
  113. printer_id=item.printer_id,
  114. printer_name=printer.name if printer else "Unknown",
  115. reason="Previous print failed or was aborted",
  116. db=db,
  117. )
  118. continue
  119. # Start the print
  120. await self._start_print(db, item)
  121. busy_printers.add(item.printer_id)
  122. elif item.target_model:
  123. # Model-based assignment - find any idle printer of matching model
  124. # Parse required filament types if present
  125. required_types = None
  126. if item.required_filament_types:
  127. try:
  128. required_types = json.loads(item.required_filament_types)
  129. except json.JSONDecodeError:
  130. pass # Ignore malformed filament types; treat as no constraint
  131. printer_id, waiting_reason = await self._find_idle_printer_for_model(
  132. db, item.target_model, busy_printers, required_types, item.target_location
  133. )
  134. # Update waiting_reason if changed and send notification when first waiting
  135. if item.waiting_reason != waiting_reason:
  136. was_waiting = item.waiting_reason is not None
  137. item.waiting_reason = waiting_reason
  138. await db.commit()
  139. # Send waiting notification only when transitioning to waiting state
  140. if waiting_reason and not was_waiting:
  141. job_name = await self._get_job_name(db, item)
  142. await notification_service.on_queue_job_waiting(
  143. job_name=job_name,
  144. target_model=item.target_model,
  145. waiting_reason=waiting_reason,
  146. db=db,
  147. )
  148. if printer_id:
  149. # Check condition (previous print success) before assigning
  150. if item.require_previous_success:
  151. if not await self._check_previous_success(db, item):
  152. item.status = "skipped"
  153. item.error_message = "Previous print failed or was aborted"
  154. item.completed_at = datetime.now()
  155. await db.commit()
  156. logger.info("Skipped queue item %s - previous print failed", item.id)
  157. # Send notification
  158. job_name = await self._get_job_name(db, item)
  159. printer = await self._get_printer(db, printer_id)
  160. await notification_service.on_queue_job_skipped(
  161. job_name=job_name,
  162. printer_id=printer_id,
  163. printer_name=printer.name if printer else "Unknown",
  164. reason="Previous print failed or was aborted",
  165. db=db,
  166. )
  167. continue
  168. # Assign printer and start - clear waiting reason
  169. item.printer_id = printer_id
  170. item.waiting_reason = None
  171. logger.info("Model-based assignment: queue item %s assigned to printer %s", item.id, printer_id)
  172. # Send assignment notification
  173. job_name = await self._get_job_name(db, item)
  174. printer = await self._get_printer(db, printer_id)
  175. await notification_service.on_queue_job_assigned(
  176. job_name=job_name,
  177. printer_id=printer_id,
  178. printer_name=printer.name if printer else "Unknown",
  179. target_model=item.target_model,
  180. db=db,
  181. )
  182. # Compute AMS mapping for the assigned printer if not already set
  183. # This is critical for model-based jobs where mapping wasn't computed upfront
  184. if not item.ams_mapping:
  185. computed_mapping = await self._compute_ams_mapping_for_printer(db, printer_id, item)
  186. if computed_mapping:
  187. item.ams_mapping = json.dumps(computed_mapping)
  188. logger.info(
  189. f"Queue item {item.id}: Computed AMS mapping for printer {printer_id}: {computed_mapping}"
  190. )
  191. await db.commit()
  192. await self._start_print(db, item)
  193. busy_printers.add(printer_id)
  194. async def _find_idle_printer_for_model(
  195. self,
  196. db: AsyncSession,
  197. model: str,
  198. exclude_ids: set[int],
  199. required_filament_types: list[str] | None = None,
  200. target_location: str | None = None,
  201. ) -> tuple[int | None, str | None]:
  202. """Find an idle, connected printer matching the model with compatible filaments.
  203. Args:
  204. db: Database session
  205. model: Printer model to match (e.g., "X1C", "P1S")
  206. exclude_ids: Printer IDs to exclude (already busy)
  207. required_filament_types: Optional list of filament types needed (e.g., ["PLA", "PETG"])
  208. If provided, only printers with all required types loaded will match.
  209. target_location: Optional location filter. If provided, only printers in this location are considered.
  210. Returns:
  211. Tuple of (printer_id, waiting_reason):
  212. - (printer_id, None) if a matching printer was found
  213. - (None, reason) if no printer is available, with explanation
  214. """
  215. # Normalize model name and use case-insensitive matching
  216. normalized_model = normalize_printer_model(model) or model
  217. query = (
  218. select(Printer)
  219. .where(func.lower(Printer.model) == normalized_model.lower())
  220. .where(Printer.is_active == True) # noqa: E712
  221. )
  222. # Add location filter if specified
  223. if target_location:
  224. query = query.where(Printer.location == target_location)
  225. result = await db.execute(query)
  226. printers = list(result.scalars().all())
  227. location_suffix = f" in {target_location}" if target_location else ""
  228. if not printers:
  229. return None, f"No active {normalized_model} printers{location_suffix} configured"
  230. # Track reasons for skipping printers
  231. printers_busy = []
  232. printers_offline = []
  233. printers_missing_filament = []
  234. for printer in printers:
  235. if printer.id in exclude_ids:
  236. printers_busy.append(printer.name)
  237. continue
  238. is_connected = printer_manager.is_connected(printer.id)
  239. is_idle = self._is_printer_idle(printer.id) if is_connected else False
  240. if not is_connected:
  241. printers_offline.append(printer.name)
  242. continue
  243. if not is_idle:
  244. printers_busy.append(printer.name)
  245. continue
  246. # Validate filament compatibility if required types are specified
  247. if required_filament_types:
  248. missing = self._get_missing_filament_types(printer.id, required_filament_types)
  249. if missing:
  250. printers_missing_filament.append((printer.name, missing))
  251. logger.debug("Skipping printer %s (%s) - missing filaments: %s", printer.id, printer.name, missing)
  252. continue
  253. # Found a matching printer - clear waiting reason
  254. return printer.id, None
  255. # Build waiting reason from what we found
  256. reasons = []
  257. if printers_missing_filament:
  258. # Filament mismatch is most actionable - show first
  259. names_and_missing = [f"{name} (needs {', '.join(missing)})" for name, missing in printers_missing_filament]
  260. reasons.append(f"Waiting for filament: {'; '.join(names_and_missing)}")
  261. if printers_busy:
  262. reasons.append(f"Busy: {', '.join(printers_busy)}")
  263. if printers_offline:
  264. reasons.append(f"Offline: {', '.join(printers_offline)}")
  265. return None, " | ".join(reasons) if reasons else f"No available {model} printers{location_suffix}"
  266. def _get_missing_filament_types(self, printer_id: int, required_types: list[str]) -> list[str]:
  267. """Get the list of required filament types that are not loaded on the printer.
  268. Args:
  269. printer_id: The printer ID
  270. required_types: List of filament types needed (e.g., ["PLA", "PETG"])
  271. Returns:
  272. List of missing filament types (empty if all are loaded)
  273. """
  274. status = printer_manager.get_status(printer_id)
  275. if not status:
  276. return required_types # Can't determine, assume all missing
  277. # Collect all filament types loaded on this printer (AMS units + external spool)
  278. loaded_types: set[str] = set()
  279. # Check AMS units (stored in raw_data["ams"])
  280. ams_data = status.raw_data.get("ams", [])
  281. if ams_data:
  282. for ams_unit in ams_data:
  283. for tray in ams_unit.get("tray", []):
  284. tray_type = tray.get("tray_type")
  285. if tray_type:
  286. loaded_types.add(tray_type.upper())
  287. # Check external spool(s) (virtual tray, stored in raw_data["vt_tray"] as list)
  288. for vt in status.raw_data.get("vt_tray") or []:
  289. vt_type = vt.get("tray_type")
  290. if vt_type:
  291. loaded_types.add(vt_type.upper())
  292. # Find which required types are missing (case-insensitive comparison)
  293. missing = []
  294. for req_type in required_types:
  295. if req_type.upper() not in loaded_types:
  296. missing.append(req_type)
  297. return missing
  298. async def _compute_ams_mapping_for_printer(
  299. self, db: AsyncSession, printer_id: int, item: PrintQueueItem
  300. ) -> list[int] | None:
  301. """Compute AMS mapping for a printer based on filament requirements.
  302. This is called for model-based queue items after a printer is assigned,
  303. to compute the correct AMS slot mapping for that specific printer's hardware.
  304. Args:
  305. db: Database session
  306. printer_id: The assigned printer ID
  307. item: The queue item (contains archive_id or library_file_id)
  308. Returns:
  309. AMS mapping array or None if no mapping needed/possible
  310. """
  311. # Get printer status
  312. status = printer_manager.get_status(printer_id)
  313. if not status:
  314. logger.warning("Cannot compute AMS mapping: printer %s status unavailable", printer_id)
  315. return None
  316. # Get filament requirements from source file
  317. filament_reqs = await self._get_filament_requirements(db, item)
  318. if not filament_reqs:
  319. logger.debug("No filament requirements found for queue item %s", item.id)
  320. return None
  321. # Build loaded filaments from printer status
  322. loaded_filaments = self._build_loaded_filaments(status)
  323. if not loaded_filaments:
  324. logger.debug("No filaments loaded on printer %s", printer_id)
  325. return None
  326. # Compute mapping: match required filaments to available slots
  327. return self._match_filaments_to_slots(filament_reqs, loaded_filaments)
  328. async def _get_filament_requirements(self, db: AsyncSession, item: PrintQueueItem) -> list[dict] | None:
  329. """Extract filament requirements from the source 3MF file.
  330. Args:
  331. db: Database session
  332. item: Queue item with archive_id or library_file_id
  333. Returns:
  334. List of filament requirement dicts with slot_id, type, color, used_grams
  335. """
  336. file_path: Path | None = None
  337. if item.archive_id:
  338. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  339. archive = result.scalar_one_or_none()
  340. if archive:
  341. file_path = settings.base_dir / archive.file_path
  342. elif item.library_file_id:
  343. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  344. library_file = result.scalar_one_or_none()
  345. if library_file:
  346. lib_path = Path(library_file.file_path)
  347. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  348. if not file_path or not file_path.exists():
  349. return None
  350. filaments = []
  351. try:
  352. with zipfile.ZipFile(file_path, "r") as zf:
  353. if "Metadata/slice_info.config" not in zf.namelist():
  354. return None
  355. content = zf.read("Metadata/slice_info.config").decode()
  356. root = ET.fromstring(content)
  357. # Check if plate_id is specified - use that plate's filaments
  358. plate_id = item.plate_id
  359. if plate_id:
  360. for plate_elem in root.findall("./plate"):
  361. plate_index = None
  362. for meta in plate_elem.findall("metadata"):
  363. if meta.get("key") == "index":
  364. plate_index = int(meta.get("value", "0"))
  365. break
  366. if plate_index == plate_id:
  367. for filament_elem in plate_elem.findall("./filament"):
  368. filament_id = filament_elem.get("id")
  369. filament_type = filament_elem.get("type", "")
  370. filament_color = filament_elem.get("color", "")
  371. # tray_info_idx identifies the specific spool selected when slicing
  372. tray_info_idx = filament_elem.get("tray_info_idx", "")
  373. used_g = filament_elem.get("used_g", "0")
  374. try:
  375. used_grams = float(used_g)
  376. if used_grams > 0 and filament_id:
  377. filaments.append(
  378. {
  379. "slot_id": int(filament_id),
  380. "type": filament_type,
  381. "color": filament_color,
  382. "tray_info_idx": tray_info_idx,
  383. "used_grams": round(used_grams, 1),
  384. }
  385. )
  386. except (ValueError, TypeError):
  387. pass # Skip filament entry with unparseable usage data
  388. break
  389. else:
  390. # No plate_id - extract all filaments with used_g > 0
  391. for filament_elem in root.findall("./filament"):
  392. filament_id = filament_elem.get("id")
  393. filament_type = filament_elem.get("type", "")
  394. filament_color = filament_elem.get("color", "")
  395. # tray_info_idx identifies the specific spool selected when slicing
  396. tray_info_idx = filament_elem.get("tray_info_idx", "")
  397. used_g = filament_elem.get("used_g", "0")
  398. try:
  399. used_grams = float(used_g)
  400. if used_grams > 0 and filament_id:
  401. filaments.append(
  402. {
  403. "slot_id": int(filament_id),
  404. "type": filament_type,
  405. "color": filament_color,
  406. "tray_info_idx": tray_info_idx,
  407. "used_grams": round(used_grams, 1),
  408. }
  409. )
  410. except (ValueError, TypeError):
  411. pass # Skip filament entry with unparseable usage data
  412. filaments.sort(key=lambda x: x["slot_id"])
  413. # Enrich with nozzle mapping for dual-nozzle printers
  414. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
  415. if nozzle_mapping:
  416. for filament in filaments:
  417. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  418. except Exception as e:
  419. logger.warning("Failed to parse filament requirements: %s", e)
  420. return None
  421. return filaments if filaments else None
  422. def _build_loaded_filaments(self, status) -> list[dict]:
  423. """Build list of loaded filaments from printer status.
  424. Args:
  425. status: PrinterState from printer_manager
  426. Returns:
  427. List of loaded filament dicts with type, color, ams_id, tray_id, global_tray_id
  428. """
  429. filaments = []
  430. # Get ams_extruder_map for dual-nozzle printers (H2D, H2D Pro)
  431. ams_extruder_map = status.raw_data.get("ams_extruder_map", {})
  432. # Parse AMS units from raw_data
  433. ams_data = status.raw_data.get("ams", [])
  434. for ams_unit in ams_data:
  435. ams_id = ams_unit.get("id", 0)
  436. trays = ams_unit.get("tray", [])
  437. is_ht = len(trays) == 1 # AMS-HT has single tray
  438. for tray in trays:
  439. tray_type = tray.get("tray_type")
  440. if tray_type:
  441. tray_id = tray.get("id", 0)
  442. tray_color = tray.get("tray_color", "")
  443. # tray_info_idx identifies the specific spool (e.g., "GFA00", "P4d64437")
  444. tray_info_idx = tray.get("tray_info_idx", "")
  445. # Normalize color: remove alpha, add hash
  446. color = self._normalize_color(tray_color)
  447. # Calculate global tray ID
  448. # AMS-HT units have IDs starting at 128 with a single tray
  449. global_tray_id = ams_id if ams_id >= 128 else ams_id * 4 + tray_id
  450. filaments.append(
  451. {
  452. "type": tray_type,
  453. "color": color,
  454. "tray_info_idx": tray_info_idx,
  455. "ams_id": ams_id,
  456. "tray_id": tray_id,
  457. "is_ht": is_ht,
  458. "is_external": False,
  459. "global_tray_id": global_tray_id,
  460. "extruder_id": ams_extruder_map.get(str(ams_id)),
  461. }
  462. )
  463. # Check external spool(s) (vt_tray is a list)
  464. for idx, vt in enumerate(status.raw_data.get("vt_tray") or []):
  465. if vt.get("tray_type"):
  466. color = self._normalize_color(vt.get("tray_color", ""))
  467. tray_id = int(vt.get("id", 254))
  468. filaments.append(
  469. {
  470. "type": vt["tray_type"],
  471. "color": color,
  472. "tray_info_idx": vt.get("tray_info_idx", ""),
  473. "ams_id": -1,
  474. "tray_id": idx,
  475. "is_ht": False,
  476. "is_external": True,
  477. "global_tray_id": tray_id,
  478. "extruder_id": (tray_id - 254) if ams_extruder_map else None,
  479. }
  480. )
  481. return filaments
  482. def _normalize_color(self, color: str | None) -> str:
  483. """Normalize color to #RRGGBB format."""
  484. if not color:
  485. return "#808080"
  486. hex_color = color.replace("#", "")[:6]
  487. return f"#{hex_color}"
  488. def _normalize_color_for_compare(self, color: str | None) -> str:
  489. """Normalize color for comparison (lowercase, no hash)."""
  490. if not color:
  491. return ""
  492. return color.replace("#", "").lower()[:6]
  493. def _colors_are_similar(self, color1: str | None, color2: str | None, threshold: int = 40) -> bool:
  494. """Check if two colors are visually similar within a threshold."""
  495. hex1 = self._normalize_color_for_compare(color1)
  496. hex2 = self._normalize_color_for_compare(color2)
  497. if not hex1 or not hex2 or len(hex1) < 6 or len(hex2) < 6:
  498. return False
  499. try:
  500. r1 = int(hex1[0:2], 16)
  501. g1 = int(hex1[2:4], 16)
  502. b1 = int(hex1[4:6], 16)
  503. r2 = int(hex2[0:2], 16)
  504. g2 = int(hex2[2:4], 16)
  505. b2 = int(hex2[4:6], 16)
  506. return abs(r1 - r2) <= threshold and abs(g1 - g2) <= threshold and abs(b1 - b2) <= threshold
  507. except ValueError:
  508. return False
  509. def _match_filaments_to_slots(self, required: list[dict], loaded: list[dict]) -> list[int] | None:
  510. """Match required filaments to loaded filaments and build AMS mapping.
  511. Priority: unique tray_info_idx match > exact color match > similar color match > type-only match
  512. The tray_info_idx is a filament type identifier stored in the 3MF file when the user
  513. slices (e.g., "GFA00" for generic PLA, "P4d64437" for custom presets). If the same
  514. tray_info_idx appears in only ONE available tray, we use that tray. If multiple trays
  515. have the same tray_info_idx (e.g., two spools of generic PLA), we fall back to color
  516. matching among those trays.
  517. Args:
  518. required: List of required filaments with slot_id, type, color, tray_info_idx
  519. loaded: List of loaded filaments with type, color, tray_info_idx, global_tray_id
  520. Returns:
  521. AMS mapping array (position = slot_id - 1, value = global_tray_id or -1)
  522. """
  523. if not required:
  524. return None
  525. # Track used trays to avoid duplicate assignment
  526. used_tray_ids: set[int] = set()
  527. comparisons = []
  528. for req in required:
  529. req_type = (req.get("type") or "").upper()
  530. req_color = req.get("color", "")
  531. req_tray_info_idx = req.get("tray_info_idx", "")
  532. # Find best match: unique tray_info_idx > exact color > similar color > type-only
  533. idx_match = None
  534. exact_match = None
  535. similar_match = None
  536. type_only_match = None
  537. # Get available trays (not already used)
  538. available = [f for f in loaded if f["global_tray_id"] not in used_tray_ids]
  539. # Nozzle-aware filtering: restrict to trays on the correct nozzle
  540. req_nozzle_id = req.get("nozzle_id")
  541. if req_nozzle_id is not None:
  542. nozzle_filtered = [f for f in available if f.get("extruder_id") == req_nozzle_id]
  543. if nozzle_filtered:
  544. available = nozzle_filtered
  545. # Check if tray_info_idx is unique among available trays
  546. if req_tray_info_idx:
  547. idx_matches = [f for f in available if f.get("tray_info_idx") == req_tray_info_idx]
  548. if len(idx_matches) == 1:
  549. # Unique tray_info_idx - use it as definitive match
  550. idx_match = idx_matches[0]
  551. logger.debug(
  552. f"Matched filament slot {req.get('slot_id')} by unique tray_info_idx={req_tray_info_idx} "
  553. f"-> tray {idx_match['global_tray_id']}"
  554. )
  555. elif len(idx_matches) > 1:
  556. # Multiple trays with same tray_info_idx - use color matching among them
  557. logger.debug(
  558. f"Non-unique tray_info_idx={req_tray_info_idx} found in {len(idx_matches)} trays, "
  559. f"using color matching among trays: {[f['global_tray_id'] for f in idx_matches]}"
  560. )
  561. # Use color matching within this subset
  562. for f in idx_matches:
  563. f_color = f.get("color", "")
  564. if self._normalize_color_for_compare(f_color) == self._normalize_color_for_compare(req_color):
  565. if not exact_match:
  566. exact_match = f
  567. elif self._colors_are_similar(f_color, req_color):
  568. if not similar_match:
  569. similar_match = f
  570. elif not type_only_match:
  571. type_only_match = f
  572. # If no idx_match yet, do standard type/color matching on all available trays
  573. if not idx_match and not exact_match and not similar_match and not type_only_match:
  574. for f in available:
  575. f_type = (f.get("type") or "").upper()
  576. if f_type != req_type:
  577. continue
  578. # Type matches - check color
  579. f_color = f.get("color", "")
  580. if self._normalize_color_for_compare(f_color) == self._normalize_color_for_compare(req_color):
  581. if not exact_match:
  582. exact_match = f
  583. elif self._colors_are_similar(f_color, req_color):
  584. if not similar_match:
  585. similar_match = f
  586. elif not type_only_match:
  587. type_only_match = f
  588. match = idx_match or exact_match or similar_match or type_only_match
  589. if match:
  590. used_tray_ids.add(match["global_tray_id"])
  591. comparisons.append({"slot_id": req.get("slot_id", 0), "global_tray_id": match["global_tray_id"]})
  592. else:
  593. comparisons.append({"slot_id": req.get("slot_id", 0), "global_tray_id": -1})
  594. # Build mapping array
  595. if not comparisons:
  596. return None
  597. max_slot_id = max(c["slot_id"] for c in comparisons)
  598. if max_slot_id <= 0:
  599. return None
  600. mapping = [-1] * max_slot_id
  601. for c in comparisons:
  602. slot_id = c["slot_id"]
  603. if slot_id and slot_id > 0:
  604. mapping[slot_id - 1] = c["global_tray_id"]
  605. return mapping
  606. def _is_printer_idle(self, printer_id: int) -> bool:
  607. """Check if a printer is connected and idle."""
  608. if not printer_manager.is_connected(printer_id):
  609. return False
  610. state = printer_manager.get_status(printer_id)
  611. if not state:
  612. return False
  613. # IDLE = ready for next print
  614. # FINISH/FAILED = ready only if user confirmed plate is cleared
  615. return state.state == "IDLE" or (
  616. state.state in ("FINISH", "FAILED") and printer_manager.is_plate_cleared(printer_id)
  617. )
  618. async def _get_smart_plug(self, db: AsyncSession, printer_id: int) -> SmartPlug | None:
  619. """Get the smart plug associated with a printer."""
  620. result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  621. return result.scalar_one_or_none()
  622. async def _power_on_and_wait(self, plug: SmartPlug, printer_id: int, db: AsyncSession) -> bool:
  623. """Turn on smart plug and wait for printer to connect.
  624. Returns True if printer connected successfully within timeout.
  625. """
  626. # Get the appropriate service for the plug type (Tasmota or Home Assistant)
  627. service = await smart_plug_manager.get_service_for_plug(plug, db)
  628. # Check current plug state
  629. status = await service.get_status(plug)
  630. if not status.get("reachable"):
  631. logger.warning("Smart plug '%s' is not reachable", plug.name)
  632. return False
  633. # Turn on if not already on
  634. if status.get("state") != "ON":
  635. success = await service.turn_on(plug)
  636. if not success:
  637. logger.warning("Failed to turn on smart plug '%s'", plug.name)
  638. return False
  639. logger.info("Powered on smart plug '%s' for printer %s", plug.name, printer_id)
  640. # Get printer from database for connection
  641. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  642. printer = result.scalar_one_or_none()
  643. if not printer:
  644. logger.error("Printer %s not found in database", printer_id)
  645. return False
  646. # Wait for printer to boot (give it some time before trying to connect)
  647. logger.info("Waiting 30s for printer %s to boot...", printer_id)
  648. await asyncio.sleep(30)
  649. # Try to connect to the printer periodically
  650. elapsed = 30 # Already waited 30s
  651. while elapsed < self._power_on_wait_time:
  652. # Try to connect
  653. logger.info("Attempting to connect to printer %s...", printer_id)
  654. try:
  655. connected = await printer_manager.connect_printer(printer)
  656. if connected:
  657. logger.info("Printer %s connected after %ss", printer_id, elapsed)
  658. # Give it a moment to stabilize and get status
  659. await asyncio.sleep(5)
  660. return True
  661. except Exception as e:
  662. logger.debug("Connection attempt failed: %s", e)
  663. await asyncio.sleep(self._power_on_check_interval)
  664. elapsed += self._power_on_check_interval
  665. logger.debug("Waiting for printer %s to connect... (%ss)", printer_id, elapsed)
  666. logger.warning("Printer %s did not connect within %ss after power on", printer_id, self._power_on_wait_time)
  667. return False
  668. async def _check_previous_success(self, db: AsyncSession, item: PrintQueueItem) -> bool:
  669. """Check if the previous print on this printer succeeded."""
  670. # Find the most recent completed queue item for this printer
  671. result = await db.execute(
  672. select(PrintQueueItem)
  673. .where(PrintQueueItem.printer_id == item.printer_id)
  674. .where(PrintQueueItem.id != item.id)
  675. .where(PrintQueueItem.status.in_(["completed", "failed", "skipped", "aborted"]))
  676. .order_by(PrintQueueItem.completed_at.desc())
  677. .limit(1)
  678. )
  679. prev_item = result.scalar_one_or_none()
  680. # If no previous item, assume success (first in queue)
  681. if not prev_item:
  682. return True
  683. return prev_item.status == "completed"
  684. async def _power_off_if_needed(self, db: AsyncSession, item: PrintQueueItem):
  685. """Power off printer if auto_off_after is enabled (waits for cooldown)."""
  686. if not item.auto_off_after:
  687. return
  688. plug = await self._get_smart_plug(db, item.printer_id)
  689. if plug and plug.enabled:
  690. logger.info("Auto-off: Waiting for printer %s to cool down before power off...", item.printer_id)
  691. # Wait for cooldown (up to 10 minutes)
  692. await printer_manager.wait_for_cooldown(item.printer_id, target_temp=50.0, timeout=600)
  693. logger.info("Auto-off: Powering off printer %s", item.printer_id)
  694. service = await smart_plug_manager.get_service_for_plug(plug, db)
  695. await service.turn_off(plug)
  696. async def _get_job_name(self, db: AsyncSession, item: PrintQueueItem) -> str:
  697. """Get a human-readable name for a queue item."""
  698. if item.archive_id:
  699. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  700. archive = result.scalar_one_or_none()
  701. if archive:
  702. return archive.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  703. if item.library_file_id:
  704. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  705. library_file = result.scalar_one_or_none()
  706. if library_file:
  707. return library_file.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  708. return f"Job #{item.id}"
  709. async def _get_printer(self, db: AsyncSession, printer_id: int) -> Printer | None:
  710. """Get printer by ID."""
  711. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  712. return result.scalar_one_or_none()
  713. async def _start_print(self, db: AsyncSession, item: PrintQueueItem):
  714. """Upload file and start print for a queue item.
  715. Supports two sources:
  716. - archive_id: Print from an existing archive
  717. - library_file_id: Print from a library file (file manager)
  718. """
  719. logger.info("Starting queue item %s", item.id)
  720. # Get printer first (needed for both paths)
  721. result = await db.execute(select(Printer).where(Printer.id == item.printer_id))
  722. printer = result.scalar_one_or_none()
  723. if not printer:
  724. item.status = "failed"
  725. item.error_message = "Printer not found"
  726. item.completed_at = datetime.utcnow()
  727. await db.commit()
  728. logger.error("Queue item %s: Printer %s not found", item.id, item.printer_id)
  729. await self._power_off_if_needed(db, item)
  730. return
  731. # Check printer is connected
  732. if not printer_manager.is_connected(item.printer_id):
  733. item.status = "failed"
  734. item.error_message = "Printer not connected"
  735. item.completed_at = datetime.utcnow()
  736. await db.commit()
  737. logger.error("Queue item %s: Printer %s not connected", item.id, item.printer_id)
  738. await self._power_off_if_needed(db, item)
  739. return
  740. # Determine source: archive or library file
  741. archive = None
  742. library_file = None
  743. file_path = None
  744. filename = None
  745. if item.archive_id:
  746. # Print from archive
  747. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  748. archive = result.scalar_one_or_none()
  749. if not archive:
  750. item.status = "failed"
  751. item.error_message = "Archive not found"
  752. item.completed_at = datetime.utcnow()
  753. await db.commit()
  754. logger.error("Queue item %s: Archive %s not found", item.id, item.archive_id)
  755. await self._power_off_if_needed(db, item)
  756. return
  757. file_path = settings.base_dir / archive.file_path
  758. filename = archive.filename
  759. elif item.library_file_id:
  760. # Print from library file (file manager)
  761. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  762. library_file = result.scalar_one_or_none()
  763. if not library_file:
  764. item.status = "failed"
  765. item.error_message = "Library file not found"
  766. item.completed_at = datetime.utcnow()
  767. await db.commit()
  768. logger.error("Queue item %s: Library file %s not found", item.id, item.library_file_id)
  769. await self._power_off_if_needed(db, item)
  770. return
  771. # Library files store absolute paths
  772. lib_path = Path(library_file.file_path)
  773. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  774. filename = library_file.filename
  775. # Create archive from library file so usage tracking has access to the 3MF
  776. try:
  777. from backend.app.services.archive import ArchiveService
  778. archive_service = ArchiveService(db)
  779. archive = await archive_service.archive_print(
  780. printer_id=item.printer_id,
  781. source_file=file_path,
  782. )
  783. if archive:
  784. item.archive_id = archive.id
  785. await db.flush()
  786. logger.info(
  787. "Queue item %s: Created archive %s from library file %s",
  788. item.id,
  789. archive.id,
  790. item.library_file_id,
  791. )
  792. except Exception as e:
  793. logger.warning("Queue item %s: Failed to create archive from library file: %s", item.id, e)
  794. else:
  795. # Neither archive nor library file specified
  796. item.status = "failed"
  797. item.error_message = "No source file specified"
  798. item.completed_at = datetime.utcnow()
  799. await db.commit()
  800. logger.error("Queue item %s: No archive_id or library_file_id specified", item.id)
  801. await self._power_off_if_needed(db, item)
  802. return
  803. # Check file exists on disk
  804. if not file_path.exists():
  805. item.status = "failed"
  806. item.error_message = "Source file not found on disk"
  807. item.completed_at = datetime.utcnow()
  808. await db.commit()
  809. logger.error("Queue item %s: File not found: %s", item.id, file_path)
  810. await self._power_off_if_needed(db, item)
  811. return
  812. # Upload file to printer via FTP
  813. # Use a clean filename to avoid issues with double extensions like .gcode.3mf
  814. base_name = filename
  815. if base_name.endswith(".gcode.3mf"):
  816. base_name = base_name[:-10] # Remove .gcode.3mf
  817. elif base_name.endswith(".3mf"):
  818. base_name = base_name[:-4] # Remove .3mf
  819. remote_filename = f"{base_name}.3mf"
  820. # Upload to root directory (not /cache/) - the start_print command references
  821. # files by name only (ftp://{filename}), so they must be in the root
  822. remote_path = f"/{remote_filename}"
  823. # Get FTP retry settings
  824. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  825. logger.info(
  826. f"Queue item {item.id}: FTP upload starting - printer={printer.name} ({printer.model}), "
  827. f"ip={printer.ip_address}, file={remote_filename}, local_path={file_path}, "
  828. f"retry_enabled={ftp_retry_enabled}, retry_count={ftp_retry_count}, timeout={ftp_timeout}"
  829. )
  830. # Delete existing file if present (avoids 553 error on overwrite)
  831. try:
  832. logger.debug("Queue item %s: Deleting existing file %s if present...", item.id, remote_path)
  833. delete_result = await delete_file_async(
  834. printer.ip_address,
  835. printer.access_code,
  836. remote_path,
  837. socket_timeout=ftp_timeout,
  838. printer_model=printer.model,
  839. )
  840. logger.debug("Queue item %s: Delete result: %s", item.id, delete_result)
  841. except Exception as e:
  842. logger.debug("Queue item %s: Delete failed (may not exist): %s", item.id, e)
  843. try:
  844. if ftp_retry_enabled:
  845. uploaded = await with_ftp_retry(
  846. upload_file_async,
  847. printer.ip_address,
  848. printer.access_code,
  849. file_path,
  850. remote_path,
  851. socket_timeout=ftp_timeout,
  852. printer_model=printer.model,
  853. max_retries=ftp_retry_count,
  854. retry_delay=ftp_retry_delay,
  855. operation_name=f"Upload print to {printer.name}",
  856. )
  857. else:
  858. uploaded = await upload_file_async(
  859. printer.ip_address,
  860. printer.access_code,
  861. file_path,
  862. remote_path,
  863. socket_timeout=ftp_timeout,
  864. printer_model=printer.model,
  865. )
  866. except Exception as e:
  867. uploaded = False
  868. logger.error("Queue item %s: FTP error: %s (type: %s)", item.id, e, type(e).__name__)
  869. if not uploaded:
  870. error_msg = (
  871. "Failed to upload file to printer. Check if SD card is inserted and properly formatted (FAT32/exFAT). "
  872. "See server logs for detailed diagnostics."
  873. )
  874. item.status = "failed"
  875. item.error_message = error_msg
  876. item.completed_at = datetime.utcnow()
  877. await db.commit()
  878. logger.error(
  879. f"Queue item {item.id}: FTP upload failed - printer={printer.name}, model={printer.model}, "
  880. f"ip={printer.ip_address}. Check logs above for storage diagnostics and specific error codes."
  881. )
  882. # Send failure notification
  883. await notification_service.on_queue_job_failed(
  884. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  885. printer_id=printer.id,
  886. printer_name=printer.name,
  887. reason="Failed to upload file to printer",
  888. db=db,
  889. )
  890. await self._power_off_if_needed(db, item)
  891. return
  892. # Parse AMS mapping if stored
  893. ams_mapping = None
  894. if item.ams_mapping:
  895. try:
  896. ams_mapping = json.loads(item.ams_mapping)
  897. except json.JSONDecodeError:
  898. logger.warning("Queue item %s: Invalid AMS mapping JSON, ignoring", item.id)
  899. # Register as expected print so we don't create a duplicate archive
  900. # Only applicable for archive-based prints
  901. if archive:
  902. from backend.app.main import register_expected_print
  903. register_expected_print(item.printer_id, remote_filename, archive.id, ams_mapping=ams_mapping)
  904. # IMPORTANT: Set status to "printing" BEFORE sending the print command.
  905. # This prevents phantom reprints if the backend crashes/restarts after the
  906. # print command is sent but before the status update is committed.
  907. # If we crash after this commit but before start_print(), the item will be
  908. # in "printing" status without actually printing - but that's safer than
  909. # accidentally reprinting the same file hours later.
  910. item.status = "printing"
  911. item.started_at = datetime.utcnow()
  912. await db.commit()
  913. # Consume the plate-cleared flag now that we're starting a print
  914. printer_manager.consume_plate_cleared(item.printer_id)
  915. logger.info("Queue item %s: Status set to 'printing', sending print command...", item.id)
  916. # Start the print with AMS mapping, plate_id and print options
  917. started = printer_manager.start_print(
  918. item.printer_id,
  919. remote_filename,
  920. plate_id=item.plate_id or 1,
  921. ams_mapping=ams_mapping,
  922. bed_levelling=item.bed_levelling,
  923. flow_cali=item.flow_cali,
  924. vibration_cali=item.vibration_cali,
  925. layer_inspect=item.layer_inspect,
  926. timelapse=item.timelapse,
  927. use_ams=item.use_ams,
  928. )
  929. if started:
  930. logger.info("Queue item %s: Print started successfully - %s", item.id, filename)
  931. # Get estimated time for notification
  932. estimated_time = None
  933. if archive and archive.print_time_seconds:
  934. estimated_time = archive.print_time_seconds
  935. elif library_file and library_file.print_time_seconds:
  936. estimated_time = library_file.print_time_seconds
  937. # Send job started notification
  938. await notification_service.on_queue_job_started(
  939. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  940. printer_id=printer.id,
  941. printer_name=printer.name,
  942. db=db,
  943. estimated_time=estimated_time,
  944. )
  945. # MQTT relay - publish queue job started
  946. try:
  947. from backend.app.services.mqtt_relay import mqtt_relay
  948. await mqtt_relay.on_queue_job_started(
  949. job_id=item.id,
  950. filename=filename,
  951. printer_id=printer.id,
  952. printer_name=printer.name,
  953. printer_serial=printer.serial_number,
  954. )
  955. except Exception:
  956. pass # Don't fail if MQTT fails
  957. else:
  958. # Print command failed - revert status
  959. item.status = "failed"
  960. item.error_message = "Failed to send print command to printer"
  961. item.completed_at = datetime.utcnow()
  962. await db.commit()
  963. logger.error(
  964. f"Queue item {item.id}: Failed to start print on {printer.name} ({printer.model}) - "
  965. f"printer_manager.start_print() returned False. "
  966. f"This may indicate: printer not connected, MQTT error, unsupported model configuration, or firmware issue. "
  967. f"Check printer status and backend logs for details."
  968. )
  969. # Send failure notification
  970. await notification_service.on_queue_job_failed(
  971. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  972. printer_id=printer.id,
  973. printer_name=printer.name,
  974. reason="Failed to send print command to printer - check printer connection and status",
  975. db=db,
  976. )
  977. await self._power_off_if_needed(db, item)
  978. # Global scheduler instance
  979. scheduler = PrintScheduler()