From 3561388061aa5e167927ee287e33ae58e2eb0950 Mon Sep 17 00:00:00 2001 From: miloit Date: Fri, 9 Apr 2021 23:18:02 +0200 Subject: [PATCH] [yioremote] Update and improve of the reconnection of the plugin (#10480) * changed reconnection Signed-off-by: Michael Loercher * reverted Signed-off-by: Michael Loercher * Update and Bugfix reconnection Signed-off-by: Michael Loercher * Update reconnection handling Signed-off-by: Michael Loercher * Update codestyle Signed-off-by: Michael Loercher --- .../internal/YIOremoteDockHandler.java | 53 +++++++++++++------ .../yioremote/internal/utils/Websocket.java | 2 +- 2 files changed, 39 insertions(+), 16 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 9e3e42033..a5d0dc60a 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 @@ -81,6 +81,7 @@ public class YIOremoteDockHandler extends BaseThingHandler { private AuthenticationMessage authenticationMessageHandler = new AuthenticationMessage(); private IRReceiverMessage irReceiverMessageHandler = new IRReceiverMessage(); private PingMessage pingMessageHandler = new PingMessage(); + private int reconnectionCounter = 0; public YIOremoteDockHandler(Thing thing) { super(thing); @@ -122,6 +123,7 @@ public class YIOremoteDockHandler extends BaseThingHandler { authenticateWebsocket(); break; case COMMUNICATION_ERROR: + disposeWebsocketPollingJob(); reconnectWebsocket(); break; case AUTHENTICATION_COMPLETE: @@ -142,12 +144,14 @@ public class YIOremoteDockHandler extends BaseThingHandler { @Override public void onClose() { + logger.debug("onClose"); disposeWebsocketPollingJob(); reconnectWebsocket(); } @Override public void onError(Throwable cause) { + logger.debug("onError"); disposeWebsocketPollingJob(); yioRemoteDockActualStatus = YioRemoteDockHandleStatus.COMMUNICATION_ERROR; updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, @@ -254,14 +258,7 @@ public class YIOremoteDockHandler extends BaseThingHandler { @Override public void dispose() { disposeWebsocketPollingJob(); - if (webSocketReconnectionPollingJob != null) { - if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) { - webSocketReconnectionPollingJob.cancel(true); - authenticationOk = false; - heartBeat = false; - } - webSocketReconnectionPollingJob = null; - } + disposeWebSocketReconnectionPollingJob(); } @Override @@ -324,10 +321,12 @@ public class YIOremoteDockHandler extends BaseThingHandler { case AUTHENTICATION_PROCESS: if (authenticationOk) { yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_COMPLETE; + disposeWebSocketReconnectionPollingJob(); + reconnectionCounter = 0; updateStatus(ThingStatus.ONLINE); updateState(STATUS_STRING_CHANNEL, StringType.EMPTY); updateState(RECEIVER_SWITCH_CHANNEL, OnOffType.OFF); - webSocketPollingJob = scheduler.scheduleWithFixedDelay(this::pollingWebsocketJob, 0, 60, + webSocketPollingJob = scheduler.scheduleWithFixedDelay(this::pollingWebsocketJob, 0, 40, TimeUnit.SECONDS); } else { yioRemoteDockActualStatus = YioRemoteDockHandleStatus.AUTHENTICATION_FAILED; @@ -351,6 +350,17 @@ public class YIOremoteDockHandler extends BaseThingHandler { } } + private void disposeWebSocketReconnectionPollingJob() { + if (webSocketReconnectionPollingJob != null) { + if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) { + webSocketReconnectionPollingJob.cancel(true); + } + } + webSocketReconnectionPollingJob = null; + logger.debug("disposereconnection"); + reconnectionCounter = 0; + } + private void pollingWebsocketJob() { switch (yioRemoteDockActualStatus) { case AUTHENTICATION_COMPLETE: @@ -395,13 +405,30 @@ public class YIOremoteDockHandler extends BaseThingHandler { } public void reconnectWebsocket() { + yioRemoteDockActualStatus = YioRemoteDockHandleStatus.COMMUNICATION_ERROR; if (webSocketReconnectionPollingJob == null) { webSocketReconnectionPollingJob = scheduler.scheduleWithFixedDelay(this::reconnectWebsocketJob, 0, 30, TimeUnit.SECONDS); + } else if (reconnectionCounter == 5) { + reconnectionCounter = 0; + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, + "Connection lost no ping from YIO DOCK"); + if (webSocketReconnectionPollingJob == null) { + webSocketReconnectionPollingJob = scheduler.scheduleWithFixedDelay(this::reconnectWebsocketJob, 0, 1, + TimeUnit.MINUTES); + } else { + disposeWebSocketReconnectionPollingJob(); + if (webSocketReconnectionPollingJob == null) { + webSocketReconnectionPollingJob = scheduler.scheduleWithFixedDelay(this::reconnectWebsocketJob, 0, + 5, TimeUnit.MINUTES); + } + } + } else { } } public void reconnectWebsocketJob() { + reconnectionCounter++; switch (yioRemoteDockActualStatus) { case COMMUNICATION_ERROR: logger.debug("Reconnecting YIORemoteHandler"); @@ -431,12 +458,8 @@ public class YIOremoteDockHandler extends BaseThingHandler { } break; case AUTHENTICATION_COMPLETE: - if (webSocketReconnectionPollingJob != null) { - if (!webSocketReconnectionPollingJob.isCancelled() && webSocketReconnectionPollingJob != null) { - webSocketReconnectionPollingJob.cancel(true); - } - webSocketReconnectionPollingJob = null; - } + disposeWebSocketReconnectionPollingJob(); + reconnectionCounter = 0; break; default: break; diff --git a/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/utils/Websocket.java b/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/utils/Websocket.java index 4f5da3972..afb1e9b50 100644 --- a/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/utils/Websocket.java +++ b/bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/utils/Websocket.java @@ -61,7 +61,7 @@ public class Websocket { @OnWebSocketError public void onError(Throwable cause) { - logger.warn("WebSocketError {}", cause.getMessage()); + logger.debug("WebSocketError {}", cause.getMessage()); if (websocketHandler != null) { websocketHandler.onError(cause); }