basic controller
This commit is contained in:
parent
46d624d478
commit
b560a65f62
4 changed files with 81 additions and 2 deletions
|
@ -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
|
||||
app.register_blueprint(routes)
|
||||
return app
|
||||
|
|
|
@ -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" },
|
||||
}
|
||||
}
|
||||
}
|
61
app/views.py
Normal file
61
app/views.py
Normal file
|
@ -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/<string:powerbar>/<int:outlet>/<string:action>')
|
||||
|
||||
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"
|
||||
|
|
@ -1 +1,2 @@
|
|||
flask
|
||||
flask
|
||||
telnetlib3
|
Loading…
Reference in a new issue