use of org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider in project netxms by netxms.
the class ServerConnection method connect.
/**
* Connect to server
*
* @throws Exception on any error
*/
private void connect() throws Exception {
if (session != null)
return;
try {
session = OpcUaClient.create(url, endpoints -> endpoints.stream().filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri())).findFirst(), configBuilder -> configBuilder.setApplicationName(LocalizedText.english("NetXMS Agent")).setApplicationUri("urn:netxms:agent").setIdentityProvider(new AnonymousProvider()).setRequestTimeout(UInteger.valueOf(5000)).build());
session.connect().get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
Platform.writeDebugLog(5, String.format("OPCUA: cannot setup connection to %s (%s)", url, e.getMessage()));
Platform.writeDebugLog(5, "OPCUA: ", e);
throw e;
}
}
use of org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider in project milo by eclipse.
the class OpcUaClientConfigTest method testCopy.
@Test
public void testCopy() {
OpcUaClientConfig original = OpcUaClientConfig.builder().setEndpoint(endpoint).setSessionName(() -> "testSessionName").setSessionTimeout(uint(60000 * 60)).setRequestTimeout(uint(120000)).setMaxResponseMessageSize(UInteger.MAX).setMaxPendingPublishRequests(uint(2)).setIdentityProvider(new AnonymousProvider()).setSessionLocaleIds(new String[] { "en", "es" }).build();
OpcUaClientConfig copy = OpcUaClientConfig.copy(original).build();
assertEquals(copy.getSessionName(), original.getSessionName());
assertEquals(copy.getSessionTimeout(), original.getSessionTimeout());
assertEquals(copy.getRequestTimeout(), original.getRequestTimeout());
assertEquals(copy.getMaxResponseMessageSize(), original.getMaxResponseMessageSize());
assertEquals(copy.getMaxPendingPublishRequests(), original.getMaxPendingPublishRequests());
assertEquals(copy.getIdentityProvider(), original.getIdentityProvider());
assertEquals(copy.getKeepAliveFailuresAllowed(), original.getKeepAliveFailuresAllowed());
assertEquals(copy.getKeepAliveInterval(), original.getKeepAliveInterval());
assertEquals(copy.getKeepAliveTimeout(), original.getKeepAliveTimeout());
assertEquals(copy.getSessionLocaleIds(), original.getSessionLocaleIds());
}
use of org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider in project milo by eclipse.
the class OpcUaClientConfigTest method testCopyAndModify.
@Test
public void testCopyAndModify() {
OpcUaClientConfig original = OpcUaClientConfig.builder().setEndpoint(endpoint).setSessionName(() -> "testSessionName").setSessionTimeout(uint(60000 * 60)).setRequestTimeout(uint(120000)).setMaxResponseMessageSize(UInteger.MAX).setMaxPendingPublishRequests(uint(2)).setIdentityProvider(new AnonymousProvider()).build();
OpcUaClientConfig copy = OpcUaClientConfig.copy(original, builder -> builder.setSessionName(() -> "foo").setSessionTimeout(uint(0)).setRequestTimeout(uint(0)).setMaxResponseMessageSize(uint(0)).setMaxPendingPublishRequests(uint(0)).setIdentityProvider(new AnonymousProvider()).setKeepAliveFailuresAllowed(uint(2)).setKeepAliveInterval(uint(10000)).setKeepAliveTimeout(uint(15000)).setSessionLocaleIds(new String[] { "en", "es" }));
assertNotEquals(copy.getSessionName(), original.getSessionName());
assertNotEquals(copy.getIdentityProvider(), original.getIdentityProvider());
assertNotEquals(copy.getSessionLocaleIds(), original.getSessionLocaleIds());
assertEquals(copy.getSessionTimeout(), uint(0));
assertEquals(copy.getRequestTimeout(), uint(0));
assertEquals(copy.getMaxResponseMessageSize(), uint(0));
assertEquals(copy.getMaxPendingPublishRequests(), uint(0));
assertEquals(copy.getKeepAliveFailuresAllowed(), uint(2));
assertEquals(copy.getKeepAliveInterval(), uint(10000));
assertEquals(copy.getKeepAliveTimeout(), uint(15000));
assertEquals(copy.getSessionLocaleIds(), new String[] { "en", "es" });
}
use of org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider in project JBM by numen06.
the class OpcUaTemplate method getOpcUaClient.
/**
* Get Opc Ua Client
*
* @param deviceId Device Id
* @param driverInfo Driver Info
* @return OpcUaClient
* @throws UaException UaException
*/
public OpcUaClient getOpcUaClient(String deviceId, OpcUaSource driverInfo) {
OpcUaClient opcUaClient = null;
if (clientMap.containsKey(deviceId))
return clientMap.get(deviceId).getOpcUaClient();
try {
KeyLoader loader = new KeyLoader().load(Paths.get(FileUtil.getTmpDirPath()));
if (null == opcUaClient) {
try {
List<EndpointDescription> remoteEndpoints = DiscoveryClient.getEndpoints(driverInfo.getUrl()).get();
EndpointDescription configPoint = EndpointUtil.updateUrl(remoteEndpoints.get(0), driverInfo.getHost(), driverInfo.getPort());
opcUaClient = OpcUaClient.create(driverInfo.getUrl(), endpoints -> remoteEndpoints.stream().findFirst(), configBuilder -> configBuilder.setIdentityProvider(new AnonymousProvider()).setCertificate(loader.getClientCertificate()).setKeepAliveInterval(uint(3000)).setRequestTimeout(uint(5000)).setEndpoint(configPoint).build());
// clientMap.put(deviceId, new OpcUaClientBean(deviceId, opcUaClient));
} catch (UaException e) {
log.error("get opc ua client error: {}", e.getMessage());
// clientMap.entrySet().removeIf(next -> next.getKey().equals(deviceId));
}
}
} catch (Exception e) {
log.error("get opc ua client error: {}", e.getMessage());
}
// return clientMap.get(deviceId).getOpcUaClient();
return opcUaClient;
}
use of org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider in project OpenMUC by isc-konstanz.
the class OpcConnection method connect.
@Connect
public void connect() throws ConnectionException {
try {
Path securityTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "security");
Files.createDirectories(securityTempDir);
if (!Files.exists(securityTempDir)) {
throw new ConnectionException("Unable to create security dir: " + securityTempDir);
}
logger.debug("Security temp dir: {}", securityTempDir.toAbsolutePath());
KeyStoreLoader loader = new KeyStoreLoader().load(securityTempDir);
if (!address.contains("opc.tcp://")) {
host = address.split(":")[0];
port = Integer.parseInt(address.split(":")[1]);
address = "opc.tcp://" + address;
} else {
host = address.split("//|:")[1];
port = Integer.parseInt(address.split("//|:")[2]);
}
List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(address).get();
EndpointDescription endpoint = endpoints.stream().filter(e -> true).findFirst().orElseThrow(() -> new UaException(StatusCodes.Bad_ConfigurationError, "No endpoint selected"));
logger.info("OPC Client connecting to {}.", address);
endpoint = EndpointUtil.updateUrl(endpoints.get(0), host, port);
OpcUaClientConfigBuilder clientBuilder = new OpcUaClientConfigBuilder().setEndpoint(endpoint).setApplicationName(LocalizedText.english("OpenMUC OPC UA Client")).setApplicationUri("urn:openmuc:client").setCertificate(loader.getClientCertificate()).setKeyPair(loader.getClientKeyPair()).setIdentityProvider(new AnonymousProvider()).setRequestTimeout(uint(5000));
client = OpcUaClient.create(clientBuilder.build());
client.connect().get();
// Get a typed reference to the Server object: ServerNode
ServerTypeNode serverNode = client.getAddressSpace().getObjectNode(Identifiers.Server, ServerTypeNode.class).get();
if (namespaceUri != null && !namespaceUri.isEmpty()) {
try {
namespaceIndex = Integer.parseInt(namespaceUri);
} catch (NumberFormatException e) {
namespaceIndex = Arrays.asList(serverNode.getNamespaceArray().get()).indexOf(namespaceUri);
}
}
} catch (Exception e) {
logger.error("OPC connection to server failed {}", e);
}
}
Aggregations