[tesla] Set the token creation time as it is no longer provided in the response (#12547)
* Set the token creation time as it is no longer provided in the response Address CME Reauthenticate in case of 401 responses Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
parent
ecf8aa32b0
commit
e8c944281a
|
@ -170,6 +170,12 @@ public class TeslaAccountHandler extends BaseBridgeHandler {
|
||||||
protected boolean checkResponse(Response response, boolean immediatelyFail) {
|
protected boolean checkResponse(Response response, boolean immediatelyFail) {
|
||||||
if (response != null && response.getStatus() == 200) {
|
if (response != null && response.getStatus() == 200) {
|
||||||
return true;
|
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 {
|
} else {
|
||||||
apiIntervalErrors++;
|
apiIntervalErrors++;
|
||||||
if (immediatelyFail || apiIntervalErrors >= API_MAXIMUM_ERRORS_IN_INTERVAL) {
|
if (immediatelyFail || apiIntervalErrors >= API_MAXIMUM_ERRORS_IN_INTERVAL) {
|
||||||
|
@ -203,7 +209,7 @@ public class TeslaAccountHandler extends BaseBridgeHandler {
|
||||||
response.getStatusInfo().getReasonPhrase());
|
response.getStatusInfo().getReasonPhrase());
|
||||||
|
|
||||||
if (!checkResponse(response, true)) {
|
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 null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,7 +248,7 @@ public class TeslaAccountHandler extends BaseBridgeHandler {
|
||||||
return this.getThing().getUID().getId();
|
return this.getThing().getUID().getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ThingStatusInfo authenticate() {
|
ThingStatusInfo authenticate() {
|
||||||
TokenResponse token = logonToken;
|
TokenResponse token = logonToken;
|
||||||
|
|
||||||
boolean hasExpired = true;
|
boolean hasExpired = true;
|
||||||
|
|
|
@ -14,6 +14,7 @@ package org.openhab.binding.tesla.internal.handler;
|
||||||
|
|
||||||
import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
|
import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
@ -68,6 +69,7 @@ public class TeslaSSOHandler {
|
||||||
TokenResponse tokenResponse = gson.fromJson(refreshTokenResponse.trim(), TokenResponse.class);
|
TokenResponse tokenResponse = gson.fromJson(refreshTokenResponse.trim(), TokenResponse.class);
|
||||||
|
|
||||||
if (tokenResponse != null && tokenResponse.access_token != null && !tokenResponse.access_token.isEmpty()) {
|
if (tokenResponse != null && tokenResponse.access_token != null && !tokenResponse.access_token.isEmpty()) {
|
||||||
|
tokenResponse.created_at = Instant.now().getEpochSecond();
|
||||||
return tokenResponse;
|
return tokenResponse;
|
||||||
} else {
|
} else {
|
||||||
logger.debug("An error occurred while exchanging SSO auth token for API access token.");
|
logger.debug("An error occurred while exchanging SSO auth token for API access token.");
|
||||||
|
|
|
@ -549,6 +549,10 @@ public class TeslaVehicleHandler extends BaseThingHandler {
|
||||||
protected boolean checkResponse(Response response, boolean immediatelyFail) {
|
protected boolean checkResponse(Response response, boolean immediatelyFail) {
|
||||||
if (response != null && response.getStatus() == 200) {
|
if (response != null && response.getStatus() == 200) {
|
||||||
return true;
|
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 {
|
} else {
|
||||||
apiIntervalErrors++;
|
apiIntervalErrors++;
|
||||||
if (immediatelyFail || apiIntervalErrors >= TeslaAccountHandler.API_MAXIMUM_ERRORS_IN_INTERVAL) {
|
if (immediatelyFail || apiIntervalErrors >= TeslaAccountHandler.API_MAXIMUM_ERRORS_IN_INTERVAL) {
|
||||||
|
@ -707,7 +711,7 @@ public class TeslaVehicleHandler extends BaseThingHandler {
|
||||||
sendCommand(COMMAND_WAKE_UP, account.wakeUpTarget);
|
sendCommand(COMMAND_WAKE_UP, account.wakeUpTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Vehicle queryVehicle() {
|
protected synchronized Vehicle queryVehicle() {
|
||||||
String authHeader = account.getAuthHeader();
|
String authHeader = account.getAuthHeader();
|
||||||
|
|
||||||
if (authHeader != null) {
|
if (authHeader != null) {
|
||||||
|
@ -720,7 +724,7 @@ public class TeslaVehicleHandler extends BaseThingHandler {
|
||||||
response.getStatusInfo().getReasonPhrase());
|
response.getStatusInfo().getReasonPhrase());
|
||||||
|
|
||||||
if (!checkResponse(response, true)) {
|
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 null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue