use of org.openremote.agent.protocol.websocket.WebsocketIOClient in project openremote by openremote.
the class GatewayClientService method createGatewayClient.
protected WebsocketIOClient<String> createGatewayClient(GatewayConnection connection) {
if (connection.isDisabled()) {
LOG.info("Disabled gateway client connection so ignoring: " + connection);
return null;
}
LOG.info("Creating gateway IO client: " + connection);
try {
WebsocketIOClient<String> client = new WebsocketIOClient<>(new URIBuilder().setScheme(connection.isSecured() ? "wss" : "ws").setHost(connection.getHost()).setPort(connection.getPort() == null ? -1 : connection.getPort()).setPath("websocket/events").setParameter(Constants.REALM_PARAM_NAME, connection.getRealm()).build(), null, new OAuthClientCredentialsGrant(new URIBuilder().setScheme(connection.isSecured() ? "https" : "http").setHost(connection.getHost()).setPort(connection.getPort() == null ? -1 : connection.getPort()).setPath("auth/realms/" + connection.getRealm() + "/protocol/openid-connect/token").build().toString(), connection.getClientId(), connection.getClientSecret(), null).setBasicAuthHeader(true));
client.setEncoderDecoderProvider(() -> new ChannelHandler[] { new AbstractNettyIOClient.MessageToMessageDecoder<>(String.class, client) });
client.addConnectionStatusConsumer(connectionStatus -> onGatewayClientConnectionStatusChanged(connection, connectionStatus));
client.addMessageConsumer(message -> onCentralManagerMessage(connection, message));
// Subscribe to Asset<?> and attribute events of local realm and pass through to connected manager
clientEventService.addInternalSubscription(getClientSessionKey(connection) + "Asset", AssetEvent.class, new AssetFilter<AssetEvent>().setRealm(connection.getLocalRealm()), assetEvent -> sendCentralManagerMessage(connection.getLocalRealm(), messageToString(SharedEvent.MESSAGE_PREFIX, assetEvent)));
clientEventService.addInternalSubscription(getClientSessionKey(connection) + "Attribute", AttributeEvent.class, new AssetFilter<AttributeEvent>().setRealm(connection.getLocalRealm()), attributeEvent -> sendCentralManagerMessage(connection.getLocalRealm(), messageToString(SharedEvent.MESSAGE_PREFIX, attributeEvent)));
client.connect();
return client;
} catch (Exception e) {
LOG.log(Level.WARNING, "Creating gateway IO client failed so marking connection as disabled: " + connection, e);
connection.setDisabled(true);
setConnection(connection);
}
return null;
}
Aggregations