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=4) 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") run_telnet_command(powerbar, on_command.rstrip(',')) print(f"Turning off outlets: {off_command.rstrip(',')}") if off_command is not "Off ": print("Running Off command") run_telnet_command(powerbar, off_command.rstrip(','))