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) {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user