[neohub] Use createWebSocketClient (#14482)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-02-24 15:02:54 +01:00 committed by GitHub
parent 7e047b3fc6
commit 561eb84f65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 15 deletions

View File

@ -30,6 +30,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.neohub.internal.NeoHubAbstractDeviceData.AbstractRecord; import org.openhab.binding.neohub.internal.NeoHubAbstractDeviceData.AbstractRecord;
import org.openhab.binding.neohub.internal.NeoHubBindingConstants.NeoHubReturnResult; 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.types.QuantityType;
import org.openhab.core.library.unit.SIUnits; import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.Units; 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 final Map<String, Boolean> connectionStates = new HashMap<>();
private WebSocketFactory webSocketFactory;
private @Nullable NeoHubConfiguration config; private @Nullable NeoHubConfiguration config;
private @Nullable NeoHubSocketBase socket; private @Nullable NeoHubSocketBase socket;
private @Nullable ScheduledFuture<?> lazyPollingScheduler; private @Nullable ScheduledFuture<?> lazyPollingScheduler;
@ -89,8 +92,9 @@ public class NeoHubHandler extends BaseBridgeHandler {
private boolean isApiOnline = false; private boolean isApiOnline = false;
private int failedSendAttempts = 0; private int failedSendAttempts = 0;
public NeoHubHandler(Bridge bridge) { public NeoHubHandler(Bridge bridge, WebSocketFactory webSocketFactory) {
super(bridge); super(bridge);
this.webSocketFactory = webSocketFactory;
} }
@Override @Override
@ -148,7 +152,7 @@ public class NeoHubHandler extends BaseBridgeHandler {
NeoHubSocketBase socket; NeoHubSocketBase socket;
try { try {
if (config.useWebSocket) { if (config.useWebSocket) {
socket = new NeoHubWebSocket(config, thing.getUID().getAsString()); socket = new NeoHubWebSocket(config, webSocketFactory, thing.getUID());
} else { } else {
socket = new NeoHubSocket(config, thing.getUID().getAsString()); socket = new NeoHubSocket(config, thing.getUID().getAsString());
} }

View File

@ -25,6 +25,7 @@ import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.discovery.DiscoveryService; 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.Bridge;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID; 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.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.framework.ServiceRegistration; 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.Component;
import org.osgi.service.component.annotations.Reference;
/** /**
* The {@link NeoHubHandlerFactory} creates things and thing handlers * 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, .unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_NEOHUB, THING_TYPE_NEOSTAT, THING_TYPE_NEOPLUG,
THING_TYPE_NEOCONTACT, THING_TYPE_NEOTEMPERATURESENSOR))); THING_TYPE_NEOCONTACT, THING_TYPE_NEOTEMPERATURESENSOR)));
private final WebSocketFactory webSocketFactory;
private final Map<ThingUID, ServiceRegistration<?>> discoServices = new HashMap<>(); private final Map<ThingUID, ServiceRegistration<?>> discoServices = new HashMap<>();
@Activate
public NeoHubHandlerFactory(final @Reference WebSocketFactory webSocketFactory) {
this.webSocketFactory = webSocketFactory;
}
@Override @Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) { public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID); return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
@ -60,7 +69,7 @@ public class NeoHubHandlerFactory extends BaseThingHandlerFactory {
ThingTypeUID thingTypeUID = thing.getThingTypeUID(); ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if ((thingTypeUID.equals(THING_TYPE_NEOHUB)) && (thing instanceof Bridge)) { if ((thingTypeUID.equals(THING_TYPE_NEOHUB)) && (thing instanceof Bridge)) {
NeoHubHandler handler = new NeoHubHandler((Bridge) thing); NeoHubHandler handler = new NeoHubHandler((Bridge) thing, webSocketFactory);
createDiscoveryService(handler); createDiscoveryService(handler);
return handler; return handler;
} }

View File

@ -19,7 +19,6 @@ import java.util.concurrent.ExecutionException;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; 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.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket; import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.WebSocketClient; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -71,19 +73,14 @@ public class NeoHubWebSocket extends NeoHubSocketBase {
public @Nullable String response; public @Nullable String response;
} }
public NeoHubWebSocket(NeoHubConfiguration config, String hubId) throws IOException { public NeoHubWebSocket(NeoHubConfiguration config, WebSocketFactory webSocketFactory, ThingUID bridgeUID)
super(config, hubId); throws IOException {
super(config, bridgeUID.getAsString());
// initialise and start ssl context factory, http client, web socket client
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
sslContextFactory.setTrustAll(true); sslContextFactory.setTrustAll(true);
HttpClient httpClient = new HttpClient(sslContextFactory); String name = ThingWebClientUtil.buildWebClientConsumerName(bridgeUID, null);
try { webSocketClient = webSocketFactory.createWebSocketClient(name, sslContextFactory);
httpClient.start();
} catch (Exception e) {
throw new IOException("Error starting HTTP client", e);
}
webSocketClient = new WebSocketClient(httpClient);
webSocketClient.setConnectTimeout(config.socketTimeout * 1000); webSocketClient.setConnectTimeout(config.socketTimeout * 1000);
try { try {
webSocketClient.start(); webSocketClient.start();

View File

@ -13,16 +13,23 @@
package org.openhab.binding.neohub.test; package org.openhab.binding.neohub.test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.io.IOException; import java.io.IOException;
import org.eclipse.jdt.annotation.NonNullByDefault; 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.junit.jupiter.api.Test;
import org.openhab.binding.neohub.internal.NeoHubBindingConstants; import org.openhab.binding.neohub.internal.NeoHubBindingConstants;
import org.openhab.binding.neohub.internal.NeoHubConfiguration; import org.openhab.binding.neohub.internal.NeoHubConfiguration;
import org.openhab.binding.neohub.internal.NeoHubException; import org.openhab.binding.neohub.internal.NeoHubException;
import org.openhab.binding.neohub.internal.NeoHubSocket; import org.openhab.binding.neohub.internal.NeoHubSocket;
import org.openhab.binding.neohub.internal.NeoHubWebSocket; 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. * JUnit for testing WSS and TCP socket protocols.
@ -70,7 +77,15 @@ public class NeoHubProtocolTests {
config.socketTimeout = SOCKET_TIMEOUT; config.socketTimeout = SOCKET_TIMEOUT;
config.apiToken = HUB_API_TOKEN; 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 requestJson = NeoHubBindingConstants.CMD_CODE_FIRMWARE;
String responseJson = socket.sendMessage(requestJson); String responseJson = socket.sendMessage(requestJson);
assertNotEquals(0, responseJson.length()); assertNotEquals(0, responseJson.length());