updates
This commit is contained in:
parent
51844306d1
commit
81b40f7e64
1 changed files with 27 additions and 9 deletions
36
app/views.py
36
app/views.py
|
@ -28,9 +28,25 @@ def home():
|
||||||
|
|
||||||
|
|
||||||
@routes.route('/<string:powerbar>/<int:outlet>/<string:action>')
|
@routes.route('/<string:powerbar>/<int:outlet>/<string:action>')
|
||||||
|
|
||||||
|
|
||||||
def powerbar_control(powerbar, outlet, action):
|
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"powerbar: {powerbar}")
|
||||||
print(f"outlet: {outlet}")
|
print(f"outlet: {outlet}")
|
||||||
print(f"action: {action}")
|
print(f"action: {action}")
|
||||||
|
@ -81,12 +97,14 @@ def powerbar_state(powerbar, outlet):
|
||||||
|
|
||||||
@routes.route('/powerbars')
|
@routes.route('/powerbars')
|
||||||
def powerbars_list():
|
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"
|
||||||
|
|
||||||
|
|
||||||
return jsonify(powerbars)
|
return jsonify(powerbars)
|
||||||
|
|
||||||
@routes.route('/powerbars/<string:powerbar>')
|
|
||||||
def powerbar_outlets(powerbar):
|
|
||||||
if not vaild_power_bar(powerbar):
|
|
||||||
print("Invalid powerbar")
|
|
||||||
return jsonify({'error': 'Invalid powerbar'}), 400
|
|
||||||
|
|
||||||
return jsonify(powerbars[powerbar]['outlets'])
|
|
||||||
|
|
Loading…
Reference in a new issue