Files
Bambu-Run/bambu_run/mcp/status.py
2026-08-19 21:55:02 +10:00

140 lines
5.4 KiB
Python

"""get_printer_status, get_metrics_history, get_hardware_health -- the
read_printer capability tools."""
from __future__ import annotations
from collections import defaultdict
from datetime import timedelta
from django.db.models import Avg
from django.utils import timezone
from bambu_run.mcp.shapes import classify_alerts, freshness, resolve_printer, smoothed_remaining_percent
from bambu_run.models import Hotend, PrinterMetrics
from bambu_run.utils import strip_color_padding
ALERT_WINDOW_HOURS = 24
def get_printer_status(printer: str | int | None = None) -> dict:
"""Current live status of one or all printers: temps, print progress, AMS slots, and alerts.
Pass a printer id or exact name to scope to one printer; omit to get every
active printer. Numeric percentages under ams_units[].slots[] are smoothed
(median of the 5 most recent readings) and paired with a remain_status
field ("ok"/"low"/"empty"/"unread") -- treat "unread" as "no RFID reading
yet", not as "empty". alerts are frequency-classified, not raw fault
codes; severity is always "unknown" until Bambu's HMS codes are mapped,
so never assert the printer is fine or faulty purely from an alert entry.
"""
now = timezone.now()
printers = resolve_printer(printer)
entries = []
for p in printers:
metric = PrinterMetrics.objects.filter(device=p).order_by("-timestamp").first()
if metric is None:
entries.append({
"id": p.id, "name": p.name, "model": p.model, "location": p.location,
"data_age_seconds": None, "stale": True, "state": "no_data",
"job": None, "temperatures": None, "ams_units": [], "alerts": [],
"fans": None, "chamber_light": None,
})
continue
entries.append({
"id": p.id,
"name": p.name,
"model": p.model,
"location": p.location,
**freshness(metric.timestamp, now),
"state": metric.gcode_state or "UNKNOWN",
"job": _job_info(metric, now),
"temperatures": _temperatures(metric),
"ams_units": _ams_units(p, metric),
"alerts": _alerts_for_printer(p, now),
"fans": {
"cooling_percent": metric.cooling_fan_speed,
"aux_percent": metric.big_fan1_speed,
"chamber_percent": metric.big_fan2_speed,
},
"chamber_light": metric.chamber_light,
})
return {"generated_at": now.isoformat(), "printers": entries}
def _job_info(metric, now):
if metric.gcode_state != "RUNNING":
return None
remaining = metric.remaining_time_min
finish_at = (now + timedelta(minutes=remaining)).isoformat() if remaining is not None else None
return {
"name": metric.subtask_name,
"print_percent": metric.print_percent,
"layer_current": metric.layer_num,
"layer_total": metric.total_layer_num,
"remaining_time_min": remaining,
"estimated_finish_at": finish_at,
}
def _temperatures(metric):
def f(v):
return float(v) if v is not None else None
return {
"nozzle_c": f(metric.nozzle_temp), "nozzle_target_c": f(metric.nozzle_target_temp),
"nozzle_left_c": f(metric.nozzle_temp_left), "nozzle_left_target_c": f(metric.nozzle_target_temp_left),
"bed_c": f(metric.bed_temp), "bed_target_c": f(metric.bed_target_temp),
"chamber_c": f(metric.chamber_temp),
}
def _ams_units(printer, latest_metric):
"""Build ams_units from the latest metric's filaments, with remain_percent
smoothed against the printer's 5 most recent metric rows."""
recent_filament_lists = list(
PrinterMetrics.objects.filter(device=printer)
.order_by("-timestamp")
.values_list("filaments", flat=True)[:5]
)
readings = defaultdict(list)
for filaments in recent_filament_lists:
for f in filaments or []:
key = (f.get("ams_unit_id"), int(f.get("tray_id", -1)))
readings[key].append(f.get("remain_percent"))
units = {}
for f in latest_metric.filaments or []:
unit_id = f.get("ams_unit_id")
tray_id = int(f.get("tray_id", -1))
value, status = smoothed_remaining_percent(readings[(unit_id, tray_id)])
unit = units.setdefault(unit_id, {
"unit_id": unit_id,
"type": f.get("ams_type", ""),
"humidity_raw": latest_metric.ams_humidity_raw,
"temp_c": float(latest_metric.ams_temp) if latest_metric.ams_temp is not None else None,
"slots": [],
})
color = f.get("color")
unit["slots"].append({
"slot": tray_id,
"label": f.get("slot", ""),
"material": f.get("type", ""),
"sub_type": f.get("sub_type", ""),
"color_hex": f"#{strip_color_padding(color)}" if color else None,
"remain_percent": value,
"remain_status": status,
})
return list(units.values())
def _alerts_for_printer(printer, now, window_hours=ALERT_WINDOW_HOURS):
since = now - timedelta(hours=window_hours)
window = PrinterMetrics.objects.filter(device=printer, timestamp__gte=since)
total = window.count()
counts = defaultdict(int)
for hms_list in window.exclude(hms=[]).values_list("hms", flat=True):
for entry in hms_list:
counts[(entry.get("attr"), entry.get("code"))] += 1
return classify_alerts(counts, total)