groups
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-19 13:26:12 +01:00
parent e80b4eb4be
commit 94d10f9e67
3 changed files with 71 additions and 7 deletions

View file

@ -20,11 +20,11 @@ powerbars = {
"groups": [
{
"name" : "Solder Lane",
"outputs" : [ 4, 17, 13 ]
"outlets" : [ 4, 17, 13 ]
},
{
"name" : "Aux Main Table",
"outputs" : [ 1 ]
"name" : "Aux Space Lights",
"outlets" : [ 1 ]
},
]
@ -52,6 +52,13 @@ powerbars = {
18 : { "name" : "MONITOR_3D_2" },
19 : { "name" : "MONITOR_AV_1" },
20 : { "name" : "MONITOR_AV_2" },
}
},
"groups": [
{
"name" : "Main Space Lights",
"outlets" : [ 4, 5, 6, 7, 8, 9, 10, 11, 12, 15 ]
},
]
}
}

View file

@ -112,7 +112,7 @@
</style>
</head>
<body>
<h1>Powerbar.ti (0.1.3)</h1>
<h1>Powerbar.ti (0.1.4)</h1>
<div class="search-container">
<div class="container">
<input type="text" id="searchInput" placeholder="Search outlet...">

View file

@ -9,6 +9,12 @@ import time
routes = Blueprint('routes', __name__)
def find_group_by_name(groups, name):
for group in groups:
if group["name"] == name:
return group
return None
def vaild_power_bar(powerbar):
if powerbar in powerbars:
return True
@ -71,8 +77,8 @@ def powerbar_control(powerbar, outlet, action):
tn.close()
powerbars[powerbar]['outlets'][outlet]['state'] = action
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
except Exception as e:
print(f"Telnet error: {e}")
except Exception as E:
print(f"Telnet error: {E}")
return jsonify({'error': 'Telnet error'}), 500
if action == 'cycle':
@ -119,5 +125,56 @@ def powerbars_list():
powerbars[powerbar]['outlets'][outlet]['cycle_url'] = f"/{powerbar}/{outlet}/cycle"
for group in powerbars[powerbar]['groups']:
group['on_url'] = f"/{powerbar}/groups/{group['name']}/on"
group['off_url'] = f"/{powerbar}/groups/{group['name']}/off"
return jsonify(powerbars)
@routes.route('/<string:powerbar>/groups/<string:group>/<string:action>')
def powebar_group_action(powerbar, group, action):
if not action in ['on', 'off', 'cycle']:
print("Invalid action")
return jsonify({'error': 'Invalid action'}), 400
if not vaild_power_bar(powerbar):
print("Invalid powerbar")
return jsonify({'error': 'Invalid powerbar'}), 400
group = find_group_by_name(powerbars[powerbar]['groups'], group)
if not group:
print("Invalid group")
return jsonify({'error': 'Invalid group'}), 400
if action in ['on', 'off']:
try:
for outlet in group['outlets']:
print(f"Turning {action} powerbar {powerbar} outlet {outlet}")
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
tn.write(f"{action} {outlet}\r\n".encode('ascii'))
tn.close()
powerbars[powerbar]['outlets'][outlet]['state'] = action
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
return jsonify({'status': "200"})
except Exception as E:
print(f"Telnet error: {E}")
return jsonify({'error': 'Telnet error'}), 500
@routes.route('/<string:powerbar>/groups')
def powebar_groups(powerbar):
try:
groups = powerbars[powerbar]['groups']
for group in groups:
group['on_url'] = f"/{powerbar}/groups/{group['name']}/on"
group['off_url'] = f"/{powerbar}/groups/{group['name']}/off"
return jsonify(groups)
except Exception as E:
print(f"Telnet error: {E}")
return jsonify({'error': 'no groups defined for powerbar'}), 404