diff --git a/app/__init__.py b/app/__init__.py index 31152c9..42da8b4 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,27 +5,23 @@ 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 socketio = SocketIO(async_mode="gevent", logger=True, engineio_logger=True, cors_allowed_origins="*") @socketio.on('connect', namespace='/powerupdates') def connect_handler(): print('connected') - emit('response', {'meta': 'WS connected'}) -def create_app(): - # create and configure the app - app = Flask(__name__) - app.config.from_pyfile('settings.py') - app.register_blueprint(routes) +def powerbar_status(): for powerbar_name, powerbar_info in powerbars.items(): print(f"Checking powerbar state {powerbar_name}") if powerbar_info['type'] == 'baytech': try: print("Is a baytech powerbar, getting outlet status") - if powerbar_info['method'] == "telnet": + if powerbar_info['method'] in ["telnet", "batch_telnet"]: outlets = get_baytech_status_outlet_telnet(powerbar_name) try: @@ -38,10 +34,20 @@ def create_app(): except: print(f"Failed to get outlet status from powerbar {powerbar_name}") continue + return + +def create_app(): + # create and configure the app + app = Flask(__name__) + app.config.from_pyfile('settings.py') + app.register_blueprint(routes) + powerbar_status() + scheduler.init_app(app) socketio.init_app(app) return app app = create_app() +scheduler.start() @app.errorhandler(403) def not_authorised(e): @@ -53,3 +59,4 @@ def internal_server_error(e): if not app.config["DEBUG"]: monkey.patch_all() + diff --git a/app/jobs.py b/app/jobs.py new file mode 100644 index 0000000..c8038d7 --- /dev/null +++ b/app/jobs.py @@ -0,0 +1,35 @@ +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) +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(',')) \ No newline at end of file diff --git a/app/settings.py b/app/settings.py index 206de31..93a1c93 100644 --- a/app/settings.py +++ b/app/settings.py @@ -22,7 +22,7 @@ powerbars = { "password" : "admin", "port" : 2167, "type" : "baytech", - "method" : "telnet", + "method" : "batch_telnet", "description" : "Powerbar Controlling Aux Space", "outlets" : { 4 : { "name" : "Power solderlane" }, @@ -45,7 +45,7 @@ powerbars = { "password" : "admin", "port" : 2168, "type" : "baytech", - "method" : "telnet", + "method" : "batch_telnet", "description" : "Powerbar Controlling Main Space", "outlets" : { 9 : { "name" : "3D Printers" }, diff --git a/app/templates/index.html b/app/templates/index.html index 94e83d8..2d92e4d 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -104,7 +104,7 @@ input:checked + .slider:before {