fix: Refactor main loop for graceful shutdown and improved error handling
This commit is contained in:
parent
d76b11430c
commit
c8102e62ee
24
main.py
24
main.py
@ -369,6 +369,9 @@ retry_ntp_attempts = 0
|
|||||||
max_ntp_attempts = 5 # Try up to 5 times after initial failure
|
max_ntp_attempts = 5 # Try up to 5 times after initial failure
|
||||||
last_ntp_sync = time.time() # Track when we last synced
|
last_ntp_sync = time.time() # Track when we last synced
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
|
||||||
# ===== START: Main Loop =====
|
# ===== START: Main Loop =====
|
||||||
# Main monitoring loop (runs forever until Ctrl+C)
|
# Main monitoring loop (runs forever until Ctrl+C)
|
||||||
last_monitor_run = {
|
last_monitor_run = {
|
||||||
@ -461,3 +464,24 @@ while True:
|
|||||||
gc.collect()
|
gc.collect()
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
# ===== END: Main Loop =====
|
# ===== END: Main Loop =====
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n" + "="*50)
|
||||||
|
print("Shutting down gracefully...")
|
||||||
|
print("="*50)
|
||||||
|
try:
|
||||||
|
print("Turning off AC...")
|
||||||
|
ac_controller.turn_off()
|
||||||
|
except Exception as e:
|
||||||
|
print("AC shutdown error:", e)
|
||||||
|
try:
|
||||||
|
print("Turning off heater...")
|
||||||
|
heater_controller.turn_off()
|
||||||
|
except Exception as e:
|
||||||
|
print("Heater shutdown error:", e)
|
||||||
|
try:
|
||||||
|
print("Turning off LED...")
|
||||||
|
led.low()
|
||||||
|
except Exception as e:
|
||||||
|
print("LED shutdown error:", e)
|
||||||
|
print("Shutdown complete!")
|
||||||
|
print("="*50)
|
||||||
Loading…
x
Reference in New Issue
Block a user