mirror of
https://github.com/danieldemus/openhab-addons
synced 2026-07-29 12:34:21 +02:00
Fix reconnection logic (#21058)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
+34
-9
@@ -170,17 +170,30 @@ public class GardenaSmartImpl implements GardenaSmart, GardenaSmartWebSocketList
|
||||
for (LocationDataItem location : locationsResponse.data) {
|
||||
WebSocketCreatedResponse webSocketCreatedResponse = getWebsocketInfo(location.id);
|
||||
Location locationAttributes = location.attributes;
|
||||
WebSocket webSocketAttributes = webSocketCreatedResponse.data.attributes;
|
||||
if (locationAttributes == null || webSocketAttributes == null) {
|
||||
String webSocketUrl = getWebSocketUrl(webSocketCreatedResponse);
|
||||
if (locationAttributes == null || webSocketUrl == null) {
|
||||
logger.warn("Cannot start Gardena Webservice for location {} ({}): missing websocket data",
|
||||
location.id, locationAttributes == null ? null : locationAttributes.name);
|
||||
continue;
|
||||
}
|
||||
String socketId = id + "-" + locationAttributes.name;
|
||||
webSockets.put(location.id, new GardenaSmartWebSocket(this, webSocketClient, scheduler,
|
||||
webSocketAttributes.url, token, socketId, location.id));
|
||||
webSockets.put(location.id, new GardenaSmartWebSocket(this, webSocketClient, scheduler, webSocketUrl,
|
||||
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.
|
||||
*/
|
||||
@@ -423,14 +436,26 @@ public class GardenaSmartImpl implements GardenaSmart, GardenaSmartWebSocketList
|
||||
Thread.sleep(3000);
|
||||
WebSocketCreatedResponse webSocketCreatedResponse = getWebsocketInfo(socket.getLocationID());
|
||||
// only restart single socket, do not restart binding
|
||||
WebSocket webSocketAttributes = webSocketCreatedResponse.data.attributes;
|
||||
if (webSocketAttributes != null) {
|
||||
socket.restart(webSocketAttributes.url);
|
||||
String webSocketUrl = getWebSocketUrl(webSocketCreatedResponse);
|
||||
if (webSocketUrl == null) {
|
||||
throw new GardenaException(
|
||||
"No websocket URL received for location " + socket.getLocationID() + " during restart");
|
||||
}
|
||||
socket.updateToken(token);
|
||||
socket.restart(webSocketUrl);
|
||||
} catch (Exception ex) {
|
||||
// restart binding on error
|
||||
logger.warn("Restarting GardenaSmart Webservice failed ({}): {}, restarting binding", socket.getSocketID(),
|
||||
ex.getMessage());
|
||||
String message = 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();
|
||||
}
|
||||
}
|
||||
|
||||
+4
@@ -108,6 +108,10 @@ public class GardenaSmartWebSocket {
|
||||
return this.locationID;
|
||||
}
|
||||
|
||||
public synchronized void updateToken(@Nullable PostOAuth2Response token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public void restart(String newUrl) throws Exception {
|
||||
logger.debug("Reconnecting to Gardena Webservice ({})", socketId);
|
||||
session = (WebSocketSession) webSocketClient.connect(this, new URI(newUrl)).get();
|
||||
|
||||
Reference in New Issue
Block a user