This commit is contained in:
Matthew Frost 2023-11-19 02:08:55 +01:00
parent 81b40f7e64
commit 81fcc28b1c

View file

@ -4,7 +4,7 @@ from app.settings import powerbars
from flask import abort from flask import abort
from flask import jsonify from flask import jsonify
import telnetlib import telnetlib
from flask import jsonify import time
routes = Blueprint('routes', __name__) routes = Blueprint('routes', __name__)
@ -75,7 +75,18 @@ def powerbar_control(powerbar, outlet, action):
print(f"Telnet error: {e}") print(f"Telnet error: {e}")
return jsonify({'error': 'Telnet error'}), 500 return jsonify({'error': 'Telnet error'}), 500
return jsonify({'state': action}) 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('/<string:powerbar>/<int:outlet>/state') @routes.route('/<string:powerbar>/<int:outlet>/state')
@ -104,6 +115,8 @@ def powerbars_list():
powerbars[powerbar]['outlets'][outlet]['state'] = state powerbars[powerbar]['outlets'][outlet]['state'] = state
powerbars[powerbar]['outlets'][outlet]['on_url'] = f"/{powerbar}/{outlet}/on" powerbars[powerbar]['outlets'][outlet]['on_url'] = f"/{powerbar}/{outlet}/on"
powerbars[powerbar]['outlets'][outlet]['off_url'] = f"/{powerbar}/{outlet}/off" 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) return jsonify(powerbars)