info.unterrainer.commons.opcuabrowser.tests.OpcUaBrowser Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2019 the Eclipse Milo Authors
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package info.unterrainer.commons.opcuabrowser.tests;
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ubyte;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.eclipse.milo.opcua.sdk.client.AddressSpace;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder;
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
import org.eclipse.milo.opcua.stack.core.Identifiers;
import org.eclipse.milo.opcua.stack.core.Stack;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
import org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class OpcUaBrowser extends Thread {
public static final String HOST = "192.168.251.16";
public static final String PORT = "4980";
public static final String PATH = "/Honeywell-OPCUA";
private OpcUaClient client;
public static void main(final String[] args) throws Exception {
OpcUaBrowser instance = new OpcUaBrowser();
instance.start();
}
public KeyStoreLoader establishSecurityContext() {
KeyStoreLoader loader = null;
try {
Path securityTempDir = Paths.get("security");
System.out.println(securityTempDir.toAbsolutePath());
Files.createDirectories(securityTempDir);
if (!Files.exists(securityTempDir))
throw new Exception("unable to create security dir: " + securityTempDir);
log.info("security temp dir: {}", securityTempDir.toAbsolutePath());
loader = new KeyStoreLoader().load(securityTempDir);
} catch (Exception e) {
e.printStackTrace();
}
return loader;
}
public OpcUaClient createClientSync(final String host, final String port, final String path)
throws InterruptedException, ExecutionException, UaException {
OpcUaClientConfigBuilder cfg = new OpcUaClientConfigBuilder();
KeyStoreLoader loader = establishSecurityContext();
EndpointDescription endpoint = new EndpointDescription(String.format("opc.tcp://%s:%s%s", HOST, PORT, PATH),
null, null, MessageSecurityMode.None, SecurityPolicy.None.getUri(), null,
Stack.TCP_UASC_UABINARY_TRANSPORT_URI, ubyte(0));
cfg.setEndpoint(endpoint)
.setApplicationName(LocalizedText.english("eclipse milo opc-ua client"))
.setApplicationUri("urn:eclipse:milo:examples:client")
.setCertificate(loader.getClientCertificate())
.setKeyPair(loader.getClientKeyPair())
.setEndpoint(endpoint)
.setIdentityProvider(new AnonymousProvider())
.setRequestTimeout(Unsigned.uint(5000))
.build();
return OpcUaClient.create(cfg.build());
}
public OpcUaClient connectSync(final String host, final String port, final String path)
throws InterruptedException, ExecutionException, UaException {
final OpcUaClient client = createClientSync(host, port, path);
client.connect().get();
return client;
}
public AddressSpace getAddressSpace() {
return client.getAddressSpace();
}
@Override
public void run() {
try {
client = connectSync(HOST, PORT, PATH);
// start browsing at root folder
browseNode("", client, Identifiers.RootFolder);
} catch (Exception e) {
e.printStackTrace();
}
}
private void browseNode(final String indent, final OpcUaClient client, final NodeId browseRoot) throws Exception {
try {
List refs = client.getAddressSpace().browse(browseRoot);
for (ReferenceDescription ref : refs) {
System.out.println(String.format("{%s} Node={%s}", indent, ref.getBrowseName().getName()));
// recursively browse to children
browseNode(indent + " ", client, ref.getNodeId().toNodeIdOrThrow(client.getNamespaceTable()));
}
} catch (InterruptedException | ExecutionException e) {
System.out.println(String.format("Browsing nodeId={%s} failed: {%s}", browseRoot, e.getMessage()));
}
}
}