fix: persist car-connected flag so trip building works across reboots (BUG-16)
connect_obd() set _car_ever_connected without a global declaration, so the module-level flag was never set and was_car_ever_connected() always returned False — local trip building has been silently blocked since BUG-10 landed. Also persist the flag to the settings table so a drive followed by a reboot still builds trips, and clean up dead code in was_car_ever_connected().
This commit is contained in:
+22
-7
@@ -165,6 +165,7 @@ def _log_connection_diagnostics(candidate):
|
||||
def connect_obd():
|
||||
"""Connect to the adapter and ECU, with actionable failure diagnostics."""
|
||||
global connection
|
||||
global _car_ever_connected
|
||||
with _connect_lock:
|
||||
if _is_car_connected():
|
||||
return connection
|
||||
@@ -232,6 +233,7 @@ def connect_obd():
|
||||
connection = candidate
|
||||
if candidate is not None:
|
||||
_car_ever_connected = True
|
||||
_persist_car_connected()
|
||||
|
||||
if candidate is None:
|
||||
_emit_disconnected()
|
||||
@@ -274,15 +276,28 @@ def obd_connection():
|
||||
start_obd()
|
||||
|
||||
|
||||
def _persist_car_connected():
|
||||
"""Record that the car ECU has been reached, so trip building keeps working
|
||||
across reboots (the in-memory flag resets every process start)."""
|
||||
try:
|
||||
from modules import app_sql
|
||||
app_sql.set_setting("car_ever_connected", "1")
|
||||
except Exception as error:
|
||||
print("Failed to persist car-connected flag:", error)
|
||||
|
||||
|
||||
def was_car_ever_connected():
|
||||
"""True if the car ECU has been reached at least once this session."""
|
||||
"""True if the car ECU has ever been reached (persisted across restarts)."""
|
||||
global _car_ever_connected
|
||||
if _car_ever_connected:
|
||||
return True
|
||||
try:
|
||||
from modules import app_sql
|
||||
if app_sql.get_setting("car_ever_connected", "0") == "1":
|
||||
_car_ever_connected = True
|
||||
except Exception:
|
||||
pass
|
||||
return _car_ever_connected
|
||||
if _is_car_connected():
|
||||
app_events.socketio.emit("obd_status", obd_status[1])
|
||||
app_events.socketio.emit("obd_speed", get_speedtime())
|
||||
app_events.socketio.emit("obd_rpm", rpm if rpm is not None else "-")
|
||||
else:
|
||||
_emit_disconnected()
|
||||
|
||||
|
||||
def get_fresh_mil(max_age=SENSOR_POLL_INTERVAL * 3):
|
||||
|
||||
Reference in New Issue
Block a user