stateless

This commit is contained in:
Matthew Frost 2023-11-19 01:35:52 +01:00
parent b98f192efc
commit 37ebab0e89

View file

@ -44,26 +44,14 @@ def powerbar_control(powerbar, outlet, action):
print("Invalid outlet") print("Invalid outlet")
abort(404) abort(404)
if action == 'on': if action in ['on', 'off']:
try: 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 = 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() tn.close()
powerbars[powerbar]['outlets'][outlet]['state'] = 'on' powerbars[powerbar]['outlets'][outlet]['state'] = action
print(f"Turned on powerbar {powerbar} outlet {outlet}") print(f"Turned {action} 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}")
except Exception as e: except Exception as e:
print(f"Telnet error: {e}") print(f"Telnet error: {e}")
abort(500) abort(500)