ti-powerbar/app/jobs.py

35 lines
1.1 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
scheduler = APScheduler()
2024-02-07 17:29:54 +00:00
@scheduler.task('interval', id='do_periodic_serial_job', seconds=4)
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}")
for outlet in powerbars[powerbar]['outlets']:
outlet_status = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
if outlet_status is not "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 is not "On ":
print("Running On command")
2024-02-07 17:29:54 +00:00
run_telnet_command(powerbar, on_command.rstrip(','))
2024-02-07 17:18:33 +00:00
print(f"Turning off outlets: {off_command.rstrip(',')}")
if off_command is not "Off ":
print("Running Off command")
2024-02-07 17:29:54 +00:00
run_telnet_command(powerbar, off_command.rstrip(','))