]> git.basschouten.com Git - openhab-addons.git/commitdiff
[tesla] Set the token creation time as it is no longer provided in the response ...
authorKai Kreuzer <kai@openhab.org>
Fri, 1 Apr 2022 19:33:15 +0000 (21:33 +0200)
committerGitHub <noreply@github.com>
Fri, 1 Apr 2022 19:33:15 +0000 (21:33 +0200)
* 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>
bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaAccountHandler.java
bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaSSOHandler.java
bundles/org.openhab.binding.tesla/src/main/java/org/openhab/binding/tesla/internal/handler/TeslaVehicleHandler.java

index 6ac8a1eb05c3ea7c3fc90e81a994d71d67ddb5e9..a72507c8d73b73654e73f04fc9fd2b82f2a4bb37 100644 (file)
@@ -170,6 +170,12 @@ public class TeslaAccountHandler extends BaseBridgeHandler {
     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) {
@@ -203,7 +209,7 @@ public class TeslaAccountHandler extends BaseBridgeHandler {
                     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;
             }
 
@@ -242,7 +248,7 @@ public class TeslaAccountHandler extends BaseBridgeHandler {
         return this.getThing().getUID().getId();
     }
 
-    private ThingStatusInfo authenticate() {
+    ThingStatusInfo authenticate() {
         TokenResponse token = logonToken;
 
         boolean hasExpired = true;
index be121492acfa774fcf4bd2ffb4aae01865f30b25..6d7602a01c80a6f765dde0640991d1485dc272ef 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.tesla.internal.handler;
 
 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;
@@ -68,6 +69,7 @@ public class TeslaSSOHandler {
             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.");
index 35a20a19cb20d5ddc103459c643db7fe6b8bf945..5ef5d209443e5d146b067bb54efa674c757755e9 100644 (file)
@@ -549,6 +549,10 @@ public class TeslaVehicleHandler extends BaseThingHandler {
     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) {
@@ -707,7 +711,7 @@ public class TeslaVehicleHandler extends BaseThingHandler {
         sendCommand(COMMAND_WAKE_UP, account.wakeUpTarget);
     }
 
-    protected Vehicle queryVehicle() {
+    protected synchronized Vehicle queryVehicle() {
         String authHeader = account.getAuthHeader();
 
         if (authHeader != null) {
@@ -720,7 +724,7 @@ public class TeslaVehicleHandler extends BaseThingHandler {
                         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;
                 }