print_scheduler.py 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  1. """Print scheduler service - processes the print queue."""
  2. import asyncio
  3. import json
  4. import logging
  5. import time
  6. import zipfile
  7. from datetime import datetime, timezone
  8. from pathlib import Path
  9. import defusedxml.ElementTree as ET
  10. from sqlalchemy import func, select
  11. from sqlalchemy.ext.asyncio import AsyncSession
  12. from backend.app.core.config import settings
  13. from backend.app.core.database import async_session
  14. from backend.app.models.archive import PrintArchive
  15. from backend.app.models.library import LibraryFile
  16. from backend.app.models.print_queue import PrintQueueItem
  17. from backend.app.models.printer import Printer
  18. from backend.app.models.settings import Settings
  19. from backend.app.models.smart_plug import SmartPlug
  20. from backend.app.services.bambu_ftp import delete_file_async, get_ftp_retry_settings, upload_file_async, with_ftp_retry
  21. from backend.app.services.notification_service import notification_service
  22. from backend.app.services.printer_manager import printer_manager, supports_drying
  23. from backend.app.services.smart_plug_manager import smart_plug_manager
  24. from backend.app.utils.printer_models import normalize_printer_model
  25. from backend.app.utils.threemf_tools import extract_nozzle_mapping_from_3mf
  26. logger = logging.getLogger(__name__)
  27. # Filament type equivalence groups — types within the same group are
  28. # interchangeable on the printer side (Bambu Lab firmware treats them as compatible).
  29. _FILAMENT_TYPE_GROUPS: list[list[str]] = [
  30. ["PA-CF", "PA12-CF", "PAHT-CF"],
  31. ]
  32. _FILAMENT_EQUIV_MAP: dict[str, str] = {}
  33. for _group in _FILAMENT_TYPE_GROUPS:
  34. _canonical = _group[0].upper()
  35. for _t in _group:
  36. _FILAMENT_EQUIV_MAP[_t.upper()] = _canonical
  37. def _canonical_filament_type(ftype: str) -> str:
  38. """Return canonical type for equivalence matching."""
  39. upper = ftype.upper()
  40. return _FILAMENT_EQUIV_MAP.get(upper, upper)
  41. class PrintScheduler:
  42. """Background scheduler that processes the print queue."""
  43. # Built-in drying presets per filament type (from BambuStudio filament profiles)
  44. # Format: { n3f_temp, n3s_temp, n3f_hours, n3s_hours }
  45. DEFAULT_DRYING_PRESETS: dict[str, dict[str, int]] = {
  46. "PLA": {"n3f": 45, "n3s": 45, "n3f_hours": 12, "n3s_hours": 12},
  47. "PETG": {"n3f": 65, "n3s": 65, "n3f_hours": 12, "n3s_hours": 12},
  48. "TPU": {"n3f": 65, "n3s": 75, "n3f_hours": 12, "n3s_hours": 18},
  49. "ABS": {"n3f": 65, "n3s": 80, "n3f_hours": 12, "n3s_hours": 8},
  50. "ASA": {"n3f": 65, "n3s": 80, "n3f_hours": 12, "n3s_hours": 8},
  51. "PA": {"n3f": 65, "n3s": 85, "n3f_hours": 12, "n3s_hours": 12},
  52. "PC": {"n3f": 65, "n3s": 80, "n3f_hours": 12, "n3s_hours": 8},
  53. "PVA": {"n3f": 65, "n3s": 85, "n3f_hours": 12, "n3s_hours": 18},
  54. }
  55. def __init__(self):
  56. self._running = False
  57. self._check_interval = 30 # seconds
  58. self._power_on_wait_time = 180 # seconds to wait for printer after power on (3 min)
  59. self._power_on_check_interval = 10 # seconds between connection checks
  60. self._min_drying_seconds = 1800 # 30 minutes minimum before humidity re-check can stop drying
  61. # Track which printers are currently auto-drying (printer_id -> start timestamp)
  62. self._drying_in_progress: dict[int, float] = {}
  63. async def run(self):
  64. """Main loop - check queue every interval."""
  65. self._running = True
  66. logger.info("Print scheduler started")
  67. while self._running:
  68. try:
  69. await self.check_queue()
  70. except Exception as e:
  71. logger.error("Scheduler error: %s", e)
  72. await asyncio.sleep(self._check_interval)
  73. def stop(self):
  74. """Stop the scheduler."""
  75. self._running = False
  76. logger.info("Print scheduler stopped")
  77. async def check_queue(self):
  78. """Check for prints ready to start."""
  79. async with async_session() as db:
  80. # Check if shortest-job-first scheduling is enabled
  81. sjf_enabled = await self._get_bool_setting(db, "queue_shortest_first")
  82. # Get all pending items, ordered by printer and position (or SJF order)
  83. if sjf_enabled:
  84. # SJF: group by printer (and target_model for model-based jobs),
  85. # then items already jumped get top priority (starvation guard),
  86. # then sort by print_time ascending. Items with no print time go last.
  87. result = await db.execute(
  88. select(PrintQueueItem)
  89. .where(PrintQueueItem.status == "pending")
  90. .order_by(
  91. PrintQueueItem.printer_id,
  92. PrintQueueItem.target_model,
  93. PrintQueueItem.been_jumped.desc(),
  94. PrintQueueItem.print_time_seconds.asc().nullslast(),
  95. PrintQueueItem.position,
  96. )
  97. )
  98. else:
  99. result = await db.execute(
  100. select(PrintQueueItem)
  101. .where(PrintQueueItem.status == "pending")
  102. .order_by(PrintQueueItem.printer_id, PrintQueueItem.position)
  103. )
  104. items = list(result.scalars().all())
  105. # Read plate-clear setting once per queue check
  106. require_plate_clear = await self._get_bool_setting(db, "require_plate_clear", default=True)
  107. if not items:
  108. # No pending items — still check auto-drying on idle printers
  109. await self._check_auto_drying(db, [], set(), require_plate_clear=require_plate_clear)
  110. return
  111. logger.info(
  112. "Queue check: found %d pending items: %s",
  113. len(items),
  114. [(i.id, i.printer_id, i.archive_id, i.library_file_id) for i in items],
  115. )
  116. # Track busy printers to avoid assigning multiple items to same printer
  117. busy_printers: set[int] = set()
  118. # Log skip reasons once per queue check (not per item)
  119. skip_reasons: dict[str, int] = {}
  120. for item in items:
  121. # Check scheduled time first (scheduled_time is stored in UTC from ISO string)
  122. if item.scheduled_time:
  123. sched = item.scheduled_time
  124. if sched.tzinfo is None:
  125. sched = sched.replace(tzinfo=timezone.utc)
  126. if sched > datetime.now(timezone.utc):
  127. skip_reasons["scheduled_future"] = skip_reasons.get("scheduled_future", 0) + 1
  128. continue
  129. # Skip items that require manual start
  130. if item.manual_start:
  131. skip_reasons["manual_start"] = skip_reasons.get("manual_start", 0) + 1
  132. continue
  133. if item.printer_id:
  134. # Specific printer assignment (existing behavior)
  135. if item.printer_id in busy_printers:
  136. continue
  137. # Check if printer is idle
  138. printer_idle = self._is_printer_idle(item.printer_id, require_plate_clear)
  139. printer_connected = printer_manager.is_connected(item.printer_id)
  140. # If printer not connected, try to power on via smart plug
  141. if not printer_connected:
  142. plugs = await self._get_smart_plugs(db, item.printer_id)
  143. auto_on_plugs = [p for p in plugs if p.auto_on and p.enabled]
  144. if auto_on_plugs:
  145. logger.info("Printer %s offline, attempting to power on via smart plug(s)", item.printer_id)
  146. # Power on using the first auto_on plug (the printer power plug)
  147. powered_on = await self._power_on_and_wait(auto_on_plugs[0], item.printer_id, db)
  148. if powered_on:
  149. # Also turn on any remaining auto_on plugs (e.g., filter)
  150. for extra_plug in auto_on_plugs[1:]:
  151. try:
  152. service = await smart_plug_manager.get_service_for_plug(extra_plug, db)
  153. await service.turn_on(extra_plug)
  154. logger.info(
  155. "Also powered on plug '%s' for printer %s", extra_plug.name, item.printer_id
  156. )
  157. except Exception as e:
  158. logger.warning("Failed to power on extra plug '%s': %s", extra_plug.name, e)
  159. printer_connected = True
  160. printer_idle = self._is_printer_idle(item.printer_id, require_plate_clear)
  161. else:
  162. logger.warning("Could not power on printer %s via smart plug", item.printer_id)
  163. busy_printers.add(item.printer_id)
  164. continue
  165. else:
  166. # No plug or auto_on disabled
  167. busy_printers.add(item.printer_id)
  168. continue
  169. # Check if printer is idle (busy with another print)
  170. if not printer_idle:
  171. # If printer is drying (not truly busy), handle based on queue_drying_block
  172. if self._drying_in_progress.get(item.printer_id):
  173. block_for_drying = await self._get_bool_setting(db, "queue_drying_block")
  174. if block_for_drying:
  175. # Drying blocks queue — skip this printer
  176. busy_printers.add(item.printer_id)
  177. continue
  178. else:
  179. # Print takes priority — stop drying
  180. await self._stop_drying(item.printer_id)
  181. # Re-check idle after stopping drying
  182. printer_idle = self._is_printer_idle(item.printer_id, require_plate_clear)
  183. if not printer_idle:
  184. busy_printers.add(item.printer_id)
  185. continue
  186. else:
  187. busy_printers.add(item.printer_id)
  188. continue
  189. # Check condition (previous print success)
  190. if item.require_previous_success:
  191. if not await self._check_previous_success(db, item):
  192. item.status = "skipped"
  193. item.error_message = "Previous print failed or was aborted"
  194. item.completed_at = datetime.now(timezone.utc)
  195. await db.commit()
  196. logger.info("Skipped queue item %s - previous print failed", item.id)
  197. # Send notification
  198. job_name = await self._get_job_name(db, item)
  199. printer = await self._get_printer(db, item.printer_id)
  200. await notification_service.on_queue_job_skipped(
  201. job_name=job_name,
  202. printer_id=item.printer_id,
  203. printer_name=printer.name if printer else "Unknown",
  204. reason="Previous print failed or was aborted",
  205. db=db,
  206. )
  207. continue
  208. # Compute AMS mapping if not already set
  209. if not item.ams_mapping:
  210. computed_mapping = await self._compute_ams_mapping_for_printer(db, item.printer_id, item)
  211. if computed_mapping:
  212. item.ams_mapping = json.dumps(computed_mapping)
  213. logger.info(
  214. f"Queue item {item.id}: Computed AMS mapping for printer {item.printer_id}: {computed_mapping}"
  215. )
  216. await db.commit()
  217. # Start the print
  218. await self._start_print(db, item)
  219. busy_printers.add(item.printer_id)
  220. # SJF starvation guard: mark items that were jumped
  221. if sjf_enabled and item.print_time_seconds is not None:
  222. for other in items:
  223. if (
  224. other.id != item.id
  225. and other.status == "pending"
  226. and other.printer_id == item.printer_id
  227. and not other.been_jumped
  228. and other.position < item.position
  229. and (
  230. other.print_time_seconds is None
  231. or other.print_time_seconds > item.print_time_seconds
  232. )
  233. ):
  234. other.been_jumped = True
  235. await db.commit()
  236. elif item.target_model:
  237. # Model-based assignment - find any idle printer of matching model
  238. # Parse required filament types if present
  239. required_types = None
  240. if item.required_filament_types:
  241. try:
  242. required_types = json.loads(item.required_filament_types)
  243. except json.JSONDecodeError:
  244. pass # Ignore malformed filament types; treat as no constraint
  245. # Parse filament overrides if present
  246. filament_overrides = None
  247. if item.filament_overrides:
  248. try:
  249. filament_overrides = json.loads(item.filament_overrides)
  250. except json.JSONDecodeError:
  251. pass
  252. # If overrides exist, use override types for validation instead
  253. effective_types = required_types
  254. if filament_overrides:
  255. override_types = sorted({o["type"] for o in filament_overrides if "type" in o})
  256. if override_types:
  257. # Merge: keep original types for non-overridden slots, add override types
  258. effective_types = sorted(set(required_types or []) | set(override_types))
  259. printer_id, waiting_reason = await self._find_idle_printer_for_model(
  260. db,
  261. item.target_model,
  262. busy_printers,
  263. effective_types,
  264. item.target_location,
  265. filament_overrides=filament_overrides,
  266. require_plate_clear=require_plate_clear,
  267. )
  268. # Update waiting_reason if changed and send notification when first waiting
  269. if item.waiting_reason != waiting_reason:
  270. was_waiting = item.waiting_reason is not None
  271. item.waiting_reason = waiting_reason
  272. await db.commit()
  273. # Send waiting notification only when transitioning to waiting state
  274. # and the reason requires user action (not just "all printers busy")
  275. if waiting_reason and not was_waiting and not self._is_busy_only(waiting_reason):
  276. job_name = await self._get_job_name(db, item)
  277. await notification_service.on_queue_job_waiting(
  278. job_name=job_name,
  279. target_model=item.target_model,
  280. waiting_reason=waiting_reason,
  281. db=db,
  282. )
  283. if printer_id:
  284. # Check condition (previous print success) before assigning
  285. if item.require_previous_success:
  286. if not await self._check_previous_success(db, item):
  287. item.status = "skipped"
  288. item.error_message = "Previous print failed or was aborted"
  289. item.completed_at = datetime.now(timezone.utc)
  290. await db.commit()
  291. logger.info("Skipped queue item %s - previous print failed", item.id)
  292. # Send notification
  293. job_name = await self._get_job_name(db, item)
  294. printer = await self._get_printer(db, printer_id)
  295. await notification_service.on_queue_job_skipped(
  296. job_name=job_name,
  297. printer_id=printer_id,
  298. printer_name=printer.name if printer else "Unknown",
  299. reason="Previous print failed or was aborted",
  300. db=db,
  301. )
  302. continue
  303. # Assign printer and start - clear waiting reason
  304. item.printer_id = printer_id
  305. item.waiting_reason = None
  306. logger.info("Model-based assignment: queue item %s assigned to printer %s", item.id, printer_id)
  307. # Send assignment notification
  308. job_name = await self._get_job_name(db, item)
  309. printer = await self._get_printer(db, printer_id)
  310. await notification_service.on_queue_job_assigned(
  311. job_name=job_name,
  312. printer_id=printer_id,
  313. printer_name=printer.name if printer else "Unknown",
  314. target_model=item.target_model,
  315. db=db,
  316. )
  317. # Compute AMS mapping for the assigned printer if not already set
  318. # This is critical for model-based jobs where mapping wasn't computed upfront
  319. if not item.ams_mapping:
  320. computed_mapping = await self._compute_ams_mapping_for_printer(db, printer_id, item)
  321. if computed_mapping:
  322. item.ams_mapping = json.dumps(computed_mapping)
  323. logger.info(
  324. f"Queue item {item.id}: Computed AMS mapping for printer {printer_id}: {computed_mapping}"
  325. )
  326. await db.commit()
  327. await self._start_print(db, item)
  328. busy_printers.add(printer_id)
  329. # SJF starvation guard: mark model-based items that were jumped
  330. if sjf_enabled and item.print_time_seconds is not None:
  331. for other in items:
  332. if (
  333. other.id != item.id
  334. and other.status == "pending"
  335. and other.printer_id is None
  336. and other.target_model
  337. and other.target_model.upper() == item.target_model.upper()
  338. and not other.been_jumped
  339. and other.position < item.position
  340. and (
  341. other.print_time_seconds is None
  342. or other.print_time_seconds > item.print_time_seconds
  343. )
  344. ):
  345. other.been_jumped = True
  346. await db.commit()
  347. # Log summary of skip reasons (helps diagnose why queue items aren't starting)
  348. if skip_reasons:
  349. logger.info("Queue skip summary: %s", skip_reasons)
  350. if busy_printers:
  351. # Log why each printer was busy (first time it was checked)
  352. for pid in busy_printers:
  353. state = printer_manager.get_status(pid)
  354. connected = printer_manager.is_connected(pid)
  355. awaiting = printer_manager.is_awaiting_plate_clear(pid)
  356. state_name = state.state if state else "NO_STATUS"
  357. logger.info(
  358. "Queue: printer %d not available — connected=%s, state=%s, awaiting_plate_clear=%s",
  359. pid,
  360. connected,
  361. state_name,
  362. awaiting,
  363. )
  364. # Auto-drying: start drying on idle printers that have no pending queue items
  365. await self._check_auto_drying(db, items, busy_printers, require_plate_clear=require_plate_clear)
  366. async def _find_idle_printer_for_model(
  367. self,
  368. db: AsyncSession,
  369. model: str,
  370. exclude_ids: set[int],
  371. required_filament_types: list[str] | None = None,
  372. target_location: str | None = None,
  373. filament_overrides: list[dict] | None = None,
  374. require_plate_clear: bool = True,
  375. ) -> tuple[int | None, str | None]:
  376. """Find an idle, connected printer matching the model with compatible filaments.
  377. Args:
  378. db: Database session
  379. model: Printer model to match (e.g., "X1C", "P1S")
  380. exclude_ids: Printer IDs to exclude (already busy)
  381. required_filament_types: Optional list of filament types needed (e.g., ["PLA", "PETG"])
  382. If provided, only printers with all required types loaded will match.
  383. target_location: Optional location filter. If provided, only printers in this location are considered.
  384. filament_overrides: Optional list of override dicts. Each entry may include
  385. ``force_color_match: true`` to require an exact type+color match
  386. on the printer for that slot. Without the flag the existing
  387. colour-preference logic applies.
  388. Returns:
  389. Tuple of (printer_id, waiting_reason):
  390. - (printer_id, None) if a matching printer was found
  391. - (None, reason) if no printer is available, with explanation
  392. """
  393. # Normalize model name and use case-insensitive matching
  394. normalized_model = normalize_printer_model(model) or model
  395. query = (
  396. select(Printer)
  397. .where(func.lower(Printer.model) == normalized_model.lower())
  398. .where(Printer.is_active == True) # noqa: E712
  399. )
  400. # Add location filter if specified
  401. if target_location:
  402. query = query.where(Printer.location == target_location)
  403. result = await db.execute(query)
  404. printers = list(result.scalars().all())
  405. location_suffix = f" in {target_location}" if target_location else ""
  406. if not printers:
  407. return None, f"No active {normalized_model} printers{location_suffix} configured"
  408. # Separate force-matched overrides from preference-only overrides
  409. force_overrides = [o for o in (filament_overrides or []) if o.get("force_color_match")]
  410. pref_overrides = [o for o in (filament_overrides or []) if not o.get("force_color_match")]
  411. # Track reasons for skipping printers
  412. printers_busy = []
  413. printers_offline = []
  414. printers_missing_filament: list[tuple[str, list[str]]] = []
  415. candidates: list[tuple[int, int]] = [] # (printer_id, color_match_count)
  416. for printer in printers:
  417. if printer.id in exclude_ids:
  418. # Printer is already claimed by another job in this scheduling run.
  419. # For force-color jobs, still check if the color would match — if not,
  420. # report it as a color mismatch rather than plain "Busy" so the user
  421. # knows the job needs a filament change, not just to wait for availability.
  422. if force_overrides and not pref_overrides:
  423. missing_colors = self._get_missing_force_color_slots(printer.id, force_overrides)
  424. if missing_colors:
  425. printers_missing_filament.append((printer.name, missing_colors))
  426. continue
  427. printers_busy.append(printer.name)
  428. continue
  429. is_connected = printer_manager.is_connected(printer.id)
  430. is_idle = self._is_printer_idle(printer.id, require_plate_clear) if is_connected else False
  431. if not is_connected:
  432. printers_offline.append(printer.name)
  433. continue
  434. if not is_idle:
  435. # Printer is currently printing. For force-color jobs, check whether the
  436. # loaded color would satisfy the requirement — if not, surface it as a
  437. # color-mismatch reason rather than plain "Busy" so the user understands
  438. # that the job is waiting for a filament change, not just printer availability.
  439. if force_overrides and not pref_overrides:
  440. missing_colors = self._get_missing_force_color_slots(printer.id, force_overrides)
  441. if missing_colors:
  442. printers_missing_filament.append((printer.name, missing_colors))
  443. logger.debug(
  444. "Printer %s (%s) is busy but also has wrong force-color: %s",
  445. printer.id,
  446. printer.name,
  447. missing_colors,
  448. )
  449. continue
  450. printers_busy.append(printer.name)
  451. continue
  452. # Validate filament compatibility if required types are specified
  453. if required_filament_types:
  454. missing = self._get_missing_filament_types(printer.id, required_filament_types)
  455. if missing:
  456. # When force_overrides are present, enrich missing entries with color info
  457. # so the "Waiting on" message includes "TYPE (color)" instead of just "TYPE"
  458. if force_overrides:
  459. force_color_map = {
  460. (o.get("type") or "").upper(): o.get("color_name") or o.get("color", "?")
  461. for o in force_overrides
  462. }
  463. missing_enriched = [
  464. f"{t} ({force_color_map[t_upper]})" if (t_upper := t.upper()) in force_color_map else t
  465. for t in missing
  466. ]
  467. printers_missing_filament.append((printer.name, missing_enriched))
  468. else:
  469. printers_missing_filament.append((printer.name, missing))
  470. logger.debug("Skipping printer %s (%s) - missing filaments: %s", printer.id, printer.name, missing)
  471. continue
  472. # Force color match: ALL flagged slots must have an exact type+color match
  473. if force_overrides:
  474. missing_colors = self._get_missing_force_color_slots(printer.id, force_overrides)
  475. if missing_colors:
  476. printers_missing_filament.append((printer.name, missing_colors))
  477. logger.debug(
  478. "Skipping printer %s (%s) - missing force-matched colors: %s",
  479. printer.id,
  480. printer.name,
  481. missing_colors,
  482. )
  483. continue
  484. # If preference-only overrides exist, rank by color matches (existing behaviour)
  485. if pref_overrides:
  486. color_matches = self._count_override_color_matches(printer.id, pref_overrides)
  487. if color_matches > 0:
  488. candidates.append((printer.id, color_matches))
  489. else:
  490. override_colors = [f"{o.get('type', '?')} ({o.get('color', '?')})" for o in pref_overrides]
  491. printers_missing_filament.append((printer.name, override_colors))
  492. logger.debug("Skipping printer %s (%s) - no matching override colors", printer.id, printer.name)
  493. continue
  494. elif force_overrides:
  495. # Passed all force checks — immediately eligible (no preference ordering needed)
  496. return printer.id, None
  497. else:
  498. # No overrides at all - take first available (existing behavior)
  499. return printer.id, None
  500. # If we have candidates from preference override matching, pick the one with most color matches
  501. if candidates:
  502. candidates.sort(key=lambda c: c[1], reverse=True)
  503. return candidates[0][0], None
  504. # Build waiting reason from what we found
  505. reasons = []
  506. if printers_missing_filament:
  507. # Filament/color mismatch is most actionable - show first
  508. if force_overrides and not pref_overrides:
  509. # All mismatches are force-color failures — use descriptive message only;
  510. # but only if there are no busy printers that DO have the matching color.
  511. # If a printer has the right color but is busy, surface "Busy" instead so
  512. # the user knows the job will start automatically once that printer is free.
  513. if not printers_busy:
  514. all_missing = sorted({c for _, cols in printers_missing_filament for c in cols})
  515. return None, f"No matching material/color. Waiting on {', '.join(all_missing)}"
  516. # else: fall through — printers_busy will be appended below
  517. else:
  518. names_and_missing = [
  519. f"{name} (needs {', '.join(missing)})" for name, missing in printers_missing_filament
  520. ]
  521. reasons.append(f"Waiting for filament: {'; '.join(names_and_missing)}")
  522. if printers_busy:
  523. reasons.append(f"Busy: {', '.join(printers_busy)}")
  524. if printers_offline:
  525. reasons.append(f"Offline: {', '.join(printers_offline)}")
  526. return None, " | ".join(reasons) if reasons else f"No available {model} printers{location_suffix}"
  527. @staticmethod
  528. def _is_busy_only(waiting_reason: str) -> bool:
  529. """Check if the waiting reason only contains 'Busy' entries.
  530. When all matching printers are simply busy printing, the queued job
  531. will start automatically once a printer finishes — no user action
  532. is required, so we skip the notification.
  533. """
  534. parts = [p.strip() for p in waiting_reason.split(" | ")]
  535. return all(p.startswith("Busy:") for p in parts)
  536. def _get_missing_force_color_slots(self, printer_id: int, force_overrides: list[dict]) -> list[str]:
  537. """Return descriptive strings for force_color_match slots not satisfied by the printer.
  538. Each entry in ``force_overrides`` must have ``type`` and ``color`` fields and is expected
  539. to carry ``force_color_match: True``. The printer must have **every** such slot loaded
  540. with an exact type+color match.
  541. Returns:
  542. List of ``"TYPE (color)"`` strings for unmatched slots (empty list means all match).
  543. """
  544. status = printer_manager.get_status(printer_id)
  545. if not status:
  546. return [f"{o.get('type', '?')} ({o.get('color_name') or o.get('color', '?')})" for o in force_overrides]
  547. # Build set of loaded type+colour pairs from AMS and external spool
  548. loaded: set[tuple[str, str]] = set()
  549. for ams_unit in status.raw_data.get("ams", []):
  550. for tray in ams_unit.get("tray", []):
  551. tray_type = tray.get("tray_type")
  552. tray_color = tray.get("tray_color", "")
  553. if tray_type:
  554. color_norm = tray_color.replace("#", "").lower()[:6]
  555. loaded.add((_canonical_filament_type(tray_type), color_norm))
  556. for vt in status.raw_data.get("vt_tray") or []:
  557. vt_type = vt.get("tray_type")
  558. if vt_type:
  559. color_norm = (vt.get("tray_color", "") or "").replace("#", "").lower()[:6]
  560. loaded.add((_canonical_filament_type(vt_type), color_norm))
  561. missing = []
  562. for o in force_overrides:
  563. o_type = _canonical_filament_type(o.get("type") or "")
  564. o_color = (o.get("color") or "").replace("#", "").lower()[:6]
  565. if (o_type, o_color) not in loaded:
  566. color_label = o.get("color_name") or o.get("color", "?")
  567. missing.append(f"{o_type} ({color_label})")
  568. return missing
  569. def _get_missing_filament_types(self, printer_id: int, required_types: list[str]) -> list[str]:
  570. """Get the list of required filament types that are not loaded on the printer.
  571. Args:
  572. printer_id: The printer ID
  573. required_types: List of filament types needed (e.g., ["PLA", "PETG"])
  574. Returns:
  575. List of missing filament types (empty if all are loaded)
  576. """
  577. status = printer_manager.get_status(printer_id)
  578. if not status:
  579. return required_types # Can't determine, assume all missing
  580. # Collect all filament types loaded on this printer (AMS units + external spool)
  581. # Use canonical types so equivalence groups (e.g. PA-CF/PA12-CF/PAHT-CF) match.
  582. loaded_types: set[str] = set()
  583. # Check AMS units (stored in raw_data["ams"])
  584. ams_data = status.raw_data.get("ams", [])
  585. if ams_data:
  586. for ams_unit in ams_data:
  587. for tray in ams_unit.get("tray", []):
  588. tray_type = tray.get("tray_type")
  589. if tray_type:
  590. loaded_types.add(_canonical_filament_type(tray_type))
  591. # Check external spool(s) (virtual tray, stored in raw_data["vt_tray"] as list)
  592. for vt in status.raw_data.get("vt_tray") or []:
  593. vt_type = vt.get("tray_type")
  594. if vt_type:
  595. loaded_types.add(_canonical_filament_type(vt_type))
  596. # Find which required types are missing (using canonical type for equivalence)
  597. missing = []
  598. for req_type in required_types:
  599. if _canonical_filament_type(req_type) not in loaded_types:
  600. missing.append(req_type)
  601. return missing
  602. def _count_override_color_matches(self, printer_id: int, overrides: list[dict]) -> int:
  603. """Count how many filament overrides have an exact color match on the printer.
  604. Used to prefer printers that already have the desired override colors loaded.
  605. """
  606. status = printer_manager.get_status(printer_id)
  607. if not status:
  608. return 0
  609. # Collect loaded filaments' type+color pairs
  610. loaded: set[tuple[str, str]] = set()
  611. for ams_unit in status.raw_data.get("ams", []):
  612. for tray in ams_unit.get("tray", []):
  613. tray_type = tray.get("tray_type")
  614. tray_color = tray.get("tray_color", "")
  615. if tray_type:
  616. color_norm = tray_color.replace("#", "").lower()[:6]
  617. loaded.add((tray_type.upper(), color_norm))
  618. for vt in status.raw_data.get("vt_tray") or []:
  619. vt_type = vt.get("tray_type")
  620. if vt_type:
  621. color_norm = (vt.get("tray_color", "") or "").replace("#", "").lower()[:6]
  622. loaded.add((vt_type.upper(), color_norm))
  623. matches = 0
  624. for o in overrides:
  625. o_type = (o.get("type") or "").upper()
  626. o_color = (o.get("color") or "").replace("#", "").lower()[:6]
  627. if (o_type, o_color) in loaded:
  628. matches += 1
  629. return matches
  630. async def _compute_ams_mapping_for_printer(
  631. self, db: AsyncSession, printer_id: int, item: PrintQueueItem
  632. ) -> list[int] | None:
  633. """Compute AMS mapping for a printer based on filament requirements.
  634. Called when a queue item has no ams_mapping set — either for model-based
  635. items after printer assignment, or printer-specific items (e.g. from VP).
  636. Args:
  637. db: Database session
  638. printer_id: The assigned printer ID
  639. item: The queue item (contains archive_id or library_file_id)
  640. Returns:
  641. AMS mapping array or None if no mapping needed/possible
  642. """
  643. # Get printer status
  644. status = printer_manager.get_status(printer_id)
  645. if not status:
  646. logger.warning("Cannot compute AMS mapping: printer %s status unavailable", printer_id)
  647. return None
  648. # Get filament requirements from source file
  649. filament_reqs = await self._get_filament_requirements(db, item)
  650. if not filament_reqs:
  651. logger.debug("No filament requirements found for queue item %s", item.id)
  652. return None
  653. # Apply filament overrides if present
  654. if item.filament_overrides:
  655. try:
  656. overrides = json.loads(item.filament_overrides)
  657. override_map = {o["slot_id"]: o for o in overrides}
  658. for req in filament_reqs:
  659. if req["slot_id"] in override_map:
  660. override = override_map[req["slot_id"]]
  661. req["type"] = override["type"]
  662. req["color"] = override["color"]
  663. # Clear tray_info_idx so matching uses type+color instead of
  664. # the original 3MF's tray_info_idx (which would match the old filament)
  665. req["tray_info_idx"] = ""
  666. logger.debug(
  667. "Queue item %s: Override slot %d -> %s %s",
  668. item.id,
  669. req["slot_id"],
  670. override["type"],
  671. override["color"],
  672. )
  673. except (json.JSONDecodeError, KeyError, TypeError) as e:
  674. logger.warning("Failed to apply filament overrides for queue item %s: %s", item.id, e)
  675. # Build loaded filaments from printer status
  676. loaded_filaments = self._build_loaded_filaments(status)
  677. if not loaded_filaments:
  678. logger.debug("No filaments loaded on printer %s", printer_id)
  679. return None
  680. # Check if user prefers lowest remaining filament when multiple spools match
  681. prefer_lowest = await self._get_bool_setting(db, "prefer_lowest_filament")
  682. # Compute mapping: match required filaments to available slots
  683. return self._match_filaments_to_slots(filament_reqs, loaded_filaments, prefer_lowest)
  684. async def _get_filament_requirements(self, db: AsyncSession, item: PrintQueueItem) -> list[dict] | None:
  685. """Extract filament requirements from the source 3MF file.
  686. Args:
  687. db: Database session
  688. item: Queue item with archive_id or library_file_id
  689. Returns:
  690. List of filament requirement dicts with slot_id, type, color, used_grams
  691. """
  692. file_path: Path | None = None
  693. if item.archive_id:
  694. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  695. archive = result.scalar_one_or_none()
  696. if archive:
  697. file_path = settings.base_dir / archive.file_path
  698. elif item.library_file_id:
  699. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  700. library_file = result.scalar_one_or_none()
  701. if library_file:
  702. lib_path = Path(library_file.file_path)
  703. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  704. if not file_path or not file_path.exists():
  705. return None
  706. filaments = []
  707. try:
  708. with zipfile.ZipFile(file_path, "r") as zf:
  709. if "Metadata/slice_info.config" not in zf.namelist():
  710. return None
  711. content = zf.read("Metadata/slice_info.config").decode()
  712. root = ET.fromstring(content)
  713. # Check if plate_id is specified - use that plate's filaments
  714. plate_id = item.plate_id
  715. if plate_id:
  716. for plate_elem in root.findall("./plate"):
  717. plate_index = None
  718. for meta in plate_elem.findall("metadata"):
  719. if meta.get("key") == "index":
  720. plate_index = int(meta.get("value", "0"))
  721. break
  722. if plate_index == plate_id:
  723. for filament_elem in plate_elem.findall("./filament"):
  724. filament_id = filament_elem.get("id")
  725. filament_type = filament_elem.get("type", "")
  726. filament_color = filament_elem.get("color", "")
  727. # tray_info_idx identifies the specific spool selected when slicing
  728. tray_info_idx = filament_elem.get("tray_info_idx", "")
  729. used_g = filament_elem.get("used_g", "0")
  730. try:
  731. used_grams = float(used_g)
  732. if used_grams > 0 and filament_id:
  733. filaments.append(
  734. {
  735. "slot_id": int(filament_id),
  736. "type": filament_type,
  737. "color": filament_color,
  738. "tray_info_idx": tray_info_idx,
  739. "used_grams": round(used_grams, 1),
  740. }
  741. )
  742. except (ValueError, TypeError):
  743. pass # Skip filament entry with unparseable usage data
  744. break
  745. else:
  746. # No plate_id - extract all filaments with used_g > 0
  747. for filament_elem in root.findall("./filament"):
  748. filament_id = filament_elem.get("id")
  749. filament_type = filament_elem.get("type", "")
  750. filament_color = filament_elem.get("color", "")
  751. # tray_info_idx identifies the specific spool selected when slicing
  752. tray_info_idx = filament_elem.get("tray_info_idx", "")
  753. used_g = filament_elem.get("used_g", "0")
  754. try:
  755. used_grams = float(used_g)
  756. if used_grams > 0 and filament_id:
  757. filaments.append(
  758. {
  759. "slot_id": int(filament_id),
  760. "type": filament_type,
  761. "color": filament_color,
  762. "tray_info_idx": tray_info_idx,
  763. "used_grams": round(used_grams, 1),
  764. }
  765. )
  766. except (ValueError, TypeError):
  767. pass # Skip filament entry with unparseable usage data
  768. filaments.sort(key=lambda x: x["slot_id"])
  769. # Enrich with nozzle mapping for dual-nozzle printers
  770. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
  771. if nozzle_mapping:
  772. for filament in filaments:
  773. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  774. except Exception as e:
  775. logger.warning("Failed to parse filament requirements: %s", e)
  776. return None
  777. return filaments if filaments else None
  778. def _build_loaded_filaments(self, status) -> list[dict]:
  779. """Build list of loaded filaments from printer status.
  780. Args:
  781. status: PrinterState from printer_manager
  782. Returns:
  783. List of loaded filament dicts with type, color, ams_id, tray_id, global_tray_id
  784. """
  785. filaments = []
  786. # Get ams_extruder_map for dual-nozzle printers (H2D, H2D Pro)
  787. ams_extruder_map = status.raw_data.get("ams_extruder_map", {})
  788. # Parse AMS units from raw_data
  789. ams_data = status.raw_data.get("ams", [])
  790. for ams_unit in ams_data:
  791. ams_id = int(ams_unit.get("id", 0))
  792. trays = ams_unit.get("tray", [])
  793. is_ht = len(trays) == 1 # AMS-HT has single tray
  794. for tray in trays:
  795. tray_type = tray.get("tray_type")
  796. if tray_type:
  797. tray_id = int(tray.get("id", 0))
  798. tray_color = tray.get("tray_color", "")
  799. # tray_info_idx identifies the specific spool (e.g., "GFA00", "P4d64437")
  800. tray_info_idx = tray.get("tray_info_idx", "")
  801. # Normalize color: remove alpha, add hash
  802. color = self._normalize_color(tray_color)
  803. # Calculate global tray ID
  804. # AMS-HT units have IDs starting at 128 with a single tray
  805. global_tray_id = ams_id if ams_id >= 128 else ams_id * 4 + tray_id
  806. filaments.append(
  807. {
  808. "type": tray_type,
  809. "color": color,
  810. "tray_info_idx": tray_info_idx,
  811. "ams_id": ams_id,
  812. "tray_id": tray_id,
  813. "is_ht": is_ht,
  814. "is_external": False,
  815. "global_tray_id": global_tray_id,
  816. "extruder_id": ams_extruder_map.get(str(ams_id)),
  817. "remain": tray.get("remain", -1),
  818. }
  819. )
  820. # Check external spool(s) (vt_tray is a list)
  821. for idx, vt in enumerate(status.raw_data.get("vt_tray") or []):
  822. if vt.get("tray_type"):
  823. color = self._normalize_color(vt.get("tray_color", ""))
  824. tray_id = int(vt.get("id", 254))
  825. filaments.append(
  826. {
  827. "type": vt["tray_type"],
  828. "color": color,
  829. "tray_info_idx": vt.get("tray_info_idx", ""),
  830. "ams_id": -1,
  831. "tray_id": idx,
  832. "is_ht": False,
  833. "is_external": True,
  834. "global_tray_id": tray_id,
  835. "extruder_id": (255 - tray_id) if ams_extruder_map else None,
  836. "remain": vt.get("remain", -1),
  837. }
  838. )
  839. return filaments
  840. def _normalize_color(self, color: str | None) -> str:
  841. """Normalize color to #RRGGBB format."""
  842. if not color:
  843. return "#808080"
  844. hex_color = color.replace("#", "")[:6]
  845. return f"#{hex_color}"
  846. def _normalize_color_for_compare(self, color: str | None) -> str:
  847. """Normalize color for comparison (lowercase, no hash)."""
  848. if not color:
  849. return ""
  850. return color.replace("#", "").lower()[:6]
  851. def _colors_are_similar(self, color1: str | None, color2: str | None, threshold: int = 40) -> bool:
  852. """Check if two colors are visually similar within a threshold."""
  853. hex1 = self._normalize_color_for_compare(color1)
  854. hex2 = self._normalize_color_for_compare(color2)
  855. if not hex1 or not hex2 or len(hex1) < 6 or len(hex2) < 6:
  856. return False
  857. try:
  858. r1 = int(hex1[0:2], 16)
  859. g1 = int(hex1[2:4], 16)
  860. b1 = int(hex1[4:6], 16)
  861. r2 = int(hex2[0:2], 16)
  862. g2 = int(hex2[2:4], 16)
  863. b2 = int(hex2[4:6], 16)
  864. return abs(r1 - r2) <= threshold and abs(g1 - g2) <= threshold and abs(b1 - b2) <= threshold
  865. except ValueError:
  866. return False
  867. def _match_filaments_to_slots(
  868. self, required: list[dict], loaded: list[dict], prefer_lowest: bool = False
  869. ) -> list[int] | None:
  870. """Match required filaments to loaded filaments and build AMS mapping.
  871. Priority: unique tray_info_idx match > exact color match > similar color match > type-only match
  872. The tray_info_idx is a filament type identifier stored in the 3MF file when the user
  873. slices (e.g., "GFA00" for generic PLA, "P4d64437" for custom presets). If the same
  874. tray_info_idx appears in only ONE available tray, we use that tray. If multiple trays
  875. have the same tray_info_idx (e.g., two spools of generic PLA), we fall back to color
  876. matching among those trays.
  877. Args:
  878. required: List of required filaments with slot_id, type, color, tray_info_idx
  879. loaded: List of loaded filaments with type, color, tray_info_idx, global_tray_id
  880. Returns:
  881. AMS mapping array (position = slot_id - 1, value = global_tray_id or -1)
  882. """
  883. if not required:
  884. return None
  885. # Track used trays to avoid duplicate assignment
  886. used_tray_ids: set[int] = set()
  887. comparisons = []
  888. for req in required:
  889. req_type = (req.get("type") or "").upper()
  890. req_color = req.get("color", "")
  891. req_tray_info_idx = req.get("tray_info_idx", "")
  892. # Find best match: unique tray_info_idx > exact color > similar color > type-only
  893. idx_match = None
  894. exact_match = None
  895. similar_match = None
  896. type_only_match = None
  897. # Get available trays (not already used)
  898. available = [f for f in loaded if f["global_tray_id"] not in used_tray_ids]
  899. # Nozzle-aware filtering: restrict to trays on the correct nozzle.
  900. # Hard filter — cross-nozzle assignment causes print failures
  901. # ("position of left hotend is abnormal"), so never fall back.
  902. req_nozzle_id = req.get("nozzle_id")
  903. if req_nozzle_id is not None:
  904. available = [f for f in available if f.get("extruder_id") == req_nozzle_id]
  905. # Sort by remaining filament (ascending) so lowest-remain spool wins .find()
  906. if prefer_lowest:
  907. available.sort(key=lambda f: f.get("remain", -1) if f.get("remain", -1) >= 0 else 101)
  908. # Check if tray_info_idx is unique among available trays
  909. if req_tray_info_idx:
  910. idx_matches = [f for f in available if f.get("tray_info_idx") == req_tray_info_idx]
  911. if len(idx_matches) == 1:
  912. # Unique tray_info_idx - use it as definitive match
  913. idx_match = idx_matches[0]
  914. logger.debug(
  915. f"Matched filament slot {req.get('slot_id')} by unique tray_info_idx={req_tray_info_idx} "
  916. f"-> tray {idx_match['global_tray_id']}"
  917. )
  918. elif len(idx_matches) > 1:
  919. # Multiple trays with same tray_info_idx - use color matching among them
  920. logger.debug(
  921. f"Non-unique tray_info_idx={req_tray_info_idx} found in {len(idx_matches)} trays, "
  922. f"using color matching among trays: {[f['global_tray_id'] for f in idx_matches]}"
  923. )
  924. if prefer_lowest:
  925. idx_matches.sort(key=lambda f: f.get("remain", -1) if f.get("remain", -1) >= 0 else 101)
  926. # Use color matching within this subset
  927. for f in idx_matches:
  928. f_color = f.get("color", "")
  929. if self._normalize_color_for_compare(f_color) == self._normalize_color_for_compare(req_color):
  930. if not exact_match:
  931. exact_match = f
  932. elif self._colors_are_similar(f_color, req_color):
  933. if not similar_match:
  934. similar_match = f
  935. elif not type_only_match:
  936. type_only_match = f
  937. # If no idx_match yet, do standard type/color matching on all available trays
  938. if not idx_match and not exact_match and not similar_match and not type_only_match:
  939. for f in available:
  940. f_type = (f.get("type") or "").upper()
  941. if _canonical_filament_type(f_type) != _canonical_filament_type(req_type):
  942. continue
  943. # Type matches - check color
  944. f_color = f.get("color", "")
  945. if self._normalize_color_for_compare(f_color) == self._normalize_color_for_compare(req_color):
  946. if not exact_match:
  947. exact_match = f
  948. elif self._colors_are_similar(f_color, req_color):
  949. if not similar_match:
  950. similar_match = f
  951. elif not type_only_match:
  952. type_only_match = f
  953. match = idx_match or exact_match or similar_match or type_only_match
  954. if match:
  955. used_tray_ids.add(match["global_tray_id"])
  956. comparisons.append({"slot_id": req.get("slot_id", 0), "global_tray_id": match["global_tray_id"]})
  957. else:
  958. comparisons.append({"slot_id": req.get("slot_id", 0), "global_tray_id": -1})
  959. # Build mapping array
  960. if not comparisons:
  961. return None
  962. max_slot_id = max(c["slot_id"] for c in comparisons)
  963. if max_slot_id <= 0:
  964. return None
  965. mapping = [-1] * max_slot_id
  966. for c in comparisons:
  967. slot_id = c["slot_id"]
  968. if slot_id and slot_id > 0:
  969. mapping[slot_id - 1] = c["global_tray_id"]
  970. return mapping
  971. def _is_printer_idle(self, printer_id: int, require_plate_clear: bool = True) -> bool:
  972. """Check if a printer is connected and idle."""
  973. if not printer_manager.is_connected(printer_id):
  974. logger.debug("Printer %d: not connected", printer_id)
  975. return False
  976. state = printer_manager.get_status(printer_id)
  977. if not state:
  978. logger.debug("Printer %d: no status available", printer_id)
  979. return False
  980. # Plate-clear gate: if the printer finished/failed a previous print and the user
  981. # hasn't acknowledged the plate was cleared, the queue must not dispatch the next
  982. # job — even if the printer currently reports IDLE. After Auto Off cycles the
  983. # printer, it boots back into IDLE with no memory of the previous finish; without
  984. # the persisted awaiting flag we'd bypass the confirmation prompt (#961).
  985. if require_plate_clear and printer_manager.is_awaiting_plate_clear(printer_id):
  986. logger.debug(
  987. "Printer %d: not idle — awaiting plate-clear acknowledgment (state=%s)",
  988. printer_id,
  989. state.state,
  990. )
  991. return False
  992. idle = state.state in ("IDLE", "FINISH", "FAILED")
  993. if not idle:
  994. logger.debug("Printer %d: not idle — state=%s", printer_id, state.state)
  995. return idle
  996. async def _get_setting(self, db: AsyncSession, key: str) -> str | None:
  997. """Read a setting value from the database."""
  998. result = await db.execute(select(Settings).where(Settings.key == key))
  999. setting = result.scalar_one_or_none()
  1000. return setting.value if setting else None
  1001. async def _get_bool_setting(self, db: AsyncSession, key: str, default: bool = False) -> bool:
  1002. """Read a boolean setting from the database."""
  1003. result = await db.execute(select(Settings).where(Settings.key == key))
  1004. setting = result.scalar_one_or_none()
  1005. if setting:
  1006. return setting.value.lower() == "true"
  1007. return default
  1008. async def _get_drying_presets(self, db: AsyncSession) -> dict[str, dict[str, int]]:
  1009. """Get drying presets (user-configured or built-in defaults)."""
  1010. result = await db.execute(select(Settings).where(Settings.key == "drying_presets"))
  1011. setting = result.scalar_one_or_none()
  1012. if setting and setting.value:
  1013. try:
  1014. presets = json.loads(setting.value)
  1015. if isinstance(presets, dict) and presets:
  1016. return presets
  1017. except json.JSONDecodeError:
  1018. pass
  1019. return self.DEFAULT_DRYING_PRESETS
  1020. def _get_conservative_drying_params(
  1021. self, trays: list[dict], module_type: str, presets: dict[str, dict[str, int]]
  1022. ) -> tuple[int, int, str] | None:
  1023. """Get the most conservative drying params for mixed filament types in an AMS unit.
  1024. Returns (temp, duration_hours, filament_type) or None if no drying-eligible filaments.
  1025. """
  1026. temp_key = module_type if module_type in ("n3f", "n3s") else "n3f"
  1027. hours_key = f"{temp_key}_hours"
  1028. min_temp = None
  1029. max_hours = None
  1030. filament_type = ""
  1031. for tray in trays:
  1032. tray_type = tray.get("tray_type", "")
  1033. if not tray_type:
  1034. continue
  1035. # Normalize filament type for preset lookup (e.g., "PLA Basic" -> "PLA")
  1036. base_type = tray_type.split()[0].upper()
  1037. preset = presets.get(base_type)
  1038. if not preset:
  1039. continue
  1040. temp = preset.get(temp_key, 55)
  1041. hours = preset.get(hours_key, 12)
  1042. # Conservative: lowest temp, longest duration
  1043. if min_temp is None or temp < min_temp:
  1044. min_temp = temp
  1045. if max_hours is None or hours > max_hours:
  1046. max_hours = hours
  1047. if not filament_type:
  1048. filament_type = base_type
  1049. if min_temp is None:
  1050. return None
  1051. return (min_temp, max_hours or 12, filament_type)
  1052. async def _check_auto_drying(
  1053. self,
  1054. db: AsyncSession,
  1055. queue_items: list[PrintQueueItem],
  1056. busy_printers: set[int],
  1057. *,
  1058. require_plate_clear: bool = True,
  1059. ):
  1060. """Start drying on idle printers based on humidity.
  1061. Two modes (can both be enabled):
  1062. - queue_drying_enabled: Dry between scheduled queue prints
  1063. - ambient_drying_enabled: Dry any idle printer when humidity is high, regardless of queue
  1064. """
  1065. queue_drying_enabled = await self._get_bool_setting(db, "queue_drying_enabled")
  1066. ambient_drying_enabled = await self._get_bool_setting(db, "ambient_drying_enabled")
  1067. if not queue_drying_enabled and not ambient_drying_enabled:
  1068. # Stop active drying on all printers if both features disabled
  1069. if self._drying_in_progress:
  1070. for pid in list(self._drying_in_progress):
  1071. logger.info("Auto-drying: printer %d — stopping, auto-drying disabled", pid)
  1072. await self._stop_drying(pid)
  1073. return
  1074. # Update drying state from printer status (handles backend restart)
  1075. self._sync_drying_state()
  1076. # Find printers with scheduled items (for queue drying mode)
  1077. printers_with_scheduled: set[int] = set()
  1078. printers_with_items: set[int] = set()
  1079. for item in queue_items:
  1080. if item.printer_id:
  1081. printers_with_items.add(item.printer_id)
  1082. if item.scheduled_time and not item.manual_start:
  1083. printers_with_scheduled.add(item.printer_id)
  1084. # If only queue mode is on and no printers have scheduled items, stop drying
  1085. if not ambient_drying_enabled and not printers_with_scheduled:
  1086. for pid in list(self._drying_in_progress):
  1087. logger.info("Auto-drying: printer %d — stopping, no scheduled prints in queue", pid)
  1088. await self._stop_drying(pid)
  1089. return
  1090. # Get humidity threshold
  1091. result = await db.execute(select(Settings).where(Settings.key == "ams_humidity_fair"))
  1092. setting = result.scalar_one_or_none()
  1093. humidity_threshold = int(setting.value) if setting else 60
  1094. # Get drying presets
  1095. presets = await self._get_drying_presets(db)
  1096. # Determine if drying should be skipped for printers with pending items
  1097. block_for_drying = await self._get_bool_setting(db, "queue_drying_block")
  1098. # Get all active printers
  1099. all_printers = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  1100. for printer in all_printers.scalars():
  1101. pid = printer.id
  1102. if pid in busy_printers:
  1103. logger.debug("Auto-drying: printer %d skipped — busy", pid)
  1104. continue
  1105. # In queue-only mode, only dry printers that have scheduled prints
  1106. if not ambient_drying_enabled and pid not in printers_with_scheduled:
  1107. if self._drying_in_progress.get(pid):
  1108. logger.info("Auto-drying: printer %d — stopping, no scheduled prints for this printer", pid)
  1109. await self._stop_drying(pid)
  1110. logger.debug("Auto-drying: printer %d skipped — no scheduled prints", pid)
  1111. continue
  1112. # When block mode is on, don't START new drying on printers with pending items.
  1113. # But allow already-drying printers through so humidity auto-stop logic still runs.
  1114. if block_for_drying and pid in printers_with_items and not self._drying_in_progress.get(pid):
  1115. logger.debug("Auto-drying: printer %d skipped — has pending items (block mode)", pid)
  1116. continue
  1117. if not printer_manager.is_connected(pid):
  1118. logger.debug("Auto-drying: printer %d skipped — not connected", pid)
  1119. continue
  1120. if not self._is_printer_idle(pid, require_plate_clear):
  1121. logger.debug("Auto-drying: printer %d skipped — not idle", pid)
  1122. continue
  1123. # Check if this printer supports drying
  1124. state = printer_manager.get_status(pid)
  1125. if not state:
  1126. logger.debug("Auto-drying: printer %d skipped — no state", pid)
  1127. continue
  1128. model = printer_manager.get_model(pid)
  1129. firmware = state.firmware_version
  1130. if not supports_drying(model, firmware):
  1131. logger.debug("Auto-drying: printer %d skipped — model %s does not support drying", pid, model)
  1132. continue
  1133. # Check each AMS unit from raw_data
  1134. ams_list = state.raw_data.get("ams", [])
  1135. logger.debug("Auto-drying: printer %d — checking %d AMS units", pid, len(ams_list))
  1136. for ams_data in ams_list:
  1137. module_type = str(ams_data.get("module_type") or "")
  1138. ams_id = int(ams_data.get("id", 0))
  1139. # Only n3f/n3s support drying
  1140. if module_type not in ("n3f", "n3s"):
  1141. logger.debug("Auto-drying: printer %d AMS %d skipped — module_type=%s", pid, ams_id, module_type)
  1142. continue
  1143. dry_time = int(ams_data.get("dry_time") or 0)
  1144. # Read humidity — prefer humidity_raw (actual %) over humidity (index 1-5)
  1145. humidity = None
  1146. h_raw = ams_data.get("humidity_raw")
  1147. if h_raw is not None:
  1148. try:
  1149. humidity = int(h_raw)
  1150. except (ValueError, TypeError):
  1151. pass
  1152. if humidity is None:
  1153. h_idx = ams_data.get("humidity")
  1154. if h_idx is not None:
  1155. try:
  1156. humidity = int(h_idx)
  1157. except (ValueError, TypeError):
  1158. pass
  1159. # Already drying — check if humidity dropped below threshold (with minimum drying time)
  1160. if dry_time > 0:
  1161. if pid not in self._drying_in_progress:
  1162. # Drying we didn't start (manual or from before restart) — track but don't stop
  1163. self._drying_in_progress[pid] = time.monotonic()
  1164. started_at = self._drying_in_progress[pid]
  1165. elapsed = time.monotonic() - started_at
  1166. if humidity is not None and humidity <= humidity_threshold and elapsed >= self._min_drying_seconds:
  1167. logger.info(
  1168. "Auto-drying: printer %d AMS %d — humidity %d%% <= threshold %d%% after %dm, stopping drying",
  1169. pid,
  1170. ams_id,
  1171. humidity,
  1172. humidity_threshold,
  1173. int(elapsed / 60),
  1174. )
  1175. printer_manager.send_drying_command(pid, ams_id, temp=0, duration=0, mode=0)
  1176. else:
  1177. logger.debug(
  1178. "Auto-drying: printer %d AMS %d — drying (%dm left, humidity %s%%, elapsed %dm/%dm min)",
  1179. pid,
  1180. ams_id,
  1181. dry_time,
  1182. humidity,
  1183. int(elapsed / 60),
  1184. self._min_drying_seconds // 60,
  1185. )
  1186. continue
  1187. # Humidity below threshold — no need to start drying
  1188. if humidity is None or humidity <= humidity_threshold:
  1189. logger.debug(
  1190. "Auto-drying: printer %d AMS %d skipped — humidity %s <= threshold %d",
  1191. pid,
  1192. ams_id,
  1193. humidity,
  1194. humidity_threshold,
  1195. )
  1196. continue
  1197. # Check cannot-dry reasons (power constraints etc.)
  1198. sf_reasons = ams_data.get("dry_sf_reason", [])
  1199. if sf_reasons:
  1200. logger.debug(
  1201. "Auto-drying: printer %d AMS %d skipped — cannot dry reasons: %s",
  1202. pid,
  1203. ams_id,
  1204. sf_reasons,
  1205. )
  1206. continue
  1207. # Get conservative drying params for mixed filaments
  1208. trays = ams_data.get("tray", [])
  1209. params = self._get_conservative_drying_params(trays, module_type, presets)
  1210. if not params:
  1211. logger.debug(
  1212. "Auto-drying: printer %d AMS %d skipped — no drying-eligible filaments in trays", pid, ams_id
  1213. )
  1214. continue
  1215. temp, duration_hours, filament_type = params
  1216. # Start drying
  1217. logger.info(
  1218. "Auto-drying: printer %d AMS %d — humidity %d%% > threshold %d%%, "
  1219. "starting %s drying at %d°C for %dh",
  1220. pid,
  1221. ams_id,
  1222. humidity,
  1223. humidity_threshold,
  1224. filament_type,
  1225. temp,
  1226. duration_hours,
  1227. )
  1228. success = printer_manager.send_drying_command(
  1229. pid, ams_id, temp, duration_hours, mode=1, filament=filament_type
  1230. )
  1231. if success:
  1232. self._drying_in_progress[pid] = time.monotonic()
  1233. def _sync_drying_state(self):
  1234. """Sync in-memory drying state with actual printer status.
  1235. Handles backend restart — if a printer is drying but we don't know about it,
  1236. update our state. If we think it's drying but it's not, clear it.
  1237. """
  1238. to_remove = []
  1239. for pid in self._drying_in_progress:
  1240. state = printer_manager.get_status(pid)
  1241. if not state:
  1242. to_remove.append(pid)
  1243. continue
  1244. # Check if any AMS unit is still drying
  1245. ams_list = state.raw_data.get("ams", [])
  1246. any_drying = any(int(a.get("dry_time") or 0) > 0 for a in ams_list)
  1247. if not any_drying:
  1248. to_remove.append(pid)
  1249. for pid in to_remove:
  1250. self._drying_in_progress.pop(pid, None)
  1251. async def _stop_drying(self, printer_id: int):
  1252. """Stop all active drying on a printer (print takes priority)."""
  1253. state = printer_manager.get_status(printer_id)
  1254. if not state:
  1255. self._drying_in_progress.pop(printer_id, None)
  1256. return
  1257. ams_list = state.raw_data.get("ams", [])
  1258. for ams_data in ams_list:
  1259. dry_time = int(ams_data.get("dry_time") or 0)
  1260. if dry_time > 0:
  1261. ams_id = int(ams_data.get("id", 0))
  1262. logger.info(
  1263. "Auto-drying: stopping drying on printer %d AMS %d — print takes priority",
  1264. printer_id,
  1265. ams_id,
  1266. )
  1267. printer_manager.send_drying_command(printer_id, ams_id, 0, 0, mode=0)
  1268. self._drying_in_progress.pop(printer_id, None)
  1269. async def _get_smart_plugs(self, db: AsyncSession, printer_id: int) -> list[SmartPlug]:
  1270. """Get all smart plugs associated with a printer."""
  1271. result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  1272. return list(result.scalars().all())
  1273. async def _power_on_and_wait(self, plug: SmartPlug, printer_id: int, db: AsyncSession) -> bool:
  1274. """Turn on smart plug and wait for printer to connect.
  1275. Returns True if printer connected successfully within timeout.
  1276. """
  1277. # Get the appropriate service for the plug type (Tasmota or Home Assistant)
  1278. service = await smart_plug_manager.get_service_for_plug(plug, db)
  1279. # Check current plug state
  1280. status = await service.get_status(plug)
  1281. if not status.get("reachable"):
  1282. logger.warning("Smart plug '%s' is not reachable", plug.name)
  1283. return False
  1284. # Turn on if not already on
  1285. if status.get("state") != "ON":
  1286. success = await service.turn_on(plug)
  1287. if not success:
  1288. logger.warning("Failed to turn on smart plug '%s'", plug.name)
  1289. return False
  1290. logger.info("Powered on smart plug '%s' for printer %s", plug.name, printer_id)
  1291. # Get printer from database for connection
  1292. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1293. printer = result.scalar_one_or_none()
  1294. if not printer:
  1295. logger.error("Printer %s not found in database", printer_id)
  1296. return False
  1297. # Wait for printer to boot (give it some time before trying to connect)
  1298. logger.info("Waiting 30s for printer %s to boot...", printer_id)
  1299. await asyncio.sleep(30)
  1300. # Try to connect to the printer periodically
  1301. elapsed = 30 # Already waited 30s
  1302. while elapsed < self._power_on_wait_time:
  1303. # Try to connect
  1304. logger.info("Attempting to connect to printer %s...", printer_id)
  1305. try:
  1306. connected = await printer_manager.connect_printer(printer)
  1307. if connected:
  1308. logger.info("Printer %s connected after %ss", printer_id, elapsed)
  1309. # Give it a moment to stabilize and get status
  1310. await asyncio.sleep(5)
  1311. return True
  1312. except Exception as e:
  1313. logger.debug("Connection attempt failed: %s", e)
  1314. await asyncio.sleep(self._power_on_check_interval)
  1315. elapsed += self._power_on_check_interval
  1316. logger.debug("Waiting for printer %s to connect... (%ss)", printer_id, elapsed)
  1317. logger.warning("Printer %s did not connect within %ss after power on", printer_id, self._power_on_wait_time)
  1318. return False
  1319. async def _check_previous_success(self, db: AsyncSession, item: PrintQueueItem) -> bool:
  1320. """Check if the previous print on this printer succeeded."""
  1321. # Find the most recent completed queue item for this printer
  1322. result = await db.execute(
  1323. select(PrintQueueItem)
  1324. .where(PrintQueueItem.printer_id == item.printer_id)
  1325. .where(PrintQueueItem.id != item.id)
  1326. .where(PrintQueueItem.status.in_(["completed", "failed", "skipped", "aborted"]))
  1327. .order_by(PrintQueueItem.completed_at.desc())
  1328. .limit(1)
  1329. )
  1330. prev_item = result.scalar_one_or_none()
  1331. # If no previous item, assume success (first in queue)
  1332. if not prev_item:
  1333. return True
  1334. return prev_item.status == "completed"
  1335. async def _power_off_if_needed(self, db: AsyncSession, item: PrintQueueItem):
  1336. """Power off printer if auto_off_after is enabled (waits for cooldown)."""
  1337. if not item.auto_off_after:
  1338. return
  1339. plugs = await self._get_smart_plugs(db, item.printer_id)
  1340. plug_ids = [p.id for p in plugs if p.enabled]
  1341. if plug_ids:
  1342. logger.info("Auto-off: Waiting for printer %s to cool down before power off...", item.printer_id)
  1343. # Wait for cooldown (up to 10 minutes)
  1344. await printer_manager.wait_for_cooldown(item.printer_id, target_temp=50.0, timeout=600)
  1345. # Re-fetch plugs in a fresh session after the long cooldown wait
  1346. async with async_session() as new_db:
  1347. for plug_id in plug_ids:
  1348. try:
  1349. result = await new_db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  1350. plug = result.scalar_one_or_none()
  1351. if plug and plug.enabled:
  1352. logger.info("Auto-off: Powering off plug '%s' for printer %s", plug.name, item.printer_id)
  1353. service = await smart_plug_manager.get_service_for_plug(plug, new_db)
  1354. await service.turn_off(plug)
  1355. except Exception as e:
  1356. logger.warning(
  1357. "Auto-off: Failed to power off plug %s for printer %s: %s", plug_id, item.printer_id, e
  1358. )
  1359. async def _get_job_name(self, db: AsyncSession, item: PrintQueueItem) -> str:
  1360. """Get a human-readable name for a queue item."""
  1361. if item.archive_id:
  1362. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  1363. archive = result.scalar_one_or_none()
  1364. if archive:
  1365. return archive.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  1366. if item.library_file_id:
  1367. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  1368. library_file = result.scalar_one_or_none()
  1369. if library_file:
  1370. return library_file.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  1371. return f"Job #{item.id}"
  1372. async def _get_printer(self, db: AsyncSession, printer_id: int) -> Printer | None:
  1373. """Get printer by ID."""
  1374. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1375. return result.scalar_one_or_none()
  1376. async def _start_print(self, db: AsyncSession, item: PrintQueueItem):
  1377. """Upload file and start print for a queue item.
  1378. Supports two sources:
  1379. - archive_id: Print from an existing archive
  1380. - library_file_id: Print from a library file (file manager)
  1381. """
  1382. logger.info("Starting queue item %s", item.id)
  1383. # Get printer first (needed for both paths)
  1384. result = await db.execute(select(Printer).where(Printer.id == item.printer_id))
  1385. printer = result.scalar_one_or_none()
  1386. if not printer:
  1387. item.status = "failed"
  1388. item.error_message = "Printer not found"
  1389. item.completed_at = datetime.now(timezone.utc)
  1390. await db.commit()
  1391. logger.error("Queue item %s: Printer %s not found", item.id, item.printer_id)
  1392. await self._power_off_if_needed(db, item)
  1393. return
  1394. # Check printer is connected
  1395. if not printer_manager.is_connected(item.printer_id):
  1396. item.status = "failed"
  1397. item.error_message = "Printer not connected"
  1398. item.completed_at = datetime.now(timezone.utc)
  1399. await db.commit()
  1400. logger.error("Queue item %s: Printer %s not connected", item.id, item.printer_id)
  1401. await self._power_off_if_needed(db, item)
  1402. return
  1403. # Determine source: archive or library file
  1404. archive = None
  1405. library_file = None
  1406. file_path = None
  1407. filename = None
  1408. if item.archive_id:
  1409. # Print from archive
  1410. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  1411. archive = result.scalar_one_or_none()
  1412. if not archive:
  1413. item.status = "failed"
  1414. item.error_message = "Archive not found"
  1415. item.completed_at = datetime.now(timezone.utc)
  1416. await db.commit()
  1417. logger.error("Queue item %s: Archive %s not found", item.id, item.archive_id)
  1418. await self._power_off_if_needed(db, item)
  1419. return
  1420. file_path = settings.base_dir / archive.file_path
  1421. filename = archive.filename
  1422. elif item.library_file_id:
  1423. # Print from library file (file manager)
  1424. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  1425. library_file = result.scalar_one_or_none()
  1426. if not library_file:
  1427. item.status = "failed"
  1428. item.error_message = "Library file not found"
  1429. item.completed_at = datetime.now(timezone.utc)
  1430. await db.commit()
  1431. logger.error("Queue item %s: Library file %s not found", item.id, item.library_file_id)
  1432. await self._power_off_if_needed(db, item)
  1433. return
  1434. # Library files store absolute paths
  1435. lib_path = Path(library_file.file_path)
  1436. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  1437. filename = library_file.filename
  1438. # Create archive from library file so usage tracking has access to the 3MF
  1439. try:
  1440. from backend.app.services.archive import ArchiveService
  1441. archive_service = ArchiveService(db)
  1442. archive = await archive_service.archive_print(
  1443. printer_id=item.printer_id,
  1444. source_file=file_path,
  1445. original_filename=filename,
  1446. created_by_id=item.created_by_id,
  1447. project_id=item.project_id,
  1448. )
  1449. if archive:
  1450. item.archive_id = archive.id
  1451. await db.flush()
  1452. logger.info(
  1453. "Queue item %s: Created archive %s from library file %s",
  1454. item.id,
  1455. archive.id,
  1456. item.library_file_id,
  1457. )
  1458. except Exception as e:
  1459. logger.warning("Queue item %s: Failed to create archive from library file: %s", item.id, e)
  1460. else:
  1461. # Neither archive nor library file specified
  1462. item.status = "failed"
  1463. item.error_message = "No source file specified"
  1464. item.completed_at = datetime.now(timezone.utc)
  1465. await db.commit()
  1466. logger.error("Queue item %s: No archive_id or library_file_id specified", item.id)
  1467. await self._power_off_if_needed(db, item)
  1468. return
  1469. # Check file exists on disk
  1470. if not file_path.exists():
  1471. item.status = "failed"
  1472. item.error_message = "Source file not found on disk"
  1473. item.completed_at = datetime.now(timezone.utc)
  1474. await db.commit()
  1475. logger.error("Queue item %s: File not found: %s", item.id, file_path)
  1476. await self._power_off_if_needed(db, item)
  1477. return
  1478. # G-code injection for auto-print systems (#422)
  1479. injected_path = None
  1480. if item.gcode_injection:
  1481. try:
  1482. snippets_raw = await self._get_setting(db, "gcode_snippets")
  1483. if snippets_raw:
  1484. snippets = json.loads(snippets_raw)
  1485. model_snippets = snippets.get(printer.model, {})
  1486. start_gc = (model_snippets.get("start_gcode") or "").strip()
  1487. end_gc = (model_snippets.get("end_gcode") or "").strip()
  1488. if start_gc or end_gc:
  1489. from backend.app.utils.threemf_tools import inject_gcode_into_3mf
  1490. injected_path = inject_gcode_into_3mf(
  1491. file_path, item.plate_id or 1, start_gc or None, end_gc or None
  1492. )
  1493. if injected_path:
  1494. file_path = injected_path
  1495. logger.info("Queue item %s: G-code injected for model %s", item.id, printer.model)
  1496. else:
  1497. logger.warning(
  1498. "Queue item %s: G-code injection returned no result, using original", item.id
  1499. )
  1500. except Exception as e:
  1501. logger.warning("Queue item %s: G-code injection failed, using original: %s", item.id, e)
  1502. # Upload file to printer via FTP
  1503. # Use a clean filename to avoid issues with double extensions like .gcode.3mf
  1504. base_name = filename
  1505. if base_name.endswith(".gcode.3mf"):
  1506. base_name = base_name[:-10] # Remove .gcode.3mf
  1507. elif base_name.endswith(".3mf"):
  1508. base_name = base_name[:-4] # Remove .3mf
  1509. remote_filename = f"{base_name}.3mf"
  1510. # Sanitize: firmware parses ftp://{filename} as a URL, spaces break it
  1511. remote_filename = remote_filename.replace(" ", "_")
  1512. # Upload to root directory (not /cache/) - the start_print command references
  1513. # files by name only (ftp://{filename}), so they must be in the root
  1514. remote_path = f"/{remote_filename}"
  1515. # Get FTP retry settings
  1516. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  1517. logger.info(
  1518. f"Queue item {item.id}: FTP upload starting - printer={printer.name} ({printer.model}), "
  1519. f"ip={printer.ip_address}, file={remote_filename}, local_path={file_path}, "
  1520. f"retry_enabled={ftp_retry_enabled}, retry_count={ftp_retry_count}, timeout={ftp_timeout}"
  1521. )
  1522. # Delete existing file if present (avoids 553 error on overwrite)
  1523. try:
  1524. logger.debug("Queue item %s: Deleting existing file %s if present...", item.id, remote_path)
  1525. delete_result = await delete_file_async(
  1526. printer.ip_address,
  1527. printer.access_code,
  1528. remote_path,
  1529. socket_timeout=ftp_timeout,
  1530. printer_model=printer.model,
  1531. )
  1532. logger.debug("Queue item %s: Delete result: %s", item.id, delete_result)
  1533. except Exception as e:
  1534. logger.debug("Queue item %s: Delete failed (may not exist): %s", item.id, e)
  1535. try:
  1536. if ftp_retry_enabled:
  1537. uploaded = await with_ftp_retry(
  1538. upload_file_async,
  1539. printer.ip_address,
  1540. printer.access_code,
  1541. file_path,
  1542. remote_path,
  1543. socket_timeout=ftp_timeout,
  1544. printer_model=printer.model,
  1545. max_retries=ftp_retry_count,
  1546. retry_delay=ftp_retry_delay,
  1547. operation_name=f"Upload print to {printer.name}",
  1548. )
  1549. else:
  1550. uploaded = await upload_file_async(
  1551. printer.ip_address,
  1552. printer.access_code,
  1553. file_path,
  1554. remote_path,
  1555. socket_timeout=ftp_timeout,
  1556. printer_model=printer.model,
  1557. )
  1558. except Exception as e:
  1559. uploaded = False
  1560. logger.error("Queue item %s: FTP error: %s (type: %s)", item.id, e, type(e).__name__)
  1561. # Clean up injected temp file after upload attempt
  1562. if injected_path and injected_path.exists():
  1563. injected_path.unlink(missing_ok=True)
  1564. if not uploaded:
  1565. error_msg = (
  1566. "Failed to upload file to printer. Check if SD card is inserted and properly formatted (FAT32/exFAT). "
  1567. "See server logs for detailed diagnostics."
  1568. )
  1569. item.status = "failed"
  1570. item.error_message = error_msg
  1571. item.completed_at = datetime.now(timezone.utc)
  1572. await db.commit()
  1573. logger.error(
  1574. f"Queue item {item.id}: FTP upload failed - printer={printer.name}, model={printer.model}, "
  1575. f"ip={printer.ip_address}. Check logs above for storage diagnostics and specific error codes."
  1576. )
  1577. # Send failure notification
  1578. await notification_service.on_queue_job_failed(
  1579. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  1580. printer_id=printer.id,
  1581. printer_name=printer.name,
  1582. reason="Failed to upload file to printer",
  1583. db=db,
  1584. )
  1585. await self._power_off_if_needed(db, item)
  1586. return
  1587. # Parse AMS mapping if stored
  1588. ams_mapping = None
  1589. if item.ams_mapping:
  1590. try:
  1591. ams_mapping = json.loads(item.ams_mapping)
  1592. except json.JSONDecodeError:
  1593. logger.warning("Queue item %s: Invalid AMS mapping JSON, ignoring", item.id)
  1594. # Register as expected print so we don't create a duplicate archive
  1595. # Only applicable for archive-based prints
  1596. if archive:
  1597. from backend.app.main import register_expected_print
  1598. register_expected_print(
  1599. item.printer_id,
  1600. remote_filename,
  1601. archive.id,
  1602. ams_mapping=ams_mapping,
  1603. created_by_id=item.created_by_id,
  1604. )
  1605. # IMPORTANT: Set status to "printing" BEFORE sending the print command.
  1606. # This prevents phantom reprints if the backend crashes/restarts after the
  1607. # print command is sent but before the status update is committed.
  1608. # If we crash after this commit but before start_print(), the item will be
  1609. # in "printing" status without actually printing - but that's safer than
  1610. # accidentally reprinting the same file hours later.
  1611. item.status = "printing"
  1612. item.started_at = datetime.now(timezone.utc)
  1613. await db.commit()
  1614. # Clear the awaiting-plate-clear flag now that we're starting a new print
  1615. printer_manager.set_awaiting_plate_clear(item.printer_id, False)
  1616. logger.info("Queue item %s: Status set to 'printing', sending print command...", item.id)
  1617. # Capture state before dispatch so the watchdog can detect whether the
  1618. # printer actually transitioned (#967). Also capture subtask_id so the
  1619. # watchdog can recognise "command landed but state hasn't flipped yet"
  1620. # on slow H2D transitions (#1078).
  1621. pre_status = printer_manager.get_status(item.printer_id)
  1622. pre_state = getattr(pre_status, "state", None) if pre_status else None
  1623. pre_subtask_id = getattr(pre_status, "subtask_id", None) if pre_status else None
  1624. # Start the print with AMS mapping, plate_id and print options
  1625. started = printer_manager.start_print(
  1626. item.printer_id,
  1627. remote_filename,
  1628. plate_id=item.plate_id or 1,
  1629. ams_mapping=ams_mapping,
  1630. bed_levelling=item.bed_levelling,
  1631. flow_cali=item.flow_cali,
  1632. vibration_cali=item.vibration_cali,
  1633. layer_inspect=item.layer_inspect,
  1634. timelapse=item.timelapse,
  1635. use_ams=item.use_ams,
  1636. )
  1637. if started:
  1638. logger.info("Queue item %s: Print started successfully - %s", item.id, filename)
  1639. # Watchdog: if the printer never transitions out of pre_state AND
  1640. # never advances subtask_id, the MQTT publish was accepted locally but
  1641. # didn't reach the printer (half-broken session — same shape as
  1642. # #887/#936). Revert the queue item so the next dispatch can pick it
  1643. # up instead of leaving it stuck in "printing" (#967). subtask_id
  1644. # check avoids false reverts on slow H2D FINISH→PREPARE transitions
  1645. # that would otherwise cause the item to re-dispatch as a reprint
  1646. # of the just-finished job (#1078).
  1647. if pre_state:
  1648. asyncio.create_task(
  1649. self._watchdog_print_start(
  1650. item.id,
  1651. item.printer_id,
  1652. pre_state,
  1653. pre_subtask_id,
  1654. )
  1655. )
  1656. # Get estimated time for notification
  1657. estimated_time = None
  1658. if archive and archive.print_time_seconds:
  1659. estimated_time = archive.print_time_seconds
  1660. elif library_file and library_file.print_time_seconds:
  1661. estimated_time = library_file.print_time_seconds
  1662. # Send job started notification
  1663. await notification_service.on_queue_job_started(
  1664. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  1665. printer_id=printer.id,
  1666. printer_name=printer.name,
  1667. db=db,
  1668. estimated_time=estimated_time,
  1669. )
  1670. # MQTT relay - publish queue job started
  1671. try:
  1672. from backend.app.services.mqtt_relay import mqtt_relay
  1673. await mqtt_relay.on_queue_job_started(
  1674. job_id=item.id,
  1675. filename=filename,
  1676. printer_id=printer.id,
  1677. printer_name=printer.name,
  1678. printer_serial=printer.serial_number,
  1679. )
  1680. except Exception:
  1681. pass # Don't fail if MQTT fails
  1682. else:
  1683. # Clean up uploaded file from SD card to prevent phantom prints
  1684. try:
  1685. await delete_file_async(
  1686. printer.ip_address,
  1687. printer.access_code,
  1688. remote_path,
  1689. printer_model=printer.model,
  1690. )
  1691. except Exception:
  1692. pass # Best-effort — don't fail the error handler
  1693. # Print command failed - revert status
  1694. item.status = "failed"
  1695. item.error_message = "Failed to send print command to printer"
  1696. item.completed_at = datetime.now(timezone.utc)
  1697. await db.commit()
  1698. logger.error(
  1699. f"Queue item {item.id}: Failed to start print on {printer.name} ({printer.model}) - "
  1700. f"printer_manager.start_print() returned False. "
  1701. f"This may indicate: printer not connected, MQTT error, unsupported model configuration, or firmware issue. "
  1702. f"Check printer status and backend logs for details."
  1703. )
  1704. # Send failure notification
  1705. await notification_service.on_queue_job_failed(
  1706. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  1707. printer_id=printer.id,
  1708. printer_name=printer.name,
  1709. reason="Failed to send print command to printer - check printer connection and status",
  1710. db=db,
  1711. )
  1712. await self._power_off_if_needed(db, item)
  1713. @staticmethod
  1714. async def _watchdog_print_start(
  1715. queue_item_id: int,
  1716. printer_id: int,
  1717. pre_state: str,
  1718. pre_subtask_id: str | None = None,
  1719. timeout: float = 90.0,
  1720. poll_interval: float = 3.0,
  1721. ) -> None:
  1722. """Revert a queue item if the printer never acknowledges the start command.
  1723. Bambuddy optimistically marks the queue item as "printing" right after the
  1724. MQTT project_file publish succeeds locally. If the printer drops/ignores the
  1725. command (half-broken MQTT session — #887/#936), the state never transitions
  1726. and the item would otherwise stay stuck in "printing" forever (#967).
  1727. Exit paths (printer picked up the job — no revert):
  1728. - gcode_state changed from pre_state, OR
  1729. - subtask_id advanced past pre_subtask_id — the printer echoes our
  1730. per-dispatch identity back on push_status, so a subtask_id change is
  1731. a definitive "command landed" signal even while state is still FINISH.
  1732. H2D can sit at FINISH for ~50 s after accepting project_file before
  1733. transitioning to PREPARE, which used to trip the state-only watchdog
  1734. and caused the scheduler to revert + re-dispatch the item; the next
  1735. successful dispatch then looked like a reprint of the just-finished
  1736. job (#1078).
  1737. Timeout raised from 45 s → 90 s as belt-and-braces for slow transitions
  1738. that also don't emit an early subtask_id tick.
  1739. """
  1740. deadline = time.monotonic() + timeout
  1741. while time.monotonic() < deadline:
  1742. await asyncio.sleep(poll_interval)
  1743. status = printer_manager.get_status(printer_id)
  1744. if not status:
  1745. return # Printer disconnected — don't mess with the DB
  1746. if status.state != pre_state:
  1747. return # Printer picked up the job (state transition)
  1748. if pre_subtask_id is not None and status.subtask_id is not None and status.subtask_id != pre_subtask_id:
  1749. return # Printer picked up the job (subtask_id advanced)
  1750. # No transition. Revert the item so the scheduler can retry.
  1751. async with async_session() as db:
  1752. item = await db.get(PrintQueueItem, queue_item_id)
  1753. if not item or item.status != "printing":
  1754. return # Already moved on (completed/cancelled/etc.)
  1755. item.status = "pending"
  1756. item.started_at = None
  1757. await db.commit()
  1758. logger.warning(
  1759. "Queue item %s: printer %d did not respond to print command within "
  1760. "%.0fs (state still %s, subtask_id still %s) — reverted to 'pending' "
  1761. "for retry (#967)",
  1762. queue_item_id,
  1763. printer_id,
  1764. timeout,
  1765. pre_state,
  1766. pre_subtask_id,
  1767. )
  1768. # Same half-broken-session recovery as background_dispatch: force the
  1769. # MQTT client to reconnect so the next dispatch lands without a power cycle.
  1770. client = printer_manager.get_client(printer_id)
  1771. if client and hasattr(client, "force_reconnect_stale_session"):
  1772. client.force_reconnect_stale_session(
  1773. f"queue print command unacknowledged after {timeout:.0f}s (state still {pre_state})"
  1774. )
  1775. # Global scheduler instance
  1776. scheduler = PrintScheduler()