]> git.basschouten.com Git - openhab-addons.git/commitdiff
Improve handling of error code 14 (#16613)
authorMark Hilbush <mark@hilbush.com>
Fri, 5 Apr 2024 14:01:50 +0000 (10:01 -0400)
committerGitHub <noreply@github.com>
Fri, 5 Apr 2024 14:01:50 +0000 (16:01 +0200)
Signed-off-by: Mark Hilbush <mark@hilbush.com>
bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java

index 0f185c8100ada1b09415f91a80bc09de16ae02a5..0085968ad76f52f29276ed43d81187f91d3b5833 100644 (file)
@@ -83,6 +83,8 @@ public class EcobeeApi {
     private static final int ECOBEE_TOKEN_EXPIRED = 14;
     private static final int ECOBEE_DEAUTHORIZED_TOKEN = 16;
     private static final int TOKEN_EXPIRES_IN_BUFFER_SECONDS = 120;
+    private static final boolean FORCE_TOKEN_REFRESH = true;
+    private static final boolean DONT_FORCE_TOKEN_REFRESH = false;
 
     public static final Properties HTTP_HEADERS;
     static {
@@ -147,14 +149,15 @@ public class EcobeeApi {
      * response, then assume that the Ecobee authorization process is complete. Otherwise,
      * start the Ecobee authorization process.
      */
-    private boolean isAuthorized() {
+    private boolean isAuthorized(boolean forceTokenRefresh) {
         boolean isAuthorized = false;
         try {
             AccessTokenResponse localAccessTokenResponse = oAuthClientService.getAccessTokenResponse();
             if (localAccessTokenResponse != null) {
                 logger.trace("API: Got AccessTokenResponse from OAuth service: {}", localAccessTokenResponse);
-                if (localAccessTokenResponse.isExpired(Instant.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) {
-                    logger.debug("API: Token is expiring soon. Refresh it now");
+                if (forceTokenRefresh
+                        || localAccessTokenResponse.isExpired(Instant.now(), TOKEN_EXPIRES_IN_BUFFER_SECONDS)) {
+                    logger.debug("API: Refreshing access token");
                     localAccessTokenResponse = oAuthClientService.refreshToken();
                 }
                 ecobeeAuth.setState(EcobeeAuthState.COMPLETE);
@@ -187,6 +190,10 @@ public class EcobeeApi {
         return isAuthorized;
     }
 
+    private boolean isAuthorized() {
+        return isAuthorized(DONT_FORCE_TOKEN_REFRESH);
+    }
+
     private void handleOAuthException(OAuthResponseException e) {
         if ("invalid_grant".equalsIgnoreCase(e.getError())) {
             // Usually indicates that the refresh token is no longer valid and will require reauthorization
@@ -346,15 +353,13 @@ public class EcobeeApi {
                     logger.debug("API: AccessTokenResponse created on: {}", localAccessTokenResponse.getCreatedOn());
                     logger.debug("API: AccessTokenResponse expires in: {}", localAccessTokenResponse.getExpiresIn());
                 }
-                // Recreating the OAuthClientService seems to be the only way to handle this error
-                closeOAuthClientService();
-                createOAuthClientService();
-                if (isAuthorized()) {
+                logger.debug("API: Ecobee API attempting to force an access token refresh");
+                if (isAuthorized(FORCE_TOKEN_REFRESH)) {
                     return true;
                 } else {
-                    logger.warn("API: isAuthorized was NOT successful on second try");
+                    logger.warn("API: isAuthorized was NOT successful forcing the access token refresh");
                     bridgeHandler.updateBridgeStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                            "Unable to refresh access token");
+                            "Unable to force refresh the access token");
                 }
             }
         } else {