From 81fcc28b1c69118d6b484421dea72bfe912df0f7 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Sun, 19 Nov 2023 02:08:55 +0100 Subject: [PATCH] test --- app/views.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/views.py b/app/views.py index 4760849..fb1ac28 100644 --- a/app/views.py +++ b/app/views.py @@ -4,7 +4,7 @@ from app.settings import powerbars from flask import abort from flask import jsonify import telnetlib -from flask import jsonify +import time routes = Blueprint('routes', __name__) @@ -74,8 +74,19 @@ def powerbar_control(powerbar, outlet, action): except Exception as e: print(f"Telnet error: {e}") return jsonify({'error': 'Telnet error'}), 500 + + if action == 'cycle': + tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port']) + print(f"Turning Off powerbar {powerbar} outlet {outlet}") + powerbars[powerbar]['outlets'][outlet]['state'] = "off" + tn.write(f"off {outlet}\r\n".encode('ascii')) + time.sleep(5) + print(f"Turning On powerbar {powerbar} outlet {outlet}") + tn.write(f"on {outlet}\r\n".encode('ascii')) + powerbars[powerbar]['outlets'][outlet]['state'] = "on" + tn.close() - return jsonify({'state': action}) + return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')}) @routes.route('///state') @@ -104,6 +115,8 @@ def powerbars_list(): powerbars[powerbar]['outlets'][outlet]['state'] = state powerbars[powerbar]['outlets'][outlet]['on_url'] = f"/{powerbar}/{outlet}/on" powerbars[powerbar]['outlets'][outlet]['off_url'] = f"/{powerbar}/{outlet}/off" + powerbars[powerbar]['outlets'][outlet]['state_url'] = f"/{powerbar}/{outlet}/state" + powerbars[powerbar]['outlets'][outlet]['cycle_url'] = f"/{powerbar}/{outlet}/cycle" return jsonify(powerbars)