from flask import render_template, Blueprint, make_response from app.settings import powerbars from flask import abort from flask import jsonify import telnetlib import time 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(): return render_template('index.html') @routes.route('///') def powerbar_control(powerbar, outlet, action): """ Controls the powerbar outlet based on the given parameters. 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'). 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. """ print(f"powerbar: {powerbar}") print(f"outlet: {outlet}") print(f"action: {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 if not vaild_outlet(powerbar, outlet): print("Invalid outlet") return jsonify({'error': 'Invalid outlet'}), 400 if action in ['on', 'off']: try: 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}") except Exception as e: print(f"Telnet error: {e}") return jsonify({'error': 'Telnet error'}), 500 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() return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')}) @routes.route('///state') def powerbar_state(powerbar, outlet): print(f"powerbar: {powerbar}") print(f"outlet: {outlet}") if not vaild_power_bar(powerbar): print("Invalid powerbar") return jsonify({'error': 'Invalid powerbar'}), 400 if not vaild_outlet(powerbar, outlet): print("Invalid outlet") return jsonify({'error': 'Invalid outlet'}), 400 state = powerbars[powerbar]['outlets'][outlet].get('state', 'unknown') return jsonify({'state': state}) @routes.route('/powerbars') def powerbars_list(): 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" powerbars[powerbar]['outlets'][outlet]['state_url'] = f"/{powerbar}/{outlet}/state" powerbars[powerbar]['outlets'][outlet]['cycle_url'] = f"/{powerbar}/{outlet}/cycle" return jsonify(powerbars)