password based outlets
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 37s
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 37s
This commit is contained in:
parent
eba21e54f7
commit
9b1d18da0a
3 changed files with 45 additions and 14 deletions
|
@ -42,7 +42,7 @@ powerbars = {
|
||||||
"description" : "Powerbar Controlling Main Space",
|
"description" : "Powerbar Controlling Main Space",
|
||||||
"outlets" : {
|
"outlets" : {
|
||||||
9 : { "name" : "3D Printers" },
|
9 : { "name" : "3D Printers" },
|
||||||
# 19 : { "name" : "Laser Cutter" },
|
19 : { "name" : "Laser Cutter", "password" : "Ozoa1vaijoilie1ohpho" },
|
||||||
4 : { "name" : "LIGHT_MAKERTABLE" },
|
4 : { "name" : "LIGHT_MAKERTABLE" },
|
||||||
5 : { "name" : "LIGHT_TABLE" },
|
5 : { "name" : "LIGHT_TABLE" },
|
||||||
6 : { "name" : "LIGHT_HAM" },
|
6 : { "name" : "LIGHT_HAM" },
|
||||||
|
|
|
@ -140,6 +140,7 @@ input:checked + .slider:before {
|
||||||
<ul class="list-group list-group-flush">
|
<ul class="list-group list-group-flush">
|
||||||
{% for outlet in powerbar[1].outlets.items() %}
|
{% for outlet in powerbar[1].outlets.items() %}
|
||||||
|
|
||||||
|
{% if not outlet[1].password %}
|
||||||
<li class="list-group-item">
|
<li class="list-group-item">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-8">{{outlet[1].name}}<br><span class="badge badge-secondary">Outlet: {{outlet[0]}}</span>
|
<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>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-2">
|
<div class="col-md-2">
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
54
app/views.py
54
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 app.settings import powerbars, groups
|
||||||
from flask import jsonify
|
from flask import jsonify
|
||||||
import telnetlib
|
import telnetlib
|
||||||
|
@ -111,6 +111,18 @@ def powerbar_control(powerbar, outlet, action):
|
||||||
print("Invalid outlet")
|
print("Invalid outlet")
|
||||||
return jsonify({'error': 'Invalid outlet'}), 400
|
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']:
|
if action in ['on', 'off']:
|
||||||
try:
|
try:
|
||||||
print(f"Turning {action} powerbar {powerbar} outlet {outlet}")
|
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}")
|
run_telnet_command(powerbar,f"on {outlet}")
|
||||||
powerbars[powerbar]['outlets'][outlet]['state'] = "on"
|
powerbars[powerbar]['outlets'][outlet]['state'] = "on"
|
||||||
app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
|
app.socketio.emit('power-event',{'powerbar': powerbar, 'outlet' : outlet, 'action' : action}, namespace='/powerupdates')
|
||||||
|
|
||||||
if action == 'toggle':
|
if action == 'toggle':
|
||||||
print(powerbars[powerbar]['outlets'][outlet]['state'])
|
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')
|
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')})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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')
|
@routes.route('/powerbars')
|
||||||
def powerbars_list():
|
def powerbars_list():
|
||||||
|
cleaned_powerbars = {}
|
||||||
|
|
||||||
for powerbar in 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']:
|
for outlet in powerbars[powerbar]['outlets']:
|
||||||
|
|
||||||
|
cleaned_outlet = {}
|
||||||
|
|
||||||
state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
|
state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
|
||||||
powerbars[powerbar]['outlets'][outlet]['state'] = state
|
cleaned_outlet['state'] = state
|
||||||
powerbars[powerbar]['outlets'][outlet]['on_url'] = f"/{powerbar}/{outlet}/on"
|
cleaned_outlet['on_url'] = f"/{powerbar}/{outlet}/on"
|
||||||
powerbars[powerbar]['outlets'][outlet]['off_url'] = f"/{powerbar}/{outlet}/off"
|
cleaned_outlet['off_url'] = f"/{powerbar}/{outlet}/off"
|
||||||
powerbars[powerbar]['outlets'][outlet]['state_url'] = f"/{powerbar}/{outlet}/state"
|
cleaned_outlet['state_url'] = f"/{powerbar}/{outlet}/state"
|
||||||
powerbars[powerbar]['outlets'][outlet]['cycle_url'] = f"/{powerbar}/{outlet}/cycle"
|
cleaned_outlet['cycle_url'] = f"/{powerbar}/{outlet}/cycle"
|
||||||
powerbars[powerbar]['outlets'][outlet]['toggle_url'] = f"/{powerbar}/{outlet}/toggle"
|
cleaned_outlet['toggle_url'] = f"/{powerbar}/{outlet}/toggle"
|
||||||
return jsonify(powerbars)
|
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')
|
@routes.route('/groups')
|
||||||
def powebar_groups():
|
def powebar_groups():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for group in groups:
|
for group in groups:
|
||||||
group['on_url'] = f"/groups/{group['name']}/on"
|
group['on_url'] = f"/groups/{group['name']}/on"
|
||||||
|
|
Loading…
Reference in a new issue