stateless
This commit is contained in:
parent
b98f192efc
commit
37ebab0e89
1 changed files with 5 additions and 17 deletions
22
app/views.py
22
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)
|
||||
|
|
Loading…
Reference in a new issue