From e18b3635d0b9689f93419e26acfb01e34758b13b Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Wed, 15 Oct 2025 19:35:30 -0700 Subject: [PATCH] cleanup power_monitor.py --- userspace/usr/comma/power_monitor.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/userspace/usr/comma/power_monitor.py b/userspace/usr/comma/power_monitor.py index c47bbcf..3cacc9e 100755 --- a/userspace/usr/comma/power_monitor.py +++ b/userspace/usr/comma/power_monitor.py @@ -9,6 +9,7 @@ from datetime import timedelta FN = "/var/tmp/power_watchdog" THRESHOLD = timedelta(hours=1.0) +timestamps = {} def read(path: str, num: bool = False): @@ -21,9 +22,8 @@ def read(path: str, num: bool = False): return 0 if num else "" -last_touch_ts = 0 def check_touches(): - global last_touch_ts + global timestamps event_format = "llHHi" event_size = struct.calcsize(event_format) @@ -34,7 +34,7 @@ def check_touches(): while (event := event_file.read(event_size)): (sec, usec, etype, code, value) = struct.unpack(event_format, event) if etype != 0 or code != 0 or value != 0: - last_touch_ts = time.monotonic() + timestamps['touch'] = time.monotonic() time.sleep(60) def ssh_active(): @@ -47,19 +47,18 @@ if __name__ == "__main__": threading.Thread(target=check_touches, daemon=True).start() - last_valid_readout = time.monotonic() + timestamps['startup'] = time.monotonic() while True: - cur_t = read(FN, True) - last_valid_readout = max(last_valid_readout, last_touch_ts, cur_t) + timestamps['watchdog'] = read(FN, True) if ssh_active(): - last_valid_readout = cur_t - - not_engaged = not read("/data/params/d/IsEngaged").startswith("1") + timestamps['ssh'] = time.monotonic() + if read("/data/params/d/IsEngaged").startswith("1"): + timestamps['engaged'] = time.monotonic() # time to shutoff? - dt = timedelta(seconds=time.monotonic() - last_valid_readout) + dt = timedelta(seconds=time.monotonic() - max(timestamps.values())) if dt > THRESHOLD and not_engaged: os.system("sudo poweroff") - print((THRESHOLD - dt), "until shutdown", "/ not engaged:", not_engaged) + print((THRESHOLD - dt), "until shutdown", f"/ {timestamps=}") time.sleep(60)