print_scheduler.py 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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. filament_overrides: Optional list of override dicts. Each entry may include
  265. ``force_color_match: true`` to require an exact type+color match
  266. on the printer for that slot. Without the flag the existing
  267. colour-preference logic applies.
  268. Returns:
  269. Tuple of (printer_id, waiting_reason):
  270. - (printer_id, None) if a matching printer was found
  271. - (None, reason) if no printer is available, with explanation
  272. """
  273. # Normalize model name and use case-insensitive matching
  274. normalized_model = normalize_printer_model(model) or model
  275. query = (
  276. select(Printer)
  277. .where(func.lower(Printer.model) == normalized_model.lower())
  278. .where(Printer.is_active == True) # noqa: E712
  279. )
  280. # Add location filter if specified
  281. if target_location:
  282. query = query.where(Printer.location == target_location)
  283. result = await db.execute(query)
  284. printers = list(result.scalars().all())
  285. location_suffix = f" in {target_location}" if target_location else ""
  286. if not printers:
  287. return None, f"No active {normalized_model} printers{location_suffix} configured"
  288. # Separate force-matched overrides from preference-only overrides
  289. force_overrides = [o for o in (filament_overrides or []) if o.get("force_color_match")]
  290. pref_overrides = [o for o in (filament_overrides or []) if not o.get("force_color_match")]
  291. # Track reasons for skipping printers
  292. printers_busy = []
  293. printers_offline = []
  294. printers_missing_filament: list[tuple[str, list[str]]] = []
  295. candidates: list[tuple[int, int]] = [] # (printer_id, color_match_count)
  296. for printer in printers:
  297. if printer.id in exclude_ids:
  298. # Printer is already claimed by another job in this scheduling run.
  299. # For force-color jobs, still check if the color would match — if not,
  300. # report it as a color mismatch rather than plain "Busy" so the user
  301. # knows the job needs a filament change, not just to wait for availability.
  302. if force_overrides and not pref_overrides:
  303. missing_colors = self._get_missing_force_color_slots(printer.id, force_overrides)
  304. if missing_colors:
  305. printers_missing_filament.append((printer.name, missing_colors))
  306. continue
  307. printers_busy.append(printer.name)
  308. continue
  309. is_connected = printer_manager.is_connected(printer.id)
  310. is_idle = self._is_printer_idle(printer.id) if is_connected else False
  311. if not is_connected:
  312. printers_offline.append(printer.name)
  313. continue
  314. if not is_idle:
  315. # Printer is currently printing. For force-color jobs, check whether the
  316. # loaded color would satisfy the requirement — if not, surface it as a
  317. # color-mismatch reason rather than plain "Busy" so the user understands
  318. # that the job is waiting for a filament change, not just printer availability.
  319. if force_overrides and not pref_overrides:
  320. missing_colors = self._get_missing_force_color_slots(printer.id, force_overrides)
  321. if missing_colors:
  322. printers_missing_filament.append((printer.name, missing_colors))
  323. logger.debug(
  324. "Printer %s (%s) is busy but also has wrong force-color: %s",
  325. printer.id,
  326. printer.name,
  327. missing_colors,
  328. )
  329. continue
  330. printers_busy.append(printer.name)
  331. continue
  332. # Validate filament compatibility if required types are specified
  333. if required_filament_types:
  334. missing = self._get_missing_filament_types(printer.id, required_filament_types)
  335. if missing:
  336. # When force_overrides are present, enrich missing entries with color info
  337. # so the "Waiting on" message includes "TYPE (color)" instead of just "TYPE"
  338. if force_overrides:
  339. force_color_map = {
  340. (o.get("type") or "").upper(): o.get("color_name") or o.get("color", "?")
  341. for o in force_overrides
  342. }
  343. missing_enriched = [
  344. f"{t} ({force_color_map[t_upper]})" if (t_upper := t.upper()) in force_color_map else t
  345. for t in missing
  346. ]
  347. printers_missing_filament.append((printer.name, missing_enriched))
  348. else:
  349. printers_missing_filament.append((printer.name, missing))
  350. logger.debug("Skipping printer %s (%s) - missing filaments: %s", printer.id, printer.name, missing)
  351. continue
  352. # Force color match: ALL flagged slots must have an exact type+color match
  353. if force_overrides:
  354. missing_colors = self._get_missing_force_color_slots(printer.id, force_overrides)
  355. if missing_colors:
  356. printers_missing_filament.append((printer.name, missing_colors))
  357. logger.debug(
  358. "Skipping printer %s (%s) - missing force-matched colors: %s",
  359. printer.id,
  360. printer.name,
  361. missing_colors,
  362. )
  363. continue
  364. # If preference-only overrides exist, rank by color matches (existing behaviour)
  365. if pref_overrides:
  366. color_matches = self._count_override_color_matches(printer.id, pref_overrides)
  367. if color_matches > 0:
  368. candidates.append((printer.id, color_matches))
  369. else:
  370. override_colors = [f"{o.get('type', '?')} ({o.get('color', '?')})" for o in pref_overrides]
  371. printers_missing_filament.append((printer.name, override_colors))
  372. logger.debug("Skipping printer %s (%s) - no matching override colors", printer.id, printer.name)
  373. continue
  374. elif force_overrides:
  375. # Passed all force checks — immediately eligible (no preference ordering needed)
  376. return printer.id, None
  377. else:
  378. # No overrides at all - take first available (existing behavior)
  379. return printer.id, None
  380. # If we have candidates from preference override matching, pick the one with most color matches
  381. if candidates:
  382. candidates.sort(key=lambda c: c[1], reverse=True)
  383. return candidates[0][0], None
  384. # Build waiting reason from what we found
  385. reasons = []
  386. if printers_missing_filament:
  387. # Filament/color mismatch is most actionable - show first
  388. if force_overrides and not pref_overrides:
  389. # All mismatches are force-color failures — use descriptive message only;
  390. # but only if there are no busy printers that DO have the matching color.
  391. # If a printer has the right color but is busy, surface "Busy" instead so
  392. # the user knows the job will start automatically once that printer is free.
  393. if not printers_busy:
  394. all_missing = sorted({c for _, cols in printers_missing_filament for c in cols})
  395. return None, f"No matching material/color. Waiting on {', '.join(all_missing)}"
  396. # else: fall through — printers_busy will be appended below
  397. else:
  398. names_and_missing = [
  399. f"{name} (needs {', '.join(missing)})" for name, missing in printers_missing_filament
  400. ]
  401. reasons.append(f"Waiting for filament: {'; '.join(names_and_missing)}")
  402. if printers_busy:
  403. reasons.append(f"Busy: {', '.join(printers_busy)}")
  404. if printers_offline:
  405. reasons.append(f"Offline: {', '.join(printers_offline)}")
  406. return None, " | ".join(reasons) if reasons else f"No available {model} printers{location_suffix}"
  407. def _get_missing_force_color_slots(self, printer_id: int, force_overrides: list[dict]) -> list[str]:
  408. """Return descriptive strings for force_color_match slots not satisfied by the printer.
  409. Each entry in ``force_overrides`` must have ``type`` and ``color`` fields and is expected
  410. to carry ``force_color_match: True``. The printer must have **every** such slot loaded
  411. with an exact type+color match.
  412. Returns:
  413. List of ``"TYPE (color)"`` strings for unmatched slots (empty list means all match).
  414. """
  415. status = printer_manager.get_status(printer_id)
  416. if not status:
  417. return [f"{o.get('type', '?')} ({o.get('color_name') or o.get('color', '?')})" for o in force_overrides]
  418. # Build set of loaded type+colour pairs from AMS and external spool
  419. loaded: set[tuple[str, str]] = set()
  420. for ams_unit in status.raw_data.get("ams", []):
  421. for tray in ams_unit.get("tray", []):
  422. tray_type = tray.get("tray_type")
  423. tray_color = tray.get("tray_color", "")
  424. if tray_type:
  425. color_norm = tray_color.replace("#", "").lower()[:6]
  426. loaded.add((tray_type.upper(), color_norm))
  427. for vt in status.raw_data.get("vt_tray") or []:
  428. vt_type = vt.get("tray_type")
  429. if vt_type:
  430. color_norm = (vt.get("tray_color", "") or "").replace("#", "").lower()[:6]
  431. loaded.add((vt_type.upper(), color_norm))
  432. missing = []
  433. for o in force_overrides:
  434. o_type = (o.get("type") or "").upper()
  435. o_color = (o.get("color") or "").replace("#", "").lower()[:6]
  436. if (o_type, o_color) not in loaded:
  437. color_label = o.get("color_name") or o.get("color", "?")
  438. missing.append(f"{o_type} ({color_label})")
  439. return missing
  440. def _get_missing_filament_types(self, printer_id: int, required_types: list[str]) -> list[str]:
  441. """Get the list of required filament types that are not loaded on the printer.
  442. Args:
  443. printer_id: The printer ID
  444. required_types: List of filament types needed (e.g., ["PLA", "PETG"])
  445. Returns:
  446. List of missing filament types (empty if all are loaded)
  447. """
  448. status = printer_manager.get_status(printer_id)
  449. if not status:
  450. return required_types # Can't determine, assume all missing
  451. # Collect all filament types loaded on this printer (AMS units + external spool)
  452. loaded_types: set[str] = set()
  453. # Check AMS units (stored in raw_data["ams"])
  454. ams_data = status.raw_data.get("ams", [])
  455. if ams_data:
  456. for ams_unit in ams_data:
  457. for tray in ams_unit.get("tray", []):
  458. tray_type = tray.get("tray_type")
  459. if tray_type:
  460. loaded_types.add(tray_type.upper())
  461. # Check external spool(s) (virtual tray, stored in raw_data["vt_tray"] as list)
  462. for vt in status.raw_data.get("vt_tray") or []:
  463. vt_type = vt.get("tray_type")
  464. if vt_type:
  465. loaded_types.add(vt_type.upper())
  466. # Find which required types are missing (case-insensitive comparison)
  467. missing = []
  468. for req_type in required_types:
  469. if req_type.upper() not in loaded_types:
  470. missing.append(req_type)
  471. return missing
  472. def _count_override_color_matches(self, printer_id: int, overrides: list[dict]) -> int:
  473. """Count how many filament overrides have an exact color match on the printer.
  474. Used to prefer printers that already have the desired override colors loaded.
  475. """
  476. status = printer_manager.get_status(printer_id)
  477. if not status:
  478. return 0
  479. # Collect loaded filaments' type+color pairs
  480. loaded: set[tuple[str, str]] = set()
  481. for ams_unit in status.raw_data.get("ams", []):
  482. for tray in ams_unit.get("tray", []):
  483. tray_type = tray.get("tray_type")
  484. tray_color = tray.get("tray_color", "")
  485. if tray_type:
  486. color_norm = tray_color.replace("#", "").lower()[:6]
  487. loaded.add((tray_type.upper(), color_norm))
  488. for vt in status.raw_data.get("vt_tray") or []:
  489. vt_type = vt.get("tray_type")
  490. if vt_type:
  491. color_norm = (vt.get("tray_color", "") or "").replace("#", "").lower()[:6]
  492. loaded.add((vt_type.upper(), color_norm))
  493. matches = 0
  494. for o in overrides:
  495. o_type = (o.get("type") or "").upper()
  496. o_color = (o.get("color") or "").replace("#", "").lower()[:6]
  497. if (o_type, o_color) in loaded:
  498. matches += 1
  499. return matches
  500. async def _compute_ams_mapping_for_printer(
  501. self, db: AsyncSession, printer_id: int, item: PrintQueueItem
  502. ) -> list[int] | None:
  503. """Compute AMS mapping for a printer based on filament requirements.
  504. Called when a queue item has no ams_mapping set — either for model-based
  505. items after printer assignment, or printer-specific items (e.g. from VP).
  506. Args:
  507. db: Database session
  508. printer_id: The assigned printer ID
  509. item: The queue item (contains archive_id or library_file_id)
  510. Returns:
  511. AMS mapping array or None if no mapping needed/possible
  512. """
  513. # Get printer status
  514. status = printer_manager.get_status(printer_id)
  515. if not status:
  516. logger.warning("Cannot compute AMS mapping: printer %s status unavailable", printer_id)
  517. return None
  518. # Get filament requirements from source file
  519. filament_reqs = await self._get_filament_requirements(db, item)
  520. if not filament_reqs:
  521. logger.debug("No filament requirements found for queue item %s", item.id)
  522. return None
  523. # Apply filament overrides if present
  524. if item.filament_overrides:
  525. try:
  526. overrides = json.loads(item.filament_overrides)
  527. override_map = {o["slot_id"]: o for o in overrides}
  528. for req in filament_reqs:
  529. if req["slot_id"] in override_map:
  530. override = override_map[req["slot_id"]]
  531. req["type"] = override["type"]
  532. req["color"] = override["color"]
  533. # Clear tray_info_idx so matching uses type+color instead of
  534. # the original 3MF's tray_info_idx (which would match the old filament)
  535. req["tray_info_idx"] = ""
  536. logger.debug(
  537. "Queue item %s: Override slot %d -> %s %s",
  538. item.id,
  539. req["slot_id"],
  540. override["type"],
  541. override["color"],
  542. )
  543. except (json.JSONDecodeError, KeyError, TypeError) as e:
  544. logger.warning("Failed to apply filament overrides for queue item %s: %s", item.id, e)
  545. # Build loaded filaments from printer status
  546. loaded_filaments = self._build_loaded_filaments(status)
  547. if not loaded_filaments:
  548. logger.debug("No filaments loaded on printer %s", printer_id)
  549. return None
  550. # Compute mapping: match required filaments to available slots
  551. return self._match_filaments_to_slots(filament_reqs, loaded_filaments)
  552. async def _get_filament_requirements(self, db: AsyncSession, item: PrintQueueItem) -> list[dict] | None:
  553. """Extract filament requirements from the source 3MF file.
  554. Args:
  555. db: Database session
  556. item: Queue item with archive_id or library_file_id
  557. Returns:
  558. List of filament requirement dicts with slot_id, type, color, used_grams
  559. """
  560. file_path: Path | None = None
  561. if item.archive_id:
  562. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  563. archive = result.scalar_one_or_none()
  564. if archive:
  565. file_path = settings.base_dir / archive.file_path
  566. elif item.library_file_id:
  567. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  568. library_file = result.scalar_one_or_none()
  569. if library_file:
  570. lib_path = Path(library_file.file_path)
  571. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  572. if not file_path or not file_path.exists():
  573. return None
  574. filaments = []
  575. try:
  576. with zipfile.ZipFile(file_path, "r") as zf:
  577. if "Metadata/slice_info.config" not in zf.namelist():
  578. return None
  579. content = zf.read("Metadata/slice_info.config").decode()
  580. root = ET.fromstring(content)
  581. # Check if plate_id is specified - use that plate's filaments
  582. plate_id = item.plate_id
  583. if plate_id:
  584. for plate_elem in root.findall("./plate"):
  585. plate_index = None
  586. for meta in plate_elem.findall("metadata"):
  587. if meta.get("key") == "index":
  588. plate_index = int(meta.get("value", "0"))
  589. break
  590. if plate_index == plate_id:
  591. for filament_elem in plate_elem.findall("./filament"):
  592. filament_id = filament_elem.get("id")
  593. filament_type = filament_elem.get("type", "")
  594. filament_color = filament_elem.get("color", "")
  595. # tray_info_idx identifies the specific spool selected when slicing
  596. tray_info_idx = filament_elem.get("tray_info_idx", "")
  597. used_g = filament_elem.get("used_g", "0")
  598. try:
  599. used_grams = float(used_g)
  600. if used_grams > 0 and filament_id:
  601. filaments.append(
  602. {
  603. "slot_id": int(filament_id),
  604. "type": filament_type,
  605. "color": filament_color,
  606. "tray_info_idx": tray_info_idx,
  607. "used_grams": round(used_grams, 1),
  608. }
  609. )
  610. except (ValueError, TypeError):
  611. pass # Skip filament entry with unparseable usage data
  612. break
  613. else:
  614. # No plate_id - extract all filaments with used_g > 0
  615. for filament_elem in root.findall("./filament"):
  616. filament_id = filament_elem.get("id")
  617. filament_type = filament_elem.get("type", "")
  618. filament_color = filament_elem.get("color", "")
  619. # tray_info_idx identifies the specific spool selected when slicing
  620. tray_info_idx = filament_elem.get("tray_info_idx", "")
  621. used_g = filament_elem.get("used_g", "0")
  622. try:
  623. used_grams = float(used_g)
  624. if used_grams > 0 and filament_id:
  625. filaments.append(
  626. {
  627. "slot_id": int(filament_id),
  628. "type": filament_type,
  629. "color": filament_color,
  630. "tray_info_idx": tray_info_idx,
  631. "used_grams": round(used_grams, 1),
  632. }
  633. )
  634. except (ValueError, TypeError):
  635. pass # Skip filament entry with unparseable usage data
  636. filaments.sort(key=lambda x: x["slot_id"])
  637. # Enrich with nozzle mapping for dual-nozzle printers
  638. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
  639. if nozzle_mapping:
  640. for filament in filaments:
  641. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  642. except Exception as e:
  643. logger.warning("Failed to parse filament requirements: %s", e)
  644. return None
  645. return filaments if filaments else None
  646. def _build_loaded_filaments(self, status) -> list[dict]:
  647. """Build list of loaded filaments from printer status.
  648. Args:
  649. status: PrinterState from printer_manager
  650. Returns:
  651. List of loaded filament dicts with type, color, ams_id, tray_id, global_tray_id
  652. """
  653. filaments = []
  654. # Get ams_extruder_map for dual-nozzle printers (H2D, H2D Pro)
  655. ams_extruder_map = status.raw_data.get("ams_extruder_map", {})
  656. # Parse AMS units from raw_data
  657. ams_data = status.raw_data.get("ams", [])
  658. for ams_unit in ams_data:
  659. ams_id = int(ams_unit.get("id", 0))
  660. trays = ams_unit.get("tray", [])
  661. is_ht = len(trays) == 1 # AMS-HT has single tray
  662. for tray in trays:
  663. tray_type = tray.get("tray_type")
  664. if tray_type:
  665. tray_id = int(tray.get("id", 0))
  666. tray_color = tray.get("tray_color", "")
  667. # tray_info_idx identifies the specific spool (e.g., "GFA00", "P4d64437")
  668. tray_info_idx = tray.get("tray_info_idx", "")
  669. # Normalize color: remove alpha, add hash
  670. color = self._normalize_color(tray_color)
  671. # Calculate global tray ID
  672. # AMS-HT units have IDs starting at 128 with a single tray
  673. global_tray_id = ams_id if ams_id >= 128 else ams_id * 4 + tray_id
  674. filaments.append(
  675. {
  676. "type": tray_type,
  677. "color": color,
  678. "tray_info_idx": tray_info_idx,
  679. "ams_id": ams_id,
  680. "tray_id": tray_id,
  681. "is_ht": is_ht,
  682. "is_external": False,
  683. "global_tray_id": global_tray_id,
  684. "extruder_id": ams_extruder_map.get(str(ams_id)),
  685. }
  686. )
  687. # Check external spool(s) (vt_tray is a list)
  688. for idx, vt in enumerate(status.raw_data.get("vt_tray") or []):
  689. if vt.get("tray_type"):
  690. color = self._normalize_color(vt.get("tray_color", ""))
  691. tray_id = int(vt.get("id", 254))
  692. filaments.append(
  693. {
  694. "type": vt["tray_type"],
  695. "color": color,
  696. "tray_info_idx": vt.get("tray_info_idx", ""),
  697. "ams_id": -1,
  698. "tray_id": idx,
  699. "is_ht": False,
  700. "is_external": True,
  701. "global_tray_id": tray_id,
  702. "extruder_id": (255 - tray_id) if ams_extruder_map else None,
  703. }
  704. )
  705. return filaments
  706. def _normalize_color(self, color: str | None) -> str:
  707. """Normalize color to #RRGGBB format."""
  708. if not color:
  709. return "#808080"
  710. hex_color = color.replace("#", "")[:6]
  711. return f"#{hex_color}"
  712. def _normalize_color_for_compare(self, color: str | None) -> str:
  713. """Normalize color for comparison (lowercase, no hash)."""
  714. if not color:
  715. return ""
  716. return color.replace("#", "").lower()[:6]
  717. def _colors_are_similar(self, color1: str | None, color2: str | None, threshold: int = 40) -> bool:
  718. """Check if two colors are visually similar within a threshold."""
  719. hex1 = self._normalize_color_for_compare(color1)
  720. hex2 = self._normalize_color_for_compare(color2)
  721. if not hex1 or not hex2 or len(hex1) < 6 or len(hex2) < 6:
  722. return False
  723. try:
  724. r1 = int(hex1[0:2], 16)
  725. g1 = int(hex1[2:4], 16)
  726. b1 = int(hex1[4:6], 16)
  727. r2 = int(hex2[0:2], 16)
  728. g2 = int(hex2[2:4], 16)
  729. b2 = int(hex2[4:6], 16)
  730. return abs(r1 - r2) <= threshold and abs(g1 - g2) <= threshold and abs(b1 - b2) <= threshold
  731. except ValueError:
  732. return False
  733. def _match_filaments_to_slots(self, required: list[dict], loaded: list[dict]) -> list[int] | None:
  734. """Match required filaments to loaded filaments and build AMS mapping.
  735. Priority: unique tray_info_idx match > exact color match > similar color match > type-only match
  736. The tray_info_idx is a filament type identifier stored in the 3MF file when the user
  737. slices (e.g., "GFA00" for generic PLA, "P4d64437" for custom presets). If the same
  738. tray_info_idx appears in only ONE available tray, we use that tray. If multiple trays
  739. have the same tray_info_idx (e.g., two spools of generic PLA), we fall back to color
  740. matching among those trays.
  741. Args:
  742. required: List of required filaments with slot_id, type, color, tray_info_idx
  743. loaded: List of loaded filaments with type, color, tray_info_idx, global_tray_id
  744. Returns:
  745. AMS mapping array (position = slot_id - 1, value = global_tray_id or -1)
  746. """
  747. if not required:
  748. return None
  749. # Track used trays to avoid duplicate assignment
  750. used_tray_ids: set[int] = set()
  751. comparisons = []
  752. for req in required:
  753. req_type = (req.get("type") or "").upper()
  754. req_color = req.get("color", "")
  755. req_tray_info_idx = req.get("tray_info_idx", "")
  756. # Find best match: unique tray_info_idx > exact color > similar color > type-only
  757. idx_match = None
  758. exact_match = None
  759. similar_match = None
  760. type_only_match = None
  761. # Get available trays (not already used)
  762. available = [f for f in loaded if f["global_tray_id"] not in used_tray_ids]
  763. # Nozzle-aware filtering: restrict to trays on the correct nozzle.
  764. # Hard filter — cross-nozzle assignment causes print failures
  765. # ("position of left hotend is abnormal"), so never fall back.
  766. req_nozzle_id = req.get("nozzle_id")
  767. if req_nozzle_id is not None:
  768. available = [f for f in available if f.get("extruder_id") == req_nozzle_id]
  769. # Check if tray_info_idx is unique among available trays
  770. if req_tray_info_idx:
  771. idx_matches = [f for f in available if f.get("tray_info_idx") == req_tray_info_idx]
  772. if len(idx_matches) == 1:
  773. # Unique tray_info_idx - use it as definitive match
  774. idx_match = idx_matches[0]
  775. logger.debug(
  776. f"Matched filament slot {req.get('slot_id')} by unique tray_info_idx={req_tray_info_idx} "
  777. f"-> tray {idx_match['global_tray_id']}"
  778. )
  779. elif len(idx_matches) > 1:
  780. # Multiple trays with same tray_info_idx - use color matching among them
  781. logger.debug(
  782. f"Non-unique tray_info_idx={req_tray_info_idx} found in {len(idx_matches)} trays, "
  783. f"using color matching among trays: {[f['global_tray_id'] for f in idx_matches]}"
  784. )
  785. # Use color matching within this subset
  786. for f in idx_matches:
  787. f_color = f.get("color", "")
  788. if self._normalize_color_for_compare(f_color) == self._normalize_color_for_compare(req_color):
  789. if not exact_match:
  790. exact_match = f
  791. elif self._colors_are_similar(f_color, req_color):
  792. if not similar_match:
  793. similar_match = f
  794. elif not type_only_match:
  795. type_only_match = f
  796. # If no idx_match yet, do standard type/color matching on all available trays
  797. if not idx_match and not exact_match and not similar_match and not type_only_match:
  798. for f in available:
  799. f_type = (f.get("type") or "").upper()
  800. if f_type != req_type:
  801. continue
  802. # Type matches - check color
  803. f_color = f.get("color", "")
  804. if self._normalize_color_for_compare(f_color) == self._normalize_color_for_compare(req_color):
  805. if not exact_match:
  806. exact_match = f
  807. elif self._colors_are_similar(f_color, req_color):
  808. if not similar_match:
  809. similar_match = f
  810. elif not type_only_match:
  811. type_only_match = f
  812. match = idx_match or exact_match or similar_match or type_only_match
  813. if match:
  814. used_tray_ids.add(match["global_tray_id"])
  815. comparisons.append({"slot_id": req.get("slot_id", 0), "global_tray_id": match["global_tray_id"]})
  816. else:
  817. comparisons.append({"slot_id": req.get("slot_id", 0), "global_tray_id": -1})
  818. # Build mapping array
  819. if not comparisons:
  820. return None
  821. max_slot_id = max(c["slot_id"] for c in comparisons)
  822. if max_slot_id <= 0:
  823. return None
  824. mapping = [-1] * max_slot_id
  825. for c in comparisons:
  826. slot_id = c["slot_id"]
  827. if slot_id and slot_id > 0:
  828. mapping[slot_id - 1] = c["global_tray_id"]
  829. return mapping
  830. def _is_printer_idle(self, printer_id: int) -> bool:
  831. """Check if a printer is connected and idle."""
  832. if not printer_manager.is_connected(printer_id):
  833. logger.debug("Printer %d: not connected", printer_id)
  834. return False
  835. state = printer_manager.get_status(printer_id)
  836. if not state:
  837. logger.debug("Printer %d: no status available", printer_id)
  838. return False
  839. # IDLE = ready for next print
  840. # FINISH/FAILED = ready only if user confirmed plate is cleared
  841. idle = state.state == "IDLE" or (
  842. state.state in ("FINISH", "FAILED") and printer_manager.is_plate_cleared(printer_id)
  843. )
  844. if not idle:
  845. logger.debug(
  846. "Printer %d: not idle — state=%s, plate_cleared=%s",
  847. printer_id,
  848. state.state,
  849. printer_manager.is_plate_cleared(printer_id),
  850. )
  851. return idle
  852. async def _get_smart_plug(self, db: AsyncSession, printer_id: int) -> SmartPlug | None:
  853. """Get the smart plug associated with a printer."""
  854. result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  855. return result.scalar_one_or_none()
  856. async def _power_on_and_wait(self, plug: SmartPlug, printer_id: int, db: AsyncSession) -> bool:
  857. """Turn on smart plug and wait for printer to connect.
  858. Returns True if printer connected successfully within timeout.
  859. """
  860. # Get the appropriate service for the plug type (Tasmota or Home Assistant)
  861. service = await smart_plug_manager.get_service_for_plug(plug, db)
  862. # Check current plug state
  863. status = await service.get_status(plug)
  864. if not status.get("reachable"):
  865. logger.warning("Smart plug '%s' is not reachable", plug.name)
  866. return False
  867. # Turn on if not already on
  868. if status.get("state") != "ON":
  869. success = await service.turn_on(plug)
  870. if not success:
  871. logger.warning("Failed to turn on smart plug '%s'", plug.name)
  872. return False
  873. logger.info("Powered on smart plug '%s' for printer %s", plug.name, printer_id)
  874. # Get printer from database for connection
  875. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  876. printer = result.scalar_one_or_none()
  877. if not printer:
  878. logger.error("Printer %s not found in database", printer_id)
  879. return False
  880. # Wait for printer to boot (give it some time before trying to connect)
  881. logger.info("Waiting 30s for printer %s to boot...", printer_id)
  882. await asyncio.sleep(30)
  883. # Try to connect to the printer periodically
  884. elapsed = 30 # Already waited 30s
  885. while elapsed < self._power_on_wait_time:
  886. # Try to connect
  887. logger.info("Attempting to connect to printer %s...", printer_id)
  888. try:
  889. connected = await printer_manager.connect_printer(printer)
  890. if connected:
  891. logger.info("Printer %s connected after %ss", printer_id, elapsed)
  892. # Give it a moment to stabilize and get status
  893. await asyncio.sleep(5)
  894. return True
  895. except Exception as e:
  896. logger.debug("Connection attempt failed: %s", e)
  897. await asyncio.sleep(self._power_on_check_interval)
  898. elapsed += self._power_on_check_interval
  899. logger.debug("Waiting for printer %s to connect... (%ss)", printer_id, elapsed)
  900. logger.warning("Printer %s did not connect within %ss after power on", printer_id, self._power_on_wait_time)
  901. return False
  902. async def _check_previous_success(self, db: AsyncSession, item: PrintQueueItem) -> bool:
  903. """Check if the previous print on this printer succeeded."""
  904. # Find the most recent completed queue item for this printer
  905. result = await db.execute(
  906. select(PrintQueueItem)
  907. .where(PrintQueueItem.printer_id == item.printer_id)
  908. .where(PrintQueueItem.id != item.id)
  909. .where(PrintQueueItem.status.in_(["completed", "failed", "skipped", "aborted"]))
  910. .order_by(PrintQueueItem.completed_at.desc())
  911. .limit(1)
  912. )
  913. prev_item = result.scalar_one_or_none()
  914. # If no previous item, assume success (first in queue)
  915. if not prev_item:
  916. return True
  917. return prev_item.status == "completed"
  918. async def _power_off_if_needed(self, db: AsyncSession, item: PrintQueueItem):
  919. """Power off printer if auto_off_after is enabled (waits for cooldown)."""
  920. if not item.auto_off_after:
  921. return
  922. plug = await self._get_smart_plug(db, item.printer_id)
  923. if plug and plug.enabled:
  924. logger.info("Auto-off: Waiting for printer %s to cool down before power off...", item.printer_id)
  925. # Wait for cooldown (up to 10 minutes)
  926. await printer_manager.wait_for_cooldown(item.printer_id, target_temp=50.0, timeout=600)
  927. logger.info("Auto-off: Powering off printer %s", item.printer_id)
  928. service = await smart_plug_manager.get_service_for_plug(plug, db)
  929. await service.turn_off(plug)
  930. async def _get_job_name(self, db: AsyncSession, item: PrintQueueItem) -> str:
  931. """Get a human-readable name for a queue item."""
  932. if item.archive_id:
  933. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  934. archive = result.scalar_one_or_none()
  935. if archive:
  936. return archive.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  937. if item.library_file_id:
  938. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  939. library_file = result.scalar_one_or_none()
  940. if library_file:
  941. return library_file.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  942. return f"Job #{item.id}"
  943. async def _get_printer(self, db: AsyncSession, printer_id: int) -> Printer | None:
  944. """Get printer by ID."""
  945. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  946. return result.scalar_one_or_none()
  947. async def _start_print(self, db: AsyncSession, item: PrintQueueItem):
  948. """Upload file and start print for a queue item.
  949. Supports two sources:
  950. - archive_id: Print from an existing archive
  951. - library_file_id: Print from a library file (file manager)
  952. """
  953. logger.info("Starting queue item %s", item.id)
  954. # Get printer first (needed for both paths)
  955. result = await db.execute(select(Printer).where(Printer.id == item.printer_id))
  956. printer = result.scalar_one_or_none()
  957. if not printer:
  958. item.status = "failed"
  959. item.error_message = "Printer not found"
  960. item.completed_at = datetime.now(timezone.utc)
  961. await db.commit()
  962. logger.error("Queue item %s: Printer %s not found", item.id, item.printer_id)
  963. await self._power_off_if_needed(db, item)
  964. return
  965. # Check printer is connected
  966. if not printer_manager.is_connected(item.printer_id):
  967. item.status = "failed"
  968. item.error_message = "Printer not connected"
  969. item.completed_at = datetime.now(timezone.utc)
  970. await db.commit()
  971. logger.error("Queue item %s: Printer %s not connected", item.id, item.printer_id)
  972. await self._power_off_if_needed(db, item)
  973. return
  974. # Determine source: archive or library file
  975. archive = None
  976. library_file = None
  977. file_path = None
  978. filename = None
  979. if item.archive_id:
  980. # Print from archive
  981. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  982. archive = result.scalar_one_or_none()
  983. if not archive:
  984. item.status = "failed"
  985. item.error_message = "Archive not found"
  986. item.completed_at = datetime.now(timezone.utc)
  987. await db.commit()
  988. logger.error("Queue item %s: Archive %s not found", item.id, item.archive_id)
  989. await self._power_off_if_needed(db, item)
  990. return
  991. file_path = settings.base_dir / archive.file_path
  992. filename = archive.filename
  993. elif item.library_file_id:
  994. # Print from library file (file manager)
  995. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  996. library_file = result.scalar_one_or_none()
  997. if not library_file:
  998. item.status = "failed"
  999. item.error_message = "Library file not found"
  1000. item.completed_at = datetime.now(timezone.utc)
  1001. await db.commit()
  1002. logger.error("Queue item %s: Library file %s not found", item.id, item.library_file_id)
  1003. await self._power_off_if_needed(db, item)
  1004. return
  1005. # Library files store absolute paths
  1006. lib_path = Path(library_file.file_path)
  1007. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  1008. filename = library_file.filename
  1009. # Create archive from library file so usage tracking has access to the 3MF
  1010. try:
  1011. from backend.app.services.archive import ArchiveService
  1012. archive_service = ArchiveService(db)
  1013. archive = await archive_service.archive_print(
  1014. printer_id=item.printer_id,
  1015. source_file=file_path,
  1016. original_filename=filename,
  1017. )
  1018. if archive:
  1019. item.archive_id = archive.id
  1020. await db.flush()
  1021. logger.info(
  1022. "Queue item %s: Created archive %s from library file %s",
  1023. item.id,
  1024. archive.id,
  1025. item.library_file_id,
  1026. )
  1027. except Exception as e:
  1028. logger.warning("Queue item %s: Failed to create archive from library file: %s", item.id, e)
  1029. else:
  1030. # Neither archive nor library file specified
  1031. item.status = "failed"
  1032. item.error_message = "No source file specified"
  1033. item.completed_at = datetime.now(timezone.utc)
  1034. await db.commit()
  1035. logger.error("Queue item %s: No archive_id or library_file_id specified", item.id)
  1036. await self._power_off_if_needed(db, item)
  1037. return
  1038. # Check file exists on disk
  1039. if not file_path.exists():
  1040. item.status = "failed"
  1041. item.error_message = "Source file not found on disk"
  1042. item.completed_at = datetime.now(timezone.utc)
  1043. await db.commit()
  1044. logger.error("Queue item %s: File not found: %s", item.id, file_path)
  1045. await self._power_off_if_needed(db, item)
  1046. return
  1047. # Upload file to printer via FTP
  1048. # Use a clean filename to avoid issues with double extensions like .gcode.3mf
  1049. base_name = filename
  1050. if base_name.endswith(".gcode.3mf"):
  1051. base_name = base_name[:-10] # Remove .gcode.3mf
  1052. elif base_name.endswith(".3mf"):
  1053. base_name = base_name[:-4] # Remove .3mf
  1054. remote_filename = f"{base_name}.3mf"
  1055. # Upload to root directory (not /cache/) - the start_print command references
  1056. # files by name only (ftp://{filename}), so they must be in the root
  1057. remote_path = f"/{remote_filename}"
  1058. # Get FTP retry settings
  1059. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  1060. logger.info(
  1061. f"Queue item {item.id}: FTP upload starting - printer={printer.name} ({printer.model}), "
  1062. f"ip={printer.ip_address}, file={remote_filename}, local_path={file_path}, "
  1063. f"retry_enabled={ftp_retry_enabled}, retry_count={ftp_retry_count}, timeout={ftp_timeout}"
  1064. )
  1065. # Delete existing file if present (avoids 553 error on overwrite)
  1066. try:
  1067. logger.debug("Queue item %s: Deleting existing file %s if present...", item.id, remote_path)
  1068. delete_result = await delete_file_async(
  1069. printer.ip_address,
  1070. printer.access_code,
  1071. remote_path,
  1072. socket_timeout=ftp_timeout,
  1073. printer_model=printer.model,
  1074. )
  1075. logger.debug("Queue item %s: Delete result: %s", item.id, delete_result)
  1076. except Exception as e:
  1077. logger.debug("Queue item %s: Delete failed (may not exist): %s", item.id, e)
  1078. try:
  1079. if ftp_retry_enabled:
  1080. uploaded = await with_ftp_retry(
  1081. upload_file_async,
  1082. printer.ip_address,
  1083. printer.access_code,
  1084. file_path,
  1085. remote_path,
  1086. socket_timeout=ftp_timeout,
  1087. printer_model=printer.model,
  1088. max_retries=ftp_retry_count,
  1089. retry_delay=ftp_retry_delay,
  1090. operation_name=f"Upload print to {printer.name}",
  1091. )
  1092. else:
  1093. uploaded = await upload_file_async(
  1094. printer.ip_address,
  1095. printer.access_code,
  1096. file_path,
  1097. remote_path,
  1098. socket_timeout=ftp_timeout,
  1099. printer_model=printer.model,
  1100. )
  1101. except Exception as e:
  1102. uploaded = False
  1103. logger.error("Queue item %s: FTP error: %s (type: %s)", item.id, e, type(e).__name__)
  1104. if not uploaded:
  1105. error_msg = (
  1106. "Failed to upload file to printer. Check if SD card is inserted and properly formatted (FAT32/exFAT). "
  1107. "See server logs for detailed diagnostics."
  1108. )
  1109. item.status = "failed"
  1110. item.error_message = error_msg
  1111. item.completed_at = datetime.now(timezone.utc)
  1112. await db.commit()
  1113. logger.error(
  1114. f"Queue item {item.id}: FTP upload failed - printer={printer.name}, model={printer.model}, "
  1115. f"ip={printer.ip_address}. Check logs above for storage diagnostics and specific error codes."
  1116. )
  1117. # Send failure notification
  1118. await notification_service.on_queue_job_failed(
  1119. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  1120. printer_id=printer.id,
  1121. printer_name=printer.name,
  1122. reason="Failed to upload file to printer",
  1123. db=db,
  1124. )
  1125. await self._power_off_if_needed(db, item)
  1126. return
  1127. # Parse AMS mapping if stored
  1128. ams_mapping = None
  1129. if item.ams_mapping:
  1130. try:
  1131. ams_mapping = json.loads(item.ams_mapping)
  1132. except json.JSONDecodeError:
  1133. logger.warning("Queue item %s: Invalid AMS mapping JSON, ignoring", item.id)
  1134. # Register as expected print so we don't create a duplicate archive
  1135. # Only applicable for archive-based prints
  1136. if archive:
  1137. from backend.app.main import register_expected_print
  1138. register_expected_print(item.printer_id, remote_filename, archive.id, ams_mapping=ams_mapping)
  1139. # IMPORTANT: Set status to "printing" BEFORE sending the print command.
  1140. # This prevents phantom reprints if the backend crashes/restarts after the
  1141. # print command is sent but before the status update is committed.
  1142. # If we crash after this commit but before start_print(), the item will be
  1143. # in "printing" status without actually printing - but that's safer than
  1144. # accidentally reprinting the same file hours later.
  1145. item.status = "printing"
  1146. item.started_at = datetime.now(timezone.utc)
  1147. await db.commit()
  1148. # Consume the plate-cleared flag now that we're starting a print
  1149. printer_manager.consume_plate_cleared(item.printer_id)
  1150. logger.info("Queue item %s: Status set to 'printing', sending print command...", item.id)
  1151. # Start the print with AMS mapping, plate_id and print options
  1152. started = printer_manager.start_print(
  1153. item.printer_id,
  1154. remote_filename,
  1155. plate_id=item.plate_id or 1,
  1156. ams_mapping=ams_mapping,
  1157. bed_levelling=item.bed_levelling,
  1158. flow_cali=item.flow_cali,
  1159. vibration_cali=item.vibration_cali,
  1160. layer_inspect=item.layer_inspect,
  1161. timelapse=item.timelapse,
  1162. use_ams=item.use_ams,
  1163. )
  1164. if started:
  1165. logger.info("Queue item %s: Print started successfully - %s", item.id, filename)
  1166. # Get estimated time for notification
  1167. estimated_time = None
  1168. if archive and archive.print_time_seconds:
  1169. estimated_time = archive.print_time_seconds
  1170. elif library_file and library_file.print_time_seconds:
  1171. estimated_time = library_file.print_time_seconds
  1172. # Send job started notification
  1173. await notification_service.on_queue_job_started(
  1174. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  1175. printer_id=printer.id,
  1176. printer_name=printer.name,
  1177. db=db,
  1178. estimated_time=estimated_time,
  1179. )
  1180. # MQTT relay - publish queue job started
  1181. try:
  1182. from backend.app.services.mqtt_relay import mqtt_relay
  1183. await mqtt_relay.on_queue_job_started(
  1184. job_id=item.id,
  1185. filename=filename,
  1186. printer_id=printer.id,
  1187. printer_name=printer.name,
  1188. printer_serial=printer.serial_number,
  1189. )
  1190. except Exception:
  1191. pass # Don't fail if MQTT fails
  1192. else:
  1193. # Clean up uploaded file from SD card to prevent phantom prints
  1194. try:
  1195. await delete_file_async(
  1196. printer.ip_address,
  1197. printer.access_code,
  1198. remote_path,
  1199. printer_model=printer.model,
  1200. )
  1201. except Exception:
  1202. pass # Best-effort — don't fail the error handler
  1203. # Print command failed - revert status
  1204. item.status = "failed"
  1205. item.error_message = "Failed to send print command to printer"
  1206. item.completed_at = datetime.now(timezone.utc)
  1207. await db.commit()
  1208. logger.error(
  1209. f"Queue item {item.id}: Failed to start print on {printer.name} ({printer.model}) - "
  1210. f"printer_manager.start_print() returned False. "
  1211. f"This may indicate: printer not connected, MQTT error, unsupported model configuration, or firmware issue. "
  1212. f"Check printer status and backend logs for details."
  1213. )
  1214. # Send failure notification
  1215. await notification_service.on_queue_job_failed(
  1216. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  1217. printer_id=printer.id,
  1218. printer_name=printer.name,
  1219. reason="Failed to send print command to printer - check printer connection and status",
  1220. db=db,
  1221. )
  1222. await self._power_off_if_needed(db, item)
  1223. # Global scheduler instance
  1224. scheduler = PrintScheduler()