Files
DCCONSTRUCTIONS f32c7b400b feat(vesc): extend motor diagnostics, configuration and transport recovery
Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
2026-10-29 09:09:59 +03:00

88 lines
3.6 KiB
Python

"""Bounded failure evidence in the service journal, never in the drive loop."""
import json
import logging
import time
from datetime import UTC, datetime
LOGGER = logging.getLogger("missioncore.vesc.failure")
def failure(error, phase, *, device=None, session_id=None, release_confirmed=None):
# NativeLink attaches the original RPC and recent calls to the exception.
# Cleanup uses the same link and replaces last_rpc; reading it afterwards
# would record 'process not running' instead of the initiating timeout.
try:
record = {
"event": "vesc_failure",
"at": datetime.now(UTC).isoformat(),
"monotonic": time.monotonic(),
"phase": phase,
"error": str(error)[:512],
"type": type(error).__name__,
"violation": getattr(error,"violation",None),
"device_id": getattr(error,"device_id",None),
"session_id": session_id,
"release_confirmed": release_confirmed,
"native_rpc": getattr(error, "native_rpc", None),
"native_startup": getattr(error, "native_startup", None),
"can_bridge": getattr(error, "can_bridge", None),
"native_history": list(getattr(error, "native_history", []))[-8:],
}
if device is not None:
record["device_id"] = device.id
record["attachment"] = {
key: getattr(device.attachment, key) for key in ("usb", "address", "tty")
}
raw = json.dumps(record, ensure_ascii=False, allow_nan=False)
if len(raw.encode()) > 16000:
record["native_history"] = []
record["history_omitted"] = True
raw = json.dumps(record, ensure_ascii=False, allow_nan=False)
if len(raw.encode()) > 16000:
record["native_rpc"] = None
record["rpc_omitted"] = True
raw = json.dumps(record, ensure_ascii=False, allow_nan=False)
if len(raw.encode()) > 16000:
record["native_startup"] = None
record["startup_omitted"] = True
raw = json.dumps(record, ensure_ascii=False, allow_nan=False)
LOGGER.warning("%s", raw)
except Exception:
# Journal failure must never bypass release/fault/ownership cleanup.
pass
def connection(event, device, error=None):
"""One event per lifecycle transition, before its session/sample is cleared."""
try:
sample = device.sample
record = {
"event": "vesc_connection",
"transition": event,
"at": datetime.now(UTC).isoformat(),
"monotonic": time.monotonic(),
"device_id": device.id,
"session_id": device.session,
"identity": device.identity,
"attachment": {
key: getattr(device.attachment, key) for key in ("usb", "address", "tty")
},
"last_sample_at": sample.get("observed_at") if sample else None,
"last_sample_age_s": max(0.0, time.monotonic() - sample["sampled_at"])
if sample
else None,
"error": str(error)[:512] if error else None,
"error_type": type(error).__name__ if error else None,
}
if sample:
record["last_values"] = {
k: sample["values"].get(k)
for k in ("erpm", "motor_current_a", "input_voltage_v", "duty", "fault_code")
}
raw = json.dumps(record, ensure_ascii=False, allow_nan=False)
if len(raw.encode()) <= 4096:
LOGGER.info("%s", raw)
except Exception:
pass