main.py 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. import asyncio
  2. import logging
  3. from contextlib import asynccontextmanager
  4. from datetime import datetime, timedelta
  5. from logging.handlers import RotatingFileHandler
  6. from fastapi import FastAPI
  7. # Import settings first for logging configuration
  8. from backend.app.core.config import APP_VERSION, settings as app_settings
  9. # Configure logging based on settings
  10. # DEBUG=true -> DEBUG level, else use LOG_LEVEL setting
  11. log_level_str = "DEBUG" if app_settings.debug else app_settings.log_level.upper()
  12. log_level = getattr(logging, log_level_str, logging.INFO)
  13. log_format = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
  14. # Create root logger
  15. root_logger = logging.getLogger()
  16. root_logger.setLevel(log_level)
  17. # Console handler - always enabled
  18. console_handler = logging.StreamHandler()
  19. console_handler.setLevel(log_level)
  20. console_handler.setFormatter(logging.Formatter(log_format))
  21. root_logger.addHandler(console_handler)
  22. # File handler - only in production or if explicitly enabled
  23. if app_settings.log_to_file:
  24. log_file = app_settings.log_dir / "bambuddy.log"
  25. file_handler = RotatingFileHandler(
  26. log_file,
  27. maxBytes=5 * 1024 * 1024, # 5MB
  28. backupCount=3,
  29. encoding="utf-8",
  30. )
  31. file_handler.setLevel(log_level)
  32. file_handler.setFormatter(logging.Formatter(log_format))
  33. root_logger.addHandler(file_handler)
  34. logging.info(f"Logging to file: {log_file}")
  35. # Reduce noise from third-party libraries in production
  36. if not app_settings.debug:
  37. logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
  38. logging.getLogger("httpcore").setLevel(logging.WARNING)
  39. logging.getLogger("httpx").setLevel(logging.WARNING)
  40. logging.info(f"Bambuddy starting - debug={app_settings.debug}, log_level={log_level_str}")
  41. from fastapi.responses import FileResponse
  42. from fastapi.staticfiles import StaticFiles
  43. from sqlalchemy import delete, or_, select
  44. from backend.app.api.routes import (
  45. ams_history,
  46. api_keys,
  47. archives,
  48. camera,
  49. cloud,
  50. discovery,
  51. external_links,
  52. filaments,
  53. kprofiles,
  54. maintenance,
  55. notification_templates,
  56. notifications,
  57. print_queue,
  58. printers,
  59. projects,
  60. settings as settings_routes,
  61. smart_plugs,
  62. spoolman,
  63. system,
  64. updates,
  65. webhook,
  66. websocket,
  67. )
  68. from backend.app.api.routes.maintenance import _get_printer_maintenance_internal, ensure_default_types
  69. from backend.app.core.database import async_session, init_db
  70. from backend.app.core.websocket import ws_manager
  71. from backend.app.models.smart_plug import SmartPlug
  72. from backend.app.services.archive import ArchiveService
  73. from backend.app.services.bambu_ftp import download_file_async
  74. from backend.app.services.bambu_mqtt import PrinterState
  75. from backend.app.services.notification_service import notification_service
  76. from backend.app.services.print_scheduler import scheduler as print_scheduler
  77. from backend.app.services.printer_manager import (
  78. init_printer_connections,
  79. printer_manager,
  80. printer_state_to_dict,
  81. )
  82. from backend.app.services.smart_plug_manager import smart_plug_manager
  83. from backend.app.services.spoolman import close_spoolman_client, get_spoolman_client, init_spoolman_client
  84. from backend.app.services.tasmota import tasmota_service
  85. from backend.app.services.telemetry import start_telemetry_loop
  86. # Track active prints: {(printer_id, filename): archive_id}
  87. _active_prints: dict[tuple[int, str], int] = {}
  88. # Track expected prints from reprint/scheduled (skip auto-archiving for these)
  89. # {(printer_id, filename): archive_id}
  90. _expected_prints: dict[tuple[int, str], int] = {}
  91. # Track starting energy for prints: {archive_id: starting_kwh}
  92. _print_energy_start: dict[int, float] = {}
  93. def register_expected_print(printer_id: int, filename: str, archive_id: int):
  94. """Register an expected print from reprint/scheduled so we don't create duplicate archives."""
  95. # Store with multiple filename variations to catch different naming patterns
  96. _expected_prints[(printer_id, filename)] = archive_id
  97. # Also store without .3mf extension if present
  98. if filename.endswith(".3mf"):
  99. base = filename[:-4]
  100. _expected_prints[(printer_id, base)] = archive_id
  101. _expected_prints[(printer_id, f"{base}.gcode")] = archive_id
  102. logging.getLogger(__name__).info(
  103. f"Registered expected print: printer={printer_id}, file={filename}, archive={archive_id}"
  104. )
  105. _last_status_broadcast: dict[int, str] = {}
  106. _nozzle_count_updated: set[int] = set() # Track printers where we've updated nozzle_count
  107. async def _report_spoolman_usage(printer_id: int, archive_id: int, logger):
  108. """Report filament usage to Spoolman after print completion.
  109. This finds the spool by RFID tag_uid from current AMS state and reports
  110. the filament_used_grams from the archive metadata.
  111. """
  112. async with async_session() as db:
  113. from backend.app.api.routes.settings import get_setting
  114. from backend.app.models.archive import PrintArchive
  115. # Check if Spoolman is enabled
  116. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  117. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  118. return
  119. # Get Spoolman URL
  120. spoolman_url = await get_setting(db, "spoolman_url")
  121. if not spoolman_url:
  122. return
  123. # Get or create Spoolman client
  124. client = await get_spoolman_client()
  125. if not client:
  126. client = await init_spoolman_client(spoolman_url)
  127. # Check if Spoolman is reachable
  128. if not await client.health_check():
  129. logger.warning("Spoolman not reachable for usage reporting")
  130. return
  131. # Get archive to find filament usage
  132. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  133. archive = result.scalar_one_or_none()
  134. if not archive or not archive.filament_used_grams:
  135. logger.debug(f"No filament usage data for archive {archive_id}")
  136. return
  137. filament_used = archive.filament_used_grams
  138. logger.info(f"[SPOOLMAN] Archive {archive_id} used {filament_used}g of filament")
  139. # Get current AMS state from printer to find the active spool
  140. state = printer_manager.get_status(printer_id)
  141. if not state or not state.raw_data:
  142. logger.debug("No printer state available for usage reporting")
  143. return
  144. ams_data = state.raw_data.get("ams")
  145. if not ams_data:
  146. logger.debug("No AMS data available for usage reporting")
  147. return
  148. # Find spools with RFID tags in Spoolman and report usage
  149. # For now, we report usage to the first spool found with a matching tag
  150. # TODO: In future, track which specific trays were used during the print
  151. spools_updated = 0
  152. for ams_unit in ams_data:
  153. trays = ams_unit.get("tray", [])
  154. for tray_data in trays:
  155. tag_uid = tray_data.get("tag_uid")
  156. if not tag_uid:
  157. continue
  158. # Find spool in Spoolman by tag
  159. spool = await client.find_spool_by_tag(tag_uid)
  160. if spool:
  161. # Report usage to Spoolman
  162. result = await client.use_spool(spool["id"], filament_used)
  163. if result:
  164. logger.info(
  165. f"[SPOOLMAN] Reported {filament_used}g usage to spool {spool['id']} (tag: {tag_uid})"
  166. )
  167. spools_updated += 1
  168. # Only report to one spool for single-material prints
  169. # Multi-material prints would need more sophisticated tracking
  170. return
  171. if spools_updated == 0:
  172. logger.debug(f"No matching Spoolman spools found for printer {printer_id}")
  173. async def on_printer_status_change(printer_id: int, state: PrinterState):
  174. """Handle printer status changes - broadcast via WebSocket."""
  175. # Only broadcast if something meaningful changed (reduce WebSocket spam)
  176. # Include rounded temperatures to detect meaningful temp changes (within 1 degree)
  177. temps = state.temperatures or {}
  178. nozzle_temp = round(temps.get("nozzle", 0))
  179. bed_temp = round(temps.get("bed", 0))
  180. nozzle_2_temp = round(temps.get("nozzle_2", 0)) if "nozzle_2" in temps else ""
  181. chamber_temp = round(temps.get("chamber", 0)) if "chamber" in temps else ""
  182. # Auto-detect dual-nozzle printers from MQTT temperature data
  183. if "nozzle_2" in temps and printer_id not in _nozzle_count_updated:
  184. _nozzle_count_updated.add(printer_id)
  185. # Update nozzle_count in database
  186. async with async_session() as db:
  187. from backend.app.models.printer import Printer
  188. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  189. printer = result.scalar_one_or_none()
  190. if printer and printer.nozzle_count != 2:
  191. printer.nozzle_count = 2
  192. await db.commit()
  193. logging.getLogger(__name__).info(
  194. f"Auto-detected dual-nozzle printer {printer_id}, updated nozzle_count=2"
  195. )
  196. # Include target temps for heating phase detection
  197. bed_target = round(temps.get("bed_target", 0))
  198. nozzle_target = round(temps.get("nozzle_target", 0))
  199. status_key = (
  200. f"{state.connected}:{state.state}:{state.progress}:{state.layer_num}:"
  201. f"{nozzle_temp}:{bed_temp}:{nozzle_2_temp}:{chamber_temp}:"
  202. f"{state.stg_cur}:{bed_target}:{nozzle_target}"
  203. )
  204. if _last_status_broadcast.get(printer_id) == status_key:
  205. return # No change, skip broadcast
  206. _last_status_broadcast[printer_id] = status_key
  207. await ws_manager.send_printer_status(
  208. printer_id,
  209. printer_state_to_dict(state, printer_id),
  210. )
  211. async def on_ams_change(printer_id: int, ams_data: list):
  212. """Handle AMS data changes - sync to Spoolman if enabled and auto mode."""
  213. import logging
  214. logger = logging.getLogger(__name__)
  215. try:
  216. async with async_session() as db:
  217. from backend.app.api.routes.settings import get_setting
  218. from backend.app.models.printer import Printer
  219. # Check if Spoolman is enabled
  220. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  221. if not spoolman_enabled or spoolman_enabled.lower() != "true":
  222. return
  223. # Check sync mode
  224. sync_mode = await get_setting(db, "spoolman_sync_mode")
  225. if sync_mode and sync_mode != "auto":
  226. return # Only sync on auto mode
  227. # Get Spoolman URL
  228. spoolman_url = await get_setting(db, "spoolman_url")
  229. if not spoolman_url:
  230. return
  231. # Get or create Spoolman client
  232. client = await get_spoolman_client()
  233. if not client:
  234. client = await init_spoolman_client(spoolman_url)
  235. # Check if Spoolman is reachable
  236. if not await client.health_check():
  237. logger.warning(f"Spoolman not reachable at {spoolman_url}")
  238. return
  239. # Get printer name for location
  240. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  241. printer = result.scalar_one_or_none()
  242. printer_name = printer.name if printer else f"Printer {printer_id}"
  243. # Sync each AMS tray
  244. synced = 0
  245. for ams_unit in ams_data:
  246. ams_id = int(ams_unit.get("id", 0))
  247. trays = ams_unit.get("tray", [])
  248. for tray_data in trays:
  249. tray = client.parse_ams_tray(ams_id, tray_data)
  250. if not tray:
  251. continue # Empty tray
  252. try:
  253. result = await client.sync_ams_tray(tray, printer_name)
  254. if result:
  255. synced += 1
  256. except Exception as e:
  257. logger.error(f"Error syncing AMS {ams_id} tray {tray.tray_id}: {e}")
  258. if synced > 0:
  259. logger.info(f"Auto-synced {synced} AMS trays to Spoolman for printer {printer_id}")
  260. except Exception as e:
  261. import logging
  262. logging.getLogger(__name__).warning(f"Spoolman AMS sync failed: {e}")
  263. async def _send_print_start_notification(
  264. printer_id: int,
  265. data: dict,
  266. archive_data: dict | None = None,
  267. logger=None,
  268. ):
  269. """Helper to send print start notification with optional archive data."""
  270. if logger is None:
  271. import logging
  272. logger = logging.getLogger(__name__)
  273. try:
  274. async with async_session() as db:
  275. from backend.app.models.printer import Printer
  276. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  277. printer = result.scalar_one_or_none()
  278. printer_name = printer.name if printer else f"Printer {printer_id}"
  279. await notification_service.on_print_start(printer_id, printer_name, data, db, archive_data=archive_data)
  280. except Exception as e:
  281. logger.warning(f"Notification on_print_start failed: {e}")
  282. async def on_print_start(printer_id: int, data: dict):
  283. """Handle print start - archive the 3MF file immediately."""
  284. import logging
  285. logger = logging.getLogger(__name__)
  286. logger.info(f"[CALLBACK] on_print_start called for printer {printer_id}, data keys: {list(data.keys())}")
  287. await ws_manager.send_print_start(printer_id, data)
  288. # Track if notification was sent (to avoid sending twice)
  289. notification_sent = False
  290. # Smart plug automation: turn on plug when print starts
  291. try:
  292. async with async_session() as db:
  293. await smart_plug_manager.on_print_start(printer_id, db)
  294. except Exception as e:
  295. logger.warning(f"Smart plug on_print_start failed: {e}")
  296. async with async_session() as db:
  297. from backend.app.models.printer import Printer
  298. from backend.app.services.bambu_ftp import list_files_async
  299. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  300. printer = result.scalar_one_or_none()
  301. if not printer or not printer.auto_archive:
  302. # Send notification without archive data (auto-archive disabled)
  303. logger.info(
  304. f"[CALLBACK] Skipping archive - printer: {printer is not None}, auto_archive: {printer.auto_archive if printer else 'N/A'}"
  305. )
  306. if not notification_sent:
  307. await _send_print_start_notification(printer_id, data, logger=logger)
  308. return
  309. # Get the filename and subtask_name
  310. filename = data.get("filename", "")
  311. subtask_name = data.get("subtask_name", "")
  312. logger.info(f"[CALLBACK] Print start detected - filename: {filename}, subtask: {subtask_name}")
  313. if not filename and not subtask_name:
  314. # Send notification without archive data (no filename)
  315. logger.info("[CALLBACK] Skipping archive - no filename or subtask_name")
  316. if not notification_sent:
  317. await _send_print_start_notification(printer_id, data, logger=logger)
  318. return
  319. # Check if this is an expected print from reprint/scheduled
  320. # Build list of possible keys to check
  321. expected_keys = []
  322. if subtask_name:
  323. expected_keys.append((printer_id, subtask_name))
  324. expected_keys.append((printer_id, f"{subtask_name}.3mf"))
  325. expected_keys.append((printer_id, f"{subtask_name}.gcode.3mf"))
  326. if filename:
  327. fname = filename.split("/")[-1] if "/" in filename else filename
  328. expected_keys.append((printer_id, fname))
  329. # Strip extensions to match
  330. base = fname.replace(".gcode", "").replace(".3mf", "")
  331. expected_keys.append((printer_id, base))
  332. expected_keys.append((printer_id, f"{base}.3mf"))
  333. expected_archive_id = None
  334. for key in expected_keys:
  335. expected_archive_id = _expected_prints.pop(key, None)
  336. if expected_archive_id:
  337. # Clean up other possible keys for this print
  338. for other_key in expected_keys:
  339. _expected_prints.pop(other_key, None)
  340. break
  341. if expected_archive_id:
  342. # This is a reprint/scheduled print - use existing archive, don't create new one
  343. logger.info(f"Using expected archive {expected_archive_id} for print (skipping duplicate)")
  344. from datetime import datetime
  345. from backend.app.models.archive import PrintArchive
  346. result = await db.execute(select(PrintArchive).where(PrintArchive.id == expected_archive_id))
  347. archive = result.scalar_one_or_none()
  348. if archive:
  349. # Update archive status to printing
  350. archive.status = "printing"
  351. archive.started_at = datetime.now()
  352. await db.commit()
  353. # Track as active print
  354. _active_prints[(printer_id, archive.filename)] = archive.id
  355. if subtask_name:
  356. _active_prints[(printer_id, f"{subtask_name}.3mf")] = archive.id
  357. # Set up energy tracking
  358. try:
  359. plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  360. plug = plug_result.scalar_one_or_none()
  361. logger.info(
  362. f"[ENERGY] Print start - archive {archive.id}, printer {printer_id}, plug found: {plug is not None}"
  363. )
  364. if plug:
  365. energy = await tasmota_service.get_energy(plug)
  366. logger.info(f"[ENERGY] Energy response from plug: {energy}")
  367. if energy and energy.get("total") is not None:
  368. _print_energy_start[archive.id] = energy["total"]
  369. logger.info(
  370. f"[ENERGY] Recorded starting energy for archive {archive.id}: {energy['total']} kWh"
  371. )
  372. else:
  373. logger.warning(f"[ENERGY] No 'total' in energy response for archive {archive.id}")
  374. else:
  375. logger.info(f"[ENERGY] No smart plug found for printer {printer_id}")
  376. except Exception as e:
  377. logger.warning(f"Failed to record starting energy: {e}")
  378. await ws_manager.send_archive_updated(
  379. {
  380. "id": archive.id,
  381. "status": "printing",
  382. }
  383. )
  384. # Send notification with archive data (reprint/scheduled)
  385. if not notification_sent:
  386. archive_data = {"print_time_seconds": archive.print_time_seconds}
  387. await _send_print_start_notification(printer_id, data, archive_data, logger)
  388. return # Skip creating a new archive
  389. # Check if there's already a "printing" archive for this printer/file
  390. # This prevents duplicates when backend restarts during an active print
  391. from backend.app.models.archive import PrintArchive
  392. check_name = subtask_name or filename.split("/")[-1].replace(".gcode", "").replace(".3mf", "")
  393. existing = await db.execute(
  394. select(PrintArchive)
  395. .where(PrintArchive.printer_id == printer_id)
  396. .where(PrintArchive.status == "printing")
  397. .where(PrintArchive.print_name.ilike(f"%{check_name}%"))
  398. .order_by(PrintArchive.created_at.desc())
  399. .limit(1)
  400. )
  401. existing_archive = existing.scalar_one_or_none()
  402. if existing_archive:
  403. logger.info(f"Skipping duplicate - already have printing archive {existing_archive.id} for {check_name}")
  404. # Track this as the active print
  405. _active_prints[(printer_id, existing_archive.filename)] = existing_archive.id
  406. # Also set up energy tracking if not already tracked
  407. if existing_archive.id not in _print_energy_start:
  408. try:
  409. plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  410. plug = plug_result.scalar_one_or_none()
  411. if plug:
  412. energy = await tasmota_service.get_energy(plug)
  413. if energy and energy.get("total") is not None:
  414. _print_energy_start[existing_archive.id] = energy["total"]
  415. logger.info(
  416. f"Recorded starting energy for existing archive {existing_archive.id}: {energy['total']} kWh"
  417. )
  418. except Exception as e:
  419. logger.warning(f"Failed to record starting energy for existing archive: {e}")
  420. # Send notification with archive data (existing archive)
  421. if not notification_sent:
  422. archive_data = {"print_time_seconds": existing_archive.print_time_seconds}
  423. await _send_print_start_notification(printer_id, data, archive_data, logger)
  424. return
  425. # Build list of possible 3MF filenames to try
  426. possible_names = []
  427. # Bambu printers typically store files as "Name.gcode.3mf"
  428. # The subtask_name is usually the best source for the filename
  429. if subtask_name:
  430. # Try common Bambu naming patterns
  431. possible_names.append(f"{subtask_name}.gcode.3mf")
  432. possible_names.append(f"{subtask_name}.3mf")
  433. # Try original filename with .3mf extension
  434. if filename:
  435. # Extract just the filename part, not the full path
  436. fname = filename.split("/")[-1] if "/" in filename else filename
  437. if fname.endswith(".3mf"):
  438. possible_names.append(fname)
  439. elif fname.endswith(".gcode"):
  440. base = fname.rsplit(".", 1)[0]
  441. possible_names.append(f"{base}.gcode.3mf")
  442. possible_names.append(f"{base}.3mf")
  443. else:
  444. possible_names.append(f"{fname}.gcode.3mf")
  445. possible_names.append(f"{fname}.3mf")
  446. # Remove duplicates while preserving order
  447. seen = set()
  448. possible_names = [x for x in possible_names if not (x in seen or seen.add(x))]
  449. logger.info(f"Trying filenames: {possible_names}")
  450. # Try to find and download the 3MF file
  451. temp_path = None
  452. downloaded_filename = None
  453. for try_filename in possible_names:
  454. if not try_filename.endswith(".3mf"):
  455. continue
  456. remote_paths = [
  457. f"/cache/{try_filename}",
  458. f"/model/{try_filename}",
  459. f"/{try_filename}",
  460. ]
  461. temp_path = app_settings.archive_dir / "temp" / try_filename
  462. temp_path.parent.mkdir(parents=True, exist_ok=True)
  463. for remote_path in remote_paths:
  464. logger.debug(f"Trying FTP download: {remote_path}")
  465. try:
  466. if await download_file_async(
  467. printer.ip_address,
  468. printer.access_code,
  469. remote_path,
  470. temp_path,
  471. ):
  472. downloaded_filename = try_filename
  473. logger.info(f"Downloaded: {remote_path}")
  474. break
  475. except Exception as e:
  476. logger.debug(f"FTP download failed for {remote_path}: {e}")
  477. if downloaded_filename:
  478. break
  479. # If still not found, try listing /cache to find matching file
  480. if not downloaded_filename and (filename or subtask_name):
  481. search_term = (subtask_name or filename).lower().replace(".gcode", "").replace(".3mf", "")
  482. try:
  483. cache_files = await list_files_async(printer.ip_address, printer.access_code, "/cache")
  484. for f in cache_files:
  485. if f.get("is_directory"):
  486. continue
  487. fname = f.get("name", "")
  488. if fname.endswith(".3mf") and search_term in fname.lower():
  489. temp_path = app_settings.archive_dir / "temp" / fname
  490. temp_path.parent.mkdir(parents=True, exist_ok=True)
  491. if await download_file_async(
  492. printer.ip_address,
  493. printer.access_code,
  494. f"/cache/{fname}",
  495. temp_path,
  496. ):
  497. downloaded_filename = fname
  498. logger.info(f"Found and downloaded from cache: {fname}")
  499. break
  500. except Exception as e:
  501. logger.warning(f"Failed to list cache: {e}")
  502. if not downloaded_filename or not temp_path:
  503. logger.warning(f"Could not find 3MF file for print: {filename or subtask_name}")
  504. # Send notification without archive data (file not found)
  505. if not notification_sent:
  506. await _send_print_start_notification(printer_id, data, logger=logger)
  507. return
  508. try:
  509. # Archive the file with status "printing"
  510. service = ArchiveService(db)
  511. archive = await service.archive_print(
  512. printer_id=printer_id,
  513. source_file=temp_path,
  514. print_data={**data, "status": "printing"},
  515. )
  516. if archive:
  517. # Track this active print (use both original filename and downloaded filename)
  518. _active_prints[(printer_id, downloaded_filename)] = archive.id
  519. if filename and filename != downloaded_filename:
  520. _active_prints[(printer_id, filename)] = archive.id
  521. if subtask_name:
  522. _active_prints[(printer_id, f"{subtask_name}.3mf")] = archive.id
  523. logger.info(f"Created archive {archive.id} for {downloaded_filename}")
  524. # Record starting energy from smart plug if available
  525. try:
  526. plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  527. plug = plug_result.scalar_one_or_none()
  528. logger.info(
  529. f"[ENERGY] Auto-archive print start - archive {archive.id}, printer {printer_id}, plug found: {plug is not None}"
  530. )
  531. if plug:
  532. energy = await tasmota_service.get_energy(plug)
  533. logger.info(f"[ENERGY] Auto-archive energy response: {energy}")
  534. if energy and energy.get("total") is not None:
  535. _print_energy_start[archive.id] = energy["total"]
  536. logger.info(
  537. f"[ENERGY] Recorded starting energy for archive {archive.id}: {energy['total']} kWh"
  538. )
  539. else:
  540. logger.warning(f"[ENERGY] No 'total' in energy response for archive {archive.id}")
  541. else:
  542. logger.info(f"[ENERGY] No smart plug found for printer {printer_id}")
  543. except Exception as e:
  544. logger.warning(f"Failed to record starting energy: {e}")
  545. await ws_manager.send_archive_created(
  546. {
  547. "id": archive.id,
  548. "printer_id": archive.printer_id,
  549. "filename": archive.filename,
  550. "print_name": archive.print_name,
  551. "status": archive.status,
  552. }
  553. )
  554. # Send notification with archive data (new archive created)
  555. if not notification_sent:
  556. archive_data = {"print_time_seconds": archive.print_time_seconds}
  557. await _send_print_start_notification(printer_id, data, archive_data, logger)
  558. notification_sent = True
  559. finally:
  560. if temp_path and temp_path.exists():
  561. temp_path.unlink()
  562. async def _scan_for_timelapse_with_retries(archive_id: int):
  563. """
  564. Scan for timelapse with retries.
  565. The printer encodes the timelapse quickly after print completion.
  566. We just need a short delay then grab the most recent file.
  567. Since we KNOW timelapse was active (from MQTT ipcam data), the most recent
  568. file in /timelapse is our target. Retries handle FTP connection issues.
  569. """
  570. import logging
  571. logger = logging.getLogger(__name__)
  572. # Short delays - printer usually finishes encoding within seconds
  573. retry_delays = [5, 10, 20]
  574. for attempt, delay in enumerate(retry_delays, 1):
  575. logger.info(
  576. f"[TIMELAPSE] Attempt {attempt}/{len(retry_delays)}: waiting {delay}s before scanning for archive {archive_id}"
  577. )
  578. await asyncio.sleep(delay)
  579. try:
  580. async with async_session() as db:
  581. from backend.app.models.printer import Printer
  582. from backend.app.services.bambu_ftp import download_file_bytes_async, list_files_async
  583. # Get archive (ArchiveService from module-level import)
  584. service = ArchiveService(db)
  585. archive = await service.get_archive(archive_id)
  586. if not archive:
  587. logger.warning(f"[TIMELAPSE] Archive {archive_id} not found, stopping retries")
  588. return
  589. if archive.timelapse_path:
  590. logger.info(f"[TIMELAPSE] Archive {archive_id} already has timelapse attached, stopping retries")
  591. return
  592. if not archive.printer_id:
  593. logger.warning(f"[TIMELAPSE] Archive {archive_id} has no printer, stopping retries")
  594. return
  595. # Get printer
  596. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  597. printer = result.scalar_one_or_none()
  598. if not printer:
  599. logger.warning(f"[TIMELAPSE] Printer not found for archive {archive_id}, stopping retries")
  600. return
  601. # Scan timelapse directory on printer
  602. # H2D may store in different locations than X1C
  603. files = []
  604. found_path = None
  605. for timelapse_path in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
  606. try:
  607. found_files = await list_files_async(printer.ip_address, printer.access_code, timelapse_path)
  608. if found_files:
  609. files = found_files
  610. found_path = timelapse_path
  611. logger.info(f"[TIMELAPSE] Attempt {attempt}: Found {len(files)} files in {timelapse_path}")
  612. break
  613. except Exception as e:
  614. logger.debug(f"[TIMELAPSE] Path {timelapse_path} failed: {e}")
  615. continue
  616. if not files:
  617. logger.info(f"[TIMELAPSE] Attempt {attempt}: No timelapse files found on printer, will retry")
  618. continue
  619. mp4_files = [f for f in files if not f.get("is_directory") and f.get("name", "").endswith(".mp4")]
  620. # Log ALL mp4 files found for debugging
  621. logger.info(f"[TIMELAPSE] Attempt {attempt}: Found {len(mp4_files)} MP4 files in {found_path}")
  622. for f in mp4_files[:5]: # Log first 5
  623. logger.info(f"[TIMELAPSE] - {f.get('name')}, mtime={f.get('mtime')}")
  624. if not mp4_files:
  625. logger.info(f"[TIMELAPSE] Attempt {attempt}: No MP4 files found, will retry")
  626. continue
  627. # Sort by mtime descending to get most recent file
  628. mp4_files_with_mtime = [f for f in mp4_files if f.get("mtime")]
  629. if not mp4_files_with_mtime:
  630. logger.info(f"[TIMELAPSE] Attempt {attempt}: No MP4 files with mtime found, will retry")
  631. continue
  632. mp4_files_with_mtime.sort(key=lambda x: x.get("mtime"), reverse=True)
  633. most_recent = mp4_files_with_mtime[0]
  634. file_name = most_recent.get("name")
  635. logger.info(f"[TIMELAPSE] Attempt {attempt}: Most recent file: {file_name}")
  636. # Since we KNOW timelapse was active (from MQTT), just grab the most recent file
  637. remote_path = most_recent.get("path") or f"/timelapse/{file_name}"
  638. logger.info(f"[TIMELAPSE] Downloading {file_name} for archive {archive_id}")
  639. timelapse_data = await download_file_bytes_async(printer.ip_address, printer.access_code, remote_path)
  640. if timelapse_data:
  641. success = await service.attach_timelapse(archive_id, timelapse_data, file_name)
  642. if success:
  643. logger.info(f"[TIMELAPSE] Successfully attached timelapse to archive {archive_id}")
  644. await ws_manager.send_archive_updated({"id": archive_id, "timelapse_attached": True})
  645. return # Success!
  646. else:
  647. logger.warning(f"[TIMELAPSE] Failed to attach timelapse to archive {archive_id}")
  648. else:
  649. logger.warning(f"[TIMELAPSE] Attempt {attempt}: Failed to download, will retry")
  650. except Exception as e:
  651. logger.warning(f"[TIMELAPSE] Attempt {attempt} failed with error: {e}")
  652. logger.warning(f"[TIMELAPSE] All {len(retry_delays)} attempts exhausted for archive {archive_id}, giving up")
  653. async def on_print_complete(printer_id: int, data: dict):
  654. """Handle print completion - update the archive status."""
  655. import logging
  656. import time
  657. logger = logging.getLogger(__name__)
  658. start_time = time.time()
  659. def log_timing(section: str):
  660. elapsed = time.time() - start_time
  661. logger.info(f"[TIMING] {section}: {elapsed:.3f}s elapsed")
  662. logger.info(f"[CALLBACK] on_print_complete started for printer {printer_id}")
  663. try:
  664. ws_data = {
  665. "status": data.get("status"),
  666. "filename": data.get("filename"),
  667. "subtask_name": data.get("subtask_name"),
  668. "timelapse_was_active": data.get("timelapse_was_active"),
  669. }
  670. await ws_manager.send_print_complete(printer_id, ws_data)
  671. log_timing("WebSocket send_print_complete")
  672. except Exception as e:
  673. logger.warning(f"[CALLBACK] WebSocket send_print_complete failed: {e}")
  674. filename = data.get("filename", "")
  675. subtask_name = data.get("subtask_name", "")
  676. if not filename and not subtask_name:
  677. logger.warning("Print complete without filename or subtask_name")
  678. return
  679. logger.info(f"Print complete - filename: {filename}, subtask: {subtask_name}, status: {data.get('status')}")
  680. # Build list of possible keys to try (matching how they were registered in on_print_start)
  681. possible_keys = []
  682. # Try subtask_name variations first (most reliable for matching)
  683. if subtask_name:
  684. possible_keys.append((printer_id, f"{subtask_name}.3mf"))
  685. possible_keys.append((printer_id, f"{subtask_name}.gcode.3mf"))
  686. possible_keys.append((printer_id, subtask_name))
  687. # Try filename variations
  688. if filename:
  689. # Extract just the filename if it's a path
  690. fname = filename.split("/")[-1] if "/" in filename else filename
  691. if fname.endswith(".3mf"):
  692. possible_keys.append((printer_id, fname))
  693. elif fname.endswith(".gcode"):
  694. base_name = fname.rsplit(".", 1)[0]
  695. possible_keys.append((printer_id, f"{base_name}.gcode.3mf"))
  696. possible_keys.append((printer_id, f"{base_name}.3mf"))
  697. possible_keys.append((printer_id, fname))
  698. else:
  699. possible_keys.append((printer_id, f"{fname}.gcode.3mf"))
  700. possible_keys.append((printer_id, f"{fname}.3mf"))
  701. possible_keys.append((printer_id, fname))
  702. # Also try full path versions
  703. if filename.endswith(".3mf"):
  704. possible_keys.append((printer_id, filename))
  705. elif filename.endswith(".gcode"):
  706. base_name = filename.rsplit(".", 1)[0]
  707. possible_keys.append((printer_id, f"{base_name}.3mf"))
  708. possible_keys.append((printer_id, filename))
  709. else:
  710. possible_keys.append((printer_id, f"{filename}.3mf"))
  711. possible_keys.append((printer_id, filename))
  712. # Find the archive for this print
  713. logger.info(f"Looking for archive in _active_prints, keys to try: {possible_keys[:5]}...")
  714. logger.info(f"Current _active_prints: {list(_active_prints.keys())}")
  715. archive_id = None
  716. for key in possible_keys:
  717. archive_id = _active_prints.pop(key, None)
  718. if archive_id:
  719. logger.info(f"Found archive {archive_id} with key {key}")
  720. # Also clean up any other keys pointing to this archive
  721. keys_to_remove = [k for k, v in _active_prints.items() if v == archive_id]
  722. for k in keys_to_remove:
  723. _active_prints.pop(k, None)
  724. break
  725. if not archive_id:
  726. # Try to find by filename or subtask_name if not tracked (for prints started before app)
  727. async with async_session() as db:
  728. from backend.app.models.archive import PrintArchive
  729. # Try matching by subtask_name (stored as print_name) first
  730. if subtask_name:
  731. result = await db.execute(
  732. select(PrintArchive)
  733. .where(PrintArchive.printer_id == printer_id)
  734. .where(PrintArchive.status == "printing")
  735. .where(
  736. or_(
  737. PrintArchive.print_name.ilike(f"%{subtask_name}%"),
  738. PrintArchive.filename.ilike(f"%{subtask_name}%"),
  739. )
  740. )
  741. .order_by(PrintArchive.created_at.desc())
  742. .limit(1)
  743. )
  744. archive = result.scalar_one_or_none()
  745. if archive:
  746. archive_id = archive.id
  747. logger.info(f"Found archive {archive_id} by subtask_name match: {subtask_name}")
  748. # Also try by filename
  749. if not archive_id and filename:
  750. result = await db.execute(
  751. select(PrintArchive)
  752. .where(PrintArchive.printer_id == printer_id)
  753. .where(PrintArchive.filename == filename)
  754. .where(PrintArchive.status == "printing")
  755. .order_by(PrintArchive.created_at.desc())
  756. .limit(1)
  757. )
  758. archive = result.scalar_one_or_none()
  759. if archive:
  760. archive_id = archive.id
  761. if not archive_id:
  762. logger.warning(f"Could not find archive for print complete: filename={filename}, subtask={subtask_name}")
  763. return
  764. log_timing("Archive lookup")
  765. # Update archive status
  766. logger.info(f"[ARCHIVE] Updating archive {archive_id} status...")
  767. try:
  768. async with async_session() as db:
  769. service = ArchiveService(db)
  770. status = data.get("status", "completed")
  771. # Auto-detect failure reason
  772. failure_reason = None
  773. if status == "aborted":
  774. failure_reason = "User cancelled"
  775. logger.info("[ARCHIVE] Print was aborted by user, setting failure_reason='User cancelled'")
  776. elif status == "failed":
  777. # Try to determine failure reason from HMS errors
  778. hms_errors = data.get("hms_errors", [])
  779. if hms_errors:
  780. logger.info(f"[ARCHIVE] HMS errors at failure: {hms_errors}")
  781. # Map known HMS error modules to failure reasons
  782. # Module 0x07 = Filament, 0x0C = MC (Motion Controller), etc.
  783. for err in hms_errors:
  784. module = err.get("module", 0)
  785. if module == 0x07: # Filament module
  786. failure_reason = "Filament runout"
  787. break
  788. elif module == 0x0C: # Motion controller
  789. failure_reason = "Layer shift"
  790. break
  791. elif module == 0x05: # Nozzle/extruder
  792. failure_reason = "Clogged nozzle"
  793. break
  794. if failure_reason:
  795. logger.info(f"[ARCHIVE] Detected failure_reason from HMS: {failure_reason}")
  796. else:
  797. logger.info("[ARCHIVE] No HMS errors available to determine failure reason")
  798. await service.update_archive_status(
  799. archive_id,
  800. status=status,
  801. completed_at=datetime.now() if status in ("completed", "failed", "aborted") else None,
  802. failure_reason=failure_reason,
  803. )
  804. logger.info(f"[ARCHIVE] Archive {archive_id} status updated to {status}, failure_reason={failure_reason}")
  805. await ws_manager.send_archive_updated(
  806. {
  807. "id": archive_id,
  808. "status": status,
  809. }
  810. )
  811. logger.info(f"[ARCHIVE] WebSocket notification sent for archive {archive_id}")
  812. except Exception as e:
  813. logger.error(f"[ARCHIVE] Failed to update archive {archive_id} status: {e}", exc_info=True)
  814. # Continue with other operations even if archive update fails
  815. log_timing("Archive status update")
  816. # Report filament usage to Spoolman if print completed successfully
  817. if data.get("status") == "completed":
  818. try:
  819. await _report_spoolman_usage(printer_id, archive_id, logger)
  820. log_timing("Spoolman usage report")
  821. except Exception as e:
  822. logger.warning(f"Spoolman usage reporting failed: {e}")
  823. # Run slow operations as background tasks to avoid blocking the event loop
  824. # These operations can take 5-10+ seconds and would freeze the UI if awaited
  825. starting_kwh = _print_energy_start.pop(archive_id, None)
  826. async def _background_energy_calculation():
  827. """Calculate and save energy usage in background."""
  828. try:
  829. logger.info(f"[ENERGY-BG] Starting energy calculation for archive {archive_id}")
  830. async with async_session() as db:
  831. plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  832. plug = plug_result.scalar_one_or_none()
  833. if plug:
  834. energy = await tasmota_service.get_energy(plug)
  835. logger.info(f"[ENERGY-BG] Energy response: {energy}")
  836. energy_used = None
  837. if starting_kwh is not None and energy and energy.get("total") is not None:
  838. ending_kwh = energy["total"]
  839. energy_used = round(ending_kwh - starting_kwh, 4)
  840. logger.info(f"[ENERGY-BG] Per-print energy: {energy_used} kWh")
  841. if energy_used is not None and energy_used >= 0:
  842. from backend.app.api.routes.settings import get_setting
  843. energy_cost_per_kwh = await get_setting(db, "energy_cost_per_kwh")
  844. cost_per_kwh = float(energy_cost_per_kwh) if energy_cost_per_kwh else 0.15
  845. energy_cost = round(energy_used * cost_per_kwh, 2)
  846. from backend.app.models.archive import PrintArchive
  847. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  848. archive = result.scalar_one_or_none()
  849. if archive:
  850. archive.energy_kwh = energy_used
  851. archive.energy_cost = energy_cost
  852. await db.commit()
  853. logger.info(f"[ENERGY-BG] Saved: {energy_used} kWh, cost={energy_cost}")
  854. else:
  855. logger.info(f"[ENERGY-BG] No smart plug for printer {printer_id}")
  856. except Exception as e:
  857. logger.warning(f"[ENERGY-BG] Failed: {e}")
  858. async def _background_finish_photo():
  859. """Capture finish photo in background."""
  860. try:
  861. logger.info(f"[PHOTO-BG] Starting finish photo capture for archive {archive_id}")
  862. from backend.app.api.routes.camera import _active_streams, get_buffered_frame
  863. async with async_session() as db:
  864. from backend.app.api.routes.settings import get_setting
  865. capture_enabled = await get_setting(db, "capture_finish_photo")
  866. if capture_enabled is None or capture_enabled.lower() == "true":
  867. from backend.app.models.printer import Printer
  868. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  869. printer = result.scalar_one_or_none()
  870. if printer and archive_id:
  871. from backend.app.models.archive import PrintArchive
  872. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  873. archive = result.scalar_one_or_none()
  874. if archive:
  875. import uuid
  876. from datetime import datetime
  877. from pathlib import Path
  878. archive_dir = app_settings.base_dir / Path(archive.file_path).parent
  879. photo_filename = None
  880. # Check if camera stream is active - use buffered frame to avoid freeze
  881. active_for_printer = [k for k in _active_streams if k.startswith(f"{printer_id}-")]
  882. buffered_frame = get_buffered_frame(printer_id)
  883. if active_for_printer and buffered_frame:
  884. # Use frame from active stream
  885. logger.info("[PHOTO-BG] Using buffered frame from active stream")
  886. photos_dir = archive_dir / "photos"
  887. photos_dir.mkdir(parents=True, exist_ok=True)
  888. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  889. photo_filename = f"finish_{timestamp}_{uuid.uuid4().hex[:8]}.jpg"
  890. photo_path = photos_dir / photo_filename
  891. await asyncio.to_thread(photo_path.write_bytes, buffered_frame)
  892. logger.info(f"[PHOTO-BG] Saved buffered frame: {photo_filename}")
  893. else:
  894. # No active stream - capture new frame
  895. from backend.app.services.camera import capture_finish_photo
  896. photo_filename = await capture_finish_photo(
  897. printer_id=printer_id,
  898. ip_address=printer.ip_address,
  899. access_code=printer.access_code,
  900. model=printer.model,
  901. archive_dir=archive_dir,
  902. )
  903. if photo_filename:
  904. photos = archive.photos or []
  905. photos.append(photo_filename)
  906. archive.photos = photos
  907. await db.commit()
  908. logger.info(f"[PHOTO-BG] Saved: {photo_filename}")
  909. except Exception as e:
  910. logger.warning(f"[PHOTO-BG] Failed: {e}")
  911. asyncio.create_task(_background_energy_calculation())
  912. asyncio.create_task(_background_finish_photo()) # Skips if camera stream active
  913. log_timing("Background tasks scheduled (energy, photo)")
  914. # Also run smart plug, notifications, and maintenance as background tasks
  915. print_status = data.get("status", "completed")
  916. async def _background_smart_plug():
  917. """Handle smart plug automation in background."""
  918. try:
  919. logger.info(f"[AUTO-OFF-BG] Starting smart plug automation for printer {printer_id}")
  920. async with async_session() as db:
  921. await smart_plug_manager.on_print_complete(printer_id, print_status, db)
  922. logger.info("[AUTO-OFF-BG] Completed")
  923. except Exception as e:
  924. logger.warning(f"[AUTO-OFF-BG] Failed: {e}")
  925. async def _background_notifications():
  926. """Send print complete notifications in background."""
  927. try:
  928. logger.info(f"[NOTIFY-BG] Starting notifications for printer {printer_id}")
  929. async with async_session() as db:
  930. from backend.app.models.archive import PrintArchive
  931. from backend.app.models.printer import Printer
  932. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  933. printer = result.scalar_one_or_none()
  934. printer_name = printer.name if printer else f"Printer {printer_id}"
  935. archive_data = None
  936. if archive_id:
  937. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  938. archive = archive_result.scalar_one_or_none()
  939. if archive:
  940. archive_data = {
  941. "print_time_seconds": archive.print_time_seconds,
  942. "actual_filament_grams": archive.filament_used_grams,
  943. "failure_reason": archive.failure_reason,
  944. }
  945. await notification_service.on_print_complete(
  946. printer_id, printer_name, print_status, data, db, archive_data=archive_data
  947. )
  948. logger.info("[NOTIFY-BG] Completed")
  949. except Exception as e:
  950. logger.warning(f"[NOTIFY-BG] Failed: {e}")
  951. async def _background_maintenance_check():
  952. """Check for maintenance due in background."""
  953. if print_status != "completed":
  954. return
  955. try:
  956. logger.info(f"[MAINT-BG] Starting maintenance check for printer {printer_id}")
  957. async with async_session() as db:
  958. from backend.app.models.printer import Printer
  959. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  960. printer = result.scalar_one_or_none()
  961. printer_name = printer.name if printer else f"Printer {printer_id}"
  962. await ensure_default_types(db)
  963. overview = await _get_printer_maintenance_internal(printer_id, db, commit=True)
  964. items_needing_attention = [
  965. {"name": item.maintenance_type_name, "is_due": item.is_due, "is_warning": item.is_warning}
  966. for item in overview.maintenance_items
  967. if item.enabled and (item.is_due or item.is_warning)
  968. ]
  969. if items_needing_attention:
  970. await notification_service.on_maintenance_due(printer_id, printer_name, items_needing_attention, db)
  971. logger.info(f"[MAINT-BG] Sent notification: {len(items_needing_attention)} items need attention")
  972. else:
  973. logger.info("[MAINT-BG] Completed (no items need attention)")
  974. except Exception as e:
  975. logger.warning(f"[MAINT-BG] Failed: {e}")
  976. asyncio.create_task(_background_smart_plug())
  977. asyncio.create_task(_background_notifications())
  978. asyncio.create_task(_background_maintenance_check())
  979. log_timing("All background tasks scheduled")
  980. # Auto-scan for timelapse if recording was active during the print
  981. if archive_id and data.get("timelapse_was_active") and data.get("status") == "completed":
  982. logger.info(f"[TIMELAPSE] Timelapse was active during print, scheduling auto-scan for archive {archive_id}")
  983. # Schedule timelapse scan as background task with retries
  984. # The printer needs time to encode the video after print completion
  985. asyncio.create_task(_scan_for_timelapse_with_retries(archive_id))
  986. log_timing("Timelapse scan scheduled")
  987. # Update queue item if this was a scheduled print
  988. try:
  989. async with async_session() as db:
  990. from backend.app.models.print_queue import PrintQueueItem
  991. # Note: SmartPlug is already imported at module level (line 56)
  992. # Do NOT import it here as it would shadow the module-level import
  993. # and cause "cannot access local variable" errors earlier in this function
  994. result = await db.execute(
  995. select(PrintQueueItem)
  996. .where(PrintQueueItem.printer_id == printer_id)
  997. .where(PrintQueueItem.status == "printing")
  998. )
  999. queue_item = result.scalar_one_or_none()
  1000. if queue_item:
  1001. status = data.get("status", "completed")
  1002. queue_item.status = status
  1003. queue_item.completed_at = datetime.now()
  1004. await db.commit()
  1005. logger.info(f"Updated queue item {queue_item.id} status to {status}")
  1006. # Handle auto_off_after - power off printer if requested (after cooldown)
  1007. if queue_item.auto_off_after:
  1008. result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  1009. plug = result.scalar_one_or_none()
  1010. if plug and plug.enabled:
  1011. logger.info(f"Auto-off requested for printer {printer_id}, waiting for cooldown...")
  1012. async def cooldown_and_poweroff(pid: int, plug_id: int):
  1013. # Wait for nozzle to cool down
  1014. await printer_manager.wait_for_cooldown(pid, target_temp=50.0, timeout=600)
  1015. # Re-fetch plug in new session
  1016. async with async_session() as new_db:
  1017. result = await new_db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  1018. p = result.scalar_one_or_none()
  1019. if p and p.enabled:
  1020. success = await tasmota_service.turn_off(p)
  1021. if success:
  1022. logger.info(f"Powered off printer {pid} via smart plug '{p.name}'")
  1023. else:
  1024. logger.warning(f"Failed to power off printer {pid} via smart plug")
  1025. asyncio.create_task(cooldown_and_poweroff(printer_id, plug.id))
  1026. except Exception as e:
  1027. import logging
  1028. logging.getLogger(__name__).warning(f"Queue item update failed: {e}")
  1029. log_timing("Queue item update")
  1030. logger.info(f"[CALLBACK] on_print_complete finished for printer {printer_id}, archive {archive_id}")
  1031. # AMS sensor history recording
  1032. _ams_history_task: asyncio.Task | None = None
  1033. AMS_HISTORY_INTERVAL = 300 # Record every 5 minutes
  1034. AMS_HISTORY_RETENTION_DAYS = 30 # Keep data for 30 days
  1035. _ams_cleanup_counter = 0 # Track recordings to trigger periodic cleanup
  1036. _ams_alarm_cooldown: dict[str, datetime] = {} # Track alarm cooldowns (printer_id:ams_id:type -> last_alarm_time)
  1037. AMS_ALARM_COOLDOWN_MINUTES = 60 # Don't send same alarm more than once per hour
  1038. async def record_ams_history():
  1039. """Background task to record AMS humidity and temperature data."""
  1040. import logging
  1041. logger = logging.getLogger(__name__)
  1042. # Wait a short time for MQTT connections to establish on startup
  1043. await asyncio.sleep(10)
  1044. while True:
  1045. try:
  1046. from backend.app.models.ams_history import AMSSensorHistory
  1047. from backend.app.models.printer import Printer
  1048. from backend.app.models.settings import Settings
  1049. async with async_session() as db:
  1050. # Get all active printers
  1051. result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
  1052. printers = result.scalars().all()
  1053. # Get alarm thresholds from settings
  1054. humidity_threshold = 60.0 # Default: fair threshold
  1055. temp_threshold = 35.0 # Default: fair threshold
  1056. result = await db.execute(select(Settings).where(Settings.key == "ams_humidity_fair"))
  1057. setting = result.scalar_one_or_none()
  1058. if setting:
  1059. try:
  1060. humidity_threshold = float(setting.value)
  1061. except (ValueError, TypeError):
  1062. pass
  1063. result = await db.execute(select(Settings).where(Settings.key == "ams_temp_fair"))
  1064. setting = result.scalar_one_or_none()
  1065. if setting:
  1066. try:
  1067. temp_threshold = float(setting.value)
  1068. except (ValueError, TypeError):
  1069. pass
  1070. recorded_count = 0
  1071. for printer in printers:
  1072. # Get current state from printer manager
  1073. state = printer_manager.get_status(printer.id)
  1074. if not state or not state.raw_data:
  1075. continue
  1076. raw_data = state.raw_data
  1077. if "ams" not in raw_data or not isinstance(raw_data["ams"], list):
  1078. continue
  1079. # Record data for each AMS unit
  1080. for ams_data in raw_data["ams"]:
  1081. ams_id = int(ams_data.get("id", 0))
  1082. # Get humidity (prefer humidity_raw)
  1083. humidity_raw = ams_data.get("humidity_raw")
  1084. humidity_idx = ams_data.get("humidity")
  1085. humidity = None
  1086. if humidity_raw is not None:
  1087. try:
  1088. humidity = float(humidity_raw)
  1089. except (ValueError, TypeError):
  1090. pass
  1091. if humidity is None and humidity_idx is not None:
  1092. try:
  1093. humidity = float(humidity_idx)
  1094. except (ValueError, TypeError):
  1095. pass
  1096. # Get temperature
  1097. temperature = None
  1098. temp_str = ams_data.get("temp")
  1099. if temp_str is not None:
  1100. try:
  1101. temperature = float(temp_str)
  1102. except (ValueError, TypeError):
  1103. pass
  1104. # Skip if no data
  1105. if humidity is None and temperature is None:
  1106. continue
  1107. # Record the data point
  1108. history = AMSSensorHistory(
  1109. printer_id=printer.id,
  1110. ams_id=ams_id,
  1111. humidity=humidity,
  1112. humidity_raw=float(humidity_raw) if humidity_raw else None,
  1113. temperature=temperature,
  1114. )
  1115. db.add(history)
  1116. recorded_count += 1
  1117. # Generate AMS label and determine if it's AMS-HT (A, B, C, D or HT-A for AMS-Lite/Hub)
  1118. is_ams_ht = ams_id >= 128
  1119. if is_ams_ht:
  1120. ams_label = f"HT-{chr(65 + (ams_id - 128))}"
  1121. else:
  1122. ams_label = f"AMS-{chr(65 + ams_id)}"
  1123. # Check humidity alarm (only if above threshold)
  1124. if humidity is not None and humidity > humidity_threshold:
  1125. cooldown_key = f"{printer.id}:{ams_id}:humidity"
  1126. last_alarm = _ams_alarm_cooldown.get(cooldown_key)
  1127. now = datetime.now()
  1128. if (
  1129. last_alarm is None
  1130. or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60
  1131. ):
  1132. _ams_alarm_cooldown[cooldown_key] = now
  1133. logger.info(
  1134. f"Sending humidity alarm for {printer.name} {ams_label}: {humidity}% > {humidity_threshold}%"
  1135. )
  1136. try:
  1137. # Call different notification method based on AMS type
  1138. if is_ams_ht:
  1139. await notification_service.on_ams_ht_humidity_high(
  1140. printer.id, printer.name, ams_label, humidity, humidity_threshold, db
  1141. )
  1142. else:
  1143. await notification_service.on_ams_humidity_high(
  1144. printer.id, printer.name, ams_label, humidity, humidity_threshold, db
  1145. )
  1146. except Exception as e:
  1147. logger.warning(f"Failed to send humidity alarm: {e}")
  1148. # Check temperature alarm (only if above threshold)
  1149. if temperature is not None and temperature > temp_threshold:
  1150. cooldown_key = f"{printer.id}:{ams_id}:temperature"
  1151. last_alarm = _ams_alarm_cooldown.get(cooldown_key)
  1152. now = datetime.now()
  1153. if (
  1154. last_alarm is None
  1155. or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60
  1156. ):
  1157. _ams_alarm_cooldown[cooldown_key] = now
  1158. logger.info(
  1159. f"Sending temperature alarm for {printer.name} {ams_label}: {temperature}°C > {temp_threshold}°C"
  1160. )
  1161. try:
  1162. # Call different notification method based on AMS type
  1163. if is_ams_ht:
  1164. await notification_service.on_ams_ht_temperature_high(
  1165. printer.id, printer.name, ams_label, temperature, temp_threshold, db
  1166. )
  1167. else:
  1168. await notification_service.on_ams_temperature_high(
  1169. printer.id, printer.name, ams_label, temperature, temp_threshold, db
  1170. )
  1171. except Exception as e:
  1172. logger.warning(f"Failed to send temperature alarm: {e}")
  1173. await db.commit()
  1174. if recorded_count > 0:
  1175. logger.info(f"Recorded {recorded_count} AMS sensor history entries")
  1176. # Periodic cleanup of old data (every ~288 recordings = ~24 hours at 5min interval)
  1177. global _ams_cleanup_counter
  1178. _ams_cleanup_counter += 1
  1179. if _ams_cleanup_counter >= 288:
  1180. _ams_cleanup_counter = 0
  1181. # Get retention days from settings
  1182. from backend.app.models.settings import Settings
  1183. result = await db.execute(select(Settings).where(Settings.key == "ams_history_retention_days"))
  1184. setting = result.scalar_one_or_none()
  1185. retention_days = int(setting.value) if setting else AMS_HISTORY_RETENTION_DAYS
  1186. cutoff = datetime.now() - timedelta(days=retention_days)
  1187. result = await db.execute(delete(AMSSensorHistory).where(AMSSensorHistory.recorded_at < cutoff))
  1188. await db.commit()
  1189. if result.rowcount > 0:
  1190. logger.info(
  1191. f"Cleaned up {result.rowcount} old AMS sensor history entries (older than {retention_days} days)"
  1192. )
  1193. # Wait until next recording interval
  1194. await asyncio.sleep(AMS_HISTORY_INTERVAL)
  1195. except asyncio.CancelledError:
  1196. break
  1197. except Exception as e:
  1198. logger.warning(f"AMS history recording failed: {e}")
  1199. await asyncio.sleep(60) # Wait a bit before retrying
  1200. def start_ams_history_recording():
  1201. """Start the AMS history recording background task."""
  1202. global _ams_history_task
  1203. if _ams_history_task is None:
  1204. _ams_history_task = asyncio.create_task(record_ams_history())
  1205. logging.getLogger(__name__).info("AMS history recording started")
  1206. def stop_ams_history_recording():
  1207. """Stop the AMS history recording background task."""
  1208. global _ams_history_task
  1209. if _ams_history_task:
  1210. _ams_history_task.cancel()
  1211. _ams_history_task = None
  1212. logging.getLogger(__name__).info("AMS history recording stopped")
  1213. @asynccontextmanager
  1214. async def lifespan(app: FastAPI):
  1215. # Startup
  1216. await init_db()
  1217. # Set up printer manager callbacks
  1218. loop = asyncio.get_event_loop()
  1219. printer_manager.set_event_loop(loop)
  1220. printer_manager.set_status_change_callback(on_printer_status_change)
  1221. printer_manager.set_print_start_callback(on_print_start)
  1222. printer_manager.set_print_complete_callback(on_print_complete)
  1223. printer_manager.set_ams_change_callback(on_ams_change)
  1224. # Connect to all active printers
  1225. async with async_session() as db:
  1226. await init_printer_connections(db)
  1227. # Auto-connect to Spoolman if enabled
  1228. async with async_session() as db:
  1229. from backend.app.api.routes.settings import get_setting
  1230. spoolman_enabled = await get_setting(db, "spoolman_enabled")
  1231. spoolman_url = await get_setting(db, "spoolman_url")
  1232. if spoolman_enabled and spoolman_enabled.lower() == "true" and spoolman_url:
  1233. try:
  1234. client = await init_spoolman_client(spoolman_url)
  1235. if await client.health_check():
  1236. logging.info(f"Auto-connected to Spoolman at {spoolman_url}")
  1237. else:
  1238. logging.warning(f"Spoolman at {spoolman_url} is not reachable")
  1239. except Exception as e:
  1240. logging.warning(f"Failed to auto-connect to Spoolman: {e}")
  1241. # Start the print scheduler
  1242. asyncio.create_task(print_scheduler.run())
  1243. # Start the smart plug scheduler for time-based on/off
  1244. smart_plug_manager.start_scheduler()
  1245. # Resume any pending auto-offs that were interrupted by restart
  1246. await smart_plug_manager.resume_pending_auto_offs()
  1247. # Start the notification digest scheduler
  1248. notification_service.start_digest_scheduler()
  1249. # Start AMS history recording
  1250. start_ams_history_recording()
  1251. # Start anonymous telemetry (opt-out via settings)
  1252. asyncio.create_task(start_telemetry_loop(async_session))
  1253. yield
  1254. # Shutdown
  1255. print_scheduler.stop()
  1256. smart_plug_manager.stop_scheduler()
  1257. notification_service.stop_digest_scheduler()
  1258. stop_ams_history_recording()
  1259. printer_manager.disconnect_all()
  1260. await close_spoolman_client()
  1261. app = FastAPI(
  1262. title=app_settings.app_name,
  1263. description="Archive and manage Bambu Lab 3MF files",
  1264. version=APP_VERSION,
  1265. lifespan=lifespan,
  1266. )
  1267. # API routes
  1268. app.include_router(printers.router, prefix=app_settings.api_prefix)
  1269. app.include_router(archives.router, prefix=app_settings.api_prefix)
  1270. app.include_router(filaments.router, prefix=app_settings.api_prefix)
  1271. app.include_router(settings_routes.router, prefix=app_settings.api_prefix)
  1272. app.include_router(cloud.router, prefix=app_settings.api_prefix)
  1273. app.include_router(smart_plugs.router, prefix=app_settings.api_prefix)
  1274. app.include_router(print_queue.router, prefix=app_settings.api_prefix)
  1275. app.include_router(kprofiles.router, prefix=app_settings.api_prefix)
  1276. app.include_router(notifications.router, prefix=app_settings.api_prefix)
  1277. app.include_router(notification_templates.router, prefix=app_settings.api_prefix)
  1278. app.include_router(spoolman.router, prefix=app_settings.api_prefix)
  1279. app.include_router(updates.router, prefix=app_settings.api_prefix)
  1280. app.include_router(maintenance.router, prefix=app_settings.api_prefix)
  1281. app.include_router(camera.router, prefix=app_settings.api_prefix)
  1282. app.include_router(external_links.router, prefix=app_settings.api_prefix)
  1283. app.include_router(projects.router, prefix=app_settings.api_prefix)
  1284. app.include_router(api_keys.router, prefix=app_settings.api_prefix)
  1285. app.include_router(webhook.router, prefix=app_settings.api_prefix)
  1286. app.include_router(ams_history.router, prefix=app_settings.api_prefix)
  1287. app.include_router(system.router, prefix=app_settings.api_prefix)
  1288. app.include_router(websocket.router, prefix=app_settings.api_prefix)
  1289. app.include_router(discovery.router, prefix=app_settings.api_prefix)
  1290. # Serve static files (React build)
  1291. if app_settings.static_dir.exists() and any(app_settings.static_dir.iterdir()):
  1292. app.mount(
  1293. "/assets",
  1294. StaticFiles(directory=app_settings.static_dir / "assets"),
  1295. name="assets",
  1296. )
  1297. if (app_settings.static_dir / "img").exists():
  1298. app.mount(
  1299. "/img",
  1300. StaticFiles(directory=app_settings.static_dir / "img"),
  1301. name="img",
  1302. )
  1303. if (app_settings.static_dir / "icons").exists():
  1304. app.mount(
  1305. "/icons",
  1306. StaticFiles(directory=app_settings.static_dir / "icons"),
  1307. name="icons",
  1308. )
  1309. @app.get("/")
  1310. async def serve_frontend():
  1311. """Serve the React frontend."""
  1312. index_file = app_settings.static_dir / "index.html"
  1313. if index_file.exists():
  1314. return FileResponse(index_file)
  1315. return {
  1316. "message": "Bambuddy API",
  1317. "docs": "/docs",
  1318. "frontend": "Build and place React app in /static directory",
  1319. }
  1320. @app.get("/health")
  1321. async def health_check():
  1322. """Health check endpoint."""
  1323. return {"status": "healthy"}
  1324. @app.get("/manifest.json")
  1325. async def serve_manifest():
  1326. """Serve PWA manifest."""
  1327. manifest_file = app_settings.static_dir / "manifest.json"
  1328. if manifest_file.exists():
  1329. return FileResponse(manifest_file, media_type="application/manifest+json")
  1330. return {"error": "Manifest not found"}
  1331. @app.get("/sw.js")
  1332. async def serve_service_worker():
  1333. """Serve service worker."""
  1334. sw_file = app_settings.static_dir / "sw.js"
  1335. if sw_file.exists():
  1336. return FileResponse(sw_file, media_type="application/javascript")
  1337. return {"error": "Service worker not found"}
  1338. # Catch-all route for React Router (must be last)
  1339. @app.get("/{full_path:path}")
  1340. async def serve_spa(full_path: str):
  1341. """Serve React app for client-side routing."""
  1342. # Don't intercept API routes
  1343. if full_path.startswith("api/"):
  1344. return {"error": "Not found"}
  1345. index_file = app_settings.static_dir / "index.html"
  1346. if index_file.exists():
  1347. return FileResponse(index_file)
  1348. return {"error": "Frontend not built"}