From 49cec570801bfd470817ffa9c8d7f5cf1abd877b Mon Sep 17 00:00:00 2001 From: gregor roth Date: Mon, 26 Apr 2021 06:41:48 +0200 Subject: [PATCH] webThingURI will be loaded in a lazy way 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",