cleanup power_monitor.py

This commit is contained in:
Adeeb Shihadeh
2025-10-15 19:35:30 -07:00
parent f9d672d350
commit e18b3635d0
+10 -11
View File
@@ -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)