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-09 11:39:28 +00:00
|
|
|
def generate_batch_jobs():
|
|
|
|
for powerbar in powerbars:
|
|
|
|
if powerbars[powerbar]['method'] == 'batch_telnet':
|
|
|
|
if powerbars[powerbar]['type'] == 'baytech':
|
2024-02-09 11:48:14 +00:00
|
|
|
scheduler.add_job(f"baytech-batch-update-job-{powerbar}", baytech_batch_update, args=[powerbar], trigger='interval', seconds=1, max_instances=1)
|
2024-02-09 11:39:28 +00:00
|
|
|
|
|
|
|
def baytech_batch_update(powerbar):
|
|
|
|
print(f"Running Sync Job: {powerbar}")
|
|
|
|
|
|
|
|
on_command = "On "
|
|
|
|
off_command = "Off "
|
|
|
|
|
|
|
|
print(f"Checking powerbar state {powerbar}")
|
|
|
|
outlets = powerbars[powerbar]['outlets']
|
|
|
|
|
|
|
|
on_list = []
|
|
|
|
off_list = []
|
|
|
|
|
2024-02-28 16:10:59 +00:00
|
|
|
print (outlets)
|
2024-02-09 11:39:28 +00:00
|
|
|
for outlet in outlets:
|
|
|
|
|
|
|
|
outlet_id = outlet
|
|
|
|
outlet_status = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
|
|
|
|
|
|
|
|
if outlet_status == 'on':
|
|
|
|
on_list.append(outlet)
|
|
|
|
|
|
|
|
if outlet_status == 'off':
|
|
|
|
off_list.append(outlet)
|
2024-02-28 16:10:59 +00:00
|
|
|
|
|
|
|
print(f"Creating command sets for: {powerbar}")
|
2024-02-09 11:39:28 +00:00
|
|
|
print(f"on_list: {on_list}")
|
|
|
|
print(f"off_list: {off_list}")
|
|
|
|
|
|
|
|
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}")
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
|
|
|
|
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(','))
|
|
|
|
|
2024-02-28 16:10:59 +00:00
|
|
|
print(f"commands: {commands} for {powerbar}")
|
|
|
|
|
2024-02-09 11:39:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
for command in commands:
|
2024-02-28 16:10:59 +00:00
|
|
|
print(f"Running command: {command} on {powerbar}")
|
2024-02-28 16:17:48 +00:00
|
|
|
run_telnet_command(powerbar, command)
|
2024-02-09 11:39:28 +00:00
|
|
|
time.sleep(0.5)
|
|
|
|
|
|
|
|
print(f"Sync Job Done {powerbar}")
|
|
|
|
return
|