This commit is contained in:
Matthew Frost 2023-11-19 01:10:51 +01:00
parent b560a65f62
commit 0e81584871

View file

@ -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"