feat: Enhance main loop with error handling and graceful shutdown

This commit is contained in:
Aaron 2025-11-08 16:15:22 -05:00
parent 988bec521f
commit 299a0abbc9

64
main.py
View File

@ -295,27 +295,51 @@ max_ntp_attempts = 5 # Try up to 5 times after initial failure
# ===== START: Main Loop ===== # ===== START: Main Loop =====
# Main monitoring loop (runs forever until Ctrl+C) # Main monitoring loop (runs forever until Ctrl+C)
while True: while True:
# Run all monitors (each checks if it's time to run via should_run()) try:
run_monitors(monitors) # Run all monitors (each checks if it's time to run via should_run())
run_monitors(monitors)
# Web requests # Web requests
web_server.check_requests(sensors, ac_monitor, heater_monitor, schedule_monitor) web_server.check_requests(sensors, ac_monitor, heater_monitor, schedule_monitor)
# Retry NTP sync every ~10s if not yet synced # Retry NTP sync every ~10s if not yet synced
if not ntp_synced and retry_ntp_attempts < max_ntp_attempts: if not ntp_synced and retry_ntp_attempts < max_ntp_attempts:
# Try once immediately, then whenever (time.time() % 10) < 1 (rough 10s window) # Try once immediately, then whenever (time.time() % 10) < 1 (rough 10s window)
try: try:
import ntptime import ntptime
if retry_ntp_attempts == 0 or (time.time() % 10) < 1: if retry_ntp_attempts == 0 or (time.time() % 10) < 1:
ntptime.settime() ntptime.settime()
ntp_synced = True ntp_synced = True
print("NTP sync succeeded on retry #{}".format(retry_ntp_attempts + 1)) print("NTP sync succeeded on retry #{}".format(retry_ntp_attempts + 1))
except Exception as e: except Exception as e:
# Increment only when an actual attempt was made # Increment only when an actual attempt was made
if retry_ntp_attempts == 0 or (time.time() % 10) < 1: if retry_ntp_attempts == 0 or (time.time() % 10) < 1:
retry_ntp_attempts += 1 retry_ntp_attempts += 1
print("NTP retry {} failed: {}".format(retry_ntp_attempts, e)) print("NTP retry {} failed: {}".format(retry_ntp_attempts, e))
# Enable garbage collection to free memory
gc.collect()
time.sleep(0.1)
gc.collect() except KeyboardInterrupt:
time.sleep(0.1) # Graceful shutdown on Ctrl+C
print("\n\n" + "="*50)
print("Shutting down gracefully...")
print("="*50)
print("Turning off AC...")
ac_controller.turn_off()
print("Turning off heater...")
heater_controller.turn_off()
print("Turning off LED...")
led.low()
print("Shutdown complete!")
print("="*50 + "\n")
break
except Exception as e:
# If loop crashes, print error and keep running
print("❌ Main loop error: {}".format(e))
import sys
sys.print_exception(e)
print("⚠️ Pausing 5 seconds before retrying...")
time.sleep(5) # Brief pause before retrying
# ===== END: Main Loop ===== # ===== END: Main Loop =====