diff --git a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java index 636217d73beb8..83abda4f575d2 100644 --- a/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java +++ b/bundles/org.openhab.binding.somfytahoma/src/main/java/org/openhab/binding/somfytahoma/internal/handler/SomfyTahomaBridgeHandler.java @@ -146,6 +146,9 @@ public class SomfyTahomaBridgeHandler extends BaseBridgeHandler { // Last login timestamp private Instant lastLoginTimestamp = Instant.MIN; + // Token expiration time + private Instant tokenExpirationTime = Instant.MAX; + /** * Our configuration */ @@ -270,7 +273,7 @@ public synchronized void login() { return; } } else { - loginOAUTH(); + loginTahoma(); } if (thingConfig.isDevMode()) { @@ -308,6 +311,12 @@ public synchronized void login() { } } + private void doOAuthLogin() throws ExecutionException, InterruptedException, TimeoutException { + lastLoginTimestamp = Instant.now(); + + loginTahoma(); + } + private boolean loginCozyTouch() throws ExecutionException, InterruptedException, TimeoutException, JsonSyntaxException { logger.debug("CozyTouch Oauth2 authentication flow"); @@ -587,6 +596,11 @@ private void getTahomaUpdates() { return; } + if (tokenNeedsRefresh()) { + logger.debug("The access token expires soon, refreshing the cloud access token"); + refreshToken(); + } + List events = getEvents(); logger.trace("Got total of {} events", events.size()); for (SomfyTahomaEvent event : events) { @@ -594,6 +608,21 @@ private void getTahomaUpdates() { } } + private void refreshToken() { + try { + doOAuthLogin(); + } catch (ExecutionException | TimeoutException e) { + logger.debug("Token refresh failed"); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + private boolean tokenNeedsRefresh() { + return !thingConfig.getCloudPortal().equalsIgnoreCase(COZYTOUCH_PORTAL) + && Instant.now().plusSeconds(thingConfig.getRefresh()).isAfter(tokenExpirationTime); + } + private void processEvent(SomfyTahomaEvent event) { logger.debug("Got event: {}", event.getName()); switch (event.getName()) { @@ -923,7 +952,7 @@ private String getCozytouchJWT() } } - private void loginOAUTH() throws InterruptedException, TimeoutException, ExecutionException, JsonSyntaxException { + private void loginTahoma() throws InterruptedException, TimeoutException, ExecutionException, JsonSyntaxException { String authBaseUrl = "https://" + SOMFY_OAUTH2_URL; String urlParameters = "client_id=" + SOMFY_OAUTH2_CLIENT_ID + "&client_secret=" + SOMFY_OAUTH2_CLIENT_SECRET @@ -954,7 +983,8 @@ private void loginOAUTH() throws InterruptedException, TimeoutException, Executi SomfyTahomaOauth2Reponse oauth2response = gson.fromJson(response.getContentAsString(), SomfyTahomaOauth2Reponse.class); - logger.debug("OAuth2 Access Token: {}", oauth2response.getAccessToken()); + tokenExpirationTime = Instant.now().plusSeconds(oauth2response.getExpiresIn()); + logger.debug("OAuth2 Access Token: {}, expires: {}", oauth2response.getAccessToken(), tokenExpirationTime); accessToken = oauth2response.getAccessToken(); }