password based outlets
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 37s

This commit is contained in:
Matthew Frost 2024-01-27 16:01:13 +01:00
parent eba21e54f7
commit 9b1d18da0a
3 changed files with 45 additions and 14 deletions

View file

@ -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" },

View file

@ -140,6 +140,7 @@ input:checked + .slider:before {
<ul class="list-group list-group-flush">
{% for outlet in powerbar[1].outlets.items() %}
{% if not outlet[1].password %}
<li class="list-group-item">
<div class="row">
<div class="col-md-8">{{outlet[1].name}}<br><span class="badge badge-secondary">Outlet: {{outlet[0]}}</span>
@ -159,10 +160,10 @@ input:checked + .slider:before {
</label>
</div>
<div class="col-md-2">
</div>
</div>
</li>
{% endif %}
{% endfor %}
</ul>

View file

@ -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'])
@ -152,8 +165,6 @@ def powerbar_control(powerbar, outlet, action):
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"