handled offline time with timer instead of loop counts
This commit is contained in:
18
main.py
18
main.py
@@ -4,6 +4,7 @@ import os
|
||||
from dotenv import load_dotenv
|
||||
import logging
|
||||
import signal
|
||||
import time
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@@ -43,8 +44,8 @@ signal.signal(signal.SIGINT, handle_shutdown)
|
||||
|
||||
def main():
|
||||
with serial.Serial(device_name, 115200, timeout=1) as ser:
|
||||
inverter_offline_counter = 0
|
||||
shutdown_triggered = False
|
||||
inverter_online = True
|
||||
while running:
|
||||
line = ser.readline().decode(errors="ignore").strip()
|
||||
if line == "":
|
||||
@@ -52,13 +53,16 @@ def main():
|
||||
continue
|
||||
elif line == "0":
|
||||
logger.debug("Inverter Online")
|
||||
inverter_offline_counter = 0
|
||||
elif inverter_offline_counter >= shutdown_timer:
|
||||
shutdown_triggered = True
|
||||
break
|
||||
inverter_online = True
|
||||
elif not inverter_online:
|
||||
if time.time() - last_online > shutdown_timer:
|
||||
shutdown_triggered = True
|
||||
break
|
||||
else:
|
||||
inverter_offline_counter += 1
|
||||
logger.info(f"Running on UPS reserve, offline for {inverter_offline_counter}s")
|
||||
if inverter_online:
|
||||
inverter_online = False
|
||||
last_online = time.time()
|
||||
logger.info(f"Running on UPS reserve, offline for {time.time()-last_online}s")
|
||||
|
||||
if shutdown_triggered:
|
||||
logger.info(f"Inverter offline for {shutdown_timer} seconds")
|
||||
|
||||
Reference in New Issue
Block a user