ti-powerbar/app/views.py

80 lines
2.3 KiB
Python
Raw Normal View History

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 00:43:36 +00:00
from flask import jsonify
2023-11-19 00:07:25 +00:00
routes = Blueprint('routes', __name__)
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():
print(powerbars)
return "hello world"
2023-11-19 00:38:44 +00:00
@routes.route('/<string:powerbar>/<int:outlet>/<string:action>')
2023-11-19 00:07:25 +00:00
2023-11-19 00:43:36 +00:00
2023-11-19 00:07:25 +00:00
def powerbar_control(powerbar, outlet, action):
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 00:07:25 +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 00:16:41 +00:00
2023-11-19 00:43:36 +00:00
return jsonify({'state': action})
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})