fix(phase26): persist DTC/MIL state + server speed_source (BUG-03, BUG-05)

BUG-03: DTC/MIL codes were never persisted; lost on OBD disconnect or reboot.
- Add new dtc table schema (code, description, MIL, first/last seen, IS_SET)
  and a migration that rebuilds the original unused NAME/STATUS/DTC_ID table.
- Persist MIL + codes to SQLite on each successful OBD poll (_persist_dtc_state).
- /api/dtc falls back to the last-persisted snapshot when OBD is offline/stale.
- Fix get_overview_stats to count the real dtc table (was querying non-existent
  dtc_codes table).
- Add cleared/since tracking via IS_SET + FIRST_SEEN_AT/LAST_SEEN_AT.

BUG-05: server trip replay hardcoded speed_source:'gps'; mirror Pi's
'obd if speed>0 else gps' logic in server get_trip_route.

Adds tests/test_dtc_persistence.py (7 tests).
This commit is contained in:
Mathieu Moran
2026-08-11 00:38:05 -04:00
parent cd0583ac84
commit e2f2314318
5 changed files with 251 additions and 15 deletions
+13
View File
@@ -368,6 +368,19 @@ def _poll_diagnostics():
app_events.socketio.emit("obd_throttle", throttle_position)
app_events.socketio.emit("obd_odometer", odometer)
# Persist the latest MIL + DTC snapshot so last-known state survives an
# OBD disconnect or a power cycle (deferred import avoids a circular import).
_persist_dtc_state()
def _persist_dtc_state():
"""Best-effort persist of the current MIL + DTC codes to SQLite."""
try:
from modules import app_sql
app_sql.save_dtc_state(mil_on, dtc_codes)
except Exception as error:
print("Failed to persist DTC state to SQLite:", error)
def obd_sensors():
with obd_sensor_lock: