diff --git a/app/views.py b/app/views.py index 5f96342..4760849 100644 --- a/app/views.py +++ b/app/views.py @@ -28,9 +28,25 @@ def home(): @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}") @@ -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/') -def powerbar_outlets(powerbar): - if not vaild_power_bar(powerbar): - print("Invalid powerbar") - return jsonify({'error': 'Invalid powerbar'}), 400 - - return jsonify(powerbars[powerbar]['outlets'])