]> git.basschouten.com Git - openhab-addons.git/commitdiff
[remoteopenhab] Detect a remote server shutdown and reconnect properly (#10060)
authorlolodomo <lg.hc@free.fr>
Sat, 6 Feb 2021 09:52:47 +0000 (10:52 +0100)
committerGitHub <noreply@github.com>
Sat, 6 Feb 2021 09:52:47 +0000 (10:52 +0100)
when the remote server is alive again

Related to #9680

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/handler/RemoteopenhabBridgeHandler.java
bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/listener/RemoteopenhabStreamingDataListener.java
bundles/org.openhab.binding.remoteopenhab/src/main/java/org/openhab/binding/remoteopenhab/internal/rest/RemoteopenhabRestClient.java

index 6554658814e64761f595f83947ee5df2f55dec32..ad196ddc52a27938f2211420899f51cffc9d5ff2 100644 (file)
@@ -183,7 +183,7 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
     @Override
     public void dispose() {
         logger.debug("Disposing remote openHAB handler for bridge {}", getThing().getUID());
-        stopStreamingUpdates();
+        stopStreamingUpdates(false);
         stopCheckConnectionJob();
         channelsLastStates.clear();
     }
@@ -380,7 +380,8 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
         if (localCheckConnectionJob == null || localCheckConnectionJob.isCancelled()) {
             checkConnectionJob = scheduler.scheduleWithFixedDelay(() -> {
                 long millisSinceLastEvent = System.currentTimeMillis() - restClient.getLastEventTimestamp();
-                if (aliveInterval == 0 || restClient.getLastEventTimestamp() == 0) {
+                if (getThing().getStatus() != ThingStatus.ONLINE || aliveInterval == 0
+                        || restClient.getLastEventTimestamp() == 0) {
                     logger.debug("Time to check server accessibility");
                     checkConnection();
                 } else if (millisSinceLastEvent > (aliveInterval * 60000)) {
@@ -421,8 +422,12 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
     }
 
     private void stopStreamingUpdates() {
+        stopStreamingUpdates(true);
+    }
+
+    private void stopStreamingUpdates(boolean waitingForCompletion) {
         synchronized (restClient) {
-            restClient.stop();
+            restClient.stop(waitingForCompletion);
             restClient.removeStreamingDataListener(this);
             restClient.removeItemsDataListener(this);
         }
@@ -437,6 +442,11 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
         updateStatus(ThingStatus.ONLINE);
     }
 
+    @Override
+    public void onDisconnected() {
+        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Disconected from the remote server");
+    }
+
     @Override
     public void onError(String message) {
         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, message);
index 969317107a8e3aa2713f5869bd7a8230bc461433..d1d325a3ca0c4c3b43e4afcda57f591b75067860 100644 (file)
@@ -24,10 +24,15 @@ import org.openhab.binding.remoteopenhab.internal.rest.RemoteopenhabRestClient;
 public interface RemoteopenhabStreamingDataListener {
 
     /**
-     * The client successfully established a connection.
+     * The client successfully established a connection and received a first event.
      */
     void onConnected();
 
+    /**
+     * The client was disconnected.
+     */
+    void onDisconnected();
+
     /**
      * An error message was published.
      */
index a6b7ca7ccd0d8505cd5ed9f68b3ac618c1a35a05..ebde376114ec6a719d3dfd52f5149f050da9a24a 100644 (file)
@@ -90,6 +90,7 @@ public class RemoteopenhabRestClient {
     private String accessToken;
     private boolean trustedCertificate;
     private boolean connected;
+    private boolean completed;
 
     private @Nullable SseEventSource eventSource;
     private long lastEventTimestamp;
@@ -237,10 +238,10 @@ public class RemoteopenhabRestClient {
         }
     }
 
-    public void stop() {
+    public void stop(boolean waitingForCompletion) {
         synchronized (startStopLock) {
             logger.debug("Closing EventSource");
-            closeEventSource(0, TimeUnit.SECONDS);
+            closeEventSource(waitingForCompletion);
             logger.debug("EventSource stopped");
             lastEventTimestamp = 0;
         }
@@ -263,7 +264,7 @@ public class RemoteopenhabRestClient {
                     .register(new RemoteopenhabStreamingRequestFilter(accessToken)).build();
         }
         SseEventSource eventSource = eventSourceFactory.newSource(client.target(restSseUrl));
-        eventSource.register(this::onEvent, this::onError);
+        eventSource.register(this::onEvent, this::onError, this::onComplete);
         return eventSource;
     }
 
@@ -279,7 +280,7 @@ public class RemoteopenhabRestClient {
             return;
         }
 
-        closeEventSource(10, TimeUnit.SECONDS);
+        closeEventSource(true);
 
         logger.debug("Opening new EventSource {}", url);
         SseEventSource localEventSource = createEventSource(url);
@@ -288,12 +289,12 @@ public class RemoteopenhabRestClient {
         eventSource = localEventSource;
     }
 
-    private void closeEventSource(long timeout, TimeUnit timeoutUnit) {
+    private void closeEventSource(boolean waitingForCompletion) {
         SseEventSource localEventSource = eventSource;
         if (localEventSource != null) {
-            if (!localEventSource.isOpen()) {
+            if (!localEventSource.isOpen() || completed) {
                 logger.debug("Existing EventSource is already closed");
-            } else if (localEventSource.close(timeout, timeoutUnit)) {
+            } else if (localEventSource.close(waitingForCompletion ? 10 : 0, TimeUnit.SECONDS)) {
                 logger.debug("Succesfully closed existing EventSource");
             } else {
                 logger.debug("Failed to close existing EventSource");
@@ -435,6 +436,12 @@ public class RemoteopenhabRestClient {
         }
     }
 
+    private void onComplete() {
+        logger.debug("Disconnected from streaming events");
+        completed = true;
+        listeners.forEach(listener -> listener.onDisconnected());
+    }
+
     private void onError(Throwable error) {
         logger.debug("Error occurred while receiving events", error);
         listeners.forEach(listener -> listener.onError("Error occurred while receiving events"));