diff --git a/bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/IndegoController.java b/bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/IndegoController.java index 70c66c9f7..1fe36dd1b 100644 --- a/bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/IndegoController.java +++ b/bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/IndegoController.java @@ -167,6 +167,21 @@ public class IndegoController { }); } + /** + * Deauthenticate session. This method should be called as part of cleanup to reduce + * lingering sessions. This can potentially avoid killed sessions in situation with + * multiple clients (e.g. openHAB and mobile app) if restrictions on concurrent + * number of sessions would be put on the service. + * + * @throws IndegoException if any communication or parsing error occurred + */ + public void deauthenticate() throws IndegoException { + if (session.isValid()) { + deleteRequest("authenticate"); + session.invalidate(); + } + } + /** * Wraps {@link #getRequest(String, Class)} into an authenticated session. * @@ -432,6 +447,32 @@ public class IndegoController { } } + /** + * Sends a DELETE request to the server. + * + * @param path the relative path to which the request should be sent + * @throws IndegoException if any communication or parsing error occurred + */ + private void deleteRequest(String path) throws IndegoException { + try { + Request request = httpClient.newRequest(BASE_URL + path).method(HttpMethod.DELETE) + .header(CONTEXT_HEADER_NAME, session.getContextId()); + if (logger.isTraceEnabled()) { + logger.trace("DELETE request for {}", BASE_URL + path); + } + ContentResponse response = sendRequest(request); + int status = response.getStatus(); + if (!HttpStatus.isSuccess(status)) { + throw new IndegoException("The request failed with error: " + status); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IndegoException(e); + } catch (TimeoutException | ExecutionException e) { + throw new IndegoException(e); + } + } + /** * Send request. This method exists for the purpose of avoiding multiple calls to * the server at the same time. diff --git a/bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/handler/BoschIndegoHandler.java b/bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/handler/BoschIndegoHandler.java index 0dec921f0..f0c3e6774 100644 --- a/bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/handler/BoschIndegoHandler.java +++ b/bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/handler/BoschIndegoHandler.java @@ -121,6 +121,14 @@ public class BoschIndegoHandler extends BaseThingHandler { pollFuture.cancel(true); } this.cuttingTimeMapPollFuture = null; + + scheduler.execute(() -> { + try { + controller.deauthenticate(); + } catch (IndegoException e) { + logger.debug("Deauthentication failed", e); + } + }); } @Override