From 858be481837ee9b45a27ffd91871728b45d1f272 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Tue, 13 Aug 2024 10:43:24 +0200 Subject: [PATCH] [freeboxos] Better fix for WEB socket registration (#17236) * Checks if the server supports VM. Replace #17124 which was more a workaround. * Change default value The value is always set when calling openSession. Signed-off-by: Laurent Garnier --- .../internal/api/rest/FreeboxOsSession.java | 10 +++++++++- .../internal/api/rest/WebSocketManager.java | 17 +++-------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeboxOsSession.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeboxOsSession.java index 911989a702d..32c8dfa1b9c 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeboxOsSession.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/FreeboxOsSession.java @@ -52,6 +52,8 @@ public class FreeboxOsSession { private @Nullable Session session; private String appToken = ""; private int wsReconnectInterval; + @Nullable + private Boolean vmSupported; public enum BoxModel { FBXGW_R1_FULL, // Freebox Server (v6) revision 1 @@ -85,7 +87,9 @@ public class FreeboxOsSession { ApiVersion.class, null, null); this.uriBuilder = config.getUriBuilder(version.baseUrl()); this.wsReconnectInterval = config.wsReconnectInterval; + this.vmSupported = null; getManager(LoginManager.class); + getManager(SystemManager.class); getManager(NetShareManager.class); getManager(LanManager.class); getManager(WifiManager.class); @@ -95,9 +99,13 @@ public class FreeboxOsSession { public void openSession(String appToken) throws FreeboxException { Session newSession = getManager(LoginManager.class).openSession(appToken); - getManager(WebSocketManager.class).openSession(newSession.sessionToken(), wsReconnectInterval); session = newSession; this.appToken = appToken; + if (vmSupported == null) { + vmSupported = getManager(SystemManager.class).getConfig().modelInfo().hasVm(); + } + getManager(WebSocketManager.class).openSession(newSession.sessionToken(), wsReconnectInterval, + Boolean.TRUE.equals(vmSupported)); } public String grant() throws FreeboxException { diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/WebSocketManager.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/WebSocketManager.java index 99aca9298ff..4b4b6b25398 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/WebSocketManager.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/api/rest/WebSocketManager.java @@ -73,8 +73,7 @@ public class WebSocketManager extends RestManager implements WebSocketListener { @Nullable private String sessionToken; private int reconnectInterval; - private boolean vmSupported = true; - private boolean retryConnectWithoutVm = false; + private boolean vmSupported; private record Register(String action, List events) { Register(String... events) { @@ -101,9 +100,10 @@ public class WebSocketManager extends RestManager implements WebSocketListener { } } - public void openSession(@Nullable String sessionToken, int reconnectInterval) { + public void openSession(@Nullable String sessionToken, int reconnectInterval, boolean vmSupported) { this.sessionToken = sessionToken; this.reconnectInterval = reconnectInterval; + this.vmSupported = vmSupported; if (reconnectInterval > 0) { try { client.start(); @@ -190,12 +190,6 @@ public class WebSocketManager extends RestManager implements WebSocketListener { default: logger.warn("Unhandled notification received: {}", result.action); } - } else if (result.action == Action.REGISTER) { - logger.debug("Event registration failed!"); - if (vmSupported && "unsupported event vm_state_changed".equals(result.msg)) { - vmSupported = false; - retryConnectWithoutVm = true; - } } } @@ -233,11 +227,6 @@ public class WebSocketManager extends RestManager implements WebSocketListener { public void onWebSocketClose(int statusCode, @NonNullByDefault({}) String reason) { logger.debug("Socket Closed: [{}] - reason {}", statusCode, reason); this.wsSession = null; - if (retryConnectWithoutVm) { - logger.debug("Retry connecting websocket client without VM support"); - retryConnectWithoutVm = false; - startReconnect(); - } } @Override