Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WebThing] If WebThing network connection is crashed, WebThing will not be reconnected. #10579

Merged
merged 3 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ public void initialize() {
// 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 (getWebThingURI() != null) {
logger.debug("try to connect WebThing {}", webThingURI);
var connected = tryReconnect();
if (connected) {
logger.debug("WebThing {} connected", getWebThingLabel());
}
Expand All @@ -118,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) {
Expand All @@ -142,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);
Expand Down Expand Up @@ -258,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();
}
}

Expand All @@ -282,15 +289,15 @@ 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());
}

} else {
// 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,7 +87,7 @@ public void close() {
}

@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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -289,8 +288,8 @@ public static class TestWebsocketConnectionFactory implements WebSocketConnectio
public final AtomicReference<WebSocketImpl> webSocketRef = new AtomicReference<>();

@Override
public WebSocketConnection create(@NonNull URI webSocketURI, @NonNull ScheduledExecutorService executor,
@NonNull Consumer<String> errorHandler, @NonNull Duration pingPeriod) {
public WebSocketConnection create(URI webSocketURI, ScheduledExecutorService executor,
Consumer<String> errorHandler, Duration pingPeriod) {
var webSocketConnection = new WebSocketConnectionImpl(executor, errorHandler, pingPeriod);
var webSocket = new WebSocketImpl(webSocketConnection);
webSocketRef.set(webSocket);
Expand Down