]> git.basschouten.com Git - openhab-addons.git/commitdiff
[yioremote] Update and improve of the reconnection of the plugin (#10480)
authormiloit <MichaelLoercher@web.de>
Fri, 9 Apr 2021 21:18:02 +0000 (23:18 +0200)
committerGitHub <noreply@github.com>
Fri, 9 Apr 2021 21:18:02 +0000 (23:18 +0200)
* changed reconnection

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>
* reverted

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>
* Update and Bugfix reconnection

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>
* Update reconnection handling

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>
* Update codestyle

Signed-off-by: Michael Loercher <MichaelLoercher@web.de>
bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/YIOremoteDockHandler.java
bundles/org.openhab.binding.yioremote/src/main/java/org/openhab/binding/yioremote/internal/utils/Websocket.java

index 9e3e42033fbe5a8e658abd3cf689b4771a7677d2..a5d0dc60adc8541c37cc788b4fd3fa9489594717 100644 (file)
@@ -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;
index 4f5da39721c3ac2c67a5e98e1b1e2b9b1b20b0d3..afb1e9b50b50ffb8b273ebcad5e09345555f8978 100644 (file)
@@ -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);
         }