This commit is contained in:
Matthew Frost 2023-11-19 02:00:53 +01:00
parent 51844306d1
commit 81b40f7e64

View file

@ -28,9 +28,25 @@ def home():
@routes.route('/<string:powerbar>/<int:outlet>/<string: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"outlet: {outlet}")
print(f"action: {action}")
@ -81,12 +97,14 @@ def powerbar_state(powerbar, outlet):
@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"
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'])