Add clear DTC codes button to Pi DTC page (FEAT-15)

This commit is contained in:
Mathieu Moran
2026-08-13 16:40:15 -04:00
parent b39faadf58
commit e873db918d
8 changed files with 187 additions and 2 deletions
+27
View File
@@ -280,6 +280,33 @@ def save_dtc_state(mil_on, dtc_codes, recorded_at=None):
print("Failed to persist DTC state:", e)
def clear_dtc_state(recorded_at=None):
"""Mark every active DTC as cleared and reset the persisted MIL flag.
Called after the user explicitly clears codes on the ECU (OBD mode 04) so
the persisted snapshot stops showing stale active codes. Cleared codes
keep their history rows (IS_SET = 0), like a normal "no longer detected"
poll would.
"""
conn = get_conn()
if conn is None:
return
now_str = (recorded_at or datetime.now()).isoformat()
with _conn_lock:
try:
conn.execute(
"INSERT INTO settings (key, value) VALUES ('dtc_mil', '0') "
"ON CONFLICT(key) DO UPDATE SET value = excluded.value"
)
conn.execute(
"UPDATE dtc SET IS_SET = 0, LAST_SEEN_AT = ? WHERE IS_SET = 1",
(now_str,),
)
conn.commit()
except sqlite3.Error as e:
print("Failed to clear DTC state:", e)
def get_persisted_dtc_state():
"""Return the last-persisted MIL + DTC codes for display when OBD is offline."""
conn = get_conn()