feat: Enhance main loop with error handling and graceful shutdown
This commit is contained in:
parent
988bec521f
commit
299a0abbc9
26
main.py
26
main.py
@ -295,6 +295,7 @@ max_ntp_attempts = 5 # Try up to 5 times after initial failure
|
||||
# ===== START: Main Loop =====
|
||||
# Main monitoring loop (runs forever until Ctrl+C)
|
||||
while True:
|
||||
try:
|
||||
# Run all monitors (each checks if it's time to run via should_run())
|
||||
run_monitors(monitors)
|
||||
|
||||
@ -315,7 +316,30 @@ while True:
|
||||
if retry_ntp_attempts == 0 or (time.time() % 10) < 1:
|
||||
retry_ntp_attempts += 1
|
||||
print("NTP retry {} failed: {}".format(retry_ntp_attempts, e))
|
||||
|
||||
# Enable garbage collection to free memory
|
||||
gc.collect()
|
||||
time.sleep(0.1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
# 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 =====
|
||||
Loading…
x
Reference in New Issue
Block a user