From 1f877cae6d268b7369ca0e16fb51786ba921aba4 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Mon, 20 Feb 2023 12:39:03 +0100 Subject: [PATCH] [yioremote] Stop the web socket client when disposing thing handler (#14340) Signed-off-by: Laurent Garnier --- .../internal/YIOremoteDockHandler.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/YIOremoteDockHandler.java b/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/YIOremoteDockHandler.java index 404f7e464..c45051b0d 100644 --- a/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/YIOremoteDockHandler.java +++ b/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/YIOremoteDockHandler.java @@ -68,6 +68,7 @@ public class YIOremoteDockHandler extends BaseThingHandler { private ClientUpgradeRequest yioremoteDockwebSocketClientrequest = new ClientUpgradeRequest(); private @Nullable URI websocketAddress; private YioRemoteDockHandleStatus yioRemoteDockActualStatus = YioRemoteDockHandleStatus.UNINITIALIZED_STATE; + private @Nullable Future initJob; private @Nullable Future webSocketPollingJob; private @Nullable Future webSocketReconnectionPollingJob; public String receivedMessage = ""; @@ -90,7 +91,7 @@ public class YIOremoteDockHandler extends BaseThingHandler { @Override public void initialize() { updateStatus(ThingStatus.UNKNOWN); - scheduler.execute(() -> { + initJob = scheduler.submit(() -> { try { websocketAddress = new URI("ws://" + localConfig.host + ":946"); yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_PROCESS; @@ -257,8 +258,18 @@ public class YIOremoteDockHandler extends BaseThingHandler { @Override public void dispose() { + Future job = initJob; + if (job != null) { + job.cancel(true); + initJob = null; + } disposeWebsocketPollingJob(); disposeWebSocketReconnectionPollingJob(); + try { + webSocketClient.stop(); + } catch (Exception e) { + logger.debug("Could not stop webSocketClient, message {}", e.getMessage()); + } } @Override @@ -342,21 +353,19 @@ public class YIOremoteDockHandler extends BaseThingHandler { } private void disposeWebsocketPollingJob() { - if (webSocketPollingJob != null) { - if (!webSocketPollingJob.isCancelled() && webSocketPollingJob != null) { - webSocketPollingJob.cancel(true); - } + Future job = webSocketPollingJob; + if (job != null) { + job.cancel(true); webSocketPollingJob = null; } } private void disposeWebSocketReconnectionPollingJob() { - if (webSocketReconnectionPollingJob != null) { - if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) { - webSocketReconnectionPollingJob.cancel(true); - } + Future job = webSocketReconnectionPollingJob; + if (job != null) { + job.cancel(true); + webSocketReconnectionPollingJob = null; } - webSocketReconnectionPollingJob = null; logger.debug("disposereconnection"); reconnectionCounter = 0; }