diff --git a/app/settings.py b/app/settings.py index b4060e6..1891146 100644 --- a/app/settings.py +++ b/app/settings.py @@ -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 ] + }, + + ] } } diff --git a/app/templates/index.html b/app/templates/index.html index 3f94127..cdc73d8 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -112,7 +112,7 @@ -

Powerbar.ti (0.1.3)

+

Powerbar.ti (0.1.4)

diff --git a/app/views.py b/app/views.py index d9c34a5..508cbe5 100644 --- a/app/views.py +++ b/app/views.py @@ -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('//groups//') +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('//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 \ No newline at end of file