| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868 |
- import asyncio
- import logging
- from contextlib import asynccontextmanager
- from datetime import datetime, timedelta, timezone
- from logging.handlers import RotatingFileHandler
- # =============================================================================
- # Dependency Check - runs before other imports to give helpful error messages
- # =============================================================================
- def _start_error_server(missing_packages: list):
- """Start a minimal HTTP server to display dependency errors in browser."""
- import os
- import signal
- from http.server import BaseHTTPRequestHandler, HTTPServer
- packages_html = "".join(f"<li><code>{p}</code></li>" for p in missing_packages)
- html = f"""<!DOCTYPE html>
- <html>
- <head>
- <title>Bambuddy - Setup Required</title>
- <style>
- body {{
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
- background: #0f172a; color: #e2e8f0;
- display: flex; justify-content: center; align-items: center;
- min-height: 100vh; margin: 0; padding: 20px; box-sizing: border-box;
- }}
- .container {{
- background: #1e293b; border-radius: 12px; padding: 40px;
- max-width: 600px; text-align: center; box-shadow: 0 4px 20px rgba(0,0,0,0.3);
- }}
- h1 {{ color: #f87171; margin-bottom: 10px; }}
- h2 {{ color: #94a3b8; font-weight: normal; margin-top: 0; }}
- .packages {{
- background: #0f172a; border-radius: 8px; padding: 20px;
- margin: 20px 0; text-align: left;
- }}
- .packages ul {{ margin: 0; padding-left: 20px; }}
- .packages li {{ color: #fbbf24; margin: 8px 0; }}
- .command {{
- background: #0f172a; border-radius: 8px; padding: 15px 20px;
- margin: 15px 0; font-family: monospace; color: #4ade80;
- text-align: left; overflow-x: auto;
- }}
- .note {{ color: #94a3b8; font-size: 14px; margin-top: 20px; }}
- </style>
- </head>
- <body>
- <div class="container">
- <h1>Setup Required</h1>
- <h2>Missing Python packages</h2>
- <div class="packages"><ul>{packages_html}</ul></div>
- <p>To fix, run this command on your server:</p>
- <div class="command">pip install -r requirements.txt</div>
- <p>Or if using a virtual environment:</p>
- <div class="command">./venv/bin/pip install -r requirements.txt</div>
- <p class="note">After installing, restart Bambuddy:<br>
- <code>sudo systemctl restart bambuddy</code></p>
- </div>
- </body>
- </html>"""
- class ErrorHandler(BaseHTTPRequestHandler):
- def do_GET(self):
- self.send_response(503)
- self.send_header("Content-type", "text/html")
- self.end_headers()
- self.wfile.write(html.encode())
- def log_message(self, format, *args):
- print(f"[Error Server] {args[0]}")
- port = int(os.environ.get("PORT", 8000))
- print(f"\nStarting error server on http://0.0.0.0:{port}")
- print("Visit this URL in your browser to see the error details.\n")
- server = HTTPServer(("0.0.0.0", port), ErrorHandler) # nosec B104 - intentional bind-all for container
- def shutdown(signum, frame):
- print("\nShutting down error server...")
- raise SystemExit(0)
- signal.signal(signal.SIGTERM, shutdown)
- signal.signal(signal.SIGINT, shutdown)
- server.serve_forever()
- def check_dependencies():
- """Check that all required packages are installed."""
- missing = []
- # Map of import name -> package name (for pip install)
- required = {
- "jwt": "PyJWT",
- "fastapi": "fastapi",
- "uvicorn": "uvicorn",
- "sqlalchemy": "sqlalchemy",
- "aiosqlite": "aiosqlite",
- "pydantic": "pydantic",
- "paho.mqtt": "paho-mqtt",
- }
- for module, package in required.items():
- try:
- __import__(module)
- except ImportError:
- missing.append(package)
- if missing:
- print("\n" + "=" * 60)
- print("ERROR: Missing required Python packages!")
- print("=" * 60)
- print(f"\nMissing packages: {', '.join(missing)}")
- print("\nTo fix, run:")
- print(" pip install -r requirements.txt")
- print("\nOr if using a virtual environment:")
- print(" ./venv/bin/pip install -r requirements.txt")
- print("=" * 60 + "\n")
- _start_error_server(missing)
- check_dependencies()
- # =============================================================================
- from fastapi import FastAPI
- # Import settings first for logging configuration
- from backend.app.core.config import APP_VERSION, settings as app_settings
- # Configure logging based on settings
- # DEBUG=true -> DEBUG level, else use LOG_LEVEL setting
- log_level_str = "DEBUG" if app_settings.debug else app_settings.log_level.upper()
- log_level = getattr(logging, log_level_str, logging.INFO)
- log_format = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
- # Create root logger
- root_logger = logging.getLogger()
- root_logger.setLevel(log_level)
- # Console handler - always enabled
- console_handler = logging.StreamHandler()
- console_handler.setLevel(log_level)
- console_handler.setFormatter(logging.Formatter(log_format))
- root_logger.addHandler(console_handler)
- # File handler - only in production or if explicitly enabled
- if app_settings.log_to_file:
- log_file = app_settings.log_dir / "bambuddy.log"
- file_handler = RotatingFileHandler(
- log_file,
- maxBytes=5 * 1024 * 1024, # 5MB
- backupCount=3,
- encoding="utf-8",
- )
- file_handler.setLevel(log_level)
- file_handler.setFormatter(logging.Formatter(log_format))
- root_logger.addHandler(file_handler)
- logging.info("Logging to file: %s", log_file)
- # Reduce noise from third-party libraries in production
- if not app_settings.debug:
- logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
- logging.getLogger("httpcore").setLevel(logging.WARNING)
- logging.getLogger("httpx").setLevel(logging.WARNING)
- logging.info("Bambuddy starting - debug=%s, log_level=%s", app_settings.debug, log_level_str)
- from fastapi.responses import FileResponse
- from fastapi.staticfiles import StaticFiles
- from sqlalchemy import delete, or_, select
- from backend.app.api.routes import (
- ams_history,
- api_keys,
- archives,
- auth,
- camera,
- cloud,
- discovery,
- external_links,
- filaments,
- firmware,
- github_backup,
- groups,
- kprofiles,
- library,
- maintenance,
- metrics,
- notification_templates,
- notifications,
- pending_uploads,
- print_queue,
- printers,
- projects,
- settings as settings_routes,
- smart_plugs,
- spoolman,
- support,
- system,
- updates,
- users,
- webhook,
- websocket,
- )
- from backend.app.api.routes.maintenance import _get_printer_maintenance_internal, ensure_default_types
- from backend.app.api.routes.support import init_debug_logging
- from backend.app.core.database import async_session, init_db
- from backend.app.core.websocket import ws_manager
- from backend.app.models.smart_plug import SmartPlug
- from backend.app.services.archive import ArchiveService
- from backend.app.services.bambu_ftp import download_file_async, get_ftp_retry_settings, with_ftp_retry
- from backend.app.services.bambu_mqtt import PrinterState
- from backend.app.services.github_backup import github_backup_service
- from backend.app.services.homeassistant import homeassistant_service
- from backend.app.services.mqtt_relay import mqtt_relay
- from backend.app.services.notification_service import notification_service
- from backend.app.services.print_scheduler import scheduler as print_scheduler
- from backend.app.services.printer_manager import (
- init_printer_connections,
- printer_manager,
- printer_state_to_dict,
- )
- from backend.app.services.smart_plug_manager import smart_plug_manager
- from backend.app.services.spoolman import close_spoolman_client, get_spoolman_client, init_spoolman_client
- from backend.app.services.spoolman_tracking import (
- cleanup_tracking as _cleanup_spoolman_tracking,
- report_usage as _report_spoolman_usage,
- store_print_data as _store_spoolman_print_data,
- )
- from backend.app.services.tasmota import tasmota_service
- # Track active prints: {(printer_id, filename): archive_id}
- _active_prints: dict[tuple[int, str], int] = {}
- # Track expected prints from reprint/scheduled (skip auto-archiving for these)
- # {(printer_id, filename): archive_id}
- _expected_prints: dict[tuple[int, str], int] = {}
- # Track starting energy for prints: {archive_id: starting_kwh}
- _print_energy_start: dict[int, float] = {}
- # Track reprints to add costs on completion: {archive_id}
- _reprint_archives: set[int] = set()
- # Track progress milestones for notifications: {printer_id: last_milestone_notified}
- # Milestones are 25, 50, 75. Value of 0 means no milestone notified yet for current print.
- _last_progress_milestone: dict[int, int] = {}
- # Track HMS errors that have been notified: {printer_id: set of error codes}
- # This prevents sending duplicate notifications for the same error
- _notified_hms_errors: dict[int, set[str]] = {}
- async def _get_plug_energy(plug, db) -> dict | None:
- """Get energy from plug regardless of type (Tasmota, Home Assistant, or MQTT).
- For HA plugs, configures the service with current settings from DB.
- For MQTT plugs, returns data from the subscription service.
- """
- if plug.plug_type == "homeassistant":
- from backend.app.api.routes.settings import get_setting
- ha_url = await get_setting(db, "ha_url") or ""
- ha_token = await get_setting(db, "ha_token") or ""
- homeassistant_service.configure(ha_url, ha_token)
- return await homeassistant_service.get_energy(plug)
- elif plug.plug_type == "mqtt":
- # MQTT plugs report "today" energy, not lifetime total
- # For per-print tracking, we use "today" as the counter (resets at midnight)
- mqtt_data = mqtt_relay.smart_plug_service.get_plug_data(plug.id)
- if mqtt_data:
- return {
- "power": mqtt_data.power,
- "today": mqtt_data.energy,
- "total": mqtt_data.energy, # Use today as total for per-print calculations
- }
- return None
- else:
- return await tasmota_service.get_energy(plug)
- def register_expected_print(printer_id: int, filename: str, archive_id: int):
- """Register an expected print from reprint/scheduled so we don't create duplicate archives."""
- # Store with multiple filename variations to catch different naming patterns
- _expected_prints[(printer_id, filename)] = archive_id
- # Also store without .3mf extension if present
- if filename.endswith(".3mf"):
- base = filename[:-4]
- _expected_prints[(printer_id, base)] = archive_id
- _expected_prints[(printer_id, f"{base}.gcode")] = archive_id
- logging.getLogger(__name__).info(
- f"Registered expected print: printer={printer_id}, file={filename}, archive={archive_id}"
- )
- _last_status_broadcast: dict[int, str] = {}
- _nozzle_count_updated: set[int] = set() # Track printers where we've updated nozzle_count
- async def on_printer_status_change(printer_id: int, state: PrinterState):
- """Handle printer status changes - broadcast via WebSocket."""
- # Only broadcast if something meaningful changed (reduce WebSocket spam)
- # Include rounded temperatures to detect meaningful temp changes (within 1 degree)
- temps = state.temperatures or {}
- nozzle_temp = round(temps.get("nozzle", 0))
- bed_temp = round(temps.get("bed", 0))
- nozzle_2_temp = round(temps.get("nozzle_2", 0)) if "nozzle_2" in temps else ""
- chamber_temp = round(temps.get("chamber", 0)) if "chamber" in temps else ""
- # Auto-detect dual-nozzle printers from MQTT temperature data
- if "nozzle_2" in temps and printer_id not in _nozzle_count_updated:
- _nozzle_count_updated.add(printer_id)
- # Update nozzle_count in database
- async with async_session() as db:
- from backend.app.models.printer import Printer
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- if printer and printer.nozzle_count != 2:
- printer.nozzle_count = 2
- await db.commit()
- logging.getLogger(__name__).info(
- f"Auto-detected dual-nozzle printer {printer_id}, updated nozzle_count=2"
- )
- # Include target temps for heating phase detection
- bed_target = round(temps.get("bed_target", 0))
- nozzle_target = round(temps.get("nozzle_target", 0))
- status_key = (
- f"{state.connected}:{state.state}:{state.progress}:{state.layer_num}:"
- f"{nozzle_temp}:{bed_temp}:{nozzle_2_temp}:{chamber_temp}:"
- f"{state.stg_cur}:{bed_target}:{nozzle_target}:"
- f"{state.cooling_fan_speed}:{state.big_fan1_speed}:{state.big_fan2_speed}:"
- f"{state.chamber_light}:{state.active_extruder}"
- )
- # MQTT relay - publish status (before dedup check - always publish to MQTT)
- try:
- printer_info = printer_manager.get_printer(printer_id)
- if printer_info:
- await mqtt_relay.on_printer_status(printer_id, state, printer_info.name, printer_info.serial_number)
- except Exception:
- pass # Don't fail status callback if MQTT fails
- if _last_status_broadcast.get(printer_id) == status_key:
- return # No change, skip WebSocket broadcast
- _last_status_broadcast[printer_id] = status_key
- # Check for progress milestone notifications (25%, 50%, 75%)
- progress = state.progress or 0
- is_printing = state.state in ("RUNNING", "PRINTING")
- if is_printing and progress > 0:
- # Determine which milestone we've reached
- current_milestone = 0
- if progress >= 75:
- current_milestone = 75
- elif progress >= 50:
- current_milestone = 50
- elif progress >= 25:
- current_milestone = 25
- last_milestone = _last_progress_milestone.get(printer_id, 0)
- # If we've crossed a new milestone, send notification
- if current_milestone > last_milestone:
- _last_progress_milestone[printer_id] = current_milestone
- try:
- async with async_session() as db:
- from backend.app.models.printer import Printer
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- printer_name = printer.name if printer else f"Printer {printer_id}"
- filename = state.subtask_name or state.gcode_file or "Unknown"
- # remaining_time is in minutes, convert to seconds for notification
- remaining_time_seconds = state.remaining_time * 60 if state.remaining_time else None
- # Capture camera snapshot for notification image attachment
- image_data = await _capture_snapshot_for_notification(
- printer_id, printer, logging.getLogger(__name__)
- )
- await notification_service.on_print_progress(
- printer_id,
- printer_name,
- filename,
- current_milestone,
- db,
- remaining_time_seconds,
- image_data=image_data,
- )
- except Exception as e:
- logging.getLogger(__name__).warning(f"Progress milestone notification failed: {e}")
- elif progress < 5:
- # Reset milestone tracking when print restarts or new print begins
- _last_progress_milestone[printer_id] = 0
- # Check for new HMS errors and send notifications
- current_hms_errors = getattr(state, "hms_errors", []) or []
- if current_hms_errors:
- # Build set of current error codes (using attr for uniqueness)
- current_error_codes = {f"{e.attr:08x}" for e in current_hms_errors}
- previously_notified = _notified_hms_errors.get(printer_id, set())
- # Find new errors that haven't been notified yet
- new_error_codes = current_error_codes - previously_notified
- if new_error_codes:
- # Get the actual new errors for the notification
- # Filter to severity >= 2 (skip informational/status messages like H2D sends)
- new_errors = [e for e in current_hms_errors if f"{e.attr:08x}" in new_error_codes and e.severity >= 2]
- try:
- async with async_session() as db:
- from backend.app.models.printer import Printer
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- printer_name = printer.name if printer else f"Printer {printer_id}"
- # Format error details for notification
- # Module 0x07 = AMS/Filament, 0x05 = Nozzle, 0x0C = Motion Controller, etc.
- module_names = {
- 0x03: "Print/Task",
- 0x05: "Nozzle/Extruder",
- 0x07: "AMS/Filament",
- 0x0C: "Motion Controller",
- 0x12: "Chamber",
- }
- from backend.app.services.hms_errors import get_error_description
- # Capture camera snapshot once for all error notifications
- error_image_data = await _capture_snapshot_for_notification(
- printer_id, printer, logging.getLogger(__name__)
- )
- for error in new_errors:
- module_name = module_names.get(error.module, f"Module 0x{error.module:02X}")
- # Build short code like "0700_8010"
- # Mask to 16 bits to handle printers that send larger values
- error_code_int = int(error.code.replace("0x", ""), 16) if error.code else 0
- error_code_masked = error_code_int & 0xFFFF
- short_code = f"{(error.attr >> 16) & 0xFFFF:04X}_{error_code_masked:04X}"
- error_type = f"{module_name} Error"
- # Look up human-readable description
- description = get_error_description(short_code)
- error_detail = description if description else f"Error code: {short_code}"
- await notification_service.on_printer_error(
- printer_id, printer_name, error_type, db, error_detail, image_data=error_image_data
- )
- logging.getLogger(__name__).info(
- f"[HMS] Sent notification for {len(new_errors)} new error(s) on printer {printer_id}"
- )
- # Also publish to MQTT relay
- printer_info = printer_manager.get_printer(printer_id)
- if printer_info:
- errors_data = [
- {
- "code": e.code,
- "attr": e.attr,
- "module": e.module,
- "severity": e.severity,
- }
- for e in new_errors
- ]
- await mqtt_relay.on_printer_error(
- printer_id, printer_info.name, printer_info.serial_number, errors_data
- )
- except Exception as e:
- logging.getLogger(__name__).warning(f"HMS error notification failed: {e}")
- # Update tracking with all current errors
- _notified_hms_errors[printer_id] = current_error_codes
- else:
- # No HMS errors - clear tracking so future errors get notified
- if printer_id in _notified_hms_errors:
- _notified_hms_errors.pop(printer_id, None)
- await ws_manager.send_printer_status(
- printer_id,
- printer_state_to_dict(state, printer_id, printer_manager.get_model(printer_id)),
- )
- async def on_ams_change(printer_id: int, ams_data: list):
- """Handle AMS data changes - sync to Spoolman if enabled and auto mode."""
- logger = logging.getLogger(__name__)
- # MQTT relay - publish AMS change
- try:
- printer_info = printer_manager.get_printer(printer_id)
- if printer_info:
- await mqtt_relay.on_ams_change(printer_id, printer_info.name, printer_info.serial_number, ams_data)
- except Exception:
- pass # Don't fail AMS callback if MQTT fails
- # Broadcast AMS change via WebSocket (bypasses status_key deduplication)
- # This ensures frontend gets immediate updates when AMS slots are configured
- try:
- state = printer_manager.get_status(printer_id)
- if state:
- logger.info("[Printer %s] Broadcasting AMS change via WebSocket", printer_id)
- await ws_manager.send_printer_status(
- printer_id,
- printer_state_to_dict(state, printer_id, printer_manager.get_model(printer_id)),
- )
- except Exception as e:
- logger.warning("Failed to broadcast AMS change for printer %s: %s", printer_id, e)
- try:
- async with async_session() as db:
- from backend.app.api.routes.settings import get_setting
- from backend.app.models.printer import Printer
- # Check if Spoolman is enabled
- spoolman_enabled = await get_setting(db, "spoolman_enabled")
- if not spoolman_enabled or spoolman_enabled.lower() != "true":
- return
- # Check sync mode
- sync_mode = await get_setting(db, "spoolman_sync_mode")
- if sync_mode and sync_mode != "auto":
- return # Only sync on auto mode
- # Check if weight sync is disabled
- disable_weight_sync_str = await get_setting(db, "spoolman_disable_weight_sync")
- disable_weight_sync = disable_weight_sync_str and disable_weight_sync_str.lower() == "true"
- # Get Spoolman URL
- spoolman_url = await get_setting(db, "spoolman_url")
- if not spoolman_url:
- return
- # Get or create Spoolman client
- client = await get_spoolman_client()
- if not client:
- client = await init_spoolman_client(spoolman_url)
- # Check if Spoolman is reachable
- if not await client.health_check():
- logger.warning("Spoolman not reachable at %s", spoolman_url)
- return
- # Get printer name for location
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- printer_name = printer.name if printer else f"Printer {printer_id}"
- # Sync each AMS tray
- synced = 0
- for ams_unit in ams_data:
- ams_id = int(ams_unit.get("id", 0))
- trays = ams_unit.get("tray", [])
- for tray_data in trays:
- tray = client.parse_ams_tray(ams_id, tray_data)
- if not tray:
- continue # Empty tray
- try:
- result = await client.sync_ams_tray(tray, printer_name, disable_weight_sync=disable_weight_sync)
- if result:
- synced += 1
- except Exception as e:
- logger.error("Error syncing AMS %s tray %s: %s", ams_id, tray.tray_id, e)
- if synced > 0:
- logger.info("Auto-synced %s AMS trays to Spoolman for printer %s", synced, printer_id)
- except Exception as e:
- logging.getLogger(__name__).warning(f"Spoolman AMS sync failed: {e}")
- async def _capture_snapshot_for_notification(printer_id: int, printer, logger) -> bytes | None:
- """Capture a camera snapshot for notification image attachment.
- Returns JPEG bytes (max 2.5MB) or None if capture fails or is unavailable.
- Uses: external camera > buffered frame > fresh capture.
- """
- if not printer:
- return None
- try:
- from backend.app.api.routes.settings import get_setting
- async with async_session() as db:
- capture_enabled = await get_setting(db, "capture_finish_photo")
- if capture_enabled is not None and capture_enabled.lower() != "true":
- return None
- # Try external camera first
- if printer.external_camera_enabled and printer.external_camera_url:
- logger.info("[SNAPSHOT] Capturing from external camera for printer %s", printer_id)
- from backend.app.services.external_camera import capture_frame
- frame_data = await capture_frame(printer.external_camera_url, printer.external_camera_type or "mjpeg")
- if frame_data and len(frame_data) <= 2_500_000:
- logger.info("[SNAPSHOT] External camera frame: %s bytes", len(frame_data))
- return frame_data
- # Try buffered frame from active stream
- from backend.app.api.routes.camera import _active_chamber_streams, _active_streams, get_buffered_frame
- active_for_printer = [k for k in _active_streams if k.startswith(f"{printer_id}-")]
- active_chamber = [k for k in _active_chamber_streams if k.startswith(f"{printer_id}-")]
- buffered_frame = get_buffered_frame(printer_id)
- if (active_for_printer or active_chamber) and buffered_frame:
- logger.info("[SNAPSHOT] Using buffered frame for printer %s: %s bytes", printer_id, len(buffered_frame))
- if len(buffered_frame) <= 2_500_000:
- return buffered_frame
- # Fresh capture from printer camera
- logger.info("[SNAPSHOT] Capturing fresh frame for printer %s", printer_id)
- from backend.app.services.camera import capture_camera_frame_bytes
- frame_data = await capture_camera_frame_bytes(
- printer.ip_address, printer.access_code, printer.model, timeout=15
- )
- if frame_data and len(frame_data) <= 2_500_000:
- logger.info("[SNAPSHOT] Fresh camera frame: %s bytes", len(frame_data))
- return frame_data
- except Exception as e:
- logger.warning("[SNAPSHOT] Failed to capture snapshot for printer %s: %s", printer_id, e)
- return None
- async def _send_print_start_notification(
- printer_id: int,
- data: dict,
- archive_data: dict | None = None,
- logger=None,
- ):
- """Helper to send print start notification with optional archive data."""
- if logger is None:
- logger = logging.getLogger(__name__)
- try:
- async with async_session() as db:
- from backend.app.models.printer import Printer
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- printer_name = printer.name if printer else f"Printer {printer_id}"
- # Capture camera snapshot for notification image attachment
- image_data = await _capture_snapshot_for_notification(printer_id, printer, logger)
- if image_data:
- if archive_data is None:
- archive_data = {}
- archive_data["image_data"] = image_data
- await notification_service.on_print_start(printer_id, printer_name, data, db, archive_data=archive_data)
- except Exception as e:
- logger.warning("Notification on_print_start failed: %s", e)
- def _load_objects_from_archive(archive, printer_id: int, logger) -> None:
- """Extract printable objects from an archive's 3MF file and store in printer state."""
- try:
- from backend.app.services.archive import extract_printable_objects_from_3mf
- file_path = app_settings.base_dir / archive.file_path
- if file_path.exists() and str(file_path).endswith(".3mf"):
- with open(file_path, "rb") as f:
- threemf_data = f.read()
- # Extract with positions for UI overlay
- printable_objects, bbox_all = extract_printable_objects_from_3mf(threemf_data, include_positions=True)
- if printable_objects:
- client = printer_manager.get_client(printer_id)
- if client:
- client.state.printable_objects = printable_objects
- client.state.printable_objects_bbox_all = bbox_all
- client.state.skipped_objects = []
- logger.info("Loaded %s printable objects for printer %s", len(printable_objects), printer_id)
- except Exception as e:
- logger.debug("Failed to extract printable objects from archive: %s", e)
- async def on_print_start(printer_id: int, data: dict):
- """Handle print start - archive the 3MF file immediately."""
- logger = logging.getLogger(__name__)
- logger.info("[CALLBACK] on_print_start called for printer %s, data keys: %s", printer_id, list(data.keys()))
- await ws_manager.send_print_start(printer_id, data)
- # MQTT relay - publish print start
- try:
- printer_info = printer_manager.get_printer(printer_id)
- if printer_info:
- await mqtt_relay.on_print_start(
- printer_id,
- printer_info.name,
- printer_info.serial_number,
- data.get("filename", ""),
- data.get("subtask_name", ""),
- )
- except Exception:
- pass # Don't fail print start callback if MQTT fails
- # Track if notification was sent (to avoid sending twice)
- notification_sent = False
- # Smart plug automation: turn on plug when print starts
- try:
- async with async_session() as db:
- await smart_plug_manager.on_print_start(printer_id, db)
- except Exception as e:
- logger.warning("Smart plug on_print_start failed: %s", e)
- async with async_session() as db:
- from backend.app.models.printer import Printer
- from backend.app.services.bambu_ftp import list_files_async
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- # Plate detection check - pause if objects detected on build plate
- logger.info(
- f"[PLATE CHECK] printer_id={printer_id}, plate_detection_enabled={printer.plate_detection_enabled if printer else 'NO PRINTER'}"
- )
- if printer and printer.plate_detection_enabled:
- logger.info("[PLATE CHECK] ENTERING plate detection code for printer %s", printer_id)
- try:
- from backend.app.services.plate_detection import check_plate_empty
- # Build ROI tuple from printer settings if available
- roi = None
- if all(
- [
- printer.plate_detection_roi_x is not None,
- printer.plate_detection_roi_y is not None,
- printer.plate_detection_roi_w is not None,
- printer.plate_detection_roi_h is not None,
- ]
- ):
- roi = (
- printer.plate_detection_roi_x,
- printer.plate_detection_roi_y,
- printer.plate_detection_roi_w,
- printer.plate_detection_roi_h,
- )
- # Auto-turn on chamber light if it's off for better detection
- light_was_off = False
- client = printer_manager.get_client(printer_id)
- if client and client.state:
- light_was_off = not client.state.chamber_light
- if light_was_off:
- logger.info("[PLATE CHECK] Turning on chamber light for printer %s", printer_id)
- client.set_chamber_light(True)
- # Wait for light to physically turn on and camera to adjust exposure
- await asyncio.sleep(2.5)
- logger.info("[PLATE CHECK] Running plate detection for printer %s", printer_id)
- plate_result = await check_plate_empty(
- printer_id=printer_id,
- ip_address=printer.ip_address,
- access_code=printer.access_code,
- model=printer.model,
- include_debug_image=False,
- external_camera_url=printer.external_camera_url,
- external_camera_type=printer.external_camera_type,
- use_external=printer.external_camera_enabled,
- roi=roi,
- )
- # Restore chamber light to original state
- if light_was_off and client:
- logger.info("[PLATE CHECK] Restoring chamber light to off for printer %s", printer_id)
- client.set_chamber_light(False)
- if not plate_result.needs_calibration and not plate_result.is_empty:
- # Objects detected - pause the print!
- logger.warning(
- f"[PLATE CHECK] Objects detected on plate for printer {printer_id}! "
- f"Confidence: {plate_result.confidence:.0%}, Diff: {plate_result.difference_percent:.1f}%"
- )
- client = printer_manager.get_client(printer_id)
- if client:
- client.pause_print()
- logger.info("[PLATE CHECK] Print paused for printer %s", printer_id)
- # Send notification about plate not empty
- await ws_manager.broadcast(
- {
- "type": "plate_not_empty",
- "printer_id": printer_id,
- "printer_name": printer.name,
- "message": f"Objects detected on build plate! Print paused. (Diff: {plate_result.difference_percent:.1f}%)",
- }
- )
- # Also send push notification
- try:
- await notification_service.on_plate_not_empty(
- printer_id=printer_id,
- printer_name=printer.name,
- db=db,
- difference_percent=plate_result.difference_percent,
- )
- except Exception as notif_err:
- logger.warning("[PLATE CHECK] Failed to send notification: %s", notif_err)
- else:
- logger.info("[PLATE CHECK] Plate is empty for printer %s, proceeding with print", printer_id)
- except Exception as plate_err:
- # Don't block print on plate detection errors
- logger.warning("[PLATE CHECK] Plate detection failed for printer %s: %s", printer_id, plate_err)
- if not printer or not printer.auto_archive:
- # Send notification without archive data (auto-archive disabled)
- logger.info(
- f"[CALLBACK] Skipping archive - printer: {printer is not None}, auto_archive: {printer.auto_archive if printer else 'N/A'}"
- )
- if not notification_sent:
- await _send_print_start_notification(printer_id, data, logger=logger)
- return
- # Get the filename and subtask_name
- filename = data.get("filename", "")
- subtask_name = data.get("subtask_name", "")
- logger.info("[CALLBACK] Print start detected - filename: %s, subtask: %s", filename, subtask_name)
- if not filename and not subtask_name:
- # Send notification without archive data (no filename)
- logger.info("[CALLBACK] Skipping archive - no filename or subtask_name")
- if not notification_sent:
- await _send_print_start_notification(printer_id, data, logger=logger)
- return
- # Check if this is an expected print from reprint/scheduled
- # Build list of possible keys to check
- expected_keys = []
- if subtask_name:
- expected_keys.append((printer_id, subtask_name))
- expected_keys.append((printer_id, f"{subtask_name}.3mf"))
- expected_keys.append((printer_id, f"{subtask_name}.gcode.3mf"))
- if filename:
- fname = filename.split("/")[-1] if "/" in filename else filename
- expected_keys.append((printer_id, fname))
- # Strip extensions to match
- base = fname.replace(".gcode", "").replace(".3mf", "")
- expected_keys.append((printer_id, base))
- expected_keys.append((printer_id, f"{base}.3mf"))
- expected_archive_id = None
- for key in expected_keys:
- expected_archive_id = _expected_prints.pop(key, None)
- if expected_archive_id:
- # Clean up other possible keys for this print
- for other_key in expected_keys:
- _expected_prints.pop(other_key, None)
- break
- if expected_archive_id:
- # This is a reprint/scheduled print - use existing archive, don't create new one
- logger.info("Using expected archive %s for print (skipping duplicate)", expected_archive_id)
- from backend.app.models.archive import PrintArchive
- result = await db.execute(select(PrintArchive).where(PrintArchive.id == expected_archive_id))
- archive = result.scalar_one_or_none()
- if archive:
- # Update archive status to printing
- archive.status = "printing"
- archive.started_at = datetime.now()
- await db.commit()
- # Track as active print
- _active_prints[(printer_id, archive.filename)] = archive.id
- if subtask_name:
- _active_prints[(printer_id, f"{subtask_name}.3mf")] = archive.id
- # Mark as reprint so we add cost on completion
- _reprint_archives.add(archive.id)
- logger.info("Marked archive %s as reprint for cost addition on completion", archive.id)
- # Set up energy tracking
- try:
- plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
- plug = plug_result.scalar_one_or_none()
- logger.info(
- f"[ENERGY] Print start - archive {archive.id}, printer {printer_id}, plug found: {plug is not None}"
- )
- if plug:
- energy = await _get_plug_energy(plug, db)
- logger.info("[ENERGY] Energy response from plug: %s", energy)
- if energy and energy.get("total") is not None:
- _print_energy_start[archive.id] = energy["total"]
- logger.info(
- f"[ENERGY] Recorded starting energy for archive {archive.id}: {energy['total']} kWh"
- )
- else:
- logger.warning("[ENERGY] No 'total' in energy response for archive %s", archive.id)
- else:
- logger.info("[ENERGY] No smart plug found for printer %s", printer_id)
- except Exception as e:
- logger.warning("Failed to record starting energy: %s", e)
- await ws_manager.send_archive_updated(
- {
- "id": archive.id,
- "status": "printing",
- }
- )
- # Send notification with archive data (reprint/scheduled)
- if not notification_sent:
- archive_data = {"print_time_seconds": archive.print_time_seconds}
- await _send_print_start_notification(printer_id, data, archive_data, logger)
- # Extract printable objects from the archived 3MF file
- _load_objects_from_archive(archive, printer_id, logger)
- # Store Spoolman tracking data for per-filament usage reporting
- try:
- await _store_spoolman_print_data(printer_id, archive.id, archive.file_path, db, printer_manager)
- except Exception as e:
- logger.warning("[SPOOLMAN] Failed to store tracking data: %s", e)
- return # Skip creating a new archive
- # Check if there's already a "printing" archive for this printer/file
- # This prevents duplicates when backend restarts during an active print
- from backend.app.models.archive import PrintArchive
- check_name = subtask_name or filename.split("/")[-1].replace(".gcode", "").replace(".3mf", "")
- existing = await db.execute(
- select(PrintArchive)
- .where(PrintArchive.printer_id == printer_id)
- .where(PrintArchive.status == "printing")
- .where(PrintArchive.print_name.ilike(f"%{check_name}%"))
- .order_by(PrintArchive.created_at.desc())
- .limit(1)
- )
- existing_archive = existing.scalar_one_or_none()
- if existing_archive:
- # Check if archive is stale (older than 4 hours) - likely a failed/cancelled print
- # that didn't get properly updated
- archive_age = datetime.now(timezone.utc) - existing_archive.created_at.replace(tzinfo=timezone.utc)
- if archive_age.total_seconds() > 4 * 60 * 60: # 4 hours
- logger.warning(
- f"Found stale 'printing' archive {existing_archive.id} (age: {archive_age}), "
- f"marking as cancelled and creating new archive"
- )
- existing_archive.status = "cancelled"
- existing_archive.failure_reason = "Stale - print likely cancelled or failed without status update"
- await db.commit()
- # Fall through to create new archive (don't return)
- existing_archive = None # Clear so we don't use stale archive
- else:
- logger.info(
- f"Skipping duplicate - already have printing archive {existing_archive.id} for {check_name}"
- )
- # Track this as the active print
- _active_prints[(printer_id, existing_archive.filename)] = existing_archive.id
- # Also set up energy tracking if not already tracked
- if existing_archive.id not in _print_energy_start:
- try:
- plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
- plug = plug_result.scalar_one_or_none()
- if plug:
- energy = await _get_plug_energy(plug, db)
- if energy and energy.get("total") is not None:
- _print_energy_start[existing_archive.id] = energy["total"]
- logger.info(
- f"Recorded starting energy for existing archive {existing_archive.id}: {energy['total']} kWh"
- )
- except Exception as e:
- logger.warning("Failed to record starting energy for existing archive: %s", e)
- # Send notification with archive data (existing archive)
- if not notification_sent:
- archive_data = {"print_time_seconds": existing_archive.print_time_seconds}
- await _send_print_start_notification(printer_id, data, archive_data, logger)
- # Extract printable objects from the archived 3MF file
- _load_objects_from_archive(existing_archive, printer_id, logger)
- return
- # Build list of possible 3MF filenames to try
- possible_names = []
- # Bambu printers typically store files as "Name.gcode.3mf"
- # The subtask_name is usually the best source for the filename
- if subtask_name:
- # Try common Bambu naming patterns
- possible_names.append(f"{subtask_name}.gcode.3mf")
- possible_names.append(f"{subtask_name}.3mf")
- # Try original filename with .3mf extension
- if filename:
- # Extract just the filename part, not the full path
- fname = filename.split("/")[-1] if "/" in filename else filename
- if fname.endswith(".3mf"):
- possible_names.append(fname)
- elif fname.endswith(".gcode"):
- base = fname.rsplit(".", 1)[0]
- possible_names.append(f"{base}.gcode.3mf")
- possible_names.append(f"{base}.3mf")
- else:
- possible_names.append(f"{fname}.gcode.3mf")
- possible_names.append(f"{fname}.3mf")
- # Also try with spaces converted to underscores (Bambu Studio may normalize filenames)
- space_variants = []
- for name in possible_names:
- if " " in name:
- space_variants.append(name.replace(" ", "_"))
- possible_names.extend(space_variants)
- # Remove duplicates while preserving order
- seen = set()
- possible_names = [x for x in possible_names if not (x in seen or seen.add(x))]
- logger.info("Trying filenames: %s", possible_names)
- # Try to find and download the 3MF file
- temp_path = None
- downloaded_filename = None
- # Get FTP retry settings
- ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
- for try_filename in possible_names:
- if not try_filename.endswith(".3mf"):
- continue
- remote_paths = [
- f"/cache/{try_filename}",
- f"/model/{try_filename}",
- f"/data/{try_filename}",
- f"/data/Metadata/{try_filename}",
- f"/{try_filename}",
- ]
- temp_path = app_settings.archive_dir / "temp" / try_filename
- temp_path.parent.mkdir(parents=True, exist_ok=True)
- for remote_path in remote_paths:
- logger.debug("Trying FTP download: %s", remote_path)
- try:
- if ftp_retry_enabled:
- downloaded = await with_ftp_retry(
- download_file_async,
- printer.ip_address,
- printer.access_code,
- remote_path,
- temp_path,
- socket_timeout=ftp_timeout,
- printer_model=printer.model,
- max_retries=ftp_retry_count,
- retry_delay=ftp_retry_delay,
- operation_name=f"Download 3MF from {remote_path}",
- )
- else:
- downloaded = await download_file_async(
- printer.ip_address,
- printer.access_code,
- remote_path,
- temp_path,
- socket_timeout=ftp_timeout,
- printer_model=printer.model,
- )
- if downloaded:
- downloaded_filename = try_filename
- logger.info("Downloaded: %s", remote_path)
- break
- except Exception as e:
- logger.debug("FTP download failed for %s: %s", remote_path, e)
- if downloaded_filename:
- break
- # If still not found, try listing directories to find matching file
- # Different printer models use different directory structures
- if not downloaded_filename and (filename or subtask_name):
- search_term = (subtask_name or filename).lower().replace(".gcode", "").replace(".3mf", "")
- logger.info("Direct FTP download failed, searching directories for '%s'", search_term)
- search_dirs = ["/cache", "/model", "/data", "/data/Metadata", "/"]
- for search_dir in search_dirs:
- if downloaded_filename:
- break
- try:
- dir_files = await list_files_async(printer.ip_address, printer.access_code, search_dir)
- threemf_files = [f.get("name") for f in dir_files if f.get("name", "").endswith(".3mf")]
- if threemf_files:
- logger.info(
- f"Found {len(threemf_files)} 3MF files in {search_dir}: {threemf_files[:5]}{'...' if len(threemf_files) > 5 else ''}"
- )
- for f in dir_files:
- if f.get("is_directory"):
- continue
- fname = f.get("name", "")
- # Normalize both for comparison (spaces and underscores are equivalent)
- fname_normalized = fname.lower().replace(" ", "_")
- search_normalized = search_term.replace(" ", "_")
- if fname.endswith(".3mf") and search_normalized in fname_normalized:
- logger.info("Found matching file in %s: %s", search_dir, fname)
- temp_path = app_settings.archive_dir / "temp" / fname
- temp_path.parent.mkdir(parents=True, exist_ok=True)
- if ftp_retry_enabled:
- downloaded = await with_ftp_retry(
- download_file_async,
- printer.ip_address,
- printer.access_code,
- f"{search_dir}/{fname}",
- temp_path,
- max_retries=ftp_retry_count,
- retry_delay=ftp_retry_delay,
- operation_name=f"Download 3MF from {search_dir}/{fname}",
- )
- else:
- downloaded = await download_file_async(
- printer.ip_address,
- printer.access_code,
- f"{search_dir}/{fname}",
- temp_path,
- )
- if downloaded:
- downloaded_filename = fname
- logger.info("Found and downloaded from %s: %s", search_dir, fname)
- break
- except Exception as e:
- logger.debug("Failed to list %s: %s", search_dir, e)
- if not downloaded_filename or not temp_path:
- logger.warning("Could not find 3MF file for print: %s", filename or subtask_name)
- # Create a fallback archive without 3MF data so the print is still tracked
- # This commonly happens with P1S/A1 printers where FTP has file size limitations
- try:
- from backend.app.models.archive import PrintArchive
- # Derive print name from subtask_name or filename
- print_name = subtask_name or filename
- if print_name:
- # Clean up the name (remove extensions, path parts)
- print_name = print_name.split("/")[-1]
- print_name = print_name.replace(".gcode.3mf", "").replace(".gcode", "").replace(".3mf", "")
- else:
- print_name = "Unknown Print"
- # Create minimal archive entry
- fallback_archive = PrintArchive(
- printer_id=printer_id,
- filename=filename or f"{print_name}.3mf",
- file_path="", # Empty - no 3MF file available
- file_size=0,
- print_name=print_name,
- status="printing",
- started_at=datetime.now(),
- extra_data={"no_3mf_available": True, "original_subtask": subtask_name, "_print_data": data},
- )
- db.add(fallback_archive)
- await db.commit()
- await db.refresh(fallback_archive)
- logger.info("Created fallback archive %s for %s (no 3MF available)", fallback_archive.id, print_name)
- # Start timelapse session if external camera is enabled
- if printer.external_camera_enabled and printer.external_camera_url:
- from backend.app.services.layer_timelapse import start_session
- start_session(
- printer_id,
- fallback_archive.id,
- printer.external_camera_url,
- printer.external_camera_type or "mjpeg",
- )
- logger.info("Started layer timelapse for printer %s, archive %s", printer_id, fallback_archive.id)
- # Track as active print
- _active_prints[(printer_id, fallback_archive.filename)] = fallback_archive.id
- if filename:
- _active_prints[(printer_id, filename)] = fallback_archive.id
- if subtask_name:
- _active_prints[(printer_id, f"{subtask_name}.3mf")] = fallback_archive.id
- _active_prints[(printer_id, subtask_name)] = fallback_archive.id
- # Record starting energy if smart plug available
- try:
- plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
- plug = plug_result.scalar_one_or_none()
- if plug:
- energy = await _get_plug_energy(plug, db)
- if energy and energy.get("total") is not None:
- _print_energy_start[fallback_archive.id] = energy["total"]
- logger.info(
- f"[ENERGY] Recorded starting energy for fallback archive {fallback_archive.id}: {energy['total']} kWh"
- )
- except Exception as e:
- logger.warning("Failed to record starting energy for fallback: %s", e)
- # Send WebSocket notification
- await ws_manager.send_archive_created(
- {
- "id": fallback_archive.id,
- "printer_id": fallback_archive.printer_id,
- "filename": fallback_archive.filename,
- "print_name": fallback_archive.print_name,
- "status": fallback_archive.status,
- }
- )
- # MQTT relay - publish archive created
- try:
- await mqtt_relay.on_archive_created(
- archive_id=fallback_archive.id,
- print_name=fallback_archive.print_name,
- printer_name=printer.name,
- status=fallback_archive.status,
- )
- except Exception:
- pass # Don't fail if MQTT fails
- # Store Spoolman tracking data (may not work for fallback since no 3MF)
- try:
- await _store_spoolman_print_data(
- printer_id, fallback_archive.id, fallback_archive.file_path, db, printer_manager
- )
- except Exception as e:
- logger.debug("[SPOOLMAN] Could not store tracking for fallback archive: %s", e)
- # Send notification without archive data (file not found)
- if not notification_sent:
- await _send_print_start_notification(printer_id, data, logger=logger)
- return
- except Exception as e:
- logger.error("Failed to create fallback archive: %s", e)
- # Send notification without archive data (file not found)
- if not notification_sent:
- await _send_print_start_notification(printer_id, data, logger=logger)
- return
- try:
- # Archive the file with status "printing"
- service = ArchiveService(db)
- archive = await service.archive_print(
- printer_id=printer_id,
- source_file=temp_path,
- print_data={**data, "status": "printing"},
- )
- if archive:
- # Track this active print (use both original filename and downloaded filename)
- _active_prints[(printer_id, downloaded_filename)] = archive.id
- if filename and filename != downloaded_filename:
- _active_prints[(printer_id, filename)] = archive.id
- if subtask_name:
- _active_prints[(printer_id, f"{subtask_name}.3mf")] = archive.id
- logger.info("Created archive %s for %s", archive.id, downloaded_filename)
- # Start timelapse session if external camera is enabled
- if printer.external_camera_enabled and printer.external_camera_url:
- from backend.app.services.layer_timelapse import start_session
- start_session(
- printer_id,
- archive.id,
- printer.external_camera_url,
- printer.external_camera_type or "mjpeg",
- )
- logger.info("Started layer timelapse for printer %s, archive %s", printer_id, archive.id)
- # Record starting energy from smart plug if available
- try:
- plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
- plug = plug_result.scalar_one_or_none()
- logger.info(
- f"[ENERGY] Auto-archive print start - archive {archive.id}, printer {printer_id}, plug found: {plug is not None}"
- )
- if plug:
- energy = await _get_plug_energy(plug, db)
- logger.info("[ENERGY] Auto-archive energy response: %s", energy)
- if energy and energy.get("total") is not None:
- _print_energy_start[archive.id] = energy["total"]
- logger.info(
- f"[ENERGY] Recorded starting energy for archive {archive.id}: {energy['total']} kWh"
- )
- else:
- logger.warning("[ENERGY] No 'total' in energy response for archive %s", archive.id)
- else:
- logger.info("[ENERGY] No smart plug found for printer %s", printer_id)
- except Exception as e:
- logger.warning("Failed to record starting energy: %s", e)
- await ws_manager.send_archive_created(
- {
- "id": archive.id,
- "printer_id": archive.printer_id,
- "filename": archive.filename,
- "print_name": archive.print_name,
- "status": archive.status,
- }
- )
- # MQTT relay - publish archive created
- try:
- await mqtt_relay.on_archive_created(
- archive_id=archive.id,
- print_name=archive.print_name,
- printer_name=printer.name,
- status=archive.status,
- )
- except Exception:
- pass # Don't fail if MQTT fails
- # Send notification with archive data (new archive created)
- if not notification_sent:
- archive_data = {"print_time_seconds": archive.print_time_seconds}
- await _send_print_start_notification(printer_id, data, archive_data, logger)
- notification_sent = True
- # Extract printable objects for skip object functionality
- try:
- from backend.app.services.archive import extract_printable_objects_from_3mf
- with open(temp_path, "rb") as f:
- threemf_data = f.read()
- # Extract with positions for UI overlay
- printable_objects, bbox_all = extract_printable_objects_from_3mf(
- threemf_data, include_positions=True
- )
- if printable_objects:
- # Store objects in printer state
- client = printer_manager.get_client(printer_id)
- if client:
- client.state.printable_objects = printable_objects
- client.state.printable_objects_bbox_all = bbox_all
- client.state.skipped_objects = [] # Reset skipped objects for new print
- logger.info(
- "Loaded %s printable objects for printer %s", len(printable_objects), printer_id
- )
- except Exception as e:
- logger.debug("Failed to extract printable objects: %s", e)
- # Store Spoolman tracking data for per-filament usage reporting
- try:
- await _store_spoolman_print_data(printer_id, archive.id, archive.file_path, db, printer_manager)
- except Exception as e:
- logger.warning("[SPOOLMAN] Failed to store tracking data: %s", e)
- finally:
- if temp_path and temp_path.exists():
- temp_path.unlink()
- async def _scan_for_timelapse_with_retries(archive_id: int):
- """
- Scan for timelapse with retries.
- The printer encodes the timelapse quickly after print completion.
- We just need a short delay then grab the most recent file.
- Since we KNOW timelapse was active (from MQTT ipcam data), the most recent
- file in /timelapse is our target. Retries handle FTP connection issues.
- """
- logger = logging.getLogger(__name__)
- # Short delays - printer usually finishes encoding within seconds
- retry_delays = [5, 10, 20]
- for attempt, delay in enumerate(retry_delays, 1):
- logger.info(
- f"[TIMELAPSE] Attempt {attempt}/{len(retry_delays)}: waiting {delay}s before scanning for archive {archive_id}"
- )
- await asyncio.sleep(delay)
- try:
- async with async_session() as db:
- from backend.app.models.printer import Printer
- from backend.app.services.bambu_ftp import download_file_bytes_async, list_files_async
- # Get archive (ArchiveService from module-level import)
- service = ArchiveService(db)
- archive = await service.get_archive(archive_id)
- if not archive:
- logger.warning("[TIMELAPSE] Archive %s not found, stopping retries", archive_id)
- return
- if archive.timelapse_path:
- logger.info("[TIMELAPSE] Archive %s already has timelapse attached, stopping retries", archive_id)
- return
- if not archive.printer_id:
- logger.warning("[TIMELAPSE] Archive %s has no printer, stopping retries", archive_id)
- return
- # Get printer
- result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
- printer = result.scalar_one_or_none()
- if not printer:
- logger.warning("[TIMELAPSE] Printer not found for archive %s, stopping retries", archive_id)
- return
- # Scan timelapse directory on printer
- # H2D may store in different locations than X1C
- files = []
- found_path = None
- for timelapse_path in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
- try:
- found_files = await list_files_async(printer.ip_address, printer.access_code, timelapse_path)
- if found_files:
- files = found_files
- found_path = timelapse_path
- logger.info(
- "[TIMELAPSE] Attempt %s: Found %s files in %s", attempt, len(files), timelapse_path
- )
- break
- except Exception as e:
- logger.debug("[TIMELAPSE] Path %s failed: %s", timelapse_path, e)
- continue
- if not files:
- logger.info("[TIMELAPSE] Attempt %s: No timelapse files found on printer, will retry", attempt)
- continue
- mp4_files = [f for f in files if not f.get("is_directory") and f.get("name", "").endswith(".mp4")]
- # Log ALL mp4 files found for debugging
- logger.info("[TIMELAPSE] Attempt %s: Found %s MP4 files in %s", attempt, len(mp4_files), found_path)
- for f in mp4_files[:5]: # Log first 5
- logger.info("[TIMELAPSE] - %s, mtime=%s", f.get("name"), f.get("mtime"))
- if not mp4_files:
- logger.info("[TIMELAPSE] Attempt %s: No MP4 files found, will retry", attempt)
- continue
- # Sort by mtime descending to get most recent file
- mp4_files_with_mtime = [f for f in mp4_files if f.get("mtime")]
- if not mp4_files_with_mtime:
- logger.info("[TIMELAPSE] Attempt %s: No MP4 files with mtime found, will retry", attempt)
- continue
- mp4_files_with_mtime.sort(key=lambda x: x.get("mtime"), reverse=True)
- most_recent = mp4_files_with_mtime[0]
- file_name = most_recent.get("name")
- logger.info("[TIMELAPSE] Attempt %s: Most recent file: %s", attempt, file_name)
- # Since we KNOW timelapse was active (from MQTT), just grab the most recent file
- remote_path = most_recent.get("path") or f"/timelapse/{file_name}"
- logger.info("[TIMELAPSE] Downloading %s for archive %s", file_name, archive_id)
- timelapse_data = await download_file_bytes_async(printer.ip_address, printer.access_code, remote_path)
- if timelapse_data:
- success = await service.attach_timelapse(archive_id, timelapse_data, file_name)
- if success:
- logger.info("[TIMELAPSE] Successfully attached timelapse to archive %s", archive_id)
- await ws_manager.send_archive_updated({"id": archive_id, "timelapse_attached": True})
- return # Success!
- else:
- logger.warning("[TIMELAPSE] Failed to attach timelapse to archive %s", archive_id)
- else:
- logger.warning("[TIMELAPSE] Attempt %s: Failed to download, will retry", attempt)
- except Exception as e:
- logger.warning("[TIMELAPSE] Attempt %s failed with error: %s", attempt, e)
- logger.warning("[TIMELAPSE] All %s attempts exhausted for archive %s, giving up", len(retry_delays), archive_id)
- async def on_print_complete(printer_id: int, data: dict):
- """Handle print completion - update the archive status."""
- import time
- logger = logging.getLogger(__name__)
- start_time = time.time()
- def log_timing(section: str):
- elapsed = time.time() - start_time
- logger.info("[TIMING] %s: %.3fs elapsed", section, elapsed)
- logger.info("[CALLBACK] on_print_complete started for printer %s", printer_id)
- try:
- ws_data = {
- "status": data.get("status"),
- "filename": data.get("filename"),
- "subtask_name": data.get("subtask_name"),
- "timelapse_was_active": data.get("timelapse_was_active"),
- }
- await ws_manager.send_print_complete(printer_id, ws_data)
- log_timing("WebSocket send_print_complete")
- except Exception as e:
- logger.warning("[CALLBACK] WebSocket send_print_complete failed: %s", e)
- # Clear current print user tracking (Issue #206)
- printer_manager.clear_current_print_user(printer_id)
- # MQTT relay - publish print complete
- try:
- printer_info = printer_manager.get_printer(printer_id)
- if printer_info:
- await mqtt_relay.on_print_complete(
- printer_id,
- printer_info.name,
- printer_info.serial_number,
- data.get("filename", ""),
- data.get("subtask_name", ""),
- data.get("status", "completed"),
- )
- except Exception:
- pass # Don't fail print complete callback if MQTT fails
- filename = data.get("filename", "")
- subtask_name = data.get("subtask_name", "")
- if not filename and not subtask_name:
- logger.warning("Print complete without filename or subtask_name")
- return
- logger.info("Print complete - filename: %s, subtask: %s, status: %s", filename, subtask_name, data.get("status"))
- # Build list of possible keys to try (matching how they were registered in on_print_start)
- possible_keys = []
- # Try subtask_name variations first (most reliable for matching)
- if subtask_name:
- possible_keys.append((printer_id, f"{subtask_name}.3mf"))
- possible_keys.append((printer_id, f"{subtask_name}.gcode.3mf"))
- possible_keys.append((printer_id, subtask_name))
- # Try filename variations
- if filename:
- # Extract just the filename if it's a path
- fname = filename.split("/")[-1] if "/" in filename else filename
- if fname.endswith(".3mf"):
- possible_keys.append((printer_id, fname))
- elif fname.endswith(".gcode"):
- base_name = fname.rsplit(".", 1)[0]
- possible_keys.append((printer_id, f"{base_name}.gcode.3mf"))
- possible_keys.append((printer_id, f"{base_name}.3mf"))
- possible_keys.append((printer_id, fname))
- else:
- possible_keys.append((printer_id, f"{fname}.gcode.3mf"))
- possible_keys.append((printer_id, f"{fname}.3mf"))
- possible_keys.append((printer_id, fname))
- # Also try full path versions
- if filename.endswith(".3mf"):
- possible_keys.append((printer_id, filename))
- elif filename.endswith(".gcode"):
- base_name = filename.rsplit(".", 1)[0]
- possible_keys.append((printer_id, f"{base_name}.3mf"))
- possible_keys.append((printer_id, filename))
- else:
- possible_keys.append((printer_id, f"{filename}.3mf"))
- possible_keys.append((printer_id, filename))
- # Find the archive for this print
- logger.info("Looking for archive in _active_prints, keys to try: %s...", possible_keys[:5])
- logger.info("Current _active_prints: %s", list(_active_prints.keys()))
- archive_id = None
- for key in possible_keys:
- archive_id = _active_prints.pop(key, None)
- if archive_id:
- logger.info("Found archive %s with key %s", archive_id, key)
- # Also clean up any other keys pointing to this archive
- keys_to_remove = [k for k, v in _active_prints.items() if v == archive_id]
- for k in keys_to_remove:
- _active_prints.pop(k, None)
- break
- if not archive_id:
- # Try to find by filename or subtask_name if not tracked (for prints started before app)
- async with async_session() as db:
- from backend.app.models.archive import PrintArchive
- # Try matching by subtask_name (stored as print_name) first
- if subtask_name:
- result = await db.execute(
- select(PrintArchive)
- .where(PrintArchive.printer_id == printer_id)
- .where(PrintArchive.status == "printing")
- .where(
- or_(
- PrintArchive.print_name.ilike(f"%{subtask_name}%"),
- PrintArchive.filename.ilike(f"%{subtask_name}%"),
- )
- )
- .order_by(PrintArchive.created_at.desc())
- .limit(1)
- )
- archive = result.scalar_one_or_none()
- if archive:
- archive_id = archive.id
- logger.info("Found archive %s by subtask_name match: %s", archive_id, subtask_name)
- # Also try by filename
- if not archive_id and filename:
- result = await db.execute(
- select(PrintArchive)
- .where(PrintArchive.printer_id == printer_id)
- .where(PrintArchive.filename == filename)
- .where(PrintArchive.status == "printing")
- .order_by(PrintArchive.created_at.desc())
- .limit(1)
- )
- archive = result.scalar_one_or_none()
- if archive:
- archive_id = archive.id
- if not archive_id:
- logger.warning("Could not find archive for print complete: filename=%s, subtask=%s", filename, subtask_name)
- return
- log_timing("Archive lookup")
- # Update archive status
- logger.info("[ARCHIVE] Updating archive %s status...", archive_id)
- try:
- async with async_session() as db:
- service = ArchiveService(db)
- status = data.get("status", "completed")
- # Auto-detect failure reason
- failure_reason = None
- if status == "aborted":
- failure_reason = "User cancelled"
- logger.info("[ARCHIVE] Print was aborted by user, setting failure_reason='User cancelled'")
- elif status == "failed":
- # Try to determine failure reason from HMS errors
- hms_errors = data.get("hms_errors", [])
- if hms_errors:
- logger.info("[ARCHIVE] HMS errors at failure: %s", hms_errors)
- # Map known HMS error modules to failure reasons
- # Module 0x07 = Filament, 0x0C = MC (Motion Controller), etc.
- for err in hms_errors:
- module = err.get("module", 0)
- if module == 0x07: # Filament module
- failure_reason = "Filament runout"
- break
- elif module == 0x0C: # Motion controller
- failure_reason = "Layer shift"
- break
- elif module == 0x05: # Nozzle/extruder
- failure_reason = "Clogged nozzle"
- break
- if failure_reason:
- logger.info("[ARCHIVE] Detected failure_reason from HMS: %s", failure_reason)
- else:
- logger.info("[ARCHIVE] No HMS errors available to determine failure reason")
- await service.update_archive_status(
- archive_id,
- status=status,
- completed_at=datetime.now() if status in ("completed", "failed", "aborted") else None,
- failure_reason=failure_reason,
- )
- logger.info(
- "[ARCHIVE] Archive %s status updated to %s, failure_reason=%s", archive_id, status, failure_reason
- )
- # Add cost for reprints (first prints have cost set in archive_print())
- if status == "completed" and archive_id in _reprint_archives:
- _reprint_archives.discard(archive_id)
- try:
- await service.add_reprint_cost(archive_id)
- logger.info("[ARCHIVE] Added reprint cost for archive %s", archive_id)
- except Exception as e:
- logger.warning("[ARCHIVE] Failed to add reprint cost for archive %s: %s", archive_id, e)
- await ws_manager.send_archive_updated(
- {
- "id": archive_id,
- "status": status,
- }
- )
- logger.info("[ARCHIVE] WebSocket notification sent for archive %s", archive_id)
- # MQTT relay - publish archive updated
- try:
- await mqtt_relay.on_archive_updated(
- archive_id=archive_id,
- print_name=filename or subtask_name,
- status=status,
- )
- except Exception:
- pass # Don't fail if MQTT fails
- except Exception as e:
- logger.error("[ARCHIVE] Failed to update archive %s status: %s", archive_id, e, exc_info=True)
- # Continue with other operations even if archive update fails
- log_timing("Archive status update")
- # Report filament usage to Spoolman if print completed successfully
- if data.get("status") == "completed":
- try:
- await _report_spoolman_usage(printer_id, archive_id)
- log_timing("Spoolman usage report")
- except Exception as e:
- logger.warning("Spoolman usage reporting failed: %s", e)
- else:
- # Report partial usage if tracking data exists (only stored when weight sync is disabled)
- try:
- async with async_session() as db:
- await _cleanup_spoolman_tracking(printer_id, archive_id, db)
- except Exception as e:
- logger.debug("[SPOOLMAN] Cleanup failed: %s", e)
- # Run slow operations as background tasks to avoid blocking the event loop
- # These operations can take 5-10+ seconds and would freeze the UI if awaited
- starting_kwh = _print_energy_start.pop(archive_id, None)
- async def _background_energy_calculation():
- """Calculate and save energy usage in background."""
- try:
- logger.info("[ENERGY-BG] Starting energy calculation for archive %s", archive_id)
- async with async_session() as db:
- plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
- plug = plug_result.scalar_one_or_none()
- if plug:
- energy = await _get_plug_energy(plug, db)
- logger.info("[ENERGY-BG] Energy response: %s", energy)
- energy_used = None
- if starting_kwh is not None and energy and energy.get("total") is not None:
- ending_kwh = energy["total"]
- energy_used = round(ending_kwh - starting_kwh, 4)
- logger.info("[ENERGY-BG] Per-print energy: %s kWh", energy_used)
- if energy_used is not None and energy_used >= 0:
- from backend.app.api.routes.settings import get_setting
- energy_cost_per_kwh = await get_setting(db, "energy_cost_per_kwh")
- cost_per_kwh = float(energy_cost_per_kwh) if energy_cost_per_kwh else 0.15
- energy_cost = round(energy_used * cost_per_kwh, 2)
- from backend.app.models.archive import PrintArchive
- result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
- archive = result.scalar_one_or_none()
- if archive:
- archive.energy_kwh = energy_used
- archive.energy_cost = energy_cost
- await db.commit()
- logger.info("[ENERGY-BG] Saved: %s kWh, cost=%s", energy_used, energy_cost)
- else:
- logger.info("[ENERGY-BG] No smart plug for printer %s", printer_id)
- except Exception as e:
- logger.warning("[ENERGY-BG] Failed: %s", e)
- async def _background_finish_photo() -> str | None:
- """Capture finish photo in background. Returns photo filename if captured."""
- try:
- logger.info("[PHOTO-BG] Starting finish photo capture for archive %s", archive_id)
- from backend.app.api.routes.camera import _active_chamber_streams, _active_streams, get_buffered_frame
- async with async_session() as db:
- from backend.app.api.routes.settings import get_setting
- capture_enabled = await get_setting(db, "capture_finish_photo")
- if capture_enabled is None or capture_enabled.lower() == "true":
- from backend.app.models.printer import Printer
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- if printer and archive_id:
- from backend.app.models.archive import PrintArchive
- result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
- archive = result.scalar_one_or_none()
- if archive:
- import uuid
- from datetime import datetime
- from pathlib import Path
- archive_dir = app_settings.base_dir / Path(archive.file_path).parent
- photo_filename = None
- # Check for external camera first
- if printer.external_camera_enabled and printer.external_camera_url:
- logger.info("[PHOTO-BG] Using external camera")
- from backend.app.services.external_camera import capture_frame
- frame_data = await capture_frame(
- printer.external_camera_url, printer.external_camera_type or "mjpeg"
- )
- if frame_data:
- photos_dir = archive_dir / "photos"
- photos_dir.mkdir(parents=True, exist_ok=True)
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
- photo_filename = f"finish_{timestamp}_{uuid.uuid4().hex[:8]}.jpg"
- photo_path = photos_dir / photo_filename
- await asyncio.to_thread(photo_path.write_bytes, frame_data)
- logger.info("[PHOTO-BG] Saved external camera frame: %s", photo_filename)
- else:
- # Check if camera stream is active - use buffered frame to avoid freeze
- # Check both RTSP streams (_active_streams) and chamber image streams (_active_chamber_streams)
- active_for_printer = [k for k in _active_streams if k.startswith(f"{printer_id}-")]
- active_chamber_for_printer = [
- k for k in _active_chamber_streams if k.startswith(f"{printer_id}-")
- ]
- buffered_frame = get_buffered_frame(printer_id)
- if (active_for_printer or active_chamber_for_printer) and buffered_frame:
- # Use frame from active stream
- logger.info("[PHOTO-BG] Using buffered frame from active stream")
- photos_dir = archive_dir / "photos"
- photos_dir.mkdir(parents=True, exist_ok=True)
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
- photo_filename = f"finish_{timestamp}_{uuid.uuid4().hex[:8]}.jpg"
- photo_path = photos_dir / photo_filename
- await asyncio.to_thread(photo_path.write_bytes, buffered_frame)
- logger.info("[PHOTO-BG] Saved buffered frame: %s", photo_filename)
- else:
- # No active stream - capture new frame
- from backend.app.services.camera import capture_finish_photo
- photo_filename = await capture_finish_photo(
- printer_id=printer_id,
- ip_address=printer.ip_address,
- access_code=printer.access_code,
- model=printer.model,
- archive_dir=archive_dir,
- )
- if photo_filename:
- photos = archive.photos or []
- photos.append(photo_filename)
- archive.photos = photos
- await db.commit()
- logger.info("[PHOTO-BG] Saved: %s", photo_filename)
- return photo_filename
- return None
- except Exception as e:
- logger.warning("[PHOTO-BG] Failed: %s", e)
- return None
- asyncio.create_task(_background_energy_calculation())
- # Photo capture task - result will be used by notifications
- photo_task = asyncio.create_task(_background_finish_photo())
- log_timing("Background tasks scheduled (energy, photo)")
- # Also run smart plug, notifications, and maintenance as background tasks
- print_status = data.get("status", "completed")
- async def _background_smart_plug():
- """Handle smart plug automation in background."""
- try:
- logger.info("[AUTO-OFF-BG] Starting smart plug automation for printer %s", printer_id)
- async with async_session() as db:
- await smart_plug_manager.on_print_complete(printer_id, print_status, db)
- logger.info("[AUTO-OFF-BG] Completed")
- except Exception as e:
- logger.warning("[AUTO-OFF-BG] Failed: %s", e)
- async def _background_notifications(finish_photo_filename: str | None = None):
- """Send print complete notifications in background."""
- try:
- logger.info(
- "[NOTIFY-BG] Starting notifications for printer %s, photo=%s", printer_id, finish_photo_filename
- )
- async with async_session() as db:
- from backend.app.models.archive import PrintArchive
- from backend.app.models.printer import Printer
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- printer_name = printer.name if printer else f"Printer {printer_id}"
- archive_data = None
- if archive_id:
- archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
- archive = archive_result.scalar_one_or_none()
- if archive:
- archive_data = {
- "print_time_seconds": archive.print_time_seconds,
- "actual_filament_grams": archive.filament_used_grams,
- "failure_reason": archive.failure_reason,
- }
- # Add finish photo URL and image bytes if available
- if finish_photo_filename:
- from backend.app.api.routes.settings import get_setting
- external_url = await get_setting(db, "external_url")
- if external_url:
- external_url = external_url.rstrip("/")
- archive_data["finish_photo_url"] = (
- f"{external_url}/api/v1/archives/{archive_id}/photos/{finish_photo_filename}"
- )
- else:
- # Fallback to relative URL (won't work for external services)
- archive_data["finish_photo_url"] = (
- f"/api/v1/archives/{archive_id}/photos/{finish_photo_filename}"
- )
- # Read finish photo bytes for image attachment (e.g. Pushover)
- try:
- from pathlib import Path
- photo_path = (
- app_settings.base_dir
- / Path(archive.file_path).parent
- / "photos"
- / finish_photo_filename
- )
- if photo_path.exists():
- photo_bytes = await asyncio.to_thread(photo_path.read_bytes)
- if len(photo_bytes) <= 2_500_000:
- archive_data["image_data"] = photo_bytes
- logger.info("[NOTIFY-BG] Loaded finish photo bytes: %s bytes", len(photo_bytes))
- else:
- logger.warning(
- f"[NOTIFY-BG] Finish photo too large for attachment: "
- f"{len(photo_bytes)} bytes"
- )
- except Exception as e:
- logger.warning("[NOTIFY-BG] Failed to read finish photo bytes: %s", e)
- await notification_service.on_print_complete(
- printer_id, printer_name, print_status, data, db, archive_data=archive_data
- )
- logger.info("[NOTIFY-BG] Completed")
- except Exception as e:
- logger.warning("[NOTIFY-BG] Failed: %s", e)
- async def _background_maintenance_check():
- """Check for maintenance due in background."""
- if print_status != "completed":
- return
- try:
- logger.info("[MAINT-BG] Starting maintenance check for printer %s", printer_id)
- async with async_session() as db:
- from backend.app.models.printer import Printer
- result = await db.execute(select(Printer).where(Printer.id == printer_id))
- printer = result.scalar_one_or_none()
- printer_name = printer.name if printer else f"Printer {printer_id}"
- await ensure_default_types(db)
- overview = await _get_printer_maintenance_internal(printer_id, db, commit=True)
- items_needing_attention = [
- {"name": item.maintenance_type_name, "is_due": item.is_due, "is_warning": item.is_warning}
- for item in overview.maintenance_items
- if item.enabled and (item.is_due or item.is_warning)
- ]
- if items_needing_attention:
- await notification_service.on_maintenance_due(printer_id, printer_name, items_needing_attention, db)
- logger.info("[MAINT-BG] Sent notification: %s items need attention", len(items_needing_attention))
- # MQTT relay - publish maintenance alerts
- for item in items_needing_attention:
- try:
- await mqtt_relay.on_maintenance_alert(
- printer_id=printer_id,
- printer_name=printer_name,
- maintenance_type=item["name"],
- current_value=0, # Not easily available here
- threshold=0, # Not easily available here
- )
- except Exception:
- pass # Don't fail if MQTT fails
- else:
- logger.info("[MAINT-BG] Completed (no items need attention)")
- except Exception as e:
- logger.warning("[MAINT-BG] Failed: %s", e)
- asyncio.create_task(_background_smart_plug())
- asyncio.create_task(_background_maintenance_check())
- # Notification task waits for photo capture to complete first
- async def _photo_then_notify():
- """Wait for photo capture, then send notification with photo URL."""
- try:
- finish_photo = await photo_task
- logger.info("[PHOTO-NOTIFY] Photo task returned: %s", finish_photo)
- await _background_notifications(finish_photo)
- except Exception as e:
- logger.warning("[PHOTO-NOTIFY] Failed: %s", e)
- # Still try to send notification without photo
- await _background_notifications(None)
- asyncio.create_task(_photo_then_notify())
- # Stitch external camera layer timelapse if session was active
- print_status = data.get("status", "completed")
- async def _background_layer_timelapse():
- """Stitch layer timelapse and attach to archive."""
- from backend.app.services.layer_timelapse import cancel_session, on_print_complete as tl_complete
- try:
- if print_status == "completed":
- logger.info("[LAYER-TL] Stitching layer timelapse for printer %s", printer_id)
- timelapse_path = await tl_complete(printer_id)
- if timelapse_path and archive_id:
- logger.info("[LAYER-TL] Attaching timelapse %s to archive %s", timelapse_path, archive_id)
- async with async_session() as db:
- service = ArchiveService(db)
- timelapse_data = await asyncio.to_thread(timelapse_path.read_bytes)
- await service.attach_timelapse(archive_id, timelapse_data, "layer_timelapse.mp4")
- # Clean up the temp file
- await asyncio.to_thread(timelapse_path.unlink, missing_ok=True)
- logger.info("[LAYER-TL] Layer timelapse attached successfully")
- elif timelapse_path:
- # Timelapse created but no archive - just clean up
- await asyncio.to_thread(timelapse_path.unlink, missing_ok=True)
- else:
- # Print failed or cancelled - cancel timelapse session
- cancel_session(printer_id)
- logger.info(
- "[LAYER-TL] Cancelled layer timelapse for printer %s (status: %s)", printer_id, print_status
- )
- except Exception as e:
- logger.warning("[LAYER-TL] Failed: %s", e)
- # Try to cancel session on error
- try:
- cancel_session(printer_id)
- except Exception:
- pass # Best-effort timelapse session cancellation on error
- asyncio.create_task(_background_layer_timelapse())
- log_timing("All background tasks scheduled")
- # Auto-scan for timelapse if recording was active during the print
- if archive_id and data.get("timelapse_was_active") and data.get("status") == "completed":
- logger.info("[TIMELAPSE] Timelapse was active during print, scheduling auto-scan for archive %s", archive_id)
- # Schedule timelapse scan as background task with retries
- # The printer needs time to encode the video after print completion
- asyncio.create_task(_scan_for_timelapse_with_retries(archive_id))
- log_timing("Timelapse scan scheduled")
- # Update queue item if this was a scheduled print
- try:
- async with async_session() as db:
- from backend.app.models.print_queue import PrintQueueItem
- # Note: SmartPlug is already imported at module level (line 56)
- # Do NOT import it here as it would shadow the module-level import
- # and cause "cannot access local variable" errors earlier in this function
- result = await db.execute(
- select(PrintQueueItem)
- .where(PrintQueueItem.printer_id == printer_id)
- .where(PrintQueueItem.status == "printing")
- )
- queue_item = result.scalar_one_or_none()
- if queue_item:
- status = data.get("status", "completed")
- queue_item.status = status
- queue_item.completed_at = datetime.now()
- await db.commit()
- logger.info("Updated queue item %s status to %s", queue_item.id, status)
- # MQTT relay - publish queue job completed
- try:
- printer_info = printer_manager.get_printer(printer_id)
- await mqtt_relay.on_queue_job_completed(
- job_id=queue_item.id,
- filename=filename or subtask_name,
- printer_id=printer_id,
- printer_name=printer_info.name if printer_info else "Unknown",
- status=status,
- )
- except Exception:
- pass # Don't fail if MQTT fails
- # Check if queue is now empty and send notification
- try:
- from sqlalchemy import func
- # Count remaining pending items
- count_result = await db.execute(
- select(func.count(PrintQueueItem.id)).where(PrintQueueItem.status == "pending")
- )
- pending_count = count_result.scalar() or 0
- if pending_count == 0:
- # Count how many completed today (rough approximation)
- today_start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
- completed_result = await db.execute(
- select(func.count(PrintQueueItem.id)).where(
- PrintQueueItem.status.in_(["completed", "failed", "skipped"]),
- PrintQueueItem.completed_at >= today_start,
- )
- )
- completed_count = completed_result.scalar() or 1
- await notification_service.on_queue_completed(
- completed_count=completed_count,
- db=db,
- )
- except Exception:
- pass # Don't fail if notification fails
- # Handle auto_off_after - power off printer if requested (after cooldown)
- if queue_item.auto_off_after:
- result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
- plug = result.scalar_one_or_none()
- if plug and plug.enabled:
- logger.info("Auto-off requested for printer %s, waiting for cooldown...", printer_id)
- async def cooldown_and_poweroff(pid: int, plug_id: int):
- # Wait for nozzle to cool down
- await printer_manager.wait_for_cooldown(pid, target_temp=50.0, timeout=600)
- # Re-fetch plug in new session
- async with async_session() as new_db:
- result = await new_db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
- p = result.scalar_one_or_none()
- if p and p.enabled:
- success = await tasmota_service.turn_off(p)
- if success:
- logger.info("Powered off printer %s via smart plug '%s'", pid, p.name)
- else:
- logger.warning("Failed to power off printer %s via smart plug", pid)
- asyncio.create_task(cooldown_and_poweroff(printer_id, plug.id))
- except Exception as e:
- logging.getLogger(__name__).warning(f"Queue item update failed: {e}")
- log_timing("Queue item update")
- logger.info("[CALLBACK] on_print_complete finished for printer %s, archive %s", printer_id, archive_id)
- # AMS sensor history recording
- _ams_history_task: asyncio.Task | None = None
- AMS_HISTORY_INTERVAL = 300 # Record every 5 minutes
- AMS_HISTORY_RETENTION_DAYS = 30 # Keep data for 30 days
- _ams_cleanup_counter = 0 # Track recordings to trigger periodic cleanup
- _ams_alarm_cooldown: dict[str, datetime] = {} # Track alarm cooldowns (printer_id:ams_id:type -> last_alarm_time)
- AMS_ALARM_COOLDOWN_MINUTES = 60 # Don't send same alarm more than once per hour
- async def record_ams_history():
- """Background task to record AMS humidity and temperature data."""
- logger = logging.getLogger(__name__)
- # Wait a short time for MQTT connections to establish on startup
- await asyncio.sleep(10)
- while True:
- try:
- from backend.app.models.ams_history import AMSSensorHistory
- from backend.app.models.printer import Printer
- from backend.app.models.settings import Settings
- async with async_session() as db:
- # Get all active printers
- result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
- printers = result.scalars().all()
- # Get alarm thresholds from settings
- humidity_threshold = 60.0 # Default: fair threshold
- temp_threshold = 35.0 # Default: fair threshold
- result = await db.execute(select(Settings).where(Settings.key == "ams_humidity_fair"))
- setting = result.scalar_one_or_none()
- if setting:
- try:
- humidity_threshold = float(setting.value)
- except (ValueError, TypeError):
- pass # Keep default threshold if stored value is invalid
- result = await db.execute(select(Settings).where(Settings.key == "ams_temp_fair"))
- setting = result.scalar_one_or_none()
- if setting:
- try:
- temp_threshold = float(setting.value)
- except (ValueError, TypeError):
- pass # Keep default threshold if stored value is invalid
- recorded_count = 0
- for printer in printers:
- # Get current state from printer manager
- state = printer_manager.get_status(printer.id)
- if not state or not state.connected or not state.raw_data:
- continue # Skip disconnected printers - don't use stale data
- raw_data = state.raw_data
- if "ams" not in raw_data or not isinstance(raw_data["ams"], list):
- continue
- # Record data for each AMS unit
- for ams_data in raw_data["ams"]:
- ams_id = int(ams_data.get("id", 0))
- # Get humidity (prefer humidity_raw)
- humidity_raw = ams_data.get("humidity_raw")
- humidity_idx = ams_data.get("humidity")
- humidity = None
- if humidity_raw is not None:
- try:
- humidity = float(humidity_raw)
- except (ValueError, TypeError):
- pass # Skip unparseable humidity; will try fallback
- if humidity is None and humidity_idx is not None:
- try:
- humidity = float(humidity_idx)
- except (ValueError, TypeError):
- pass # Skip unparseable humidity index value
- # Get temperature
- temperature = None
- temp_str = ams_data.get("temp")
- if temp_str is not None:
- try:
- temperature = float(temp_str)
- except (ValueError, TypeError):
- pass # Skip unparseable temperature value
- # Skip if no data
- if humidity is None and temperature is None:
- continue
- # Record the data point
- history = AMSSensorHistory(
- printer_id=printer.id,
- ams_id=ams_id,
- humidity=humidity,
- humidity_raw=float(humidity_raw) if humidity_raw else None,
- temperature=temperature,
- )
- db.add(history)
- recorded_count += 1
- # Generate AMS label and determine if it's AMS-HT (A, B, C, D or HT-A for AMS-Lite/Hub)
- is_ams_ht = ams_id >= 128
- if is_ams_ht:
- ams_label = f"HT-{chr(65 + (ams_id - 128))}"
- else:
- ams_label = f"AMS-{chr(65 + ams_id)}"
- # Check humidity alarm (only if above threshold)
- if humidity is not None and humidity > humidity_threshold:
- cooldown_key = f"{printer.id}:{ams_id}:humidity"
- last_alarm = _ams_alarm_cooldown.get(cooldown_key)
- now = datetime.now()
- if (
- last_alarm is None
- or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60
- ):
- _ams_alarm_cooldown[cooldown_key] = now
- logger.info(
- f"Sending humidity alarm for {printer.name} {ams_label}: {humidity}% > {humidity_threshold}%"
- )
- try:
- # Call different notification method based on AMS type
- if is_ams_ht:
- await notification_service.on_ams_ht_humidity_high(
- printer.id, printer.name, ams_label, humidity, humidity_threshold, db
- )
- else:
- await notification_service.on_ams_humidity_high(
- printer.id, printer.name, ams_label, humidity, humidity_threshold, db
- )
- except Exception as e:
- logger.warning("Failed to send humidity alarm: %s", e)
- # Check temperature alarm (only if above threshold)
- if temperature is not None and temperature > temp_threshold:
- cooldown_key = f"{printer.id}:{ams_id}:temperature"
- last_alarm = _ams_alarm_cooldown.get(cooldown_key)
- now = datetime.now()
- if (
- last_alarm is None
- or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60
- ):
- _ams_alarm_cooldown[cooldown_key] = now
- logger.info(
- f"Sending temperature alarm for {printer.name} {ams_label}: {temperature}°C > {temp_threshold}°C"
- )
- try:
- # Call different notification method based on AMS type
- if is_ams_ht:
- await notification_service.on_ams_ht_temperature_high(
- printer.id, printer.name, ams_label, temperature, temp_threshold, db
- )
- else:
- await notification_service.on_ams_temperature_high(
- printer.id, printer.name, ams_label, temperature, temp_threshold, db
- )
- except Exception as e:
- logger.warning("Failed to send temperature alarm: %s", e)
- await db.commit()
- if recorded_count > 0:
- logger.info("Recorded %s AMS sensor history entries", recorded_count)
- # Periodic cleanup of old data (every ~288 recordings = ~24 hours at 5min interval)
- global _ams_cleanup_counter
- _ams_cleanup_counter += 1
- if _ams_cleanup_counter >= 288:
- _ams_cleanup_counter = 0
- # Get retention days from settings
- from backend.app.models.settings import Settings
- result = await db.execute(select(Settings).where(Settings.key == "ams_history_retention_days"))
- setting = result.scalar_one_or_none()
- retention_days = int(setting.value) if setting else AMS_HISTORY_RETENTION_DAYS
- cutoff = datetime.now() - timedelta(days=retention_days)
- result = await db.execute(delete(AMSSensorHistory).where(AMSSensorHistory.recorded_at < cutoff))
- await db.commit()
- if result.rowcount > 0:
- logger.info(
- f"Cleaned up {result.rowcount} old AMS sensor history entries (older than {retention_days} days)"
- )
- # Wait until next recording interval
- await asyncio.sleep(AMS_HISTORY_INTERVAL)
- except asyncio.CancelledError:
- break
- except Exception as e:
- logger.warning("AMS history recording failed: %s", e)
- await asyncio.sleep(60) # Wait a bit before retrying
- def start_ams_history_recording():
- """Start the AMS history recording background task."""
- global _ams_history_task
- if _ams_history_task is None:
- _ams_history_task = asyncio.create_task(record_ams_history())
- logging.getLogger(__name__).info("AMS history recording started")
- def stop_ams_history_recording():
- """Stop the AMS history recording background task."""
- global _ams_history_task
- if _ams_history_task:
- _ams_history_task.cancel()
- _ams_history_task = None
- logging.getLogger(__name__).info("AMS history recording stopped")
- # Printer runtime tracking
- _runtime_tracking_task: asyncio.Task | None = None
- RUNTIME_TRACKING_INTERVAL = 30 # Update every 30 seconds
- async def track_printer_runtime():
- """Background task to track printer active runtime (RUNNING/PAUSE states)."""
- logger = logging.getLogger(__name__)
- # Wait for MQTT connections to establish on startup
- await asyncio.sleep(15)
- while True:
- try:
- from backend.app.models.printer import Printer
- async with async_session() as db:
- # Get all active printers
- result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
- printers = result.scalars().all()
- now = datetime.now()
- updated_count = 0
- needs_commit = False
- for printer in printers:
- # Get current state from printer manager
- state = printer_manager.get_status(printer.id)
- if not state:
- logger.debug("[%s] Runtime tracking: no state available", printer.name)
- continue
- if not state.connected:
- logger.debug("[%s] Runtime tracking: not connected", printer.name)
- continue
- # Check if printer is in an active state (RUNNING or PAUSE)
- if state.state in ("RUNNING", "PAUSE"):
- # Calculate time since last update
- if printer.last_runtime_update:
- elapsed = (now - printer.last_runtime_update).total_seconds()
- if elapsed > 0:
- printer.runtime_seconds += int(elapsed)
- updated_count += 1
- needs_commit = True
- logger.debug(
- f"[{printer.name}] Runtime tracking: added {int(elapsed)}s, "
- f"total={printer.runtime_seconds}s ({printer.runtime_seconds / 3600:.2f}h)"
- )
- else:
- # First time seeing printer active - need to commit to save timestamp
- needs_commit = True
- logger.debug("[%s] Runtime tracking: first active detection", printer.name)
- printer.last_runtime_update = now
- else:
- # Printer is idle/offline - clear last_runtime_update
- if printer.last_runtime_update is not None:
- logger.debug(
- f"[{printer.name}] Runtime tracking: state={state.state}, clearing last_runtime_update"
- )
- printer.last_runtime_update = None
- needs_commit = True
- if needs_commit:
- await db.commit()
- if updated_count > 0:
- logger.debug("Updated runtime for %s printer(s)", updated_count)
- except asyncio.CancelledError:
- logger.info("Runtime tracking cancelled")
- break
- except Exception as e:
- logger.warning("Runtime tracking failed: %s", e)
- await asyncio.sleep(RUNTIME_TRACKING_INTERVAL)
- def start_runtime_tracking():
- """Start the printer runtime tracking background task."""
- global _runtime_tracking_task
- if _runtime_tracking_task is None:
- _runtime_tracking_task = asyncio.create_task(track_printer_runtime())
- logging.getLogger(__name__).info("Printer runtime tracking started")
- def stop_runtime_tracking():
- """Stop the printer runtime tracking background task."""
- global _runtime_tracking_task
- if _runtime_tracking_task:
- _runtime_tracking_task.cancel()
- _runtime_tracking_task = None
- logging.getLogger(__name__).info("Printer runtime tracking stopped")
- @asynccontextmanager
- async def lifespan(app: FastAPI):
- # Startup
- await init_db()
- # Restore debug logging state from previous session
- await init_debug_logging()
- # Set up printer manager callbacks
- loop = asyncio.get_event_loop()
- printer_manager.set_event_loop(loop)
- printer_manager.set_status_change_callback(on_printer_status_change)
- printer_manager.set_print_start_callback(on_print_start)
- printer_manager.set_print_complete_callback(on_print_complete)
- printer_manager.set_ams_change_callback(on_ams_change)
- # Layer change callback for external camera timelapse
- async def on_layer_change(printer_id: int, layer_num: int):
- """Capture timelapse frame on layer change."""
- from backend.app.services.layer_timelapse import on_layer_change as tl_layer_change
- await tl_layer_change(printer_id, layer_num)
- printer_manager.set_layer_change_callback(on_layer_change)
- # Initialize MQTT relay from settings
- async with async_session() as db:
- from backend.app.api.routes.settings import get_setting
- mqtt_settings = {
- "mqtt_enabled": (await get_setting(db, "mqtt_enabled") or "false") == "true",
- "mqtt_broker": await get_setting(db, "mqtt_broker") or "",
- "mqtt_port": int(await get_setting(db, "mqtt_port") or "1883"),
- "mqtt_username": await get_setting(db, "mqtt_username") or "",
- "mqtt_password": await get_setting(db, "mqtt_password") or "",
- "mqtt_topic_prefix": await get_setting(db, "mqtt_topic_prefix") or "bambuddy",
- "mqtt_use_tls": (await get_setting(db, "mqtt_use_tls") or "false") == "true",
- }
- await mqtt_relay.configure(mqtt_settings)
- # Restore MQTT smart plug subscriptions
- if mqtt_settings.get("mqtt_enabled"):
- from backend.app.models.smart_plug import SmartPlug
- result = await db.execute(select(SmartPlug).where(SmartPlug.plug_type == "mqtt"))
- mqtt_plugs = result.scalars().all()
- for plug in mqtt_plugs:
- if plug.mqtt_topic:
- mqtt_relay.smart_plug_service.subscribe(
- plug_id=plug.id,
- topic=plug.mqtt_topic,
- power_path=plug.mqtt_power_path,
- energy_path=plug.mqtt_energy_path,
- state_path=plug.mqtt_state_path,
- multiplier=plug.mqtt_multiplier or 1.0,
- )
- if mqtt_plugs:
- logging.info("Restored %s MQTT smart plug subscriptions", len(mqtt_plugs))
- # Connect to all active printers
- async with async_session() as db:
- await init_printer_connections(db)
- # Auto-connect to Spoolman if enabled
- async with async_session() as db:
- from backend.app.api.routes.settings import get_setting
- spoolman_enabled = await get_setting(db, "spoolman_enabled")
- spoolman_url = await get_setting(db, "spoolman_url")
- if spoolman_enabled and spoolman_enabled.lower() == "true" and spoolman_url:
- try:
- client = await init_spoolman_client(spoolman_url)
- if await client.health_check():
- logging.info("Auto-connected to Spoolman at %s", spoolman_url)
- # Ensure the 'tag' extra field exists for RFID/UUID storage
- await client.ensure_tag_extra_field()
- else:
- logging.warning("Spoolman at %s is not reachable", spoolman_url)
- except Exception as e:
- logging.warning("Failed to auto-connect to Spoolman: %s", e)
- # Start the print scheduler
- asyncio.create_task(print_scheduler.run())
- # Start the smart plug scheduler for time-based on/off
- smart_plug_manager.start_scheduler()
- # Resume any pending auto-offs that were interrupted by restart
- await smart_plug_manager.resume_pending_auto_offs()
- # Start the notification digest scheduler
- notification_service.start_digest_scheduler()
- # Start the GitHub backup scheduler
- await github_backup_service.start_scheduler()
- # Start AMS history recording
- start_ams_history_recording()
- # Start printer runtime tracking
- start_runtime_tracking()
- # Initialize virtual printer manager
- from backend.app.services.virtual_printer import virtual_printer_manager
- virtual_printer_manager.set_session_factory(async_session)
- # Auto-start virtual printer if enabled
- async with async_session() as db:
- from backend.app.api.routes.settings import get_setting
- vp_enabled = await get_setting(db, "virtual_printer_enabled")
- if vp_enabled and vp_enabled.lower() == "true":
- vp_access_code = await get_setting(db, "virtual_printer_access_code") or ""
- vp_mode = await get_setting(db, "virtual_printer_mode") or "immediate"
- vp_model = await get_setting(db, "virtual_printer_model") or ""
- vp_target_printer_id = await get_setting(db, "virtual_printer_target_printer_id")
- vp_remote_iface = await get_setting(db, "virtual_printer_remote_interface_ip") or ""
- # Look up printer IP and serial if in proxy mode
- vp_target_ip = ""
- vp_target_serial = ""
- if vp_mode == "proxy" and vp_target_printer_id:
- from backend.app.models.printer import Printer
- result = await db.execute(select(Printer).where(Printer.id == int(vp_target_printer_id)))
- printer = result.scalar_one_or_none()
- if printer:
- vp_target_ip = printer.ip_address
- vp_target_serial = printer.serial_number
- # Proxy mode requires target IP, other modes require access code
- can_start = (vp_mode == "proxy" and vp_target_ip) or (vp_mode != "proxy" and vp_access_code)
- if can_start:
- try:
- await virtual_printer_manager.configure(
- enabled=True,
- access_code=vp_access_code,
- mode=vp_mode,
- model=vp_model,
- target_printer_ip=vp_target_ip,
- target_printer_serial=vp_target_serial,
- remote_interface_ip=vp_remote_iface,
- )
- if vp_mode == "proxy":
- logging.info("Virtual printer proxy started (target=%s)", vp_target_ip)
- else:
- logging.info("Virtual printer started (model=%s)", vp_model or "default")
- except Exception as e:
- logging.warning("Failed to start virtual printer: %s", e)
- yield
- # Shutdown
- print_scheduler.stop()
- smart_plug_manager.stop_scheduler()
- notification_service.stop_digest_scheduler()
- github_backup_service.stop_scheduler()
- stop_ams_history_recording()
- stop_runtime_tracking()
- printer_manager.disconnect_all()
- await close_spoolman_client()
- # Stop virtual printer if running
- if virtual_printer_manager.is_enabled:
- await virtual_printer_manager.configure(enabled=False)
- app = FastAPI(
- title=app_settings.app_name,
- description="Archive and manage Bambu Lab 3MF files",
- version=APP_VERSION,
- lifespan=lifespan,
- )
- # =============================================================================
- # Authentication Middleware - Secures ALL API routes by default
- # =============================================================================
- # Public routes that don't require authentication even when auth is enabled
- PUBLIC_API_ROUTES = {
- # Auth routes needed before/during login
- "/api/v1/auth/status",
- "/api/v1/auth/login",
- "/api/v1/auth/setup", # Needed for initial setup and recovery
- # Version check for updates (no sensitive data)
- "/api/v1/updates/version",
- # Metrics endpoint handles its own prometheus_token authentication
- "/api/v1/metrics",
- }
- # Route prefixes that are public (for routes with dynamic segments)
- PUBLIC_API_PREFIXES = [
- # WebSocket connections handle their own auth
- "/api/v1/ws",
- ]
- # Route patterns that are public (read-only display data)
- # These are checked with "in path" - needed because browsers load images/videos
- # via <img src> and <video src> which don't include Authorization headers
- PUBLIC_API_PATTERNS = [
- # Thumbnails
- "/thumbnail", # /archives/{id}/thumbnail, /library/files/{id}/thumbnail
- "/plate-thumbnail/", # /archives/{id}/plate-thumbnail/{plate_id}
- # Images and media
- "/photos/", # /archives/{id}/photos/{filename}
- "/project-image/", # /archives/{id}/project-image/{path}
- "/qrcode", # /archives/{id}/qrcode
- "/timelapse", # /archives/{id}/timelapse (video)
- "/cover", # /printers/{id}/cover
- "/icon", # /external-links/{id}/icon
- # Camera (streams loaded via <img> tag)
- "/camera/stream", # /printers/{id}/camera/stream
- "/camera/snapshot", # /printers/{id}/camera/snapshot
- ]
- @app.middleware("http")
- async def auth_middleware(request, call_next):
- """Enforce authentication on all API routes when auth is enabled.
- This middleware provides defense-in-depth by checking auth at the API gateway level,
- regardless of whether individual routes have auth dependencies.
- """
- from starlette.responses import JSONResponse
- path = request.url.path
- # Only apply to API routes
- if not path.startswith("/api/"):
- return await call_next(request)
- # Allow public routes
- if path in PUBLIC_API_ROUTES:
- return await call_next(request)
- # Allow public prefixes
- for prefix in PUBLIC_API_PREFIXES:
- if path.startswith(prefix):
- return await call_next(request)
- # Allow public patterns (read-only display data like thumbnails)
- for pattern in PUBLIC_API_PATTERNS:
- if pattern in path:
- return await call_next(request)
- # Check if auth is enabled
- try:
- async with async_session() as db:
- from backend.app.core.auth import is_auth_enabled
- auth_enabled = await is_auth_enabled(db)
- if not auth_enabled:
- # Auth disabled, allow all requests
- return await call_next(request)
- except Exception:
- # If we can't check auth status, allow request (fail open for DB issues)
- return await call_next(request)
- # Auth is enabled - require valid token
- auth_header = request.headers.get("Authorization")
- x_api_key = request.headers.get("X-API-Key")
- # Check for API key auth first
- if x_api_key or (auth_header and auth_header.startswith("Bearer bb_")):
- # API key authentication - let the request through to be validated by route handler
- # API keys are validated per-route since they have different permission levels
- return await call_next(request)
- # Check for JWT auth
- if not auth_header or not auth_header.startswith("Bearer "):
- return JSONResponse(
- status_code=401,
- content={"detail": "Authentication required"},
- headers={"WWW-Authenticate": "Bearer"},
- )
- # Validate JWT token
- import jwt
- try:
- from backend.app.core.auth import ALGORITHM, SECRET_KEY
- token = auth_header.replace("Bearer ", "")
- payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
- username = payload.get("sub")
- if not username:
- raise ValueError("No username in token")
- # Verify user exists and is active
- async with async_session() as db:
- from backend.app.core.auth import get_user_by_username
- user = await get_user_by_username(db, username)
- if not user or not user.is_active:
- return JSONResponse(
- status_code=401,
- content={"detail": "User not found or inactive"},
- headers={"WWW-Authenticate": "Bearer"},
- )
- except jwt.ExpiredSignatureError:
- return JSONResponse(
- status_code=401,
- content={"detail": "Token has expired"},
- headers={"WWW-Authenticate": "Bearer"},
- )
- except (jwt.InvalidTokenError, ValueError, Exception):
- return JSONResponse(
- status_code=401,
- content={"detail": "Invalid token"},
- headers={"WWW-Authenticate": "Bearer"},
- )
- return await call_next(request)
- # API routes
- app.include_router(auth.router, prefix=app_settings.api_prefix)
- app.include_router(users.router, prefix=app_settings.api_prefix)
- app.include_router(groups.router, prefix=app_settings.api_prefix)
- app.include_router(printers.router, prefix=app_settings.api_prefix)
- app.include_router(archives.router, prefix=app_settings.api_prefix)
- app.include_router(filaments.router, prefix=app_settings.api_prefix)
- app.include_router(settings_routes.router, prefix=app_settings.api_prefix)
- app.include_router(cloud.router, prefix=app_settings.api_prefix)
- app.include_router(smart_plugs.router, prefix=app_settings.api_prefix)
- app.include_router(print_queue.router, prefix=app_settings.api_prefix)
- app.include_router(kprofiles.router, prefix=app_settings.api_prefix)
- app.include_router(notifications.router, prefix=app_settings.api_prefix)
- app.include_router(notification_templates.router, prefix=app_settings.api_prefix)
- app.include_router(spoolman.router, prefix=app_settings.api_prefix)
- app.include_router(updates.router, prefix=app_settings.api_prefix)
- app.include_router(maintenance.router, prefix=app_settings.api_prefix)
- app.include_router(camera.router, prefix=app_settings.api_prefix)
- app.include_router(external_links.router, prefix=app_settings.api_prefix)
- app.include_router(projects.router, prefix=app_settings.api_prefix)
- app.include_router(library.router, prefix=app_settings.api_prefix)
- app.include_router(api_keys.router, prefix=app_settings.api_prefix)
- app.include_router(webhook.router, prefix=app_settings.api_prefix)
- app.include_router(ams_history.router, prefix=app_settings.api_prefix)
- app.include_router(system.router, prefix=app_settings.api_prefix)
- app.include_router(support.router, prefix=app_settings.api_prefix)
- app.include_router(websocket.router, prefix=app_settings.api_prefix)
- app.include_router(discovery.router, prefix=app_settings.api_prefix)
- app.include_router(pending_uploads.router, prefix=app_settings.api_prefix)
- app.include_router(firmware.router, prefix=app_settings.api_prefix)
- app.include_router(github_backup.router, prefix=app_settings.api_prefix)
- app.include_router(metrics.router, prefix=app_settings.api_prefix)
- # Serve static files (React build)
- if app_settings.static_dir.exists() and any(app_settings.static_dir.iterdir()):
- app.mount(
- "/assets",
- StaticFiles(directory=app_settings.static_dir / "assets"),
- name="assets",
- )
- if (app_settings.static_dir / "img").exists():
- app.mount(
- "/img",
- StaticFiles(directory=app_settings.static_dir / "img"),
- name="img",
- )
- if (app_settings.static_dir / "icons").exists():
- app.mount(
- "/icons",
- StaticFiles(directory=app_settings.static_dir / "icons"),
- name="icons",
- )
- @app.get("/")
- async def serve_frontend():
- """Serve the React frontend."""
- index_file = app_settings.static_dir / "index.html"
- if index_file.exists():
- return FileResponse(index_file)
- return {
- "message": "Bambuddy API",
- "docs": "/docs",
- "frontend": "Build and place React app in /static directory",
- }
- @app.get("/health")
- async def health_check():
- """Health check endpoint."""
- return {"status": "healthy"}
- @app.get("/manifest.json")
- async def serve_manifest():
- """Serve PWA manifest."""
- manifest_file = app_settings.static_dir / "manifest.json"
- if manifest_file.exists():
- return FileResponse(manifest_file, media_type="application/manifest+json")
- return {"error": "Manifest not found"}
- @app.get("/sw.js")
- async def serve_service_worker():
- """Serve service worker."""
- sw_file = app_settings.static_dir / "sw.js"
- if sw_file.exists():
- return FileResponse(sw_file, media_type="application/javascript")
- return {"error": "Service worker not found"}
- # Catch-all route for React Router (must be last)
- @app.get("/{full_path:path}")
- async def serve_spa(full_path: str):
- """Serve React app for client-side routing."""
- # Don't intercept API routes - raise proper 404 so FastAPI can handle redirects
- if full_path.startswith("api/"):
- from fastapi import HTTPException
- raise HTTPException(status_code=404, detail="Not found")
- index_file = app_settings.static_dir / "index.html"
- if index_file.exists():
- return FileResponse(index_file)
- return {"error": "Frontend not built"}
|