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

This commit is contained in:
Matthew Frost 2023-11-22 10:09:11 +01:00
parent 62a34fd54c
commit 26fd0eb802
3 changed files with 49 additions and 66 deletions

View file

@ -1,5 +1,26 @@
from app.utils import get_baytech_status_outlet
groups = [
{
"name" : "Solder Lane",
"devices" : [ { "powerbar": "powerbar-aux-space", "outlets" : [ 4, 17, 13 ] } ],
},
{
"name" : "Aux Space Lights",
"devices" : [ { "powerbar": "powerbar-aux-space", "outlets" : [ 1, 17, 13] } ],
},
{
"name" : "Main Space Lights",
"devices" : [ { "powerbar" : "powerbar-main-space", "outlets" : [ 4, 5, 6, 7, 8, 9, 10, 11, 12, 15 ] } ],
},
{
"name" : "All Space Lights",
"devices" : [ { "powerbar" : "powerbar-main-space", "outlets" : [ 4, 5, 6, 7, 8, 9, 10, 11, 12, 15 ] },
{ "powerbar" : "powerbar-aux-space", "outlets" : [ 1, 17, 13] }],
},
]
powerbars = {
"powerbar-aux-space" : {
"name" : "Powerbar Aux Space",
@ -17,19 +38,9 @@ powerbars = {
17 : { "name" : "Solder Lane Top Light Left" },
# 19 : { "name" : "Broken-ish leds entry aux-space" },
20 : { "name" : "Power extension cord top shelf near glass room" },
},
"groups": [
{
"name" : "Solder Lane",
"outlets" : [ 4, 17, 13 ]
},
{
"name" : "Aux Space Lights",
"outlets" : [ 1, 17, 13]
},
]
}
},
"powerbar-main-space" : {
"name" : "Powerbar Main Space",
"host" : "10.209.10.111",
@ -54,14 +65,7 @@ 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 ]
},
]
}
}
}

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,6 @@
from flask import render_template, Blueprint, make_response
from app.settings import powerbars
from flask import abort
from flask import render_template, Blueprint
from app.settings import powerbars, groups
from flask import jsonify
import telnetlib
import time
@ -9,7 +8,7 @@ import time
routes = Blueprint('routes', __name__)
def find_group_by_name(groups, name):
def find_group_by_name(name):
for group in groups:
if group["name"] == name:
return group
@ -29,20 +28,10 @@ def vaild_outlet (powerbar, outlet):
@routes.route('/')
def home():
all_groups = []
for powerbar in powerbars:
for outlet in powerbars[powerbar]['outlets']:
powerbars[powerbar]['outlets'][outlet]['state'] = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
for group in powerbars[powerbar]['groups']:
group["powerbar"] = powerbar
all_groups.append(group)
return render_template('index.html', powerbars=powerbars, all_groups=all_groups)
return render_template('index.html', powerbars=powerbars, all_groups=groups)
@ -157,29 +146,19 @@ def powerbars_list():
powerbars[powerbar]['outlets'][outlet]['state_url'] = f"/{powerbar}/{outlet}/state"
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']:
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):
@routes.route('/groups/<string:group>/<string:action>')
def powebar_group_action(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)
group = find_group_by_name(group)
if not group:
print("Invalid group")
@ -188,29 +167,29 @@ def powebar_group_action(powerbar, group, action):
if action in ['on', 'off']:
try:
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
outlets = ",".join([str(x) for x in group['outlets']])
print(f"Turning {action} powerbar {powerbar} outlet {outlets}")
tn.write(f"{action} {outlets}\r\n".encode('ascii'))
for device in group["devices"]:
powerbar = device["powerbar"]
print(f"Powerbar: {powerbar}")
outlets = ",".join([str(x) for x in device['outlets']])
print(f"Turning {action} powerbar {powerbar} outlet {outlets}")
tn.write(f"{action} {outlets}\r\n".encode('ascii'))
for outlet in outlets:
powerbars[powerbar]['outlets'][outlet]['state'] = action
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
tn.close()
for outlet in group['outlets']:
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):
@routes.route('/groups')
def powebar_groups():
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"
group['on_url'] = f"/groups/{group['name']}/on"
group['off_url'] = f"/groups/{group['name']}/off"
return jsonify(groups)
except Exception as E:
print(f"Telnet error: {E}")