test
This commit is contained in:
parent
81b40f7e64
commit
81fcc28b1c
1 changed files with 15 additions and 2 deletions
17
app/views.py
17
app/views.py
|
@ -4,7 +4,7 @@ from app.settings import powerbars
|
|||
from flask import abort
|
||||
from flask import jsonify
|
||||
import telnetlib
|
||||
from flask import jsonify
|
||||
import time
|
||||
|
||||
|
||||
routes = Blueprint('routes', __name__)
|
||||
|
@ -74,8 +74,19 @@ def powerbar_control(powerbar, outlet, action):
|
|||
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': action})
|
||||
return jsonify({'state': powerbars[powerbar]['outlets'][outlet].get('state', 'unknown')})
|
||||
|
||||
|
||||
@routes.route('/<string:powerbar>/<int:outlet>/state')
|
||||
|
@ -104,6 +115,8 @@ def powerbars_list():
|
|||
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)
|
||||
|
|
Loading…
Reference in a new issue