diff --git a/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java b/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java index 2d1eb403861..0f185c8100a 100644 --- a/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java +++ b/bundles/org.openhab.binding.ecobee/src/main/java/org/openhab/binding/ecobee/internal/api/EcobeeApi.java @@ -46,13 +46,14 @@ import org.openhab.binding.ecobee.internal.dto.thermostat.summary.SummaryRespons import org.openhab.binding.ecobee.internal.function.FunctionRequest; import org.openhab.binding.ecobee.internal.handler.EcobeeAccountBridgeHandler; import org.openhab.binding.ecobee.internal.util.ExceptionUtils; -import org.openhab.core.auth.client.oauth2.AccessTokenRefreshListener; import org.openhab.core.auth.client.oauth2.AccessTokenResponse; import org.openhab.core.auth.client.oauth2.OAuthClientService; import org.openhab.core.auth.client.oauth2.OAuthException; import org.openhab.core.auth.client.oauth2.OAuthFactory; import org.openhab.core.auth.client.oauth2.OAuthResponseException; import org.openhab.core.io.net.http.HttpUtil; +import org.openhab.core.thing.ThingStatus; +import org.openhab.core.thing.ThingStatusDetail; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,7 +68,7 @@ import com.google.gson.JsonSyntaxException; * @author Mark Hilbush - Initial contribution */ @NonNullByDefault -public class EcobeeApi implements AccessTokenRefreshListener { +public class EcobeeApi { private static final Gson GSON = new GsonBuilder().registerTypeAdapter(Instant.class, new InstantDeserializer()) .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeDeserializer()) @@ -124,7 +125,6 @@ public class EcobeeApi implements AccessTokenRefreshListener { logger.debug("API: Creating OAuth Client Service for {}", bridgeUID); OAuthClientService service = oAuthFactory.createOAuthClientService(bridgeUID, ECOBEE_TOKEN_URL, null, apiKey, "", ECOBEE_SCOPE, false); - service.addAccessTokenRefreshListener(this); ecobeeAuth = new EcobeeAuth(bridgeHandler, apiKey, apiTimeout, service, httpClient); oAuthClientService = service; } @@ -132,14 +132,12 @@ public class EcobeeApi implements AccessTokenRefreshListener { public void deleteOAuthClientService() { String bridgeUID = bridgeHandler.getThing().getUID().getAsString(); logger.debug("API: Deleting OAuth Client Service for {}", bridgeUID); - oAuthClientService.removeAccessTokenRefreshListener(this); oAuthFactory.deleteServiceAndAccessToken(bridgeUID); } public void closeOAuthClientService() { String bridgeUID = bridgeHandler.getThing().getUID().getAsString(); logger.debug("API: Closing OAuth Client Service for {}", bridgeUID); - oAuthClientService.removeAccessTokenRefreshListener(this); oAuthFactory.ungetOAuthService(bridgeUID); } @@ -202,10 +200,6 @@ public class EcobeeApi implements AccessTokenRefreshListener { } } - @Override - public void onAccessTokenResponse(AccessTokenResponse accessTokenResponse) { - } - public @Nullable SummaryResponseDTO performThermostatSummaryQuery() { logger.debug("API: Perform thermostat summary query"); if (!isAuthorized()) { @@ -245,8 +239,10 @@ public class EcobeeApi implements AccessTokenRefreshListener { if (response != null) { try { ThermostatResponseDTO thermostatsResponse = GSON.fromJson(response, ThermostatResponseDTO.class); - if (isSuccess(thermostatsResponse)) { - return thermostatsResponse.thermostatList; + if (thermostatsResponse != null && isSuccess(thermostatsResponse)) { + if (thermostatsResponse.thermostatList != null) { + return thermostatsResponse.thermostatList; + } } } catch (JsonSyntaxException e) { logJSException(e, response); @@ -343,15 +339,26 @@ public class EcobeeApi implements AccessTokenRefreshListener { deleteOAuthClientService(); createOAuthClientService(); } else if (response.status.code == ECOBEE_TOKEN_EXPIRED) { - // Check isAuthorized again to see if we can get a valid token - logger.debug("API: Unable to complete API call because token is expired"); + logger.debug("API: Unable to complete API call because token is expired. Try to refresh the token..."); + // Log some additional debug information about the current AccessTokenResponse + AccessTokenResponse localAccessTokenResponse = accessTokenResponse; + if (localAccessTokenResponse != null) { + 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()) { return true; } else { - logger.debug("API: isAuthorized was NOT successful on second try"); + logger.warn("API: isAuthorized was NOT successful on second try"); + bridgeHandler.updateBridgeStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, + "Unable to refresh access token"); } } } else { + bridgeHandler.updateBridgeStatus(ThingStatus.ONLINE); return true; } return false;