print_scheduler.py 66 KB

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