ti-powerbar/app/jobs.py
Matthew Frost 80f6d34ed2
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 7s
start
2024-02-07 18:37:14 +01:00

35 lines
No EOL
1.1 KiB
Python

from flask_apscheduler import APScheduler
from app.settings import powerbars
from app.utils import run_telnet_command
scheduler = APScheduler()
@scheduler.task('interval', id='do_periodic_serial_job', seconds=3, max_instances=3)
def periodic_serial_job():
print("Running Sync Job")
for powerbar in powerbars:
on_command = "On "
off_command = "Off "
print(f"Checking powerbar state {powerbar}")
for outlet in powerbars[powerbar]['outlets']:
outlet_status = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
if outlet_status != "unknown":
if outlet_status == "on":
on_command += f"{outlet},"
if outlet_status == "off":
off_command += f"{outlet},"
print(f"Turning on outlets: {on_command.rstrip(',')}")
if on_command != "On ":
print("Running On command")
run_telnet_command(powerbar, on_command.rstrip(','))
print(f"Turning off outlets: {off_command.rstrip(',')}")
if off_command != "Off ":
print("Running Off command")
run_telnet_command(powerbar, off_command.rstrip(','))