mirror of
https://github.com/lumapu/ahoy.git
synced 2025-08-06 01:48:19 +02:00
RPI: refactor: move more stuff into main_loop
This commit is contained in:
parent
c1502fcaa4
commit
ed205fba47
1 changed files with 33 additions and 33 deletions
|
@ -17,17 +17,6 @@ from yaml.loader import SafeLoader
|
||||||
import paho.mqtt.client
|
import paho.mqtt.client
|
||||||
import hoymiles
|
import hoymiles
|
||||||
|
|
||||||
def main_loop(do_init):
|
|
||||||
"""Main loop"""
|
|
||||||
inverters = [
|
|
||||||
inverter for inverter in ahoy_config.get('inverters', [])
|
|
||||||
if not inverter.get('disabled', False)]
|
|
||||||
|
|
||||||
for inverter in inverters:
|
|
||||||
if hoymiles.HOYMILES_DEBUG_LOGGING:
|
|
||||||
print(f'Poll inverter {inverter["serial"]}')
|
|
||||||
poll_inverter(inverter, do_init)
|
|
||||||
|
|
||||||
class InfoCommands(IntEnum):
|
class InfoCommands(IntEnum):
|
||||||
InverterDevInform_Simple = 0 # 0x00
|
InverterDevInform_Simple = 0 # 0x00
|
||||||
InverterDevInform_All = 1 # 0x01
|
InverterDevInform_All = 1 # 0x01
|
||||||
|
@ -48,6 +37,38 @@ class InfoCommands(IntEnum):
|
||||||
GetSelfCheckState = 30 # 0x1e
|
GetSelfCheckState = 30 # 0x1e
|
||||||
InitDataState = 0xff
|
InitDataState = 0xff
|
||||||
|
|
||||||
|
def main_loop(ahoy_config):
|
||||||
|
"""Main loop"""
|
||||||
|
inverters = [
|
||||||
|
inverter for inverter in ahoy_config.get('inverters', [])
|
||||||
|
if not inverter.get('disabled', False)]
|
||||||
|
|
||||||
|
loop_interval = ahoy_config.get('interval', 1)
|
||||||
|
try:
|
||||||
|
do_init = True
|
||||||
|
while True:
|
||||||
|
t_loop_start = time.time()
|
||||||
|
|
||||||
|
for inverter in inverters:
|
||||||
|
if hoymiles.HOYMILES_DEBUG_LOGGING:
|
||||||
|
print(f'Poll inverter {inverter["serial"]}')
|
||||||
|
poll_inverter(inverter, do_init)
|
||||||
|
do_init = False
|
||||||
|
|
||||||
|
print('', end='', flush=True)
|
||||||
|
|
||||||
|
if loop_interval > 0:
|
||||||
|
time_to_sleep = loop_interval - (time.time() - t_loop_start)
|
||||||
|
if time_to_sleep > 0:
|
||||||
|
time.sleep(time_to_sleep)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
sys.exit()
|
||||||
|
except Exception as e:
|
||||||
|
print ('Exception catched: %s' % e)
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
def poll_inverter(inverter, do_init, retries=4):
|
def poll_inverter(inverter, do_init, retries=4):
|
||||||
"""
|
"""
|
||||||
Send/Receive command_queue, initiate status poll on inverter
|
Send/Receive command_queue, initiate status poll on inverter
|
||||||
|
@ -310,25 +331,4 @@ if __name__ == '__main__':
|
||||||
mqtt_client.subscribe(topic_item[1])
|
mqtt_client.subscribe(topic_item[1])
|
||||||
mqtt_command_topic_subs.append(topic_item)
|
mqtt_command_topic_subs.append(topic_item)
|
||||||
|
|
||||||
loop_interval = ahoy_config.get('interval', 1)
|
main_loop(ahoy_config)
|
||||||
try:
|
|
||||||
do_init = True
|
|
||||||
while True:
|
|
||||||
t_loop_start = time.time()
|
|
||||||
|
|
||||||
main_loop(do_init)
|
|
||||||
|
|
||||||
do_init = False
|
|
||||||
|
|
||||||
print('', end='', flush=True)
|
|
||||||
|
|
||||||
time_to_sleep = loop_interval - (time.time() - t_loop_start)
|
|
||||||
|
|
||||||
if loop_interval > 0 and time_to_sleep > 0:
|
|
||||||
time.sleep(time_to_sleep)
|
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
sys.exit()
|
|
||||||
except Exception as e:
|
|
||||||
print ('Exception catched: %s' % e)
|
|
||||||
raise
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue