|
@@ -1,5 +1,4 @@
|
|
|
import asyncio
|
|
import asyncio
|
|
|
-import json
|
|
|
|
|
import logging
|
|
import logging
|
|
|
from contextlib import asynccontextmanager
|
|
from contextlib import asynccontextmanager
|
|
|
from datetime import datetime, timedelta, timezone
|
|
from datetime import datetime, timedelta, timezone
|
|
@@ -225,6 +224,11 @@ from backend.app.services.printer_manager import (
|
|
|
)
|
|
)
|
|
|
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.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
|
|
from backend.app.services.tasmota import tasmota_service
|
|
|
|
|
|
|
|
# Track active prints: {(printer_id, filename): archive_id}
|
|
# Track active prints: {(printer_id, filename): archive_id}
|
|
@@ -295,466 +299,6 @@ _last_status_broadcast: dict[int, str] = {}
|
|
|
_nozzle_count_updated: set[int] = set() # Track printers where we've updated nozzle_count
|
|
_nozzle_count_updated: set[int] = set() # Track printers where we've updated nozzle_count
|
|
|
|
|
|
|
|
|
|
|
|
|
-def _build_ams_tray_lookup(raw_data: dict) -> dict[int, dict]:
|
|
|
|
|
- """Build lookup of global_tray_id -> tray info from printer state.
|
|
|
|
|
-
|
|
|
|
|
- Returns: {0: {"tray_uuid": "...", "tag_uid": "..."}, 1: {...}, ...}
|
|
|
|
|
- """
|
|
|
|
|
- lookup = {}
|
|
|
|
|
- ams_data = raw_data.get("ams", [])
|
|
|
|
|
- for ams_unit in ams_data:
|
|
|
|
|
- ams_id = ams_unit.get("id", 0)
|
|
|
|
|
- for tray in ams_unit.get("tray", []):
|
|
|
|
|
- tray_id = tray.get("id", 0)
|
|
|
|
|
- global_tray_id = ams_id * 4 + tray_id
|
|
|
|
|
- lookup[global_tray_id] = {
|
|
|
|
|
- "tray_uuid": tray.get("tray_uuid", ""),
|
|
|
|
|
- "tag_uid": tray.get("tag_uid", ""),
|
|
|
|
|
- "tray_type": tray.get("tray_type", ""),
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- # External spool (global_tray_id = 254)
|
|
|
|
|
- vt_tray = raw_data.get("vt_tray")
|
|
|
|
|
- if vt_tray and vt_tray.get("tray_type"):
|
|
|
|
|
- lookup[254] = {
|
|
|
|
|
- "tray_uuid": vt_tray.get("tray_uuid", ""),
|
|
|
|
|
- "tag_uid": vt_tray.get("tag_uid", ""),
|
|
|
|
|
- "tray_type": vt_tray.get("tray_type", ""),
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return lookup
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-async def _store_spoolman_print_data(printer_id: int, archive_id: int, file_path: str, db, logger):
|
|
|
|
|
- """Store Spoolman tracking data at print start (persisted to database)."""
|
|
|
|
|
- from backend.app.api.routes.settings import get_setting
|
|
|
|
|
- from backend.app.models.active_print_spoolman import ActivePrintSpoolman
|
|
|
|
|
- from backend.app.utils.threemf_tools import (
|
|
|
|
|
- extract_filament_properties_from_3mf,
|
|
|
|
|
- extract_filament_usage_from_3mf,
|
|
|
|
|
- extract_layer_filament_usage_from_3mf,
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
- # Check if Spoolman is enabled
|
|
|
|
|
- spoolman_enabled = await get_setting(db, "spoolman_enabled")
|
|
|
|
|
- if not spoolman_enabled or spoolman_enabled.lower() != "true":
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Only store tracking data if "Disable AMS Weight Sync" is enabled
|
|
|
|
|
- # (otherwise we're using AMS percentage-based estimates, not per-usage tracking)
|
|
|
|
|
- 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"
|
|
|
|
|
- if not disable_weight_sync:
|
|
|
|
|
- logger.debug("[SPOOLMAN] Weight sync enabled, skipping per-usage tracking data storage")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Get 3MF file path
|
|
|
|
|
- full_path = app_settings.base_dir / file_path
|
|
|
|
|
- if not full_path.exists():
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] 3MF file not found: {full_path}")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Extract per-filament usage from 3MF (total usage per slot)
|
|
|
|
|
- filament_usage = extract_filament_usage_from_3mf(full_path)
|
|
|
|
|
- if not filament_usage:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] No filament usage data in 3MF for archive {archive_id}")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Get current AMS tray state
|
|
|
|
|
- state = printer_manager.get_status(printer_id)
|
|
|
|
|
- ams_trays = {}
|
|
|
|
|
- if state and state.raw_data:
|
|
|
|
|
- ams_trays = _build_ams_tray_lookup(state.raw_data)
|
|
|
|
|
-
|
|
|
|
|
- # Get custom slot-to-tray mapping from queue item (if this is a queued print)
|
|
|
|
|
- slot_to_tray = None
|
|
|
|
|
- from backend.app.models.print_queue import PrintQueueItem
|
|
|
|
|
-
|
|
|
|
|
- queue_result = await db.execute(
|
|
|
|
|
- select(PrintQueueItem).where(PrintQueueItem.archive_id == archive_id).where(PrintQueueItem.status == "printing")
|
|
|
|
|
- )
|
|
|
|
|
- queue_item = queue_result.scalar_one_or_none()
|
|
|
|
|
- if queue_item and queue_item.ams_mapping:
|
|
|
|
|
- try:
|
|
|
|
|
- slot_to_tray = json.loads(queue_item.ams_mapping)
|
|
|
|
|
- except json.JSONDecodeError:
|
|
|
|
|
- pass
|
|
|
|
|
-
|
|
|
|
|
- # Parse G-code for per-layer filament usage (for accurate partial usage tracking)
|
|
|
|
|
- layer_usage = extract_layer_filament_usage_from_3mf(full_path)
|
|
|
|
|
- layer_usage_json = None
|
|
|
|
|
- if layer_usage:
|
|
|
|
|
- # Convert int keys to string for JSON serialization
|
|
|
|
|
- layer_usage_json = {str(k): v for k, v in layer_usage.items()}
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Parsed {len(layer_usage)} layers from G-code")
|
|
|
|
|
-
|
|
|
|
|
- # Extract filament properties (density, diameter) for mm -> grams conversion
|
|
|
|
|
- filament_properties = extract_filament_properties_from_3mf(full_path)
|
|
|
|
|
-
|
|
|
|
|
- # Delete any existing row for this printer/archive (shouldn't exist, but just in case)
|
|
|
|
|
- from sqlalchemy import delete
|
|
|
|
|
-
|
|
|
|
|
- await db.execute(
|
|
|
|
|
- delete(ActivePrintSpoolman)
|
|
|
|
|
- .where(ActivePrintSpoolman.printer_id == printer_id)
|
|
|
|
|
- .where(ActivePrintSpoolman.archive_id == archive_id)
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
- # Insert new tracking data
|
|
|
|
|
- tracking = ActivePrintSpoolman(
|
|
|
|
|
- printer_id=printer_id,
|
|
|
|
|
- archive_id=archive_id,
|
|
|
|
|
- filament_usage=filament_usage,
|
|
|
|
|
- ams_trays=ams_trays,
|
|
|
|
|
- slot_to_tray=slot_to_tray,
|
|
|
|
|
- layer_usage=layer_usage_json,
|
|
|
|
|
- filament_properties=filament_properties,
|
|
|
|
|
- )
|
|
|
|
|
- db.add(tracking)
|
|
|
|
|
- await db.commit()
|
|
|
|
|
-
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Stored tracking data for print: printer={printer_id}, archive={archive_id}")
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Filament usage: {filament_usage}")
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] AMS trays: {list(ams_trays.keys())}")
|
|
|
|
|
- if slot_to_tray:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Custom slot mapping: {slot_to_tray}")
|
|
|
|
|
- if layer_usage_json:
|
|
|
|
|
- logger.debug("[SPOOLMAN] Layer usage data available for partial tracking")
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-async def _cleanup_spoolman_tracking(printer_id: int, archive_id: int, db, logger):
|
|
|
|
|
- """Report partial usage and clean up Spoolman tracking data for failed/aborted prints."""
|
|
|
|
|
- from sqlalchemy import delete, select
|
|
|
|
|
-
|
|
|
|
|
- from backend.app.models.active_print_spoolman import ActivePrintSpoolman
|
|
|
|
|
-
|
|
|
|
|
- # Get tracking data first (needed for partial usage reporting)
|
|
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(ActivePrintSpoolman)
|
|
|
|
|
- .where(ActivePrintSpoolman.printer_id == printer_id)
|
|
|
|
|
- .where(ActivePrintSpoolman.archive_id == archive_id)
|
|
|
|
|
- )
|
|
|
|
|
- tracking = result.scalar_one_or_none()
|
|
|
|
|
-
|
|
|
|
|
- if not tracking:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] No tracking data to clean up for printer={printer_id}, archive={archive_id}")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Try to report partial usage before cleanup
|
|
|
|
|
- try:
|
|
|
|
|
- await _report_partial_spoolman_usage(printer_id, tracking, logger)
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- logger.warning(f"[SPOOLMAN] Partial usage report failed: {e}")
|
|
|
|
|
-
|
|
|
|
|
- # Delete tracking data
|
|
|
|
|
- await db.execute(
|
|
|
|
|
- delete(ActivePrintSpoolman)
|
|
|
|
|
- .where(ActivePrintSpoolman.printer_id == printer_id)
|
|
|
|
|
- .where(ActivePrintSpoolman.archive_id == archive_id)
|
|
|
|
|
- )
|
|
|
|
|
- await db.commit()
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Cleaned up tracking data for printer={printer_id}, archive={archive_id}")
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-async def _report_partial_spoolman_usage(printer_id: int, tracking, logger):
|
|
|
|
|
- """Report partial filament usage based on actual G-code layer data.
|
|
|
|
|
-
|
|
|
|
|
- Uses per-layer cumulative extrusion from G-code parsing for accurate
|
|
|
|
|
- multi-material tracking. Falls back to linear interpolation if G-code
|
|
|
|
|
- data is unavailable.
|
|
|
|
|
-
|
|
|
|
|
- This is called when a print fails or is cancelled.
|
|
|
|
|
- """
|
|
|
|
|
- from backend.app.api.routes.settings import get_setting
|
|
|
|
|
- from backend.app.utils.threemf_tools import get_cumulative_usage_at_layer, mm_to_grams
|
|
|
|
|
-
|
|
|
|
|
- async with async_session() as db:
|
|
|
|
|
- # Check if partial usage reporting is enabled (default: true)
|
|
|
|
|
- report_partial = await get_setting(db, "spoolman_report_partial_usage")
|
|
|
|
|
- if report_partial and report_partial.lower() == "false":
|
|
|
|
|
- logger.debug("[SPOOLMAN] Partial usage reporting disabled by setting")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Check if Spoolman is enabled
|
|
|
|
|
- spoolman_enabled = await get_setting(db, "spoolman_enabled")
|
|
|
|
|
- if not spoolman_enabled or spoolman_enabled.lower() != "true":
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Get current printer state for layer progress
|
|
|
|
|
- state = printer_manager.get_status(printer_id)
|
|
|
|
|
- if not state:
|
|
|
|
|
- logger.debug("[SPOOLMAN] No printer state available for partial usage")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- current_layer = state.layer_num
|
|
|
|
|
- total_layers = state.total_layers
|
|
|
|
|
-
|
|
|
|
|
- # Need current layer to calculate usage
|
|
|
|
|
- if not current_layer or current_layer <= 0:
|
|
|
|
|
- logger.debug("[SPOOLMAN] No progress to report (layer 0 or unknown)")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Reporting partial usage at layer {current_layer}/{total_layers or '?'}")
|
|
|
|
|
-
|
|
|
|
|
- # Get tracking data
|
|
|
|
|
- layer_usage = tracking.layer_usage
|
|
|
|
|
- filament_properties = tracking.filament_properties or {}
|
|
|
|
|
- filament_usage = tracking.filament_usage or []
|
|
|
|
|
- ams_trays = tracking.ams_trays or {}
|
|
|
|
|
- slot_to_tray = tracking.slot_to_tray
|
|
|
|
|
-
|
|
|
|
|
- # Convert ams_trays keys from string to int (JSON serialization)
|
|
|
|
|
- ams_trays = {int(k): v for k, v in ams_trays.items()}
|
|
|
|
|
-
|
|
|
|
|
- async with async_session() as db:
|
|
|
|
|
- from backend.app.api.routes.settings import get_setting
|
|
|
|
|
-
|
|
|
|
|
- # Get Spoolman client
|
|
|
|
|
- client = await get_spoolman_client()
|
|
|
|
|
- if not client:
|
|
|
|
|
- spoolman_url = await get_setting(db, "spoolman_url")
|
|
|
|
|
- if spoolman_url:
|
|
|
|
|
- client = await init_spoolman_client(spoolman_url)
|
|
|
|
|
-
|
|
|
|
|
- if not client or not await client.health_check():
|
|
|
|
|
- logger.warning("[SPOOLMAN] Not reachable for partial usage reporting")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- spools_updated = 0
|
|
|
|
|
-
|
|
|
|
|
- # Try to use accurate G-code parsed data
|
|
|
|
|
- if layer_usage:
|
|
|
|
|
- # Convert string keys back to int (JSON serialization issue)
|
|
|
|
|
- # Both outer (layer) and inner (filament_id) keys need conversion
|
|
|
|
|
- layer_usage_int = {
|
|
|
|
|
- int(layer): {int(fid): mm for fid, mm in filaments.items()} for layer, filaments in layer_usage.items()
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- # Get cumulative usage at current layer
|
|
|
|
|
- usage_mm = get_cumulative_usage_at_layer(layer_usage_int, current_layer)
|
|
|
|
|
-
|
|
|
|
|
- if usage_mm:
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Using G-code parsed data for layer {current_layer}")
|
|
|
|
|
-
|
|
|
|
|
- # Process each filament's usage
|
|
|
|
|
- for filament_id, mm_used in usage_mm.items():
|
|
|
|
|
- # filament_id is 0-based from G-code, slot_id is 1-based
|
|
|
|
|
- slot_id = filament_id + 1
|
|
|
|
|
-
|
|
|
|
|
- # Determine which tray was used for this slot
|
|
|
|
|
- global_tray_id = slot_id - 1 # Default: slot 1 -> tray 0
|
|
|
|
|
- if slot_to_tray and slot_id <= len(slot_to_tray):
|
|
|
|
|
- mapped_tray = slot_to_tray[slot_id - 1]
|
|
|
|
|
- if mapped_tray >= 0:
|
|
|
|
|
- global_tray_id = mapped_tray
|
|
|
|
|
-
|
|
|
|
|
- tray_info = ams_trays.get(global_tray_id)
|
|
|
|
|
- if not tray_info:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Slot {slot_id}: no tray at global_tray_id {global_tray_id}")
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Get spool identifier (prefer tray_uuid over tag_uid)
|
|
|
|
|
- tray_uuid = tray_info.get("tray_uuid", "")
|
|
|
|
|
- tag_uid = tray_info.get("tag_uid", "")
|
|
|
|
|
- spool_tag = tray_uuid if tray_uuid and tray_uuid != "00000000000000000000000000000000" else tag_uid
|
|
|
|
|
-
|
|
|
|
|
- if not spool_tag:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Slot {slot_id}: no identifier for tray {global_tray_id}")
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Find the spool in Spoolman
|
|
|
|
|
- spool = await client.find_spool_by_tag(spool_tag)
|
|
|
|
|
- if not spool:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Slot {slot_id}: no spool for tag {spool_tag[:16]}...")
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Get density from Spoolman's filament data (most accurate)
|
|
|
|
|
- # Falls back to 3MF properties, then to default PLA density
|
|
|
|
|
- filament_data = spool.get("filament", {})
|
|
|
|
|
- density = filament_data.get("density")
|
|
|
|
|
- diameter = filament_data.get("diameter", 1.75)
|
|
|
|
|
-
|
|
|
|
|
- if not density:
|
|
|
|
|
- # Fallback to 3MF properties
|
|
|
|
|
- props = filament_properties.get(str(slot_id), filament_properties.get(slot_id, {}))
|
|
|
|
|
- density = props.get("density", 1.24)
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Using fallback density {density} for slot {slot_id}")
|
|
|
|
|
-
|
|
|
|
|
- # Convert mm to grams using Spoolman's filament density
|
|
|
|
|
- grams_used = round(mm_to_grams(mm_used, diameter, density), 2)
|
|
|
|
|
-
|
|
|
|
|
- if grams_used <= 0:
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Report usage to Spoolman
|
|
|
|
|
- result = await client.use_spool(spool["id"], grams_used)
|
|
|
|
|
- if result:
|
|
|
|
|
- logger.info(
|
|
|
|
|
- f"[SPOOLMAN] Partial (G-code): slot {slot_id}: {grams_used}g ({mm_used:.1f}mm, d={density}) -> spool {spool['id']}"
|
|
|
|
|
- )
|
|
|
|
|
- spools_updated += 1
|
|
|
|
|
-
|
|
|
|
|
- if spools_updated > 0:
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Reported partial usage to {spools_updated} spool(s) using G-code data")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Fallback: linear interpolation (if no G-code data available)
|
|
|
|
|
- if not total_layers or total_layers <= 0:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Cannot use linear fallback: total_layers={total_layers}")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- progress_ratio = min(current_layer / total_layers, 1.0)
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Falling back to linear interpolation ({progress_ratio:.1%})")
|
|
|
|
|
-
|
|
|
|
|
- for usage in filament_usage:
|
|
|
|
|
- slot_id = usage.get("slot_id", 0)
|
|
|
|
|
- total_used_g = usage.get("used_g", 0)
|
|
|
|
|
-
|
|
|
|
|
- if total_used_g <= 0:
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Calculate partial usage using linear interpolation
|
|
|
|
|
- partial_used_g = round(total_used_g * progress_ratio, 2)
|
|
|
|
|
-
|
|
|
|
|
- if partial_used_g <= 0:
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Determine which tray was used for this slot
|
|
|
|
|
- global_tray_id = slot_id - 1
|
|
|
|
|
- if slot_to_tray and slot_id <= len(slot_to_tray):
|
|
|
|
|
- mapped_tray = slot_to_tray[slot_id - 1]
|
|
|
|
|
- if mapped_tray >= 0:
|
|
|
|
|
- global_tray_id = mapped_tray
|
|
|
|
|
-
|
|
|
|
|
- tray_info = ams_trays.get(global_tray_id)
|
|
|
|
|
- if not tray_info:
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Get spool identifier
|
|
|
|
|
- tray_uuid = tray_info.get("tray_uuid", "")
|
|
|
|
|
- tag_uid = tray_info.get("tag_uid", "")
|
|
|
|
|
- spool_tag = tray_uuid if tray_uuid and tray_uuid != "00000000000000000000000000000000" else tag_uid
|
|
|
|
|
-
|
|
|
|
|
- if not spool_tag:
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Find and update spool
|
|
|
|
|
- spool = await client.find_spool_by_tag(spool_tag)
|
|
|
|
|
- if spool:
|
|
|
|
|
- result = await client.use_spool(spool["id"], partial_used_g)
|
|
|
|
|
- if result:
|
|
|
|
|
- logger.info(
|
|
|
|
|
- f"[SPOOLMAN] Partial (linear): slot {slot_id}: {partial_used_g}g/{total_used_g}g -> spool {spool['id']}"
|
|
|
|
|
- )
|
|
|
|
|
- spools_updated += 1
|
|
|
|
|
-
|
|
|
|
|
- if spools_updated > 0:
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Reported partial usage to {spools_updated} spool(s) using linear interpolation")
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-async def _report_spoolman_usage(printer_id: int, archive_id: int, logger):
|
|
|
|
|
- """Report filament usage to Spoolman after print completion.
|
|
|
|
|
-
|
|
|
|
|
- Uses per-filament usage data captured at print start to report
|
|
|
|
|
- usage to the correct spools.
|
|
|
|
|
- """
|
|
|
|
|
- async with async_session() as db:
|
|
|
|
|
- from backend.app.api.routes.settings import get_setting
|
|
|
|
|
- from backend.app.models.active_print_spoolman import ActivePrintSpoolman
|
|
|
|
|
-
|
|
|
|
|
- # Get tracking data stored at print start
|
|
|
|
|
- result = await db.execute(
|
|
|
|
|
- select(ActivePrintSpoolman)
|
|
|
|
|
- .where(ActivePrintSpoolman.printer_id == printer_id)
|
|
|
|
|
- .where(ActivePrintSpoolman.archive_id == archive_id)
|
|
|
|
|
- )
|
|
|
|
|
- tracking = result.scalar_one_or_none()
|
|
|
|
|
-
|
|
|
|
|
- if not tracking:
|
|
|
|
|
- logger.info(f"[SPOOLMAN] No tracking data for print (printer={printer_id}, archive={archive_id})")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- filament_usage = tracking.filament_usage or []
|
|
|
|
|
- ams_trays = tracking.ams_trays or {}
|
|
|
|
|
- slot_to_tray = tracking.slot_to_tray
|
|
|
|
|
-
|
|
|
|
|
- # Delete tracking row (we're done with it)
|
|
|
|
|
- await db.delete(tracking)
|
|
|
|
|
- await db.commit()
|
|
|
|
|
-
|
|
|
|
|
- if not filament_usage:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] No filament usage data for archive {archive_id}")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Check if Spoolman is enabled
|
|
|
|
|
- spoolman_enabled = await get_setting(db, "spoolman_enabled")
|
|
|
|
|
- if not spoolman_enabled or spoolman_enabled.lower() != "true":
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- # Get Spoolman client
|
|
|
|
|
- client = await get_spoolman_client()
|
|
|
|
|
- if not client:
|
|
|
|
|
- spoolman_url = await get_setting(db, "spoolman_url")
|
|
|
|
|
- if spoolman_url:
|
|
|
|
|
- client = await init_spoolman_client(spoolman_url)
|
|
|
|
|
-
|
|
|
|
|
- if not client or not await client.health_check():
|
|
|
|
|
- logger.warning("[SPOOLMAN] Not reachable for usage reporting")
|
|
|
|
|
- return
|
|
|
|
|
-
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Reporting per-filament usage for archive {archive_id}")
|
|
|
|
|
-
|
|
|
|
|
- # Convert ams_trays keys from string to int (JSON serialization converts int keys to strings)
|
|
|
|
|
- ams_trays = {int(k): v for k, v in ams_trays.items()}
|
|
|
|
|
-
|
|
|
|
|
- spools_updated = 0
|
|
|
|
|
- for usage in filament_usage:
|
|
|
|
|
- slot_id = usage.get("slot_id", 0)
|
|
|
|
|
- used_g = usage.get("used_g", 0)
|
|
|
|
|
-
|
|
|
|
|
- if used_g <= 0:
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Determine which tray was used for this slot
|
|
|
|
|
- # Default: slot_id 1 -> global_tray_id 0, etc.
|
|
|
|
|
- global_tray_id = slot_id - 1
|
|
|
|
|
-
|
|
|
|
|
- # Apply custom mapping if provided
|
|
|
|
|
- if slot_to_tray and slot_id <= len(slot_to_tray):
|
|
|
|
|
- mapped_tray = slot_to_tray[slot_id - 1]
|
|
|
|
|
- if mapped_tray >= 0: # -1 = unmapped
|
|
|
|
|
- global_tray_id = mapped_tray
|
|
|
|
|
-
|
|
|
|
|
- tray_info = ams_trays.get(global_tray_id)
|
|
|
|
|
- if not tray_info:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Slot {slot_id}: no tray at global_tray_id {global_tray_id}")
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Get spool identifier (prefer tray_uuid over tag_uid)
|
|
|
|
|
- tray_uuid = tray_info.get("tray_uuid", "")
|
|
|
|
|
- tag_uid = tray_info.get("tag_uid", "")
|
|
|
|
|
- spool_tag = tray_uuid if tray_uuid and tray_uuid != "00000000000000000000000000000000" else tag_uid
|
|
|
|
|
-
|
|
|
|
|
- if not spool_tag:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Slot {slot_id}: no identifier for tray {global_tray_id}")
|
|
|
|
|
- continue
|
|
|
|
|
-
|
|
|
|
|
- # Find and update spool
|
|
|
|
|
- spool = await client.find_spool_by_tag(spool_tag)
|
|
|
|
|
- if spool:
|
|
|
|
|
- spool_result = await client.use_spool(spool["id"], used_g)
|
|
|
|
|
- if spool_result:
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Slot {slot_id}: {used_g}g -> spool {spool['id']} (tray {global_tray_id})")
|
|
|
|
|
- spools_updated += 1
|
|
|
|
|
- else:
|
|
|
|
|
- logger.debug(f"[SPOOLMAN] Slot {slot_id}: no spool for tag {spool_tag[:16]}...")
|
|
|
|
|
-
|
|
|
|
|
- if spools_updated == 0:
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Archive {archive_id}: no spools updated")
|
|
|
|
|
- else:
|
|
|
|
|
- logger.info(f"[SPOOLMAN] Archive {archive_id}: updated {spools_updated} spool(s)")
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
async def on_printer_status_change(printer_id: int, state: PrinterState):
|
|
async def on_printer_status_change(printer_id: int, state: PrinterState):
|
|
|
"""Handle printer status changes - broadcast via WebSocket."""
|
|
"""Handle printer status changes - broadcast via WebSocket."""
|
|
|
# Only broadcast if something meaningful changed (reduce WebSocket spam)
|
|
# Only broadcast if something meaningful changed (reduce WebSocket spam)
|
|
@@ -1394,7 +938,7 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
|
|
|
|
|
# Store Spoolman tracking data for per-filament usage reporting
|
|
# Store Spoolman tracking data for per-filament usage reporting
|
|
|
try:
|
|
try:
|
|
|
- await _store_spoolman_print_data(printer_id, archive.id, archive.file_path, db, logger)
|
|
|
|
|
|
|
+ await _store_spoolman_print_data(printer_id, archive.id, archive.file_path, db, printer_manager)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.warning(f"[SPOOLMAN] Failed to store tracking data: {e}")
|
|
logger.warning(f"[SPOOLMAN] Failed to store tracking data: {e}")
|
|
|
|
|
|
|
@@ -1695,7 +1239,7 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
# Store Spoolman tracking data (may not work for fallback since no 3MF)
|
|
# Store Spoolman tracking data (may not work for fallback since no 3MF)
|
|
|
try:
|
|
try:
|
|
|
await _store_spoolman_print_data(
|
|
await _store_spoolman_print_data(
|
|
|
- printer_id, fallback_archive.id, fallback_archive.file_path, db, logger
|
|
|
|
|
|
|
+ printer_id, fallback_archive.id, fallback_archive.file_path, db, printer_manager
|
|
|
)
|
|
)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.debug(f"[SPOOLMAN] Could not store tracking for fallback archive: {e}")
|
|
logger.debug(f"[SPOOLMAN] Could not store tracking for fallback archive: {e}")
|
|
@@ -1814,7 +1358,7 @@ async def on_print_start(printer_id: int, data: dict):
|
|
|
|
|
|
|
|
# Store Spoolman tracking data for per-filament usage reporting
|
|
# Store Spoolman tracking data for per-filament usage reporting
|
|
|
try:
|
|
try:
|
|
|
- await _store_spoolman_print_data(printer_id, archive.id, archive.file_path, db, logger)
|
|
|
|
|
|
|
+ await _store_spoolman_print_data(printer_id, archive.id, archive.file_path, db, printer_manager)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.warning(f"[SPOOLMAN] Failed to store tracking data: {e}")
|
|
logger.warning(f"[SPOOLMAN] Failed to store tracking data: {e}")
|
|
|
finally:
|
|
finally:
|
|
@@ -2164,7 +1708,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
# 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":
|
|
|
try:
|
|
try:
|
|
|
- await _report_spoolman_usage(printer_id, archive_id, logger)
|
|
|
|
|
|
|
+ await _report_spoolman_usage(printer_id, archive_id)
|
|
|
log_timing("Spoolman usage report")
|
|
log_timing("Spoolman usage report")
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.warning(f"Spoolman usage reporting failed: {e}")
|
|
logger.warning(f"Spoolman usage reporting failed: {e}")
|
|
@@ -2172,7 +1716,7 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
# Report partial usage if tracking data exists (only stored when weight sync is disabled)
|
|
# Report partial usage if tracking data exists (only stored when weight sync is disabled)
|
|
|
try:
|
|
try:
|
|
|
async with async_session() as db:
|
|
async with async_session() as db:
|
|
|
- await _cleanup_spoolman_tracking(printer_id, archive_id, db, logger)
|
|
|
|
|
|
|
+ await _cleanup_spoolman_tracking(printer_id, archive_id, db)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.debug(f"[SPOOLMAN] Cleanup failed: {e}")
|
|
logger.debug(f"[SPOOLMAN] Cleanup failed: {e}")
|
|
|
|
|
|