Skip to content

Commit

Permalink
feat: add config modification from API
Browse files Browse the repository at this point in the history
  • Loading branch information
flobz committed Jan 8, 2024
1 parent b61a038 commit 53f6c95
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/psa_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@
12. Lock (1)/Unlock (0) the doors

http://localhost:5000/lock_door/YOURVIN/1 or 0

13. Get config

http://localhost:5000/settings

14. Change config parameter in config.ini

http://localhost:5000/settings/electricity_config?night_price=0.2

http://127.0.0.1:5000/settings/general?currency=%C2%A3
10 changes: 10 additions & 0 deletions psa_car_controller/web/tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from datetime import datetime, timedelta

import dash_bootstrap_components as dbc
Expand Down Expand Up @@ -88,3 +89,12 @@ def diff_dashtable(data, data_previous, row_id_name="row_id"):
}
)
return changes


def convert_to_number_if_number_else_return_str(value: str) -> typing.Union[float, int, str]:
if value.isnumeric():
return int(value)
try:
return float(value)
except ValueError:
return value
33 changes: 33 additions & 0 deletions psa_car_controller/web/view/api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import logging

from flask import jsonify, request, Response as FlaskResponse
from pydantic import BaseModel

from psa_car_controller.common.utils import RateLimitException
from psa_car_controller.psacc.application.car_controller import PSACarController
from psa_car_controller.psacc.repository.db import Database
from psa_car_controller.web.app import app

import json

from psa_car_controller.web.tools.utils import convert_to_number_if_number_else_return_str

logger = logging.getLogger(__name__)

STYLE_CACHE = None
APP = PSACarController()


def json_response(json: str, status=200):
return app.response_class(
response=json,
status=status,
mimetype='application/json'
)


@app.route('/get_vehicles')
def get_vehicules():
response = app.response_class(
Expand Down Expand Up @@ -149,3 +166,19 @@ def lock_door(vin, lock):
return jsonify(APP.myp.remote_client.lock_door(vin, lock))
except RateLimitException:
return jsonify({"error": "Locks rate limit exceeded"})


@app.route('/settings/<string:section>')
def settings_section(section: str):
config_section: BaseModel = getattr(APP.config, section.capitalize())
for key, value in request.args.items():
typed_value = convert_to_number_if_number_else_return_str(value)
setattr(config_section, key, typed_value)
APP.config.write_config()
return json_response(config_section.json())


@app.route('/settings')
def settings():
return json_response(APP.config.json())

0 comments on commit 53f6c95

Please sign in to comment.