groups
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 7s
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 7s
This commit is contained in:
parent
62a34fd54c
commit
26fd0eb802
3 changed files with 49 additions and 66 deletions
|
@ -1,5 +1,26 @@
|
||||||
from app.utils import get_baytech_status_outlet
|
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 = {
|
powerbars = {
|
||||||
"powerbar-aux-space" : {
|
"powerbar-aux-space" : {
|
||||||
"name" : "Powerbar Aux Space",
|
"name" : "Powerbar Aux Space",
|
||||||
|
@ -17,19 +38,9 @@ powerbars = {
|
||||||
17 : { "name" : "Solder Lane Top Light Left" },
|
17 : { "name" : "Solder Lane Top Light Left" },
|
||||||
# 19 : { "name" : "Broken-ish leds entry aux-space" },
|
# 19 : { "name" : "Broken-ish leds entry aux-space" },
|
||||||
20 : { "name" : "Power extension cord top shelf near glass room" },
|
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" : {
|
"powerbar-main-space" : {
|
||||||
"name" : "Powerbar Main Space",
|
"name" : "Powerbar Main Space",
|
||||||
"host" : "10.209.10.111",
|
"host" : "10.209.10.111",
|
||||||
|
@ -54,14 +65,7 @@ powerbars = {
|
||||||
18 : { "name" : "MONITOR_3D_2" },
|
18 : { "name" : "MONITOR_3D_2" },
|
||||||
19 : { "name" : "MONITOR_AV_1" },
|
19 : { "name" : "MONITOR_AV_1" },
|
||||||
20 : { "name" : "MONITOR_AV_2" },
|
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
63
app/views.py
63
app/views.py
|
@ -1,7 +1,6 @@
|
||||||
|
|
||||||
from flask import render_template, Blueprint, make_response
|
from flask import render_template, Blueprint
|
||||||
from app.settings import powerbars
|
from app.settings import powerbars, groups
|
||||||
from flask import abort
|
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
import telnetlib
|
import telnetlib
|
||||||
import time
|
import time
|
||||||
|
@ -9,7 +8,7 @@ import time
|
||||||
|
|
||||||
routes = Blueprint('routes', __name__)
|
routes = Blueprint('routes', __name__)
|
||||||
|
|
||||||
def find_group_by_name(groups, name):
|
def find_group_by_name(name):
|
||||||
for group in groups:
|
for group in groups:
|
||||||
if group["name"] == name:
|
if group["name"] == name:
|
||||||
return group
|
return group
|
||||||
|
@ -29,20 +28,10 @@ def vaild_outlet (powerbar, outlet):
|
||||||
|
|
||||||
@routes.route('/')
|
@routes.route('/')
|
||||||
def home():
|
def home():
|
||||||
|
|
||||||
all_groups = []
|
|
||||||
|
|
||||||
for powerbar in powerbars:
|
for powerbar in powerbars:
|
||||||
for outlet in powerbars[powerbar]['outlets']:
|
for outlet in powerbars[powerbar]['outlets']:
|
||||||
powerbars[powerbar]['outlets'][outlet]['state'] = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
|
powerbars[powerbar]['outlets'][outlet]['state'] = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
|
||||||
|
return render_template('index.html', powerbars=powerbars, all_groups=groups)
|
||||||
for group in powerbars[powerbar]['groups']:
|
|
||||||
|
|
||||||
group["powerbar"] = powerbar
|
|
||||||
all_groups.append(group)
|
|
||||||
|
|
||||||
|
|
||||||
return render_template('index.html', powerbars=powerbars, all_groups=all_groups)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -157,29 +146,19 @@ def powerbars_list():
|
||||||
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"
|
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)
|
return jsonify(powerbars)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@routes.route('/<string:powerbar>/groups/<string:group>/<string:action>')
|
@routes.route('/groups/<string:group>/<string:action>')
|
||||||
def powebar_group_action(powerbar, group, action):
|
def powebar_group_action(group, action):
|
||||||
|
|
||||||
if not action in ['on', 'off', 'cycle']:
|
if not action in ['on', 'off', 'cycle']:
|
||||||
print("Invalid action")
|
print("Invalid action")
|
||||||
return jsonify({'error': 'Invalid action'}), 400
|
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(group)
|
||||||
group = find_group_by_name(powerbars[powerbar]['groups'], group)
|
|
||||||
|
|
||||||
if not group:
|
if not group:
|
||||||
print("Invalid group")
|
print("Invalid group")
|
||||||
|
@ -188,29 +167,29 @@ def powebar_group_action(powerbar, group, action):
|
||||||
if action in ['on', 'off']:
|
if action in ['on', 'off']:
|
||||||
try:
|
try:
|
||||||
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
|
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
|
||||||
outlets = ",".join([str(x) for x in group['outlets']])
|
for device in group["devices"]:
|
||||||
print(f"Turning {action} powerbar {powerbar} outlet {outlets}")
|
powerbar = device["powerbar"]
|
||||||
tn.write(f"{action} {outlets}\r\n".encode('ascii'))
|
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()
|
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"})
|
return jsonify({'status': "200"})
|
||||||
except Exception as E:
|
except Exception as E:
|
||||||
print(f"Telnet error: {E}")
|
print(f"Telnet error: {E}")
|
||||||
return jsonify({'error': 'Telnet error'}), 500
|
return jsonify({'error': 'Telnet error'}), 500
|
||||||
|
|
||||||
@routes.route('/<string:powerbar>/groups')
|
@routes.route('/groups')
|
||||||
def powebar_groups(powerbar):
|
def powebar_groups():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
groups = powerbars[powerbar]['groups']
|
|
||||||
|
|
||||||
for group in groups:
|
for group in groups:
|
||||||
group['on_url'] = f"/{powerbar}/groups/{group['name']}/on"
|
group['on_url'] = f"/groups/{group['name']}/on"
|
||||||
group['off_url'] = f"/{powerbar}/groups/{group['name']}/off"
|
group['off_url'] = f"/groups/{group['name']}/off"
|
||||||
return jsonify(groups)
|
return jsonify(groups)
|
||||||
except Exception as E:
|
except Exception as E:
|
||||||
print(f"Telnet error: {E}")
|
print(f"Telnet error: {E}")
|
||||||
|
|
Loading…
Reference in a new issue