From 561eb84f6522d535103a5f57ff9a942adb2215a5 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Fri, 24 Feb 2023 15:02:54 +0100 Subject: [PATCH] [neohub] Use createWebSocketClient (#14482) Signed-off-by: Laurent Garnier --- .../neohub/internal/NeoHubHandler.java | 8 ++++++-- .../neohub/internal/NeoHubHandlerFactory.java | 11 ++++++++++- .../neohub/internal/NeoHubWebSocket.java | 19 ++++++++----------- .../neohub/test/NeoHubProtocolTests.java | 17 ++++++++++++++++- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java index 3e22cddf44..40715180f4 100644 --- a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java +++ b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java @@ -30,6 +30,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.neohub.internal.NeoHubAbstractDeviceData.AbstractRecord; import org.openhab.binding.neohub.internal.NeoHubBindingConstants.NeoHubReturnResult; +import org.openhab.core.io.net.http.WebSocketFactory; import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.unit.SIUnits; import org.openhab.core.library.unit.Units; @@ -65,6 +66,8 @@ public class NeoHubHandler extends BaseBridgeHandler { private final Map connectionStates = new HashMap<>(); + private WebSocketFactory webSocketFactory; + private @Nullable NeoHubConfiguration config; private @Nullable NeoHubSocketBase socket; private @Nullable ScheduledFuture lazyPollingScheduler; @@ -89,8 +92,9 @@ public class NeoHubHandler extends BaseBridgeHandler { private boolean isApiOnline = false; private int failedSendAttempts = 0; - public NeoHubHandler(Bridge bridge) { + public NeoHubHandler(Bridge bridge, WebSocketFactory webSocketFactory) { super(bridge); + this.webSocketFactory = webSocketFactory; } @Override @@ -148,7 +152,7 @@ public class NeoHubHandler extends BaseBridgeHandler { NeoHubSocketBase socket; try { if (config.useWebSocket) { - socket = new NeoHubWebSocket(config, thing.getUID().getAsString()); + socket = new NeoHubWebSocket(config, webSocketFactory, thing.getUID()); } else { socket = new NeoHubSocket(config, thing.getUID().getAsString()); } diff --git a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandlerFactory.java b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandlerFactory.java index 729868a20f..cc90d6fc85 100644 --- a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandlerFactory.java +++ b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandlerFactory.java @@ -25,6 +25,7 @@ import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.config.discovery.DiscoveryService; +import org.openhab.core.io.net.http.WebSocketFactory; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; @@ -33,7 +34,9 @@ import org.openhab.core.thing.binding.BaseThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandlerFactory; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; /** * The {@link NeoHubHandlerFactory} creates things and thing handlers @@ -48,8 +51,14 @@ public class NeoHubHandlerFactory extends BaseThingHandlerFactory { .unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_NEOHUB, THING_TYPE_NEOSTAT, THING_TYPE_NEOPLUG, THING_TYPE_NEOCONTACT, THING_TYPE_NEOTEMPERATURESENSOR))); + private final WebSocketFactory webSocketFactory; private final Map> discoServices = new HashMap<>(); + @Activate + public NeoHubHandlerFactory(final @Reference WebSocketFactory webSocketFactory) { + this.webSocketFactory = webSocketFactory; + } + @Override public boolean supportsThingType(ThingTypeUID thingTypeUID) { return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID); @@ -60,7 +69,7 @@ public class NeoHubHandlerFactory extends BaseThingHandlerFactory { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if ((thingTypeUID.equals(THING_TYPE_NEOHUB)) && (thing instanceof Bridge)) { - NeoHubHandler handler = new NeoHubHandler((Bridge) thing); + NeoHubHandler handler = new NeoHubHandler((Bridge) thing, webSocketFactory); createDiscoveryService(handler); return handler; } diff --git a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubWebSocket.java b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubWebSocket.java index fddfd06e0c..7d24d9b807 100644 --- a/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubWebSocket.java +++ b/bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubWebSocket.java @@ -19,7 +19,6 @@ import java.util.concurrent.ExecutionException; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; -import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; @@ -28,6 +27,9 @@ import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.openhab.core.io.net.http.WebSocketFactory; +import org.openhab.core.thing.ThingUID; +import org.openhab.core.thing.util.ThingWebClientUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,19 +73,14 @@ public class NeoHubWebSocket extends NeoHubSocketBase { public @Nullable String response; } - public NeoHubWebSocket(NeoHubConfiguration config, String hubId) throws IOException { - super(config, hubId); + public NeoHubWebSocket(NeoHubConfiguration config, WebSocketFactory webSocketFactory, ThingUID bridgeUID) + throws IOException { + super(config, bridgeUID.getAsString()); - // initialise and start ssl context factory, http client, web socket client SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); sslContextFactory.setTrustAll(true); - HttpClient httpClient = new HttpClient(sslContextFactory); - try { - httpClient.start(); - } catch (Exception e) { - throw new IOException("Error starting HTTP client", e); - } - webSocketClient = new WebSocketClient(httpClient); + String name = ThingWebClientUtil.buildWebClientConsumerName(bridgeUID, null); + webSocketClient = webSocketFactory.createWebSocketClient(name, sslContextFactory); webSocketClient.setConnectTimeout(config.socketTimeout * 1000); try { webSocketClient.start(); diff --git a/bundles/org.openhab.binding.neohub/src/test/java/org/openhab/binding/neohub/test/NeoHubProtocolTests.java b/bundles/org.openhab.binding.neohub/src/test/java/org/openhab/binding/neohub/test/NeoHubProtocolTests.java index 81ff9b7b48..8fd11d71ec 100644 --- a/bundles/org.openhab.binding.neohub/src/test/java/org/openhab/binding/neohub/test/NeoHubProtocolTests.java +++ b/bundles/org.openhab.binding.neohub/src/test/java/org/openhab/binding/neohub/test/NeoHubProtocolTests.java @@ -13,16 +13,23 @@ package org.openhab.binding.neohub.test; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; import java.io.IOException; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.eclipse.jetty.websocket.client.WebSocketClient; import org.junit.jupiter.api.Test; import org.openhab.binding.neohub.internal.NeoHubBindingConstants; import org.openhab.binding.neohub.internal.NeoHubConfiguration; import org.openhab.binding.neohub.internal.NeoHubException; import org.openhab.binding.neohub.internal.NeoHubSocket; import org.openhab.binding.neohub.internal.NeoHubWebSocket; +import org.openhab.core.io.net.http.WebSocketFactory; +import org.openhab.core.thing.ThingUID; /** * JUnit for testing WSS and TCP socket protocols. @@ -70,7 +77,15 @@ public class NeoHubProtocolTests { config.socketTimeout = SOCKET_TIMEOUT; config.apiToken = HUB_API_TOKEN; - NeoHubWebSocket socket = new NeoHubWebSocket(config, "test"); + SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); + sslContextFactory.setTrustAll(true); + HttpClient httpClient = new HttpClient(sslContextFactory); + WebSocketClient webSocketClient = new WebSocketClient(httpClient); + + WebSocketFactory webSocketFactory = mock(WebSocketFactory.class); + when(webSocketFactory.createWebSocketClient(anyString(), any())).thenReturn(webSocketClient); + + NeoHubWebSocket socket = new NeoHubWebSocket(config, webSocketFactory, new ThingUID("neohub:account:test")); String requestJson = NeoHubBindingConstants.CMD_CODE_FIRMWARE; String responseJson = socket.sendMessage(requestJson); assertNotEquals(0, responseJson.length()); -- 2.47.3