From 196849dc60bc0bec21bb0f53fd8c71f738153322 Mon Sep 17 00:00:00 2001 From: gregor roth Date: Sun, 25 Apr 2021 17:44:32 +0200 Subject: [PATCH 1/3] Unnecessary NonNull annotations removed Signed-off-by: Gregor Roth Signed-off-by: gregor roth --- .../webthing/internal/client/WebSocketConnectionImpl.java | 3 +-- .../binding/webthing/internal/client/WebthingTest.java | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/client/WebSocketConnectionImpl.java b/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/client/WebSocketConnectionImpl.java index e491a9e8dcc2f..ccab0ef466727 100644 --- a/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/client/WebSocketConnectionImpl.java +++ b/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/client/WebSocketConnectionImpl.java @@ -26,7 +26,6 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.websocket.api.Session; @@ -88,7 +87,7 @@ public void close() { } @Override - public void observeProperty(@NonNull String propertyName, @NonNull BiConsumer listener) { + public void observeProperty(String propertyName, BiConsumer listener) { propertyChangedListeners.put(propertyName, listener); } diff --git a/bundles/org.openhab.binding.webthing/src/test/java/org/openhab/binding/webthing/internal/client/WebthingTest.java b/bundles/org.openhab.binding.webthing/src/test/java/org/openhab/binding/webthing/internal/client/WebthingTest.java index 8d7699e73c317..7ba7901c07c54 100644 --- a/bundles/org.openhab.binding.webthing/src/test/java/org/openhab/binding/webthing/internal/client/WebthingTest.java +++ b/bundles/org.openhab.binding.webthing/src/test/java/org/openhab/binding/webthing/internal/client/WebthingTest.java @@ -34,7 +34,6 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; @@ -289,8 +288,8 @@ public static class TestWebsocketConnectionFactory implements WebSocketConnectio public final AtomicReference webSocketRef = new AtomicReference<>(); @Override - public WebSocketConnection create(@NonNull URI webSocketURI, @NonNull ScheduledExecutorService executor, - @NonNull Consumer errorHandler, @NonNull Duration pingPeriod) { + public WebSocketConnection create(URI webSocketURI, ScheduledExecutorService executor, + Consumer errorHandler, Duration pingPeriod) { var webSocketConnection = new WebSocketConnectionImpl(executor, errorHandler, pingPeriod); var webSocket = new WebSocketImpl(webSocketConnection); webSocketRef.set(webSocket); From 860d01aa9e63d5ca9dcb07301fb896ee6f0ddf2c Mon Sep 17 00:00:00 2001 From: gregor roth Date: Sun, 25 Apr 2021 17:46:14 +0200 Subject: [PATCH 2/3] Bugfix "If network connection is interrupted, WebThing will not be reconnected." Due to the variable webThingURI is unset, re connection fails Signed-off-by: Gregor Roth Signed-off-by: gregor roth --- .../binding/webthing/internal/WebThingHandler.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/WebThingHandler.java b/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/WebThingHandler.java index 8e1c7cc61db14..79c4e7f7f1ab7 100644 --- a/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/WebThingHandler.java +++ b/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/WebThingHandler.java @@ -94,13 +94,14 @@ public void initialize() { isActivated.set(true); // set with true, even though the connect may fail. In this case retries will be // triggered + webThingURI = toUri(getConfigAs(WebThingConfiguration.class).webThingURI); + // perform connect in background scheduler.execute(() -> { // WebThing URI present? - var uri = toUri(getConfigAs(WebThingConfiguration.class).webThingURI); - if (uri != null) { - logger.debug("try to connect WebThing {}", uri); - var connected = tryReconnect(uri); + if (webThingURI != null) { + logger.debug("try to connect WebThing {}", webThingURI); + var connected = tryReconnect(webThingURI); if (connected) { logger.debug("WebThing {} connected", getWebThingLabel()); } From adade81ef800c1917d8a9453eb3f77380fc1dc75 Mon Sep 17 00:00:00 2001 From: gregor roth Date: Mon, 26 Apr 2021 06:41:48 +0200 Subject: [PATCH 3/3] webThingURI will be loaded in a lazy way Signed-off-by: Gregor Roth Signed-off-by: gregor roth --- .../webthing/internal/WebThingHandler.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/WebThingHandler.java b/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/WebThingHandler.java index 79c4e7f7f1ab7..7c0327a2a4751 100644 --- a/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/WebThingHandler.java +++ b/bundles/org.openhab.binding.webthing/src/main/java/org/openhab/binding/webthing/internal/WebThingHandler.java @@ -94,14 +94,12 @@ public void initialize() { isActivated.set(true); // set with true, even though the connect may fail. In this case retries will be // triggered - webThingURI = toUri(getConfigAs(WebThingConfiguration.class).webThingURI); - // perform connect in background scheduler.execute(() -> { // WebThing URI present? - if (webThingURI != null) { + if (getWebThingURI() != null) { logger.debug("try to connect WebThing {}", webThingURI); - var connected = tryReconnect(webThingURI); + var connected = tryReconnect(); if (connected) { logger.debug("WebThing {} connected", getWebThingLabel()); } @@ -119,6 +117,13 @@ public void initialize() { .ifPresent(future -> future.cancel(true)); } + private @Nullable URI getWebThingURI() { + if (webThingURI == null) { + webThingURI = toUri(getConfigAs(WebThingConfiguration.class).webThingURI); + } + return webThingURI; + } + private @Nullable URI toUri(@Nullable String uri) { try { if (uri != null) { @@ -143,10 +148,11 @@ public void dispose() { } } - private boolean tryReconnect(@Nullable URI uri) { + private boolean tryReconnect() { if (isActivated.get()) { // will try reconnect only, if activated try { // create the client-side WebThing representation + var uri = getWebThingURI(); if (uri != null) { var webThing = ConsumedThingFactory.instance().create(webSocketClient, httpClient, uri, scheduler, this::onError); @@ -259,7 +265,7 @@ public void handleCommand(ChannelUID channelUID, Command command) { itemChangedListenerMap.getOrDefault(channelUID, EMPTY_ITEM_CHANGED_LISTENER).onItemStateChanged(channelUID, (State) command); } else if (command instanceof RefreshType) { - tryReconnect(webThingURI); + tryReconnect(); } } @@ -283,7 +289,7 @@ private void checkWebThingConnection() { // try reconnect, if necessary if (isDisconnected() || (isOnline() && !isAlive())) { logger.debug("try reconnecting WebThing {}", getWebThingLabel()); - if (tryReconnect(webThingURI)) { + if (tryReconnect()) { logger.debug("WebThing {} reconnected", getWebThingLabel()); } @@ -291,7 +297,7 @@ private void checkWebThingConnection() { // force reconnecting periodically, to fix erroneous states that occurs for unknown reasons var elapsedSinceLastReconnect = Duration.between(lastReconnect.get(), Instant.now()); if (isOnline() && (elapsedSinceLastReconnect.getSeconds() > RECONNECT_PERIOD.getSeconds())) { - if (tryReconnect(webThingURI)) { + if (tryReconnect()) { logger.debug("WebThing {} reconnected. Initiated by periodic reconnect", getWebThingLabel()); } else { logger.debug("could not reconnect WebThing {} (periodic reconnect failed). Next trial in {} sec",