Skip to content

Instantly share code, notes, and snippets.

@kevinherron
Created October 5, 2023 19:12
Show Gist options
  • Save kevinherron/75ebdca52bbaf4fc433c0886c00208bc to your computer and use it in GitHub Desktop.
Save kevinherron/75ebdca52bbaf4fc433c0886c00208bc to your computer and use it in GitHub Desktop.
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.NodeIds;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection;
import org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask;
import org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass;
import org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription;
import org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult;
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;
class Scratch {
public static void main(String[] args) throws UaException {
var client = OpcUaClient.create("opc.tcp://localhost:4840");
client.connect();
BrowseResult result = client.browse(new BrowseDescription(
NodeIds.Server_ServerStatus,
BrowseDirection.Forward,
NodeIds.HasTypeDefinition,
false,
uint(NodeClass.ObjectType.getValue() | NodeClass.VariableType.getValue()),
uint(BrowseResultMask.All.getValue())
));
System.out.println("Browse returned " + result.getReferences().length + " references");
}
}
@impet14
Copy link

impet14 commented Oct 24, 2023

Now, with the contribution from freeopcua developer. he has suggested >>

BrowseResult result = client.browse(new BrowseDescription(
            Identifiers.Server_ServerStatus,
            BrowseDirection.Forward,
            Identifiers.HasComponent,
            false,
            uint(NodeClass.Object.getValue() | NodeClass.Variable.getValue()),
            uint(BrowseResultMask.All.getValue())
        )).get();

System.out.println("freeopcua Browse returned " + result.getReferences().length + " references\r\n" + result.toString());

result :
freeopc Browse returned 6 references
BrowseResult(statusCode=StatusCode{name=Good, value=0x00000000, quality=good}, continuationPoint=ByteString{bytes=null}, references=[ReferenceDescription(referenceTypeId=NodeId{ns=0, id=47}, isForward=true, nodeId=ExpandedNodeId{ns=0, id=2257, serverIndex=0}, browseName=QualifiedName{name=StartTime, namespaceIndex=0}, displayName=LocalizedText{text=StartTime, locale=null}, nodeClass=Variable, typeDefinition=ExpandedNodeId{ns=0, id=63, serverIndex=0}), ReferenceDescription(referenceTypeId=NodeId{ns=0, id=47}, isForward=true, nodeId=ExpandedNodeId{ns=0, id=2258, serverIndex=0}, browseName=QualifiedName{name=CurrentTime, namespaceIndex=0}, displayName=LocalizedText{text=CurrentTime, locale=null}, nodeClass=Variable, typeDefinition=ExpandedNodeId{ns=0, id=63, serverIndex=0}), ReferenceDescription(referenceTypeId=NodeId{ns=0, id=47}, isForward=true, nodeId=ExpandedNodeId{ns=0, id=2259, serverIndex=0}, browseName=QualifiedName{name=State, namespaceIndex=0}, displayName=LocalizedText{text=State, locale=null}, nodeClass=Variable, typeDefinition=ExpandedNodeId{ns=0, id=63, serverIndex=0}), ReferenceDescription(referenceTypeId=NodeId{ns=0, id=47}, isForward=true, nodeId=ExpandedNodeId{ns=0, id=2260, serverIndex=0}, browseName=QualifiedName{name=BuildInfo, namespaceIndex=0}, displayName=LocalizedText{text=BuildInfo, locale=null}, nodeClass=Variable, typeDefinition=ExpandedNodeId{ns=0, id=3051, serverIndex=0}), ReferenceDescription(referenceTypeId=NodeId{ns=0, id=47}, isForward=true, nodeId=ExpandedNodeId{ns=0, id=2992, serverIndex=0}, browseName=QualifiedName{name=SecondsTillShutdown, namespaceIndex=0}, displayName=LocalizedText{text=SecondsTillShutdown, locale=null}, nodeClass=Variable, typeDefinition=ExpandedNodeId{ns=0, id=63, serverIndex=0}), ReferenceDescription(referenceTypeId=NodeId{ns=0, id=47}, isForward=true, nodeId=ExpandedNodeId{ns=0, id=2993, serverIndex=0}, browseName=QualifiedName{name=ShutdownReason, namespaceIndex=0}, displayName=LocalizedText{text=ShutdownReason, locale=null}, nodeClass=Variable, typeDefinition=ExpandedNodeId{ns=0, id=63, serverIndex=0})])

now we can read the reference but got 6 references from freeopcua server. do you have any idea please?

@kevinherron
Copy link
Author

You changed the requested reference type from HasTypeDefinition to HasComponent. This is asking for a different set of references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment