Fix reconnection logic (#21058)

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel
2026-06-26 12:03:13 +02:00
committed by GitHub
parent b26db1fbdd
commit cc3bf8c910
2 changed files with 38 additions and 9 deletions
@@ -170,17 +170,30 @@ public class GardenaSmartImpl implements GardenaSmart, GardenaSmartWebSocketList
for (LocationDataItem location : locationsResponse.data) { for (LocationDataItem location : locationsResponse.data) {
WebSocketCreatedResponse webSocketCreatedResponse = getWebsocketInfo(location.id); WebSocketCreatedResponse webSocketCreatedResponse = getWebsocketInfo(location.id);
Location locationAttributes = location.attributes; Location locationAttributes = location.attributes;
WebSocket webSocketAttributes = webSocketCreatedResponse.data.attributes; String webSocketUrl = getWebSocketUrl(webSocketCreatedResponse);
if (locationAttributes == null || webSocketAttributes == null) { if (locationAttributes == null || webSocketUrl == null) {
logger.warn("Cannot start Gardena Webservice for location {} ({}): missing websocket data",
location.id, locationAttributes == null ? null : locationAttributes.name);
continue; continue;
} }
String socketId = id + "-" + locationAttributes.name; String socketId = id + "-" + locationAttributes.name;
webSockets.put(location.id, new GardenaSmartWebSocket(this, webSocketClient, scheduler, webSockets.put(location.id, new GardenaSmartWebSocket(this, webSocketClient, scheduler, webSocketUrl,
webSocketAttributes.url, token, socketId, location.id)); token, socketId, location.id));
} }
} }
} }
private @Nullable String getWebSocketUrl(@Nullable WebSocketCreatedResponse webSocketCreatedResponse) {
if (webSocketCreatedResponse == null || webSocketCreatedResponse.data == null) {
return null;
}
WebSocket webSocketAttributes = webSocketCreatedResponse.data.attributes;
if (webSocketAttributes == null || webSocketAttributes.url == null || webSocketAttributes.url.isBlank()) {
return null;
}
return webSocketAttributes.url;
}
/** /**
* Stops all websockets. * Stops all websockets.
*/ */
@@ -423,14 +436,26 @@ public class GardenaSmartImpl implements GardenaSmart, GardenaSmartWebSocketList
Thread.sleep(3000); Thread.sleep(3000);
WebSocketCreatedResponse webSocketCreatedResponse = getWebsocketInfo(socket.getLocationID()); WebSocketCreatedResponse webSocketCreatedResponse = getWebsocketInfo(socket.getLocationID());
// only restart single socket, do not restart binding // only restart single socket, do not restart binding
WebSocket webSocketAttributes = webSocketCreatedResponse.data.attributes; String webSocketUrl = getWebSocketUrl(webSocketCreatedResponse);
if (webSocketAttributes != null) { if (webSocketUrl == null) {
socket.restart(webSocketAttributes.url); throw new GardenaException(
"No websocket URL received for location " + socket.getLocationID() + " during restart");
} }
socket.updateToken(token);
socket.restart(webSocketUrl);
} catch (Exception ex) { } catch (Exception ex) {
// restart binding on error // restart binding on error
logger.warn("Restarting GardenaSmart Webservice failed ({}): {}, restarting binding", socket.getSocketID(), String message = ex.getMessage();
ex.getMessage()); if (message == null || message.isBlank()) {
message = ex.getClass().getSimpleName();
}
if (logger.isDebugEnabled()) {
logger.warn("Restarting GardenaSmart Webservice failed ({}): {}, restarting binding",
socket.getSocketID(), message, ex);
} else {
logger.warn("Restarting GardenaSmart Webservice failed ({}): {}, restarting binding",
socket.getSocketID(), message);
}
eventListener.onError(); eventListener.onError();
} }
} }
@@ -108,6 +108,10 @@ public class GardenaSmartWebSocket {
return this.locationID; return this.locationID;
} }
public synchronized void updateToken(@Nullable PostOAuth2Response token) {
this.token = token;
}
public void restart(String newUrl) throws Exception { public void restart(String newUrl) throws Exception {
logger.debug("Reconnecting to Gardena Webservice ({})", socketId); logger.debug("Reconnecting to Gardena Webservice ({})", socketId);
session = (WebSocketSession) webSocketClient.connect(this, new URI(newUrl)).get(); session = (WebSocketSession) webSocketClient.connect(this, new URI(newUrl)).get();