error
This commit is contained in:
parent
b34ecf5466
commit
765d56ecc7
1 changed files with 11 additions and 8 deletions
19
app/views.py
19
app/views.py
|
@ -2,7 +2,9 @@
|
||||||
from flask import render_template, Blueprint, make_response
|
from flask import render_template, Blueprint, make_response
|
||||||
from app.settings import powerbars
|
from app.settings import powerbars
|
||||||
from flask import abort
|
from flask import abort
|
||||||
|
from flask import jsonify
|
||||||
import telnetlib
|
import telnetlib
|
||||||
|
from flask import jsonify
|
||||||
|
|
||||||
|
|
||||||
routes = Blueprint('routes', __name__)
|
routes = Blueprint('routes', __name__)
|
||||||
|
@ -27,6 +29,7 @@ def home():
|
||||||
|
|
||||||
@routes.route('/<string:powerbar>/<int:outlet>/<string:action>')
|
@routes.route('/<string:powerbar>/<int:outlet>/<string:action>')
|
||||||
|
|
||||||
|
|
||||||
def powerbar_control(powerbar, outlet, action):
|
def powerbar_control(powerbar, outlet, action):
|
||||||
print(f"powerbar: {powerbar}")
|
print(f"powerbar: {powerbar}")
|
||||||
print(f"outlet: {outlet}")
|
print(f"outlet: {outlet}")
|
||||||
|
@ -34,15 +37,15 @@ def powerbar_control(powerbar, outlet, action):
|
||||||
|
|
||||||
if not action in ['on', 'off', 'cycle']:
|
if not action in ['on', 'off', 'cycle']:
|
||||||
print("Invalid action")
|
print("Invalid action")
|
||||||
abort(404)
|
return jsonify({'error': 'Invalid action'}), 400
|
||||||
|
|
||||||
if not vaild_power_bar(powerbar):
|
if not vaild_power_bar(powerbar):
|
||||||
print("Invalid powerbar")
|
print("Invalid powerbar")
|
||||||
abort(404)
|
return jsonify({'error': 'Invalid powerbar'}), 400
|
||||||
|
|
||||||
if not vaild_outlet(powerbar, outlet):
|
if not vaild_outlet(powerbar, outlet):
|
||||||
print("Invalid outlet")
|
print("Invalid outlet")
|
||||||
abort(404)
|
return jsonify({'error': 'Invalid outlet'}), 400
|
||||||
|
|
||||||
if action in ['on', 'off']:
|
if action in ['on', 'off']:
|
||||||
try:
|
try:
|
||||||
|
@ -54,9 +57,9 @@ def powerbar_control(powerbar, outlet, action):
|
||||||
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
|
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Telnet error: {e}")
|
print(f"Telnet error: {e}")
|
||||||
abort(500)
|
return jsonify({'error': 'Telnet error'}), 500
|
||||||
|
|
||||||
return action
|
return jsonify({'state': action})
|
||||||
|
|
||||||
|
|
||||||
@routes.route('/<string:powerbar>/<int:outlet>/state')
|
@routes.route('/<string:powerbar>/<int:outlet>/state')
|
||||||
|
@ -66,11 +69,11 @@ def powerbar_state(powerbar, outlet):
|
||||||
|
|
||||||
if not vaild_power_bar(powerbar):
|
if not vaild_power_bar(powerbar):
|
||||||
print("Invalid powerbar")
|
print("Invalid powerbar")
|
||||||
abort(404)
|
return jsonify({'error': 'Invalid powerbar'}), 400
|
||||||
|
|
||||||
if not vaild_outlet(powerbar, outlet):
|
if not vaild_outlet(powerbar, outlet):
|
||||||
print("Invalid outlet")
|
print("Invalid outlet")
|
||||||
abort(404)
|
return jsonify({'error': 'Invalid outlet'}), 400
|
||||||
|
|
||||||
state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
|
state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
|
||||||
return state
|
return jsonify({'state': state})
|
||||||
|
|
Loading…
Reference in a new issue