From 147bae8fe1d44584184cead5e1ce2ca0dca8ddde Mon Sep 17 00:00:00 2001 From: lolodomo Date: Thu, 16 Jul 2020 20:37:43 +0200 Subject: [PATCH] [lgwebos] Improved WebSocket reconnect/disconnect (#8038) Related to #8027 Signed-off-by: Laurent Garnier Signed-off-by: CSchlipp --- .../binding/lgwebos/internal/handler/LGWebOSHandler.java | 6 ++++-- .../binding/lgwebos/internal/handler/LGWebOSTVSocket.java | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSHandler.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSHandler.java index 71437142569a6..22e613279b4c7 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSHandler.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSHandler.java @@ -168,8 +168,10 @@ public void dispose() { private void startReconnectJob() { ScheduledFuture job = reconnectJob; if (job == null || job.isCancelled()) { - reconnectJob = scheduler.scheduleWithFixedDelay(() -> getSocket().connect(), - RECONNECT_START_UP_DELAY_SECONDS, RECONNECT_INTERVAL_SECONDS, TimeUnit.SECONDS); + reconnectJob = scheduler.scheduleWithFixedDelay(() -> { + getSocket().disconnect(); + getSocket().connect(); + }, RECONNECT_START_UP_DELAY_SECONDS, RECONNECT_INTERVAL_SECONDS, TimeUnit.SECONDS); } } diff --git a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java index 70b0f8f953dfb..a619750f6505a 100644 --- a/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java +++ b/bundles/org.openhab.binding.lgwebos/src/main/java/org/openhab/binding/lgwebos/internal/handler/LGWebOSTVSocket.java @@ -48,6 +48,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; +import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -127,6 +128,7 @@ public enum State { private State state = State.DISCONNECTED; private @Nullable Session session; + private @Nullable Future sessionFuture; private @Nullable WebOSTVSocketListener listener; /** @@ -176,7 +178,7 @@ public void clearRequests() { public void connect() { try { - this.client.connect(this, this.destUri); + sessionFuture = this.client.connect(this, this.destUri); logger.debug("Connecting to: {}", this.destUri); } catch (IOException e) { logger.debug("Unable to connect.", e); @@ -185,6 +187,10 @@ public void connect() { public void disconnect() { Optional.ofNullable(this.session).ifPresent(s -> s.close()); + Future future = sessionFuture; + if (future != null && !future.isDone()) { + future.cancel(true); + } stopDisconnectingJob(); setState(State.DISCONNECTED); }