From 26fd0eb802728bf560e2acc21324c11f5dc562d7 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Wed, 22 Nov 2023 10:09:11 +0100 Subject: [PATCH] groups --- app/settings.py | 44 +++++++++++++++------------- app/templates/index.html | 8 ++--- app/views.py | 63 ++++++++++++++-------------------------- 3 files changed, 49 insertions(+), 66 deletions(-) diff --git a/app/settings.py b/app/settings.py index 885e367..95083c7 100644 --- a/app/settings.py +++ b/app/settings.py @@ -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 ] - }, - - ] + } } } diff --git a/app/templates/index.html b/app/templates/index.html index 16d23f0..7d487e3 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -104,10 +104,10 @@ input:checked + .slider:before {
-

Techinc Logo powerbar.ti (2.0.18)

+

Techinc Logo powerbar.ti (2.1.0)


@@ -185,8 +185,8 @@ input:checked + .slider:before {
{{group.name}}
- - + +
diff --git a/app/views.py b/app/views.py index e68ba6c..e275576 100644 --- a/app/views.py +++ b/app/views.py @@ -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('//groups//') -def powebar_group_action(powerbar, group, action): +@routes.route('/groups//') +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('//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}")