diff --git a/psa_car_controller/psa/RemoteClient.py b/psa_car_controller/psa/RemoteClient.py index e21c7423..b4704001 100644 --- a/psa_car_controller/psa/RemoteClient.py +++ b/psa_car_controller/psa/RemoteClient.py @@ -43,6 +43,7 @@ def __init__(self, account_info: AccountInformation, vehicles_list: Cars, manage self.last_request = None self.mqtt_client = None self.otp = None + self._lock = threading.Lock() def __on_mqtt_connect(self, client, userdata, result_code, _): # pylint: disable=unused-argument logger.info("Connected with result code %s", result_code) @@ -146,39 +147,40 @@ def mqtt_request(self, vin, req_parameters, topic): return MQTTRequest(topic, vin, req_parameters, self.account_info.get_mqtt_customer_id()) def _refresh_remote_token(self, force=False): - bad_remote_token = self.remoteCredentials.refresh_token is None - if not force and not bad_remote_token and self.remoteCredentials.last_update: - last_update: datetime = self.remoteCredentials.last_update - if (datetime.now() - last_update).total_seconds() < MQTT_TOKEN_TTL: - return True - try: - self.manager.refresh_token_now() - if bad_remote_token: - logger.error("remote_refresh_token isn't defined") - else: - res = self.manager.post(REMOTE_URL + self.account_info.client_id, - json={"grant_type": "refresh_token", - "refresh_token": self.remoteCredentials.refresh_token}, - headers=self.headers) - data = res.json() - logger.debug("refresh_remote_token: %s", data) - if "access_token" in data: - self.remoteCredentials.access_token = data["access_token"] - self.remoteCredentials.refresh_token = data["refresh_token"] - bad_remote_token = False + with self._lock: + bad_remote_token = self.remoteCredentials.refresh_token is None + if not force and not bad_remote_token and self.remoteCredentials.last_update: + last_update: datetime = self.remoteCredentials.last_update + if (datetime.now() - last_update).total_seconds() < MQTT_TOKEN_TTL: + return True + try: + self.manager.refresh_token_now() + if bad_remote_token: + logger.warning("remote_refresh_token isn't defined") else: - logger.error("can't refresh_remote_token: %s\n Create a new one", data) - bad_remote_token = True - if bad_remote_token: - otp_code = self.get_otp_code() - self._get_remote_access_token(otp_code) - self.remote_token_last_update = datetime.now() - self.mqtt_client.username_pw_set("IMA_OAUTH_ACCESS_TOKEN", self.remoteCredentials.access_token) - return True - except (RequestException, RateLimitException, KeyError) as e: - logger.exception("Can't refresh remote token %s", e) - time.sleep(60) - return False + res = self.manager.post(REMOTE_URL + self.account_info.client_id, + json={"grant_type": "refresh_token", + "refresh_token": self.remoteCredentials.refresh_token}, + headers=self.headers) + data = res.json() + logger.debug("refresh_remote_token: %s", data) + if "access_token" in data: + self.remoteCredentials.access_token = data["access_token"] + bad_remote_token = False + if "refresh_token" in data: + self.remoteCredentials.refresh_token = data["refresh_token"] + else: + logger.error("can't refresh_remote_token: %s\n Create a new one", data) + bad_remote_token = True + if bad_remote_token: + otp_code = self.get_otp_code() + self._get_remote_access_token(otp_code) + self.remote_token_last_update = datetime.now() + self.mqtt_client.username_pw_set("IMA_OAUTH_ACCESS_TOKEN", self.remoteCredentials.access_token) + return True + except (RequestException, RateLimitException, KeyError): + logger.exception("Can't refresh remote token, please redo otp procedure") + return False def get_sms_otp_code(self): res = self.manager.post(