print_scheduler.py 59 KB

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