ti-powerbar/app/jobs.py

69 lines
1.9 KiB
Python
Raw Normal View History

2024-02-07 17:18:33 +00:00
from flask_apscheduler import APScheduler
from app.settings import powerbars
from app.utils import run_telnet_command
2024-02-08 00:28:36 +00:00
import time
2024-02-07 17:18:33 +00:00
scheduler = APScheduler()
2024-02-08 00:28:36 +00:00
@scheduler.task('interval', id='do_periodic_serial_job', seconds=3, max_instances=1)
2024-02-07 17:18:33 +00:00
def periodic_serial_job():
print("Running Sync Job")
for powerbar in powerbars:
on_command = "On "
off_command = "Off "
print(f"Checking powerbar state {powerbar}")
2024-02-08 00:28:36 +00:00
outlets = powerbars[powerbar]['outlets']
on_list = []
off_list = []
counter_on = 0
counter_off = 0
2024-02-08 00:38:39 +00:00
max = 9
2024-02-08 00:28:36 +00:00
for outlet in outlets:
outlet_id = outlet
2024-02-07 17:18:33 +00:00
outlet_status = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
2024-02-08 00:28:36 +00:00
if outlet_status == 'on':
on_list.append(outlet)
2024-02-07 17:18:33 +00:00
2024-02-08 00:28:36 +00:00
if outlet_status == 'off':
off_list.append(outlet)
print(f"on_list: {on_list}")
print(f"off_list: {off_list}")
2024-02-07 17:18:33 +00:00
2024-02-08 00:28:36 +00:00
n = 5 # Size of each sublist
# Using list comprehension and slicing
sublist_on = [on_list[i:i + n] for i in range(0, len(on_list), n)]
sublist_off = [off_list[i:i + n] for i in range(0, len(off_list), n)]
print(f"sublist_on: {sublist_on}")
print(f"sublist_off: {sublist_off}")
2024-02-07 17:18:33 +00:00
2024-02-08 00:28:36 +00:00
commands = []
2024-02-07 17:18:33 +00:00
2024-02-08 00:28:36 +00:00
for list in sublist_on:
on_command = "On "
for outlet in list:
on_command += f"{outlet},"
commands.append(on_command.rstrip(','))
for list in sublist_off:
off_command = "Off "
for outlet in list:
off_command += f"{outlet},"
commands.append(off_command.rstrip(','))
print(f"commands: {commands}")
for command in commands:
print(f"Running command: {command}")
run_telnet_command(powerbar, command)
time.sleep(0.1)
2024-02-07 17:18:33 +00:00
2024-02-08 00:28:36 +00:00
print("Sync Job Done")