This commit is contained in:
Matthew Frost 2023-11-19 01:43:36 +01:00
parent b34ecf5466
commit 765d56ecc7

View file

@ -2,7 +2,9 @@
from flask import render_template, Blueprint, make_response
from app.settings import powerbars
from flask import abort
from flask import jsonify
import telnetlib
from flask import jsonify
routes = Blueprint('routes', __name__)
@ -27,6 +29,7 @@ def home():
@routes.route('/<string:powerbar>/<int:outlet>/<string:action>')
def powerbar_control(powerbar, outlet, action):
print(f"powerbar: {powerbar}")
print(f"outlet: {outlet}")
@ -34,15 +37,15 @@ def powerbar_control(powerbar, outlet, action):
if not action in ['on', 'off', 'cycle']:
print("Invalid action")
abort(404)
return jsonify({'error': 'Invalid action'}), 400
if not vaild_power_bar(powerbar):
print("Invalid powerbar")
abort(404)
return jsonify({'error': 'Invalid powerbar'}), 400
if not vaild_outlet(powerbar, outlet):
print("Invalid outlet")
abort(404)
return jsonify({'error': 'Invalid outlet'}), 400
if action in ['on', 'off']:
try:
@ -54,9 +57,9 @@ def powerbar_control(powerbar, outlet, action):
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
except Exception as 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')
@ -66,11 +69,11 @@ def powerbar_state(powerbar, outlet):
if not vaild_power_bar(powerbar):
print("Invalid powerbar")
abort(404)
return jsonify({'error': 'Invalid powerbar'}), 400
if not vaild_outlet(powerbar, outlet):
print("Invalid outlet")
abort(404)
return jsonify({'error': 'Invalid outlet'}), 400
state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
return state
return jsonify({'state': state})