From 007a53ac8c3c5f9ad85637c7f088d680abd3db4f Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Mon, 10 May 2021 17:58:35 +0200 Subject: [PATCH] add from_cache option --- README.md | 3 +++ my_psacc.py | 29 ++++++++++++++++------------- web/views.py | 3 ++- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index c8fe49f..d4bc19d 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ For information on configuring the psa_car_controller Docker container [see this 2.1 Get the car state : http://localhost:5000/get_vehicleinfo/YOURVIN + 2.2 Get the car state from cache to avoid to use psa api too much + http://localhost:5000/get_vehicleinfo/YOURVIN?from_cache=1 + 2.2 Stop charge http://localhost:5000/charge_now/YOURVIN/0 diff --git a/my_psacc.py b/my_psacc.py index ee5cdd1..692cd8e 100644 --- a/my_psacc.py +++ b/my_psacc.py @@ -134,21 +134,24 @@ class MyPSACC: self.manager.proxies = self._proxies Otp.set_proxies(proxies) - def get_vehicle_info(self, vin): + def get_vehicle_info(self, vin, cache=False): res = None car = self.vehicles_list.get_car_by_vin(vin) - for _ in range(0, 2): - try: - res = self.api().get_vehicle_status(car.vehicle_id, extension=["odometer"]) - if res is not None: - car.status = res - if self._record_enabled: - self.record_info(car) - return res - except (ApiException, InvalidHeader) as ex: - logger.error("get_vehicle_info: ApiException: %s", ex) - logger.debug(exc_info=True) - car.status = res + if cache and car.status is not None: + res = car.status + else: + for _ in range(0, 2): + try: + res = self.api().get_vehicle_status(car.vehicle_id, extension=["odometer"]) + if res is not None: + car.status = res + if self._record_enabled: + self.record_info(car) + return res + except (ApiException, InvalidHeader) as ex: + logger.error("get_vehicle_info: ApiException: %s", ex) + logger.debug(exc_info=True) + car.status = res return res def refresh_vehicle_info(self): diff --git a/web/views.py b/web/views.py index 822f678..e5daa0d 100644 --- a/web/views.py +++ b/web/views.py @@ -151,8 +151,9 @@ def get_vehicules(): @app.route('/get_vehicleinfo/') def get_vehicle_info(vin): + from_cache = int(request.args.get('from_cache', 0)) == 1 response = app.response_class( - response=json.dumps(myp.get_vehicle_info(vin).to_dict(), default=str), + response=json.dumps(myp.get_vehicle_info(vin, from_cache).to_dict(), default=str), status=200, mimetype='application/json' )