[WebThing] If WebThing network connection is crashed, WebThing will not be reconnected. (#10579)
* Unnecessary NonNull annotations removed Signed-off-by: Gregor Roth <gregor.roth@web.de> Signed-off-by: gregor roth <gregor.roth@web.de> * 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 <gregor.roth@web.de> Signed-off-by: gregor roth <gregor.roth@web.de> * webThingURI will be loaded in a lazy way Signed-off-by: Gregor Roth <gregor.roth@web.de> Signed-off-by: gregor roth <gregor.roth@web.de>
This commit is contained in:
parent
cfea5a2185
commit
8a21f9b715
@ -97,10 +97,9 @@ public class WebThingHandler extends BaseThingHandler implements ChannelHandler
|
|||||||
// perform connect in background
|
// perform connect in background
|
||||||
scheduler.execute(() -> {
|
scheduler.execute(() -> {
|
||||||
// WebThing URI present?
|
// WebThing URI present?
|
||||||
var uri = toUri(getConfigAs(WebThingConfiguration.class).webThingURI);
|
if (getWebThingURI() != null) {
|
||||||
if (uri != null) {
|
logger.debug("try to connect WebThing {}", webThingURI);
|
||||||
logger.debug("try to connect WebThing {}", uri);
|
var connected = tryReconnect();
|
||||||
var connected = tryReconnect(uri);
|
|
||||||
if (connected) {
|
if (connected) {
|
||||||
logger.debug("WebThing {} connected", getWebThingLabel());
|
logger.debug("WebThing {} connected", getWebThingLabel());
|
||||||
}
|
}
|
||||||
@ -118,6 +117,13 @@ public class WebThingHandler extends BaseThingHandler implements ChannelHandler
|
|||||||
.ifPresent(future -> future.cancel(true));
|
.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) {
|
private @Nullable URI toUri(@Nullable String uri) {
|
||||||
try {
|
try {
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
@ -142,10 +148,11 @@ public class WebThingHandler extends BaseThingHandler implements ChannelHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean tryReconnect(@Nullable URI uri) {
|
private boolean tryReconnect() {
|
||||||
if (isActivated.get()) { // will try reconnect only, if activated
|
if (isActivated.get()) { // will try reconnect only, if activated
|
||||||
try {
|
try {
|
||||||
// create the client-side WebThing representation
|
// create the client-side WebThing representation
|
||||||
|
var uri = getWebThingURI();
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
var webThing = ConsumedThingFactory.instance().create(webSocketClient, httpClient, uri, scheduler,
|
var webThing = ConsumedThingFactory.instance().create(webSocketClient, httpClient, uri, scheduler,
|
||||||
this::onError);
|
this::onError);
|
||||||
@ -258,7 +265,7 @@ public class WebThingHandler extends BaseThingHandler implements ChannelHandler
|
|||||||
itemChangedListenerMap.getOrDefault(channelUID, EMPTY_ITEM_CHANGED_LISTENER).onItemStateChanged(channelUID,
|
itemChangedListenerMap.getOrDefault(channelUID, EMPTY_ITEM_CHANGED_LISTENER).onItemStateChanged(channelUID,
|
||||||
(State) command);
|
(State) command);
|
||||||
} else if (command instanceof RefreshType) {
|
} else if (command instanceof RefreshType) {
|
||||||
tryReconnect(webThingURI);
|
tryReconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,7 +289,7 @@ public class WebThingHandler extends BaseThingHandler implements ChannelHandler
|
|||||||
// try reconnect, if necessary
|
// try reconnect, if necessary
|
||||||
if (isDisconnected() || (isOnline() && !isAlive())) {
|
if (isDisconnected() || (isOnline() && !isAlive())) {
|
||||||
logger.debug("try reconnecting WebThing {}", getWebThingLabel());
|
logger.debug("try reconnecting WebThing {}", getWebThingLabel());
|
||||||
if (tryReconnect(webThingURI)) {
|
if (tryReconnect()) {
|
||||||
logger.debug("WebThing {} reconnected", getWebThingLabel());
|
logger.debug("WebThing {} reconnected", getWebThingLabel());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,7 +297,7 @@ public class WebThingHandler extends BaseThingHandler implements ChannelHandler
|
|||||||
// force reconnecting periodically, to fix erroneous states that occurs for unknown reasons
|
// force reconnecting periodically, to fix erroneous states that occurs for unknown reasons
|
||||||
var elapsedSinceLastReconnect = Duration.between(lastReconnect.get(), Instant.now());
|
var elapsedSinceLastReconnect = Duration.between(lastReconnect.get(), Instant.now());
|
||||||
if (isOnline() && (elapsedSinceLastReconnect.getSeconds() > RECONNECT_PERIOD.getSeconds())) {
|
if (isOnline() && (elapsedSinceLastReconnect.getSeconds() > RECONNECT_PERIOD.getSeconds())) {
|
||||||
if (tryReconnect(webThingURI)) {
|
if (tryReconnect()) {
|
||||||
logger.debug("WebThing {} reconnected. Initiated by periodic reconnect", getWebThingLabel());
|
logger.debug("WebThing {} reconnected. Initiated by periodic reconnect", getWebThingLabel());
|
||||||
} else {
|
} else {
|
||||||
logger.debug("could not reconnect WebThing {} (periodic reconnect failed). Next trial in {} sec",
|
logger.debug("could not reconnect WebThing {} (periodic reconnect failed). Next trial in {} sec",
|
||||||
|
|||||||
@ -26,7 +26,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNull;
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.eclipse.jetty.websocket.api.Session;
|
import org.eclipse.jetty.websocket.api.Session;
|
||||||
@ -88,7 +87,7 @@ public class WebSocketConnectionImpl implements WebSocketConnection, WebSocketLi
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void observeProperty(@NonNull String propertyName, @NonNull BiConsumer<String, Object> listener) {
|
public void observeProperty(String propertyName, BiConsumer<String, Object> listener) {
|
||||||
propertyChangedListeners.put(propertyName, listener);
|
propertyChangedListeners.put(propertyName, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -34,7 +34,6 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNull;
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
@ -289,8 +288,8 @@ public class WebthingTest {
|
|||||||
public final AtomicReference<WebSocketImpl> webSocketRef = new AtomicReference<>();
|
public final AtomicReference<WebSocketImpl> webSocketRef = new AtomicReference<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WebSocketConnection create(@NonNull URI webSocketURI, @NonNull ScheduledExecutorService executor,
|
public WebSocketConnection create(URI webSocketURI, ScheduledExecutorService executor,
|
||||||
@NonNull Consumer<String> errorHandler, @NonNull Duration pingPeriod) {
|
Consumer<String> errorHandler, Duration pingPeriod) {
|
||||||
var webSocketConnection = new WebSocketConnectionImpl(executor, errorHandler, pingPeriod);
|
var webSocketConnection = new WebSocketConnectionImpl(executor, errorHandler, pingPeriod);
|
||||||
var webSocket = new WebSocketImpl(webSocketConnection);
|
var webSocket = new WebSocketImpl(webSocketConnection);
|
||||||
webSocketRef.set(webSocket);
|
webSocketRef.set(webSocket);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user