From b560a65f6269e1e6f95fe2c89ec360efb1df6798 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Sun, 19 Nov 2023 01:07:25 +0100 Subject: [PATCH] basic controller --- app/__init__.py | 4 +++- app/settings.py | 15 ++++++++++++ app/views.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 3 ++- 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 app/views.py diff --git a/app/__init__.py b/app/__init__.py index a82ac87..34ded42 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,7 +1,9 @@ from flask import Flask, request, redirect, url_for, render_template +from app.views import routes def create_app(): # create and configure the app app = Flask(__name__) app.config.from_pyfile('settings.py') - return app \ No newline at end of file + app.register_blueprint(routes) + return app diff --git a/app/settings.py b/app/settings.py index e69de29..6ae6a52 100644 --- a/app/settings.py +++ b/app/settings.py @@ -0,0 +1,15 @@ + + +powerbars = { + "powerbar-aux-space" : { + "name" : "Powerbar Aux Space", + "host" : "10.209.10.111", + "port" : 3696, + "description" : "Powerbar Controlling Aux Space", + "outlets" : { + 1 : { "name" : "Aux Main Table Light" }, + 13 : { "name" : "Solder Lane Top Light Right" }, + 17 : { "name" : "Solder Lane Top Light Left" }, + } + } +} \ No newline at end of file diff --git a/app/views.py b/app/views.py new file mode 100644 index 0000000..a2e62fe --- /dev/null +++ b/app/views.py @@ -0,0 +1,61 @@ + +from flask import render_template, Blueprint, make_response +from app.settings import powerbars +from flask import abort +import telnetlib + + +routes = Blueprint('routes', __name__) + +def vaild_power_bar(powerbar): + if powerbar in powerbars: + return True + else: + return False + +def vaild_outlet (powerbar, outlet): + if outlet in powerbars[powerbar]['outlets']: + return True + else: + return False + +@routes.route('/') +def home(): + print(powerbars) + return "hello world" + + +@routes.route('/powerbar_control///') + +def powerbar_control(powerbar, outlet, action): + + if not action in ['on', 'off', 'cycle']: + abort(404) + + if not vaild_power_bar(powerbar): + abort(404) + + if not vaild_outlet(powerbar, outlet): + abort(404) + + if action == 'on': + # Telnet to powerbar and send command + try: + tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port']) + tn.write(f"on {outlet}\r\n".encode('ascii')) + tn.close() + except Exception as e: + print(f"Telnet error: {e}") + abort(500) + + if action == 'off': + # Telnet to powerbar and send command + try: + tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port']) + tn.write(f"off {outlet}\r\n".encode('ascii')) + tn.close() + except Exception as e: + print(f"Telnet error: {e}") + abort(500) + return "yay" + diff --git a/requirements.txt b/requirements.txt index 8ab6294..08f8c18 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -flask \ No newline at end of file +flask +telnetlib3 \ No newline at end of file