From 9b1d18da0a16c1c0c332c8ceb3ef502ca9ffebee Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Sat, 27 Jan 2024 16:01:13 +0100 Subject: [PATCH] password based outlets --- app/settings.py | 2 +- app/templates/index.html | 3 ++- app/views.py | 54 +++++++++++++++++++++++++++++++--------- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/settings.py b/app/settings.py index afbef79..832183c 100644 --- a/app/settings.py +++ b/app/settings.py @@ -42,7 +42,7 @@ powerbars = { "description" : "Powerbar Controlling Main Space", "outlets" : { 9 : { "name" : "3D Printers" }, - # 19 : { "name" : "Laser Cutter" }, + 19 : { "name" : "Laser Cutter", "password" : "Ozoa1vaijoilie1ohpho" }, 4 : { "name" : "LIGHT_MAKERTABLE" }, 5 : { "name" : "LIGHT_TABLE" }, 6 : { "name" : "LIGHT_HAM" }, diff --git a/app/templates/index.html b/app/templates/index.html index 593eb87..58ccaca 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -140,6 +140,7 @@ input:checked + .slider:before { diff --git a/app/views.py b/app/views.py index 308224b..0b0771b 100644 --- a/app/views.py +++ b/app/views.py @@ -1,4 +1,4 @@ -from flask import render_template, Blueprint +from flask import render_template, Blueprint, request from app.settings import powerbars, groups from flask import jsonify import telnetlib @@ -111,6 +111,18 @@ def powerbar_control(powerbar, outlet, action): print("Invalid outlet") return jsonify({'error': 'Invalid outlet'}), 400 + + if "password" in powerbars[powerbar]['outlets'][outlet].keys(): + user_password = request.args.get('password') + outlet_password = powerbars[powerbar]['outlets'][outlet]["password"] + + if user_password == None: + print("Password missing") + return jsonify({'error': 'Password missing'}), 400 + + if user_password != outlet_password: + return jsonify({'error': 'Password incorrect'}), 400 + if action in ['on', 'off']: try: print(f"Turning {action} powerbar {powerbar} outlet {outlet}") @@ -134,6 +146,7 @@ def powerbar_control(powerbar, outlet, action): run_telnet_command(powerbar,f"on {outlet}") powerbars[powerbar]['outlets'][outlet]['state'] = "on" app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') + if action == 'toggle': print(powerbars[powerbar]['outlets'][outlet]['state']) @@ -151,8 +164,6 @@ def powerbar_control(powerbar, outlet, action): app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates') return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')}) - - return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')}) @@ -176,17 +187,37 @@ def powerbar_state(powerbar, outlet): @routes.route('/powerbars') def powerbars_list(): - + cleaned_powerbars = {} + for powerbar in powerbars: + + cleaned_powerbar = { + "name" : powerbars[powerbar]['name'], + "description" : powerbars[powerbar]['description'], + "host" : powerbars[powerbar]['host'], + "port" : powerbars[powerbar]['port'], + "type" : powerbars[powerbar]['type'], + "outlets" : {}, + } + for outlet in powerbars[powerbar]['outlets']: + + cleaned_outlet = {} + state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown') - powerbars[powerbar]['outlets'][outlet]['state'] = state - powerbars[powerbar]['outlets'][outlet]['on_url'] = f"/{powerbar}/{outlet}/on" - powerbars[powerbar]['outlets'][outlet]['off_url'] = f"/{powerbar}/{outlet}/off" - 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" - return jsonify(powerbars) + cleaned_outlet['state'] = state + cleaned_outlet['on_url'] = f"/{powerbar}/{outlet}/on" + cleaned_outlet['off_url'] = f"/{powerbar}/{outlet}/off" + cleaned_outlet['state_url'] = f"/{powerbar}/{outlet}/state" + cleaned_outlet['cycle_url'] = f"/{powerbar}/{outlet}/cycle" + cleaned_outlet['toggle_url'] = f"/{powerbar}/{outlet}/toggle" + cleaned_outlet['password_protected'] = True if 'password' in powerbars[powerbar]['outlets'][outlet].keys() else False + + cleaned_powerbar['outlets'][outlet] = cleaned_outlet + + cleaned_powerbars[powerbar] = cleaned_powerbar + + return jsonify(cleaned_powerbars) @@ -226,7 +257,6 @@ def powebar_group_action(group, action): @routes.route('/groups') def powebar_groups(): - try: for group in groups: group['on_url'] = f"/groups/{group['name']}/on"