add from_cache option

This commit is contained in:
Florian Bezannier
2021-05-10 17:58:35 +02:00
parent a0fcba1dee
commit 007a53ac8c
3 changed files with 21 additions and 14 deletions
+3
View File
@@ -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
+16 -13
View File
@@ -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):
+2 -1
View File
@@ -151,8 +151,9 @@ def get_vehicules():
@app.route('/get_vehicleinfo/<string:vin>')
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'
)