]> git.basschouten.com Git - openhab-addons.git/commitdiff
[neohub] Use createWebSocketClient (#14482)
authorlolodomo <lg.hc@free.fr>
Fri, 24 Feb 2023 14:02:54 +0000 (15:02 +0100)
committerGitHub <noreply@github.com>
Fri, 24 Feb 2023 14:02:54 +0000 (15:02 +0100)
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandler.java
bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubHandlerFactory.java
bundles/org.openhab.binding.neohub/src/main/java/org/openhab/binding/neohub/internal/NeoHubWebSocket.java
bundles/org.openhab.binding.neohub/src/test/java/org/openhab/binding/neohub/test/NeoHubProtocolTests.java

index 3e22cddf44e3bf6b699424e652afec156f56b78d..40715180f440991b1a8cb11b91905b1ab54ccea1 100644 (file)
@@ -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<String, Boolean> 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());
             }
index 729868a20f9b406e0fdfa12119c3378b76b0885c..cc90d6fc85c05814fa4e60820987d9d08aca865c 100644 (file)
@@ -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<ThingUID, ServiceRegistration<?>> 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;
         }
index fddfd06e0c03faf7f6206fa69f1336d86a624c49..7d24d9b80788058e091fe2439d96d18d0569061f 100644 (file)
@@ -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();
index 81ff9b7b48785754ea8cec642497f788cec6b72e..8fd11d71ec9f446eb4d2482c9398be2cd425ea02 100644 (file)
 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());