protected boolean checkResponse(Response response, boolean immediatelyFail) {
if (response != null && response.getStatus() == 200) {
return true;
+ } else if (response != null && response.getStatus() == 401) {
+ logger.debug("The access token has expired, trying to get a new one.");
+ ThingStatusInfo authenticationResult = authenticate();
+ updateStatus(authenticationResult.getStatus(), authenticationResult.getStatusDetail(),
+ authenticationResult.getDescription());
+ return false;
} else {
apiIntervalErrors++;
if (immediatelyFail || apiIntervalErrors >= API_MAXIMUM_ERRORS_IN_INTERVAL) {
response.getStatusInfo().getReasonPhrase());
if (!checkResponse(response, true)) {
- logger.error("An error occurred while querying the vehicle");
+ logger.debug("An error occurred while querying the vehicle");
return null;
}
return this.getThing().getUID().getId();
}
- private ThingStatusInfo authenticate() {
+ ThingStatusInfo authenticate() {
TokenResponse token = logonToken;
boolean hasExpired = true;
import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
+import java.time.Instant;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
TokenResponse tokenResponse = gson.fromJson(refreshTokenResponse.trim(), TokenResponse.class);
if (tokenResponse != null && tokenResponse.access_token != null && !tokenResponse.access_token.isEmpty()) {
+ tokenResponse.created_at = Instant.now().getEpochSecond();
return tokenResponse;
} else {
logger.debug("An error occurred while exchanging SSO auth token for API access token.");
protected boolean checkResponse(Response response, boolean immediatelyFail) {
if (response != null && response.getStatus() == 200) {
return true;
+ } else if (response != null && response.getStatus() == 401) {
+ logger.debug("The access token has expired, trying to get a new one.");
+ account.authenticate();
+ return false;
} else {
apiIntervalErrors++;
if (immediatelyFail || apiIntervalErrors >= TeslaAccountHandler.API_MAXIMUM_ERRORS_IN_INTERVAL) {
sendCommand(COMMAND_WAKE_UP, account.wakeUpTarget);
}
- protected Vehicle queryVehicle() {
+ protected synchronized Vehicle queryVehicle() {
String authHeader = account.getAuthHeader();
if (authHeader != null) {
response.getStatusInfo().getReasonPhrase());
if (!checkResponse(response, true)) {
- logger.error("An error occurred while querying the vehicle");
+ logger.debug("An error occurred while querying the vehicle");
return null;
}