|
@@ -1,21 +1,19 @@
|
|
|
import asyncio
|
|
import asyncio
|
|
|
import logging
|
|
import logging
|
|
|
-import os
|
|
|
|
|
-from datetime import datetime, timedelta
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
from contextlib import asynccontextmanager
|
|
|
-from pathlib import Path
|
|
|
|
|
|
|
+from datetime import datetime, timedelta
|
|
|
from logging.handlers import RotatingFileHandler
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi import FastAPI
|
|
|
|
|
|
|
|
# Import settings first for logging configuration
|
|
# Import settings first for logging configuration
|
|
|
-from backend.app.core.config import settings as app_settings, APP_VERSION
|
|
|
|
|
|
|
+from backend.app.core.config import APP_VERSION, settings as app_settings
|
|
|
|
|
|
|
|
# Configure logging based on settings
|
|
# Configure logging based on settings
|
|
|
# DEBUG=true -> DEBUG level, else use LOG_LEVEL setting
|
|
# 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_str = "DEBUG" if app_settings.debug else app_settings.log_level.upper()
|
|
|
log_level = getattr(logging, log_level_str, logging.INFO)
|
|
log_level = getattr(logging, log_level_str, logging.INFO)
|
|
|
-log_format = '%(asctime)s %(levelname)s [%(name)s] %(message)s'
|
|
|
|
|
|
|
+log_format = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
|
|
|
|
|
|
|
|
# Create root logger
|
|
# Create root logger
|
|
|
root_logger = logging.getLogger()
|
|
root_logger = logging.getLogger()
|
|
@@ -32,9 +30,9 @@ if app_settings.log_to_file:
|
|
|
log_file = app_settings.log_dir / "bambuddy.log"
|
|
log_file = app_settings.log_dir / "bambuddy.log"
|
|
|
file_handler = RotatingFileHandler(
|
|
file_handler = RotatingFileHandler(
|
|
|
log_file,
|
|
log_file,
|
|
|
- maxBytes=5*1024*1024, # 5MB
|
|
|
|
|
|
|
+ maxBytes=5 * 1024 * 1024, # 5MB
|
|
|
backupCount=3,
|
|
backupCount=3,
|
|
|
- encoding='utf-8'
|
|
|
|
|
|
|
+ encoding="utf-8",
|
|
|
)
|
|
)
|
|
|
file_handler.setLevel(log_level)
|
|
file_handler.setLevel(log_level)
|
|
|
file_handler.setFormatter(logging.Formatter(log_format))
|
|
file_handler.setFormatter(logging.Formatter(log_format))
|
|
@@ -48,32 +46,52 @@ if not app_settings.debug:
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
|
|
|
|
|
logging.info(f"Bambuddy starting - debug={app_settings.debug}, log_level={log_level_str}")
|
|
logging.info(f"Bambuddy starting - debug={app_settings.debug}, log_level={log_level_str}")
|
|
|
-from fastapi.staticfiles import StaticFiles
|
|
|
|
|
from fastapi.responses import FileResponse
|
|
from fastapi.responses import FileResponse
|
|
|
-
|
|
|
|
|
-from backend.app.core.database import init_db, async_session
|
|
|
|
|
-from sqlalchemy import select, or_, delete
|
|
|
|
|
|
|
+from fastapi.staticfiles import StaticFiles
|
|
|
|
|
+from sqlalchemy import delete, or_, select
|
|
|
|
|
+
|
|
|
|
|
+from backend.app.api.routes import (
|
|
|
|
|
+ ams_history,
|
|
|
|
|
+ api_keys,
|
|
|
|
|
+ archives,
|
|
|
|
|
+ camera,
|
|
|
|
|
+ cloud,
|
|
|
|
|
+ external_links,
|
|
|
|
|
+ filaments,
|
|
|
|
|
+ kprofiles,
|
|
|
|
|
+ maintenance,
|
|
|
|
|
+ notification_templates,
|
|
|
|
|
+ notifications,
|
|
|
|
|
+ print_queue,
|
|
|
|
|
+ printers,
|
|
|
|
|
+ projects,
|
|
|
|
|
+ settings as settings_routes,
|
|
|
|
|
+ smart_plugs,
|
|
|
|
|
+ spoolman,
|
|
|
|
|
+ system,
|
|
|
|
|
+ updates,
|
|
|
|
|
+ webhook,
|
|
|
|
|
+ websocket,
|
|
|
|
|
+)
|
|
|
|
|
+from backend.app.api.routes.maintenance import _get_printer_maintenance_internal, ensure_default_types
|
|
|
|
|
+from backend.app.core.database import async_session, init_db
|
|
|
from backend.app.core.websocket import ws_manager
|
|
from backend.app.core.websocket import ws_manager
|
|
|
-from backend.app.api.routes import printers, archives, websocket, filaments, cloud, smart_plugs, print_queue, kprofiles, notifications, notification_templates, spoolman, updates, maintenance, camera, external_links, projects, api_keys, webhook, ams_history, system
|
|
|
|
|
-from backend.app.api.routes import settings as settings_routes
|
|
|
|
|
|
|
+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
|
|
|
|
|
+from backend.app.services.bambu_mqtt import PrinterState
|
|
|
from backend.app.services.notification_service import notification_service
|
|
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 (
|
|
from backend.app.services.printer_manager import (
|
|
|
|
|
+ init_printer_connections,
|
|
|
printer_manager,
|
|
printer_manager,
|
|
|
printer_state_to_dict,
|
|
printer_state_to_dict,
|
|
|
- init_printer_connections,
|
|
|
|
|
)
|
|
)
|
|
|
-from backend.app.services.print_scheduler import scheduler as print_scheduler
|
|
|
|
|
-from backend.app.services.bambu_mqtt import PrinterState
|
|
|
|
|
-from backend.app.services.archive import ArchiveService
|
|
|
|
|
-from backend.app.services.bambu_ftp import download_file_async
|
|
|
|
|
from backend.app.services.smart_plug_manager import smart_plug_manager
|
|
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.tasmota import tasmota_service
|
|
from backend.app.services.tasmota import tasmota_service
|
|
|
-from backend.app.models.smart_plug import SmartPlug
|
|
|
|
|
-from backend.app.services.spoolman import get_spoolman_client, init_spoolman_client, close_spoolman_client
|
|
|
|
|
-from backend.app.api.routes.maintenance import _get_printer_maintenance_internal, ensure_default_types
|
|
|
|
|
from backend.app.services.telemetry import start_telemetry_loop
|
|
from backend.app.services.telemetry import start_telemetry_loop
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# Track active prints: {(printer_id, filename): archive_id}
|
|
# Track active prints: {(printer_id, filename): archive_id}
|
|
|
_active_prints: dict[tuple[int, str], int] = {}
|
|
_active_prints: dict[tuple[int, str], int] = {}
|
|
|
|
|
|
|
@@ -130,13 +148,11 @@ async def _report_spoolman_usage(printer_id: int, archive_id: int, logger):
|
|
|
|
|
|
|
|
# Check if Spoolman is reachable
|
|
# Check if Spoolman is reachable
|
|
|
if not await client.health_check():
|
|
if not await client.health_check():
|
|
|
- logger.warning(f"Spoolman not reachable for usage reporting")
|
|
|
|
|
|
|
+ logger.warning("Spoolman not reachable for usage reporting")
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
# Get archive to find filament usage
|
|
# Get archive to find filament usage
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(PrintArchive).where(PrintArchive.id == archive_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
|
|
|
archive = result.scalar_one_or_none()
|
|
archive = result.scalar_one_or_none()
|
|
|
if not archive or not archive.filament_used_grams:
|
|
if not archive or not archive.filament_used_grams:
|
|
|
logger.debug(f"No filament usage data for archive {archive_id}")
|
|
logger.debug(f"No filament usage data for archive {archive_id}")
|
|
@@ -148,12 +164,12 @@ async def _report_spoolman_usage(printer_id: int, archive_id: int, logger):
|
|
|
# Get current AMS state from printer to find the active spool
|
|
# Get current AMS state from printer to find the active spool
|
|
|
state = printer_manager.get_status(printer_id)
|
|
state = printer_manager.get_status(printer_id)
|
|
|
if not state or not state.raw_data:
|
|
if not state or not state.raw_data:
|
|
|
- logger.debug(f"No printer state available for usage reporting")
|
|
|
|
|
|
|
+ logger.debug("No printer state available for usage reporting")
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
ams_data = state.raw_data.get("ams")
|
|
ams_data = state.raw_data.get("ams")
|
|
|
if not ams_data:
|
|
if not ams_data:
|
|
|
- logger.debug(f"No AMS data available for usage reporting")
|
|
|
|
|
|
|
+ logger.debug("No AMS data available for usage reporting")
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
# Find spools with RFID tags in Spoolman and report usage
|
|
# Find spools with RFID tags in Spoolman and report usage
|
|
@@ -161,7 +177,6 @@ async def _report_spoolman_usage(printer_id: int, archive_id: int, logger):
|
|
|
# TODO: In future, track which specific trays were used during the print
|
|
# TODO: In future, track which specific trays were used during the print
|
|
|
spools_updated = 0
|
|
spools_updated = 0
|
|
|
for ams_unit in ams_data:
|
|
for ams_unit in ams_data:
|
|
|
- ams_id = int(ams_unit.get("id", 0))
|
|
|
|
|
trays = ams_unit.get("tray", [])
|
|
trays = ams_unit.get("tray", [])
|
|
|
|
|
|
|
|
for tray_data in trays:
|
|
for tray_data in trays:
|
|
@@ -176,8 +191,7 @@ async def _report_spoolman_usage(printer_id: int, archive_id: int, logger):
|
|
|
result = await client.use_spool(spool["id"], filament_used)
|
|
result = await client.use_spool(spool["id"], filament_used)
|
|
|
if result:
|
|
if result:
|
|
|
logger.info(
|
|
logger.info(
|
|
|
- f"[SPOOLMAN] Reported {filament_used}g usage to spool {spool['id']} "
|
|
|
|
|
- f"(tag: {tag_uid})"
|
|
|
|
|
|
|
+ f"[SPOOLMAN] Reported {filament_used}g usage to spool {spool['id']} (tag: {tag_uid})"
|
|
|
)
|
|
)
|
|
|
spools_updated += 1
|
|
spools_updated += 1
|
|
|
# Only report to one spool for single-material prints
|
|
# Only report to one spool for single-material prints
|
|
@@ -204,9 +218,8 @@ async def on_printer_status_change(printer_id: int, state: PrinterState):
|
|
|
# Update nozzle_count in database
|
|
# Update nozzle_count in database
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
from backend.app.models.printer import Printer
|
|
from backend.app.models.printer import Printer
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Printer).where(Printer.id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+
|
|
|
|
|
+ result = await db.execute(select(Printer).where(Printer.id == printer_id))
|
|
|
printer = result.scalar_one_or_none()
|
|
printer = result.scalar_one_or_none()
|
|
|
if printer and printer.nozzle_count != 2:
|
|
if printer and printer.nozzle_count != 2:
|
|
|
printer.nozzle_count = 2
|
|
printer.nozzle_count = 2
|
|
@@ -233,6 +246,7 @@ async def on_printer_status_change(printer_id: int, state: PrinterState):
|
|
|
async def on_ams_change(printer_id: int, ams_data: list):
|
|
async def on_ams_change(printer_id: int, ams_data: list):
|
|
|
"""Handle AMS data changes - sync to Spoolman if enabled and auto mode."""
|
|
"""Handle AMS data changes - sync to Spoolman if enabled and auto mode."""
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
@@ -266,9 +280,7 @@ async def on_ams_change(printer_id: int, ams_data: list):
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
# Get printer name for location
|
|
# Get printer name for location
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Printer).where(Printer.id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ result = await db.execute(select(Printer).where(Printer.id == printer_id))
|
|
|
printer = result.scalar_one_or_none()
|
|
printer = result.scalar_one_or_none()
|
|
|
printer_name = printer.name if printer else f"Printer {printer_id}"
|
|
printer_name = printer.name if printer else f"Printer {printer_id}"
|
|
|
|
|
|
|
@@ -295,29 +307,47 @@ async def on_ams_change(printer_id: int, ams_data: list):
|
|
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logging.getLogger(__name__).warning(f"Spoolman AMS sync failed: {e}")
|
|
logging.getLogger(__name__).warning(f"Spoolman AMS sync failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
-async def on_print_start(printer_id: int, data: dict):
|
|
|
|
|
- """Handle print start - archive the 3MF file immediately."""
|
|
|
|
|
- import logging
|
|
|
|
|
- logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
+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:
|
|
|
|
|
+ import logging
|
|
|
|
|
|
|
|
- await ws_manager.send_print_start(printer_id, data)
|
|
|
|
|
|
|
+ logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
- # Send print start notifications FIRST (before any early returns)
|
|
|
|
|
try:
|
|
try:
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
from backend.app.models.printer import Printer
|
|
from backend.app.models.printer import Printer
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Printer).where(Printer.id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+
|
|
|
|
|
+ result = await db.execute(select(Printer).where(Printer.id == printer_id))
|
|
|
printer = result.scalar_one_or_none()
|
|
printer = result.scalar_one_or_none()
|
|
|
printer_name = printer.name if printer else f"Printer {printer_id}"
|
|
printer_name = printer.name if printer else f"Printer {printer_id}"
|
|
|
- await notification_service.on_print_start(printer_id, printer_name, data, db)
|
|
|
|
|
|
|
+ await notification_service.on_print_start(printer_id, printer_name, data, db, archive_data=archive_data)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.warning(f"Notification on_print_start failed: {e}")
|
|
logger.warning(f"Notification on_print_start failed: {e}")
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+async def on_print_start(printer_id: int, data: dict):
|
|
|
|
|
+ """Handle print start - archive the 3MF file immediately."""
|
|
|
|
|
+ import logging
|
|
|
|
|
+
|
|
|
|
|
+ logger = logging.getLogger(__name__)
|
|
|
|
|
+
|
|
|
|
|
+ logger.info(f"[CALLBACK] on_print_start called for printer {printer_id}, data keys: {list(data.keys())}")
|
|
|
|
|
+
|
|
|
|
|
+ await ws_manager.send_print_start(printer_id, data)
|
|
|
|
|
+
|
|
|
|
|
+ # Track if notification was sent (to avoid sending twice)
|
|
|
|
|
+ notification_sent = False
|
|
|
|
|
+
|
|
|
# Smart plug automation: turn on plug when print starts
|
|
# Smart plug automation: turn on plug when print starts
|
|
|
try:
|
|
try:
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
@@ -329,21 +359,29 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
from backend.app.models.printer import Printer
|
|
from backend.app.models.printer import Printer
|
|
|
from backend.app.services.bambu_ftp import list_files_async
|
|
from backend.app.services.bambu_ftp import list_files_async
|
|
|
|
|
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Printer).where(Printer.id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ result = await db.execute(select(Printer).where(Printer.id == printer_id))
|
|
|
printer = result.scalar_one_or_none()
|
|
printer = result.scalar_one_or_none()
|
|
|
|
|
|
|
|
if not printer or not printer.auto_archive:
|
|
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
|
|
return
|
|
|
|
|
|
|
|
# Get the filename and subtask_name
|
|
# Get the filename and subtask_name
|
|
|
filename = data.get("filename", "")
|
|
filename = data.get("filename", "")
|
|
|
subtask_name = data.get("subtask_name", "")
|
|
subtask_name = data.get("subtask_name", "")
|
|
|
|
|
|
|
|
- logger.info(f"Print start detected - filename: {filename}, subtask: {subtask_name}")
|
|
|
|
|
|
|
+ logger.info(f"[CALLBACK] Print start detected - filename: {filename}, subtask: {subtask_name}")
|
|
|
|
|
|
|
|
if not filename and not 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
|
|
return
|
|
|
|
|
|
|
|
# Check if this is an expected print from reprint/scheduled
|
|
# Check if this is an expected print from reprint/scheduled
|
|
@@ -373,12 +411,11 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
if expected_archive_id:
|
|
if expected_archive_id:
|
|
|
# This is a reprint/scheduled print - use existing archive, don't create new one
|
|
# This is a reprint/scheduled print - use existing archive, don't create new one
|
|
|
logger.info(f"Using expected archive {expected_archive_id} for print (skipping duplicate)")
|
|
logger.info(f"Using expected archive {expected_archive_id} for print (skipping duplicate)")
|
|
|
- from backend.app.models.archive import PrintArchive
|
|
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(PrintArchive).where(PrintArchive.id == 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()
|
|
archive = result.scalar_one_or_none()
|
|
|
|
|
|
|
|
if archive:
|
|
if archive:
|
|
@@ -394,17 +431,19 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
|
|
|
|
|
# Set up energy tracking
|
|
# Set up energy tracking
|
|
|
try:
|
|
try:
|
|
|
- plug_result = await db.execute(
|
|
|
|
|
- select(SmartPlug).where(SmartPlug.printer_id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
|
|
|
plug = plug_result.scalar_one_or_none()
|
|
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}")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"[ENERGY] Print start - archive {archive.id}, printer {printer_id}, plug found: {plug is not None}"
|
|
|
|
|
+ )
|
|
|
if plug:
|
|
if plug:
|
|
|
energy = await tasmota_service.get_energy(plug)
|
|
energy = await tasmota_service.get_energy(plug)
|
|
|
logger.info(f"[ENERGY] Energy response from plug: {energy}")
|
|
logger.info(f"[ENERGY] Energy response from plug: {energy}")
|
|
|
if energy and energy.get("total") is not None:
|
|
if energy and energy.get("total") is not None:
|
|
|
_print_energy_start[archive.id] = energy["total"]
|
|
_print_energy_start[archive.id] = energy["total"]
|
|
|
- logger.info(f"[ENERGY] Recorded starting energy for archive {archive.id}: {energy['total']} kWh")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"[ENERGY] Recorded starting energy for archive {archive.id}: {energy['total']} kWh"
|
|
|
|
|
+ )
|
|
|
else:
|
|
else:
|
|
|
logger.warning(f"[ENERGY] No 'total' in energy response for archive {archive.id}")
|
|
logger.warning(f"[ENERGY] No 'total' in energy response for archive {archive.id}")
|
|
|
else:
|
|
else:
|
|
@@ -412,16 +451,24 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.warning(f"Failed to record starting energy: {e}")
|
|
logger.warning(f"Failed to record starting energy: {e}")
|
|
|
|
|
|
|
|
- await ws_manager.send_archive_updated({
|
|
|
|
|
- "id": archive.id,
|
|
|
|
|
- "status": "printing",
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ 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)
|
|
|
|
|
|
|
|
return # Skip creating a new archive
|
|
return # Skip creating a new archive
|
|
|
|
|
|
|
|
# Check if there's already a "printing" archive for this printer/file
|
|
# Check if there's already a "printing" archive for this printer/file
|
|
|
# This prevents duplicates when backend restarts during an active print
|
|
# This prevents duplicates when backend restarts during an active print
|
|
|
from backend.app.models.archive import PrintArchive
|
|
from backend.app.models.archive import PrintArchive
|
|
|
|
|
+
|
|
|
check_name = subtask_name or filename.split("/")[-1].replace(".gcode", "").replace(".3mf", "")
|
|
check_name = subtask_name or filename.split("/")[-1].replace(".gcode", "").replace(".3mf", "")
|
|
|
existing = await db.execute(
|
|
existing = await db.execute(
|
|
|
select(PrintArchive)
|
|
select(PrintArchive)
|
|
@@ -439,17 +486,21 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
# Also set up energy tracking if not already tracked
|
|
# Also set up energy tracking if not already tracked
|
|
|
if existing_archive.id not in _print_energy_start:
|
|
if existing_archive.id not in _print_energy_start:
|
|
|
try:
|
|
try:
|
|
|
- plug_result = await db.execute(
|
|
|
|
|
- select(SmartPlug).where(SmartPlug.printer_id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
|
|
|
plug = plug_result.scalar_one_or_none()
|
|
plug = plug_result.scalar_one_or_none()
|
|
|
if plug:
|
|
if plug:
|
|
|
energy = await tasmota_service.get_energy(plug)
|
|
energy = await tasmota_service.get_energy(plug)
|
|
|
if energy and energy.get("total") is not None:
|
|
if energy and energy.get("total") is not None:
|
|
|
_print_energy_start[existing_archive.id] = energy["total"]
|
|
_print_energy_start[existing_archive.id] = energy["total"]
|
|
|
- logger.info(f"Recorded starting energy for existing archive {existing_archive.id}: {energy['total']} kWh")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"Recorded starting energy for existing archive {existing_archive.id}: {energy['total']} kWh"
|
|
|
|
|
+ )
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.warning(f"Failed to record starting energy for existing archive: {e}")
|
|
logger.warning(f"Failed to record starting energy for existing archive: {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)
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
# Build list of possible 3MF filenames to try
|
|
# Build list of possible 3MF filenames to try
|
|
@@ -543,6 +594,9 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
|
|
|
|
|
if not downloaded_filename or not temp_path:
|
|
if not downloaded_filename or not temp_path:
|
|
|
logger.warning(f"Could not find 3MF file for print: {filename or subtask_name}")
|
|
logger.warning(f"Could not find 3MF file for print: {filename or subtask_name}")
|
|
|
|
|
+ # Send notification without archive data (file not found)
|
|
|
|
|
+ if not notification_sent:
|
|
|
|
|
+ await _send_print_start_notification(printer_id, data, logger=logger)
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
@@ -566,17 +620,19 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
|
|
|
|
|
# Record starting energy from smart plug if available
|
|
# Record starting energy from smart plug if available
|
|
|
try:
|
|
try:
|
|
|
- plug_result = await db.execute(
|
|
|
|
|
- select(SmartPlug).where(SmartPlug.printer_id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
|
|
|
plug = plug_result.scalar_one_or_none()
|
|
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}")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"[ENERGY] Auto-archive print start - archive {archive.id}, printer {printer_id}, plug found: {plug is not None}"
|
|
|
|
|
+ )
|
|
|
if plug:
|
|
if plug:
|
|
|
energy = await tasmota_service.get_energy(plug)
|
|
energy = await tasmota_service.get_energy(plug)
|
|
|
logger.info(f"[ENERGY] Auto-archive energy response: {energy}")
|
|
logger.info(f"[ENERGY] Auto-archive energy response: {energy}")
|
|
|
if energy and energy.get("total") is not None:
|
|
if energy and energy.get("total") is not None:
|
|
|
_print_energy_start[archive.id] = energy["total"]
|
|
_print_energy_start[archive.id] = energy["total"]
|
|
|
- logger.info(f"[ENERGY] Recorded starting energy for archive {archive.id}: {energy['total']} kWh")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"[ENERGY] Recorded starting energy for archive {archive.id}: {energy['total']} kWh"
|
|
|
|
|
+ )
|
|
|
else:
|
|
else:
|
|
|
logger.warning(f"[ENERGY] No 'total' in energy response for archive {archive.id}")
|
|
logger.warning(f"[ENERGY] No 'total' in energy response for archive {archive.id}")
|
|
|
else:
|
|
else:
|
|
@@ -584,30 +640,166 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.warning(f"Failed to record starting energy: {e}")
|
|
logger.warning(f"Failed to record starting energy: {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,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ 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,
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # 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
|
|
|
finally:
|
|
finally:
|
|
|
if temp_path and temp_path.exists():
|
|
if temp_path and temp_path.exists():
|
|
|
temp_path.unlink()
|
|
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.
|
|
|
|
|
+ """
|
|
|
|
|
+ import logging
|
|
|
|
|
+
|
|
|
|
|
+ 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(f"[TIMELAPSE] Archive {archive_id} not found, stopping retries")
|
|
|
|
|
+ return
|
|
|
|
|
+ if archive.timelapse_path:
|
|
|
|
|
+ logger.info(f"[TIMELAPSE] Archive {archive_id} already has timelapse attached, stopping retries")
|
|
|
|
|
+ return
|
|
|
|
|
+ if not archive.printer_id:
|
|
|
|
|
+ logger.warning(f"[TIMELAPSE] Archive {archive_id} has no printer, stopping retries")
|
|
|
|
|
+ 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(f"[TIMELAPSE] Printer not found for archive {archive_id}, stopping retries")
|
|
|
|
|
+ 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(f"[TIMELAPSE] Attempt {attempt}: Found {len(files)} files in {timelapse_path}")
|
|
|
|
|
+ break
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logger.debug(f"[TIMELAPSE] Path {timelapse_path} failed: {e}")
|
|
|
|
|
+ continue
|
|
|
|
|
+
|
|
|
|
|
+ if not files:
|
|
|
|
|
+ logger.info(f"[TIMELAPSE] Attempt {attempt}: No timelapse files found on printer, will retry")
|
|
|
|
|
+ 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(f"[TIMELAPSE] Attempt {attempt}: Found {len(mp4_files)} MP4 files in {found_path}")
|
|
|
|
|
+ for f in mp4_files[:5]: # Log first 5
|
|
|
|
|
+ logger.info(f"[TIMELAPSE] - {f.get('name')}, mtime={f.get('mtime')}")
|
|
|
|
|
+
|
|
|
|
|
+ if not mp4_files:
|
|
|
|
|
+ logger.info(f"[TIMELAPSE] Attempt {attempt}: No MP4 files found, will retry")
|
|
|
|
|
+ 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(f"[TIMELAPSE] Attempt {attempt}: No MP4 files with mtime found, will retry")
|
|
|
|
|
+ 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(f"[TIMELAPSE] Attempt {attempt}: Most recent file: {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(f"[TIMELAPSE] Downloading {file_name} for archive {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(f"[TIMELAPSE] Successfully attached timelapse to archive {archive_id}")
|
|
|
|
|
+ await ws_manager.send_archive_updated({"id": archive_id, "timelapse_attached": True})
|
|
|
|
|
+ return # Success!
|
|
|
|
|
+ else:
|
|
|
|
|
+ logger.warning(f"[TIMELAPSE] Failed to attach timelapse to archive {archive_id}")
|
|
|
|
|
+ else:
|
|
|
|
|
+ logger.warning(f"[TIMELAPSE] Attempt {attempt}: Failed to download, will retry")
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logger.warning(f"[TIMELAPSE] Attempt {attempt} failed with error: {e}")
|
|
|
|
|
+
|
|
|
|
|
+ logger.warning(f"[TIMELAPSE] All {len(retry_delays)} attempts exhausted for archive {archive_id}, giving up")
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
async def on_print_complete(printer_id: int, data: dict):
|
|
async def on_print_complete(printer_id: int, data: dict):
|
|
|
"""Handle print completion - update the archive status."""
|
|
"""Handle print completion - update the archive status."""
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
- await ws_manager.send_print_complete(printer_id, data)
|
|
|
|
|
|
|
+ logger.info(f"[CALLBACK] on_print_complete started for printer {printer_id}")
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ # Only send necessary fields to WebSocket (not raw_data which can be large)
|
|
|
|
|
+ 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)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logger.warning(f"[CALLBACK] WebSocket send_print_complete failed: {e}")
|
|
|
|
|
|
|
|
filename = data.get("filename", "")
|
|
filename = data.get("filename", "")
|
|
|
subtask_name = data.get("subtask_name", "")
|
|
subtask_name = data.get("subtask_name", "")
|
|
|
|
|
|
|
|
if not filename and not subtask_name:
|
|
if not filename and not subtask_name:
|
|
|
- logger.warning(f"Print complete without filename or subtask_name")
|
|
|
|
|
|
|
+ logger.warning("Print complete without filename or subtask_name")
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
logger.info(f"Print complete - filename: {filename}, subtask: {subtask_name}, status: {data.get('status')}")
|
|
logger.info(f"Print complete - filename: {filename}, subtask: {subtask_name}, status: {data.get('status')}")
|
|
@@ -674,10 +866,12 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
select(PrintArchive)
|
|
select(PrintArchive)
|
|
|
.where(PrintArchive.printer_id == printer_id)
|
|
.where(PrintArchive.printer_id == printer_id)
|
|
|
.where(PrintArchive.status == "printing")
|
|
.where(PrintArchive.status == "printing")
|
|
|
- .where(or_(
|
|
|
|
|
- PrintArchive.print_name.ilike(f"%{subtask_name}%"),
|
|
|
|
|
- PrintArchive.filename.ilike(f"%{subtask_name}%"),
|
|
|
|
|
- ))
|
|
|
|
|
|
|
+ .where(
|
|
|
|
|
+ or_(
|
|
|
|
|
+ PrintArchive.print_name.ilike(f"%{subtask_name}%"),
|
|
|
|
|
+ PrintArchive.filename.ilike(f"%{subtask_name}%"),
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
.order_by(PrintArchive.created_at.desc())
|
|
.order_by(PrintArchive.created_at.desc())
|
|
|
.limit(1)
|
|
.limit(1)
|
|
|
)
|
|
)
|
|
@@ -705,19 +899,58 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
# Update archive status
|
|
# Update archive status
|
|
|
- async with async_session() as db:
|
|
|
|
|
- service = ArchiveService(db)
|
|
|
|
|
- status = data.get("status", "completed")
|
|
|
|
|
- await service.update_archive_status(
|
|
|
|
|
- archive_id,
|
|
|
|
|
- status=status,
|
|
|
|
|
- completed_at=datetime.now() if status in ("completed", "failed", "aborted") else None,
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ logger.info(f"[ARCHIVE] Updating archive {archive_id} status...")
|
|
|
|
|
+ try:
|
|
|
|
|
+ async with async_session() as db:
|
|
|
|
|
+ service = ArchiveService(db)
|
|
|
|
|
+ status = data.get("status", "completed")
|
|
|
|
|
|
|
|
- await ws_manager.send_archive_updated({
|
|
|
|
|
- "id": archive_id,
|
|
|
|
|
- "status": status,
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ # 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(f"[ARCHIVE] HMS errors at failure: {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(f"[ARCHIVE] Detected failure_reason from HMS: {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(f"[ARCHIVE] Archive {archive_id} status updated to {status}, failure_reason={failure_reason}")
|
|
|
|
|
+
|
|
|
|
|
+ await ws_manager.send_archive_updated(
|
|
|
|
|
+ {
|
|
|
|
|
+ "id": archive_id,
|
|
|
|
|
+ "status": status,
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ logger.info(f"[ARCHIVE] WebSocket notification sent for archive {archive_id}")
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ logger.error(f"[ARCHIVE] Failed to update archive {archive_id} status: {e}", exc_info=True)
|
|
|
|
|
+ # Continue with other operations even if archive update fails
|
|
|
|
|
|
|
|
# Report filament usage to Spoolman if print completed successfully
|
|
# Report filament usage to Spoolman if print completed successfully
|
|
|
if data.get("status") == "completed":
|
|
if data.get("status") == "completed":
|
|
@@ -733,9 +966,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
|
|
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
# Get smart plug for this printer (SmartPlug is imported at module level)
|
|
# Get smart plug for this printer (SmartPlug is imported at module level)
|
|
|
- plug_result = await db.execute(
|
|
|
|
|
- select(SmartPlug).where(SmartPlug.printer_id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ plug_result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
|
|
|
plug = plug_result.scalar_one_or_none()
|
|
plug = plug_result.scalar_one_or_none()
|
|
|
|
|
|
|
|
if plug:
|
|
if plug:
|
|
@@ -748,24 +979,26 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
if starting_kwh is not None and energy and energy.get("total") is not None:
|
|
if starting_kwh is not None and energy and energy.get("total") is not None:
|
|
|
ending_kwh = energy["total"]
|
|
ending_kwh = energy["total"]
|
|
|
energy_used = round(ending_kwh - starting_kwh, 4)
|
|
energy_used = round(ending_kwh - starting_kwh, 4)
|
|
|
- logger.info(f"[ENERGY] Per-print energy: ending={ending_kwh}, starting={starting_kwh}, used={energy_used}")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"[ENERGY] Per-print energy: ending={ending_kwh}, starting={starting_kwh}, used={energy_used}"
|
|
|
|
|
+ )
|
|
|
elif starting_kwh is None:
|
|
elif starting_kwh is None:
|
|
|
- logger.info(f"[ENERGY] No starting energy recorded for this archive")
|
|
|
|
|
|
|
+ logger.info("[ENERGY] No starting energy recorded for this archive")
|
|
|
else:
|
|
else:
|
|
|
- logger.warning(f"[ENERGY] No 'total' in ending energy response")
|
|
|
|
|
|
|
+ logger.warning("[ENERGY] No 'total' in ending energy response")
|
|
|
|
|
|
|
|
if energy_used is not None and energy_used >= 0:
|
|
if energy_used is not None and energy_used >= 0:
|
|
|
# Get energy cost per kWh from settings (default to 0.15)
|
|
# Get energy cost per kWh from settings (default to 0.15)
|
|
|
from backend.app.api.routes.settings import get_setting
|
|
from backend.app.api.routes.settings import get_setting
|
|
|
|
|
+
|
|
|
energy_cost_per_kwh = await get_setting(db, "energy_cost_per_kwh")
|
|
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
|
|
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)
|
|
energy_cost = round(energy_used * cost_per_kwh, 2)
|
|
|
|
|
|
|
|
# Update archive with energy data
|
|
# Update archive with energy data
|
|
|
from backend.app.models.archive import PrintArchive
|
|
from backend.app.models.archive import PrintArchive
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(PrintArchive).where(PrintArchive.id == archive_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+
|
|
|
|
|
+ result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
|
|
|
archive = result.scalar_one_or_none()
|
|
archive = result.scalar_one_or_none()
|
|
|
if archive:
|
|
if archive:
|
|
|
archive.energy_kwh = energy_used
|
|
archive.energy_kwh = energy_used
|
|
@@ -778,6 +1011,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
logger.info(f"[ENERGY] No smart plug found for printer {printer_id} at print complete")
|
|
logger.info(f"[ENERGY] No smart plug found for printer {printer_id} at print complete")
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logging.getLogger(__name__).warning(f"Failed to calculate energy: {e}")
|
|
logging.getLogger(__name__).warning(f"Failed to calculate energy: {e}")
|
|
|
|
|
|
|
|
# Capture finish photo from printer camera
|
|
# Capture finish photo from printer camera
|
|
@@ -786,28 +1020,28 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
# Check if finish photo capture is enabled
|
|
# Check if finish photo capture is enabled
|
|
|
from backend.app.api.routes.settings import get_setting
|
|
from backend.app.api.routes.settings import get_setting
|
|
|
|
|
+
|
|
|
capture_enabled = await get_setting(db, "capture_finish_photo")
|
|
capture_enabled = await get_setting(db, "capture_finish_photo")
|
|
|
logger.info(f"[PHOTO] capture_finish_photo setting: {capture_enabled}")
|
|
logger.info(f"[PHOTO] capture_finish_photo setting: {capture_enabled}")
|
|
|
if capture_enabled is None or capture_enabled.lower() == "true":
|
|
if capture_enabled is None or capture_enabled.lower() == "true":
|
|
|
# Get printer details
|
|
# Get printer details
|
|
|
from backend.app.models.printer import Printer
|
|
from backend.app.models.printer import Printer
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Printer).where(Printer.id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+
|
|
|
|
|
+ result = await db.execute(select(Printer).where(Printer.id == printer_id))
|
|
|
printer = result.scalar_one_or_none()
|
|
printer = result.scalar_one_or_none()
|
|
|
|
|
|
|
|
if printer and archive_id:
|
|
if printer and archive_id:
|
|
|
# Get archive to find its directory
|
|
# Get archive to find its directory
|
|
|
from backend.app.models.archive import PrintArchive
|
|
from backend.app.models.archive import PrintArchive
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(PrintArchive).where(PrintArchive.id == archive_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+
|
|
|
|
|
+ result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
|
|
|
archive = result.scalar_one_or_none()
|
|
archive = result.scalar_one_or_none()
|
|
|
|
|
|
|
|
if archive:
|
|
if archive:
|
|
|
- from backend.app.services.camera import capture_finish_photo
|
|
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
+ from backend.app.services.camera import capture_finish_photo
|
|
|
|
|
+
|
|
|
archive_dir = app_settings.base_dir / Path(archive.file_path).parent
|
|
archive_dir = app_settings.base_dir / Path(archive.file_path).parent
|
|
|
photo_filename = await capture_finish_photo(
|
|
photo_filename = await capture_finish_photo(
|
|
|
printer_id=printer_id,
|
|
printer_id=printer_id,
|
|
@@ -826,6 +1060,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
logger.info(f"Added finish photo to archive {archive_id}: {photo_filename}")
|
|
logger.info(f"Added finish photo to archive {archive_id}: {photo_filename}")
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logging.getLogger(__name__).warning(f"Finish photo capture failed: {e}")
|
|
logging.getLogger(__name__).warning(f"Finish photo capture failed: {e}")
|
|
|
|
|
|
|
|
# Smart plug automation: schedule turn off when print completes
|
|
# Smart plug automation: schedule turn off when print completes
|
|
@@ -834,19 +1069,19 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
status = data.get("status", "completed")
|
|
status = data.get("status", "completed")
|
|
|
await smart_plug_manager.on_print_complete(printer_id, status, db)
|
|
await smart_plug_manager.on_print_complete(printer_id, status, db)
|
|
|
- logger.info(f"[AUTO-OFF] smart_plug_manager.on_print_complete completed")
|
|
|
|
|
|
|
+ logger.info("[AUTO-OFF] smart_plug_manager.on_print_complete completed")
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logging.getLogger(__name__).warning(f"Smart plug on_print_complete failed: {e}")
|
|
logging.getLogger(__name__).warning(f"Smart plug on_print_complete failed: {e}")
|
|
|
|
|
|
|
|
# Send print complete notifications
|
|
# Send print complete notifications
|
|
|
try:
|
|
try:
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
- from backend.app.models.printer import Printer
|
|
|
|
|
from backend.app.models.archive import PrintArchive
|
|
from backend.app.models.archive import PrintArchive
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Printer).where(Printer.id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ 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 = result.scalar_one_or_none()
|
|
|
printer_name = printer.name if printer else f"Printer {printer_id}"
|
|
printer_name = printer.name if printer else f"Printer {printer_id}"
|
|
|
status = data.get("status", "completed")
|
|
status = data.get("status", "completed")
|
|
@@ -854,9 +1089,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
# Fetch archive data for notification variables
|
|
# Fetch archive data for notification variables
|
|
|
archive_data = None
|
|
archive_data = None
|
|
|
if archive_id:
|
|
if archive_id:
|
|
|
- archive_result = await db.execute(
|
|
|
|
|
- select(PrintArchive).where(PrintArchive.id == archive_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
|
|
|
archive = archive_result.scalar_one_or_none()
|
|
archive = archive_result.scalar_one_or_none()
|
|
|
if archive:
|
|
if archive:
|
|
|
archive_data = {
|
|
archive_data = {
|
|
@@ -871,6 +1104,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
)
|
|
)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logging.getLogger(__name__).warning(f"Notification on_print_complete failed: {e}")
|
|
logging.getLogger(__name__).warning(f"Notification on_print_complete failed: {e}")
|
|
|
|
|
|
|
|
# Check for maintenance due and send notifications (only for completed prints)
|
|
# Check for maintenance due and send notifications (only for completed prints)
|
|
@@ -880,9 +1114,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
from backend.app.models.printer import Printer
|
|
from backend.app.models.printer import Printer
|
|
|
|
|
|
|
|
# Get printer name
|
|
# Get printer name
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Printer).where(Printer.id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ result = await db.execute(select(Printer).where(Printer.id == printer_id))
|
|
|
printer = result.scalar_one_or_none()
|
|
printer = result.scalar_one_or_none()
|
|
|
printer_name = printer.name if printer else f"Printer {printer_id}"
|
|
printer_name = printer.name if printer else f"Printer {printer_id}"
|
|
|
|
|
|
|
@@ -902,17 +1134,23 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
]
|
|
]
|
|
|
|
|
|
|
|
if items_needing_attention:
|
|
if items_needing_attention:
|
|
|
- await notification_service.on_maintenance_due(
|
|
|
|
|
- printer_id, printer_name, items_needing_attention, db
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ await notification_service.on_maintenance_due(printer_id, printer_name, items_needing_attention, db)
|
|
|
logger.info(
|
|
logger.info(
|
|
|
f"Sent maintenance notification for printer {printer_id}: "
|
|
f"Sent maintenance notification for printer {printer_id}: "
|
|
|
f"{len(items_needing_attention)} items need attention"
|
|
f"{len(items_needing_attention)} items need attention"
|
|
|
)
|
|
)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logging.getLogger(__name__).warning(f"Maintenance notification check failed: {e}")
|
|
logging.getLogger(__name__).warning(f"Maintenance notification check failed: {e}")
|
|
|
|
|
|
|
|
|
|
+ # 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(f"[TIMELAPSE] Timelapse was active during print, scheduling auto-scan for archive {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))
|
|
|
|
|
+
|
|
|
# Update queue item if this was a scheduled print
|
|
# Update queue item if this was a scheduled print
|
|
|
try:
|
|
try:
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
@@ -936,9 +1174,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
|
|
|
|
|
# Handle auto_off_after - power off printer if requested (after cooldown)
|
|
# Handle auto_off_after - power off printer if requested (after cooldown)
|
|
|
if queue_item.auto_off_after:
|
|
if queue_item.auto_off_after:
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(SmartPlug).where(SmartPlug.printer_id == printer_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
|
|
|
plug = result.scalar_one_or_none()
|
|
plug = result.scalar_one_or_none()
|
|
|
if plug and plug.enabled:
|
|
if plug and plug.enabled:
|
|
|
logger.info(f"Auto-off requested for printer {printer_id}, waiting for cooldown...")
|
|
logger.info(f"Auto-off requested for printer {printer_id}, waiting for cooldown...")
|
|
@@ -948,9 +1184,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
await printer_manager.wait_for_cooldown(pid, target_temp=50.0, timeout=600)
|
|
await printer_manager.wait_for_cooldown(pid, target_temp=50.0, timeout=600)
|
|
|
# Re-fetch plug in new session
|
|
# Re-fetch plug in new session
|
|
|
async with async_session() as new_db:
|
|
async with async_session() as new_db:
|
|
|
- result = await new_db.execute(
|
|
|
|
|
- select(SmartPlug).where(SmartPlug.id == plug_id)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ result = await new_db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
|
|
|
p = result.scalar_one_or_none()
|
|
p = result.scalar_one_or_none()
|
|
|
if p and p.enabled:
|
|
if p and p.enabled:
|
|
|
success = await tasmota_service.turn_off(p)
|
|
success = await tasmota_service.turn_off(p)
|
|
@@ -962,8 +1196,11 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
asyncio.create_task(cooldown_and_poweroff(printer_id, plug.id))
|
|
asyncio.create_task(cooldown_and_poweroff(printer_id, plug.id))
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logging.getLogger(__name__).warning(f"Queue item update failed: {e}")
|
|
logging.getLogger(__name__).warning(f"Queue item update failed: {e}")
|
|
|
|
|
|
|
|
|
|
+ logger.info(f"[CALLBACK] on_print_complete finished for printer {printer_id}, archive {archive_id}")
|
|
|
|
|
+
|
|
|
|
|
|
|
|
# AMS sensor history recording
|
|
# AMS sensor history recording
|
|
|
_ams_history_task: asyncio.Task | None = None
|
|
_ams_history_task: asyncio.Task | None = None
|
|
@@ -977,6 +1214,7 @@ AMS_ALARM_COOLDOWN_MINUTES = 60 # Don't send same alarm more than once per hour
|
|
|
async def record_ams_history():
|
|
async def record_ams_history():
|
|
|
"""Background task to record AMS humidity and temperature data."""
|
|
"""Background task to record AMS humidity and temperature data."""
|
|
|
import logging
|
|
import logging
|
|
|
|
|
+
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Wait a short time for MQTT connections to establish on startup
|
|
# Wait a short time for MQTT connections to establish on startup
|
|
@@ -990,9 +1228,7 @@ async def record_ams_history():
|
|
|
|
|
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
# Get all active printers
|
|
# Get all active printers
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Printer).where(Printer.is_active == True)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ result = await db.execute(select(Printer).where(Printer.is_active.is_(True)))
|
|
|
printers = result.scalars().all()
|
|
printers = result.scalars().all()
|
|
|
|
|
|
|
|
# Get alarm thresholds from settings
|
|
# Get alarm thresholds from settings
|
|
@@ -1079,9 +1315,14 @@ async def record_ams_history():
|
|
|
cooldown_key = f"{printer.id}:{ams_id}:humidity"
|
|
cooldown_key = f"{printer.id}:{ams_id}:humidity"
|
|
|
last_alarm = _ams_alarm_cooldown.get(cooldown_key)
|
|
last_alarm = _ams_alarm_cooldown.get(cooldown_key)
|
|
|
now = datetime.now()
|
|
now = datetime.now()
|
|
|
- if last_alarm is None or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60:
|
|
|
|
|
|
|
+ if (
|
|
|
|
|
+ last_alarm is None
|
|
|
|
|
+ or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60
|
|
|
|
|
+ ):
|
|
|
_ams_alarm_cooldown[cooldown_key] = now
|
|
_ams_alarm_cooldown[cooldown_key] = now
|
|
|
- logger.info(f"Sending humidity alarm for {printer.name} {ams_label}: {humidity}% > {humidity_threshold}%")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"Sending humidity alarm for {printer.name} {ams_label}: {humidity}% > {humidity_threshold}%"
|
|
|
|
|
+ )
|
|
|
try:
|
|
try:
|
|
|
# Call different notification method based on AMS type
|
|
# Call different notification method based on AMS type
|
|
|
if is_ams_ht:
|
|
if is_ams_ht:
|
|
@@ -1100,9 +1341,14 @@ async def record_ams_history():
|
|
|
cooldown_key = f"{printer.id}:{ams_id}:temperature"
|
|
cooldown_key = f"{printer.id}:{ams_id}:temperature"
|
|
|
last_alarm = _ams_alarm_cooldown.get(cooldown_key)
|
|
last_alarm = _ams_alarm_cooldown.get(cooldown_key)
|
|
|
now = datetime.now()
|
|
now = datetime.now()
|
|
|
- if last_alarm is None or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60:
|
|
|
|
|
|
|
+ if (
|
|
|
|
|
+ last_alarm is None
|
|
|
|
|
+ or (now - last_alarm).total_seconds() >= AMS_ALARM_COOLDOWN_MINUTES * 60
|
|
|
|
|
+ ):
|
|
|
_ams_alarm_cooldown[cooldown_key] = now
|
|
_ams_alarm_cooldown[cooldown_key] = now
|
|
|
- logger.info(f"Sending temperature alarm for {printer.name} {ams_label}: {temperature}°C > {temp_threshold}°C")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"Sending temperature alarm for {printer.name} {ams_label}: {temperature}°C > {temp_threshold}°C"
|
|
|
|
|
+ )
|
|
|
try:
|
|
try:
|
|
|
# Call different notification method based on AMS type
|
|
# Call different notification method based on AMS type
|
|
|
if is_ams_ht:
|
|
if is_ams_ht:
|
|
@@ -1127,19 +1373,18 @@ async def record_ams_history():
|
|
|
_ams_cleanup_counter = 0
|
|
_ams_cleanup_counter = 0
|
|
|
# Get retention days from settings
|
|
# Get retention days from settings
|
|
|
from backend.app.models.settings import Settings
|
|
from backend.app.models.settings import Settings
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(Settings).where(Settings.key == "ams_history_retention_days")
|
|
|
|
|
- )
|
|
|
|
|
|
|
+
|
|
|
|
|
+ result = await db.execute(select(Settings).where(Settings.key == "ams_history_retention_days"))
|
|
|
setting = result.scalar_one_or_none()
|
|
setting = result.scalar_one_or_none()
|
|
|
retention_days = int(setting.value) if setting else AMS_HISTORY_RETENTION_DAYS
|
|
retention_days = int(setting.value) if setting else AMS_HISTORY_RETENTION_DAYS
|
|
|
|
|
|
|
|
cutoff = datetime.now() - timedelta(days=retention_days)
|
|
cutoff = datetime.now() - timedelta(days=retention_days)
|
|
|
- result = await db.execute(
|
|
|
|
|
- delete(AMSSensorHistory).where(AMSSensorHistory.recorded_at < cutoff)
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ result = await db.execute(delete(AMSSensorHistory).where(AMSSensorHistory.recorded_at < cutoff))
|
|
|
await db.commit()
|
|
await db.commit()
|
|
|
if result.rowcount > 0:
|
|
if result.rowcount > 0:
|
|
|
- logger.info(f"Cleaned up {result.rowcount} old AMS sensor history entries (older than {retention_days} days)")
|
|
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ f"Cleaned up {result.rowcount} old AMS sensor history entries (older than {retention_days} days)"
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
# Wait until next recording interval
|
|
# Wait until next recording interval
|
|
|
await asyncio.sleep(AMS_HISTORY_INTERVAL)
|
|
await asyncio.sleep(AMS_HISTORY_INTERVAL)
|
|
@@ -1188,6 +1433,7 @@ async def lifespan(app: FastAPI):
|
|
|
# Auto-connect to Spoolman if enabled
|
|
# Auto-connect to Spoolman if enabled
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
from backend.app.api.routes.settings import get_setting
|
|
from backend.app.api.routes.settings import get_setting
|
|
|
|
|
+
|
|
|
spoolman_enabled = await get_setting(db, "spoolman_enabled")
|
|
spoolman_enabled = await get_setting(db, "spoolman_enabled")
|
|
|
spoolman_url = await get_setting(db, "spoolman_url")
|
|
spoolman_url = await get_setting(db, "spoolman_url")
|
|
|
|
|
|