2023-11-19 00:07:25 +00:00
|
|
|
|
|
|
|
from flask import render_template, Blueprint, make_response
|
|
|
|
from app.settings import powerbars
|
|
|
|
from flask import abort
|
2023-11-19 00:43:36 +00:00
|
|
|
from flask import jsonify
|
2023-11-19 00:07:25 +00:00
|
|
|
import telnetlib
|
2023-11-19 01:08:55 +00:00
|
|
|
import time
|
2023-11-19 00:07:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
routes = Blueprint('routes', __name__)
|
|
|
|
|
2023-11-19 12:26:12 +00:00
|
|
|
def find_group_by_name(groups, name):
|
|
|
|
for group in groups:
|
|
|
|
if group["name"] == name:
|
|
|
|
return group
|
|
|
|
return None
|
|
|
|
|
2023-11-19 00:07:25 +00:00
|
|
|
def vaild_power_bar(powerbar):
|
|
|
|
if powerbar in powerbars:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def vaild_outlet (powerbar, outlet):
|
|
|
|
if outlet in powerbars[powerbar]['outlets']:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@routes.route('/')
|
|
|
|
def home():
|
2023-11-19 16:06:40 +00:00
|
|
|
|
|
|
|
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']:
|
2023-11-19 16:49:38 +00:00
|
|
|
|
|
|
|
group["powerbar"] = powerbar
|
2023-11-19 16:06:40 +00:00
|
|
|
all_groups.append(group)
|
|
|
|
|
|
|
|
|
|
|
|
return render_template('index.html', powerbars=powerbars, all_groups=all_groups)
|
2023-11-19 01:30:54 +00:00
|
|
|
|
2023-11-19 00:07:25 +00:00
|
|
|
|
|
|
|
|
2023-11-19 00:38:44 +00:00
|
|
|
@routes.route('/<string:powerbar>/<int:outlet>/<string:action>')
|
2023-11-19 01:00:53 +00:00
|
|
|
def powerbar_control(powerbar, outlet, action):
|
|
|
|
"""
|
|
|
|
Controls the powerbar outlet based on the given parameters.
|
2023-11-19 00:07:25 +00:00
|
|
|
|
2023-11-19 01:00:53 +00:00
|
|
|
Args:
|
|
|
|
powerbar (str): The name of the powerbar.
|
|
|
|
outlet (int): The outlet number.
|
|
|
|
action (str): The action to perform on the outlet ('on', 'off', 'cycle').
|
2023-11-19 00:43:36 +00:00
|
|
|
|
2023-11-19 01:00:53 +00:00
|
|
|
Returns:
|
|
|
|
dict: A JSON response containing the state of the action or an error message.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: If the action is not valid.
|
|
|
|
ValueError: If the powerbar is not valid.
|
|
|
|
ValueError: If the outlet is not valid.
|
|
|
|
Exception: If there is a telnet error.
|
|
|
|
|
|
|
|
"""
|
2023-11-19 00:10:51 +00:00
|
|
|
print(f"powerbar: {powerbar}")
|
|
|
|
print(f"outlet: {outlet}")
|
|
|
|
print(f"action: {action}")
|
2023-11-19 00:07:25 +00:00
|
|
|
|
|
|
|
if not action in ['on', 'off', 'cycle']:
|
2023-11-19 00:10:51 +00:00
|
|
|
print("Invalid action")
|
2023-11-19 00:43:36 +00:00
|
|
|
return jsonify({'error': 'Invalid action'}), 400
|
2023-11-19 00:07:25 +00:00
|
|
|
|
|
|
|
if not vaild_power_bar(powerbar):
|
2023-11-19 00:10:51 +00:00
|
|
|
print("Invalid powerbar")
|
2023-11-19 00:43:36 +00:00
|
|
|
return jsonify({'error': 'Invalid powerbar'}), 400
|
2023-11-19 00:07:25 +00:00
|
|
|
|
|
|
|
if not vaild_outlet(powerbar, outlet):
|
2023-11-19 00:10:51 +00:00
|
|
|
print("Invalid outlet")
|
2023-11-19 00:43:36 +00:00
|
|
|
return jsonify({'error': 'Invalid outlet'}), 400
|
2023-11-19 00:07:25 +00:00
|
|
|
|
2023-11-19 00:35:52 +00:00
|
|
|
if action in ['on', 'off']:
|
2023-11-19 00:07:25 +00:00
|
|
|
try:
|
2023-11-19 00:35:52 +00:00
|
|
|
print(f"Turning {action} powerbar {powerbar} outlet {outlet}")
|
2023-11-19 00:07:25 +00:00
|
|
|
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
|
2023-11-19 00:35:52 +00:00
|
|
|
tn.write(f"{action} {outlet}\r\n".encode('ascii'))
|
2023-11-19 00:07:25 +00:00
|
|
|
tn.close()
|
2023-11-19 00:35:52 +00:00
|
|
|
powerbars[powerbar]['outlets'][outlet]['state'] = action
|
|
|
|
print(f"Turned {action} powerbar {powerbar} outlet {outlet}")
|
2023-11-19 12:26:12 +00:00
|
|
|
except Exception as E:
|
|
|
|
print(f"Telnet error: {E}")
|
2023-11-19 00:43:36 +00:00
|
|
|
return jsonify({'error': 'Telnet error'}), 500
|
2023-11-19 01:08:55 +00:00
|
|
|
|
|
|
|
if action == 'cycle':
|
|
|
|
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
|
|
|
|
print(f"Turning Off powerbar {powerbar} outlet {outlet}")
|
|
|
|
powerbars[powerbar]['outlets'][outlet]['state'] = "off"
|
|
|
|
tn.write(f"off {outlet}\r\n".encode('ascii'))
|
|
|
|
time.sleep(5)
|
|
|
|
print(f"Turning On powerbar {powerbar} outlet {outlet}")
|
|
|
|
tn.write(f"on {outlet}\r\n".encode('ascii'))
|
|
|
|
powerbars[powerbar]['outlets'][outlet]['state'] = "on"
|
|
|
|
tn.close()
|
2023-11-19 00:16:41 +00:00
|
|
|
|
2023-11-19 01:08:55 +00:00
|
|
|
return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')})
|
2023-11-19 00:07:25 +00:00
|
|
|
|
2023-11-19 00:33:11 +00:00
|
|
|
|
2023-11-19 00:38:44 +00:00
|
|
|
@routes.route('/<string:powerbar>/<int:outlet>/state')
|
2023-11-19 00:33:11 +00:00
|
|
|
def powerbar_state(powerbar, outlet):
|
|
|
|
print(f"powerbar: {powerbar}")
|
|
|
|
print(f"outlet: {outlet}")
|
|
|
|
|
|
|
|
if not vaild_power_bar(powerbar):
|
|
|
|
print("Invalid powerbar")
|
2023-11-19 00:43:36 +00:00
|
|
|
return jsonify({'error': 'Invalid powerbar'}), 400
|
2023-11-19 00:33:11 +00:00
|
|
|
|
|
|
|
if not vaild_outlet(powerbar, outlet):
|
|
|
|
print("Invalid outlet")
|
2023-11-19 00:43:36 +00:00
|
|
|
return jsonify({'error': 'Invalid outlet'}), 400
|
2023-11-19 00:33:11 +00:00
|
|
|
|
|
|
|
state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')
|
2023-11-19 00:43:36 +00:00
|
|
|
return jsonify({'state': state})
|
2023-11-19 00:55:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@routes.route('/powerbars')
|
|
|
|
def powerbars_list():
|
|
|
|
|
2023-11-19 01:00:53 +00:00
|
|
|
for powerbar in powerbars:
|
|
|
|
for outlet in powerbars[powerbar]['outlets']:
|
|
|
|
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"
|
2023-11-19 01:08:55 +00:00
|
|
|
powerbars[powerbar]['outlets'][outlet]['state_url'] = f"/{powerbar}/{outlet}/state"
|
|
|
|
powerbars[powerbar]['outlets'][outlet]['cycle_url'] = f"/{powerbar}/{outlet}/cycle"
|
2023-11-19 01:00:53 +00:00
|
|
|
|
|
|
|
|
2023-11-19 12:26:12 +00:00
|
|
|
for group in powerbars[powerbar]['groups']:
|
|
|
|
group['on_url'] = f"/{powerbar}/groups/{group['name']}/on"
|
|
|
|
group['off_url'] = f"/{powerbar}/groups/{group['name']}/off"
|
|
|
|
|
2023-11-19 01:00:53 +00:00
|
|
|
return jsonify(powerbars)
|
2023-11-19 00:55:54 +00:00
|
|
|
|
2023-11-19 12:26:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@routes.route('/<string:powerbar>/groups/<string:group>/<string:action>')
|
|
|
|
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}")
|
2023-11-19 17:13:49 +00:00
|
|
|
time.sleep(1)
|
|
|
|
|
2023-11-19 12:26:12 +00:00
|
|
|
return jsonify({'status': "200"})
|
|
|
|
except Exception as E:
|
|
|
|
print(f"Telnet error: {E}")
|
|
|
|
return jsonify({'error': 'Telnet error'}), 500
|
|
|
|
|
|
|
|
@routes.route('/<string:powerbar>/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
|