From 37ebab0e8942a3346960f01d2593b70757258c4a Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Sun, 19 Nov 2023 01:35:52 +0100 Subject: [PATCH] stateless --- app/views.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/app/views.py b/app/views.py index e2f8a6f..5134085 100644 --- a/app/views.py +++ b/app/views.py @@ -44,26 +44,14 @@ def powerbar_control(powerbar, outlet, action): print("Invalid outlet") abort(404) - if action == 'on': + if action in ['on', 'off']: try: - print(f"Turning on powerbar {powerbar} outlet {outlet}") + print(f"Turning {action} powerbar {powerbar} outlet {outlet}") tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port']) - tn.write(f"on {outlet}\r\n".encode('ascii')) + tn.write(f"{action} {outlet}\r\n".encode('ascii')) tn.close() - powerbars[powerbar]['outlets'][outlet]['state'] = 'on' - print(f"Turned on powerbar {powerbar} outlet {outlet}") - except Exception as e: - print(f"Telnet error: {e}") - abort(500) - - if action == 'off': - try: - print(f"Turning off powerbar {powerbar} outlet {outlet}") - tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port']) - tn.write(f"off {outlet}\r\n".encode('ascii')) - tn.close() - powerbars[powerbar]['outlets'][outlet]['state'] = 'off' - print(f"Turned off powerbar {powerbar} outlet {outlet}") + powerbars[powerbar]['outlets'][outlet]['state'] = action + print(f"Turned {action} powerbar {powerbar} outlet {outlet}") except Exception as e: print(f"Telnet error: {e}") abort(500)