This commit is contained in:
Matthew Frost 2023-11-19 01:33:11 +01:00
parent 45a0b72266
commit b98f192efc

View file

@ -50,6 +50,7 @@ def powerbar_control(powerbar, outlet, action):
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
tn.write(f"on {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}")
@ -61,6 +62,7 @@ def powerbar_control(powerbar, outlet, action):
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:
print(f"Telnet error: {e}")
@ -68,3 +70,19 @@ def powerbar_control(powerbar, outlet, action):
return "200"
@routes.route('/powerbar_state/<string:powerbar>/<int:outlet>')
def powerbar_state(powerbar, outlet):
print(f"powerbar: {powerbar}")
print(f"outlet: {outlet}")
if not vaild_power_bar(powerbar):
print("Invalid powerbar")
abort(404)
if not vaild_outlet(powerbar, outlet):
print("Invalid outlet")
abort(404)
state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
return state