From 2746ac340b257ceb84bfd9acf1bc80ca8a9643a8 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Fri, 9 Feb 2024 12:39:28 +0100 Subject: [PATCH] Batch Updates --- app/__init__.py | 3 +- app/jobs.py | 76 ++++++++++++++++++++++++++++++++++++++-- app/templates/index.html | 2 +- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 42da8b4..e963b9c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,7 +5,7 @@ from app.settings import powerbars from app.utils import get_baytech_status_outlet_telnet from flask_socketio import SocketIO, emit from gevent import monkey -from app.jobs import scheduler +from app.jobs import scheduler, generate_batch_jobs socketio = SocketIO(async_mode="gevent", logger=True, engineio_logger=True, cors_allowed_origins="*") @@ -43,6 +43,7 @@ def create_app(): app.register_blueprint(routes) powerbar_status() scheduler.init_app(app) + generate_batch_jobs() socketio.init_app(app) return app diff --git a/app/jobs.py b/app/jobs.py index 93c81fd..2a0d004 100644 --- a/app/jobs.py +++ b/app/jobs.py @@ -5,7 +5,7 @@ import time scheduler = APScheduler() -@scheduler.task('interval', id='do_periodic_serial_job', seconds=3, max_instances=1) +#@scheduler.task('interval', id='do_periodic_serial_job', seconds=3, max_instances=1) def periodic_serial_job(): print("Running Sync Job") for powerbar in powerbars: @@ -66,4 +66,76 @@ def periodic_serial_job(): run_telnet_command(powerbar, command) time.sleep(0.1) - print("Sync Job Done") \ No newline at end of file + print("Sync Job Done") + + + +def generate_batch_jobs(): + for powerbar in powerbars: + if powerbars[powerbar]['method'] == 'batch_telnet': + if powerbars[powerbar]['type'] == 'baytech': + scheduler.add_job(f"baytech-batch-update-job-{powerbar}", baytech_batch_update, args=[powerbar], trigger='interval', seconds=3, max_instances=1) + + + +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 = [] + + counter_on = 0 + counter_off = 0 + max = 9 + 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) + + 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(',')) + + print(f"commands: {commands}") + + + for command in commands: + print(f"Running command: {command}") + run_telnet_command(powerbar, command) + time.sleep(0.5) + + print(f"Sync Job Done {powerbar}") + return diff --git a/app/templates/index.html b/app/templates/index.html index 67faad6..85af103 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -104,7 +104,7 @@ input:checked + .slider:before {
-

Techinc Logo powerbar.ti (3.2.5)

+

Techinc Logo powerbar.ti (3.2.6)