toggle
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 5s

This commit is contained in:
Matthew Frost 2023-11-20 12:01:18 +01:00
parent 212b05cc3e
commit 4676068942

View file

@ -70,7 +70,7 @@ def powerbar_control(powerbar, outlet, action):
print(f"outlet: {outlet}") print(f"outlet: {outlet}")
print(f"action: {action}") print(f"action: {action}")
if not action in ['on', 'off', 'cycle']: if not action in ['on', 'off', 'cycle', 'toggle']:
print("Invalid action") print("Invalid action")
return jsonify({'error': 'Invalid action'}), 400 return jsonify({'error': 'Invalid action'}), 400
@ -102,8 +102,28 @@ def powerbar_control(powerbar, outlet, action):
time.sleep(5) time.sleep(5)
print(f"Turning On powerbar {powerbar} outlet {outlet}") print(f"Turning On powerbar {powerbar} outlet {outlet}")
tn.write(f"on {outlet}\r\n".encode('ascii')) tn.write(f"on {outlet}\r\n".encode('ascii'))
powerbars[powerbar]['outlets'][outlet]['state'] = "on"
tn.close() tn.close()
powerbars[powerbar]['outlets'][outlet]['state'] = "on"
if action == 'toggle':
print(powerbars[powerbar]['outlets'][outlet]['state'])
if powerbars[powerbar]['outlets'][outlet]['state'] == "on":
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
print(f"Turning Off powerbar {powerbar} outlet {outlet}")
tn.write(f"off {outlet}\r\n".encode('ascii'))
tn.close()
powerbars[powerbar]['outlets'][outlet]['state'] = "off"
elif powerbars[powerbar]['outlets'][outlet]['state'] == "off":
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
print(f"Turning On powerbar {powerbar} outlet {outlet}")
tn.write(f"on {outlet}\r\n".encode('ascii'))
tn.close()
powerbars[powerbar]['outlets'][outlet]['state'] = "on"
return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')})
return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')}) return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')})
@ -136,6 +156,7 @@ def powerbars_list():
powerbars[powerbar]['outlets'][outlet]['off_url'] = f"/{powerbar}/{outlet}/off" powerbars[powerbar]['outlets'][outlet]['off_url'] = f"/{powerbar}/{outlet}/off"
powerbars[powerbar]['outlets'][outlet]['state_url'] = f"/{powerbar}/{outlet}/state" powerbars[powerbar]['outlets'][outlet]['state_url'] = f"/{powerbar}/{outlet}/state"
powerbars[powerbar]['outlets'][outlet]['cycle_url'] = f"/{powerbar}/{outlet}/cycle" powerbars[powerbar]['outlets'][outlet]['cycle_url'] = f"/{powerbar}/{outlet}/cycle"
powerbars[powerbar]['outlets'][outlet]['toggle_url'] = f"/{powerbar}/{outlet}/toggle"
for group in powerbars[powerbar]['groups']: for group in powerbars[powerbar]['groups']: