From 765d56ecc7783ad8a466be1d1c5f7fee137e8de1 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Sun, 19 Nov 2023 01:43:36 +0100 Subject: [PATCH] error --- app/views.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/app/views.py b/app/views.py index 320cb16..70ae074 100644 --- a/app/views.py +++ b/app/views.py @@ -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('///') + 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('///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})