TEST
This commit is contained in:
parent
b560a65f62
commit
0e81584871
1 changed files with 10 additions and 1 deletions
11
app/views.py
11
app/views.py
|
@ -28,14 +28,20 @@ def home():
|
||||||
@routes.route('/powerbar_control/<string:powerbar>/<int:outlet>/<string:action>')
|
@routes.route('/powerbar_control/<string:powerbar>/<int:outlet>/<string:action>')
|
||||||
|
|
||||||
def powerbar_control(powerbar, outlet, action):
|
def powerbar_control(powerbar, outlet, action):
|
||||||
|
print(f"powerbar: {powerbar}")
|
||||||
|
print(f"outlet: {outlet}")
|
||||||
|
print(f"action: {action}")
|
||||||
|
|
||||||
if not action in ['on', 'off', 'cycle']:
|
if not action in ['on', 'off', 'cycle']:
|
||||||
|
print("Invalid action")
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
if not vaild_power_bar(powerbar):
|
if not vaild_power_bar(powerbar):
|
||||||
|
print("Invalid powerbar")
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
if not vaild_outlet(powerbar, outlet):
|
if not vaild_outlet(powerbar, outlet):
|
||||||
|
print("Invalid outlet")
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
if action == 'on':
|
if action == 'on':
|
||||||
|
@ -44,9 +50,11 @@ def powerbar_control(powerbar, outlet, action):
|
||||||
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"on {outlet}\r\n".encode('ascii'))
|
||||||
tn.close()
|
tn.close()
|
||||||
|
print(f"Turned on 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)
|
||||||
|
return "200"
|
||||||
|
|
||||||
if action == 'off':
|
if action == 'off':
|
||||||
# Telnet to powerbar and send command
|
# Telnet to powerbar and send command
|
||||||
|
@ -54,8 +62,9 @@ def powerbar_control(powerbar, outlet, action):
|
||||||
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
|
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
|
||||||
tn.write(f"off {outlet}\r\n".encode('ascii'))
|
tn.write(f"off {outlet}\r\n".encode('ascii'))
|
||||||
tn.close()
|
tn.close()
|
||||||
|
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)
|
||||||
return "yay"
|
return "200"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue