session management implemented
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 6s

This commit is contained in:
Matthew Frost 2023-11-24 13:11:36 +01:00
parent cee86b3af2
commit 00238b3033
2 changed files with 19 additions and 7 deletions

File diff suppressed because one or more lines are too long

View file

@ -30,6 +30,17 @@ def get_telnet_connection(powerbar):
return tn return tn
def run_telnet_command(powerbar, command):
try:
tn.write(f"{command}\r\n".encode('ascii'))
return
except Exception as e:
print(f"Telnet error: {e}")
active_telnet_connections.pop(powerbar)
tn = get_telnet_connection(powerbar)
tn.write(f"{command}\r\n".encode('ascii'))
return
def find_group_by_name(name): def find_group_by_name(name):
for group in groups: for group in groups:
@ -98,7 +109,7 @@ def powerbar_control(powerbar, outlet, action):
try: try:
print(f"Turning {action} powerbar {powerbar} outlet {outlet}") print(f"Turning {action} powerbar {powerbar} outlet {outlet}")
tn = get_telnet_connection(powerbar) tn = get_telnet_connection(powerbar)
tn.write(f"{action} {outlet}\r\n".encode('ascii')) run_telnet_command(powerbar,f"{action} {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = action powerbars[powerbar]['outlets'][outlet]['state'] = action
print(f"Turned {action} powerbar {powerbar} outlet {outlet}") print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
except Exception as E: except Exception as E:
@ -109,10 +120,10 @@ def powerbar_control(powerbar, outlet, action):
tn = get_telnet_connection(powerbar) tn = get_telnet_connection(powerbar)
print(f"Turning Off powerbar {powerbar} outlet {outlet}") print(f"Turning Off powerbar {powerbar} outlet {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "off" powerbars[powerbar]['outlets'][outlet]['state'] = "off"
tn.write(f"off {outlet}\r\n".encode('ascii')) run_telnet_command(powerbar,f"off {outlet}")
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')) run_telnet_command(powerbar,f"on {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "on" powerbars[powerbar]['outlets'][outlet]['state'] = "on"
if action == 'toggle': if action == 'toggle':
@ -121,12 +132,13 @@ def powerbar_control(powerbar, outlet, action):
if powerbars[powerbar]['outlets'][outlet]['state'] == "on": if powerbars[powerbar]['outlets'][outlet]['state'] == "on":
tn = get_telnet_connection(powerbar) tn = get_telnet_connection(powerbar)
print(f"Turning Off powerbar {powerbar} outlet {outlet}") print(f"Turning Off powerbar {powerbar} outlet {outlet}")
tn.write(f"off {outlet}\r\n".encode('ascii')) run_telnet_command(powerbar,f"off {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "off" powerbars[powerbar]['outlets'][outlet]['state'] = "off"
elif powerbars[powerbar]['outlets'][outlet]['state'] == "off": elif powerbars[powerbar]['outlets'][outlet]['state'] == "off":
tn = get_telnet_connection(powerbar) tn = get_telnet_connection(powerbar)
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')) run_telnet_command(powerbar,f"on {outlet}")
powerbars[powerbar]['outlets'][outlet]['state'] = "on" 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')})
@ -191,7 +203,7 @@ def powebar_group_action(group, action):
print(f"Powerbar: {powerbar}") print(f"Powerbar: {powerbar}")
outlets = ",".join([str(x) for x in device['outlets']]) outlets = ",".join([str(x) for x in device['outlets']])
print(f"Turning {action} powerbar {powerbar} outlet {outlets}") print(f"Turning {action} powerbar {powerbar} outlet {outlets}")
tn.write(f"{action} {outlets}\r\n".encode('ascii')) run_telnet_command(powerbar,f"{action} {outlets}")
for outlet in device['outlets']: for outlet in device['outlets']:
if outlet in powerbars[powerbar]['outlets']: if outlet in powerbars[powerbar]['outlets']:
powerbars[powerbar]['outlets'][outlet]['state'] = action powerbars[powerbar]['outlets'][outlet]['state'] = action