|
| 1 | +package org.connectorio.addons.binding.opcua.internal; |
| 2 | + |
| 3 | +import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint; |
| 4 | +import static org.eclipse.milo.opcua.stack.core.util.ConversionUtil.toList; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.concurrent.CompletableFuture; |
| 9 | +import java.util.concurrent.ExecutionException; |
| 10 | +import org.eclipse.milo.opcua.sdk.client.OpcUaClient; |
| 11 | +import org.eclipse.milo.opcua.sdk.client.api.UaClient; |
| 12 | +import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig; |
| 13 | +import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder; |
| 14 | +import org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider; |
| 15 | +import org.eclipse.milo.opcua.sdk.client.api.identity.SignedIdentityToken; |
| 16 | +import org.eclipse.milo.opcua.sdk.client.nodes.UaNode; |
| 17 | +import org.eclipse.milo.opcua.stack.client.DiscoveryClient; |
| 18 | +import org.eclipse.milo.opcua.stack.client.UaStackClient; |
| 19 | +import org.eclipse.milo.opcua.stack.client.UaStackClientConfigBuilder; |
| 20 | +import org.eclipse.milo.opcua.stack.core.Identifiers; |
| 21 | +import org.eclipse.milo.opcua.stack.core.NamespaceTable; |
| 22 | +import org.eclipse.milo.opcua.stack.core.UaException; |
| 23 | +import org.eclipse.milo.opcua.stack.core.types.builtin.ByteString; |
| 24 | +import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText; |
| 25 | +import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId; |
| 26 | +import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger; |
| 27 | +import org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection; |
| 28 | +import org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask; |
| 29 | +import org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass; |
| 30 | +import org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription; |
| 31 | +import org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult; |
| 32 | +import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription; |
| 33 | +import org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +public class Main { |
| 38 | + |
| 39 | + private static Logger logger = LoggerFactory.getLogger(Main.class); |
| 40 | + |
| 41 | + public static void main(String[] args) throws Exception { |
| 42 | + OpcUaClientConfigBuilder cfg = new OpcUaClientConfigBuilder(); |
| 43 | + |
| 44 | + List<EndpointDescription> descriptions = DiscoveryClient.getEndpoints( |
| 45 | + "opc.tcp://localhost:4840").get(); |
| 46 | + |
| 47 | + for (EndpointDescription description : descriptions) { |
| 48 | + System.out.println(description.getEndpointUrl() + " " + Arrays.toString(description.getUserIdentityTokens())); |
| 49 | + } |
| 50 | + |
| 51 | + OpcUaClientConfig config = OpcUaClientConfig.builder() |
| 52 | + .setApplicationName(LocalizedText.english("eclipse milo opc-ua client of the apache PLC4X:PLC4J project")) |
| 53 | + .setApplicationUri("urn:eclipse:milo:plc4x:client") |
| 54 | + .setEndpoint(descriptions.get(0)) |
| 55 | +// .setIdentityProvider(new IdentityProvider() { |
| 56 | +// @Override |
| 57 | +// public SignedIdentityToken getIdentityToken(EndpointDescription endpointDescription, ByteString byteString) throws Exception { |
| 58 | +// return null; |
| 59 | +// } |
| 60 | +// }) |
| 61 | + .setRequestTimeout(UInteger.valueOf(100)) |
| 62 | + .build(); |
| 63 | + |
| 64 | + OpcUaClient client = OpcUaClient.create(config); |
| 65 | + UaClient uaClient = client.connect().get(); |
| 66 | + |
| 67 | + // start browsing at root folder |
| 68 | + //browseNode("", client, Identifiers.RootFolder); |
| 69 | + browseNode2("", client, Identifiers.ObjectsFolder); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + private static void browseNode(String indent, OpcUaClient client, NodeId browseRoot) { |
| 75 | + BrowseDescription browse = new BrowseDescription( |
| 76 | + browseRoot, |
| 77 | + BrowseDirection.Forward, |
| 78 | + Identifiers.References, |
| 79 | + true, |
| 80 | + uint(NodeClass.Object.getValue() | NodeClass.Variable.getValue() | NodeClass.View.getValue()), |
| 81 | + uint(BrowseResultMask.All.getValue()) |
| 82 | + ); |
| 83 | + |
| 84 | + try { |
| 85 | + BrowseResult browseResult = client.browse(browse).get(); |
| 86 | + |
| 87 | + List<ReferenceDescription> references = toList(browseResult.getReferences()); |
| 88 | + |
| 89 | + for (ReferenceDescription rd : references) { |
| 90 | + System.out.println(indent + " Node=" + rd.getBrowseName().getName() + " " + rd.getDisplayName().getText()); |
| 91 | + |
| 92 | + // recursively browse to children |
| 93 | + rd.getNodeId().toNodeId(client.getNamespaceTable()) |
| 94 | + .ifPresent(nodeId -> browseNode(indent + " ", client, nodeId)); |
| 95 | + } |
| 96 | + } catch (InterruptedException | ExecutionException e) { |
| 97 | + logger.error("Browsing nodeId={} failed: {}", browseRoot, e.getMessage(), e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void browseNode2(String indent, OpcUaClient client, NodeId browseRoot) { |
| 102 | + try { |
| 103 | + List<? extends UaNode> nodes = client.getAddressSpace().browseNodes(browseRoot); |
| 104 | + |
| 105 | + for (UaNode node : nodes) { |
| 106 | + System.out.println(indent + " Node=" + node.getBrowseName().getName() + " " + node.getDisplayName().getText() + " " + node.getNodeId()); |
| 107 | + |
| 108 | + // recursively browse to children |
| 109 | + browseNode2(indent + " ", client, node.getNodeId()); |
| 110 | + } |
| 111 | + } catch (UaException e) { |
| 112 | + logger.error("Browsing nodeId={} failed: {}", browseRoot, e.getMessage(), e); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments