Created
June 7, 2017 22:45
-
-
Save kevinherron/aba24ad544d6937a2e1a8fb376adf31c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BrowseExample implements ClientExample { | |
public static void main(String[] args) throws Exception { | |
BrowseExample example = new BrowseExample(); | |
new ClientExampleRunner(example).run(); | |
} | |
private final Logger logger = LoggerFactory.getLogger(getClass()); | |
@Override | |
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception { | |
// synchronous connect | |
client.connect().get(); | |
// start browsing at root folder | |
browseNode("", client, Identifiers.RootFolder); | |
future.complete(client); | |
} | |
private void browseNode(String indent, OpcUaClient client, NodeId browseRoot) { | |
BrowseDescription browse = new BrowseDescription( | |
browseRoot, | |
BrowseDirection.Forward, | |
Identifiers.References, | |
true, | |
uint(NodeClass.Object.getValue() | NodeClass.Variable.getValue()), | |
uint(BrowseResultMask.All.getValue()) | |
); | |
try { | |
BrowseResult browseResult = client.browse(browse).get(); | |
List<ReferenceDescription> references = toList(browseResult.getReferences()); | |
for (ReferenceDescription rd : references) { | |
if (rd.getBrowseName().getName().contains("Int16")) { | |
rd.getNodeId().local().ifPresent(nodeId -> { | |
logger.info("{} Node={}", indent, rd.getBrowseName().getName()); | |
VariableNode node = client.getAddressSpace().createVariableNode(nodeId); | |
try { | |
DataValue value = node.readValue().get(); | |
DataValue dataType = node.readDataType().get(); | |
logger.info("DataValue={}", value.getValue().getValue()); | |
logger.info("DataType={}", dataType.getValue().getValue()); | |
} catch (InterruptedException | ExecutionException e) { | |
e.printStackTrace(); | |
} | |
}); | |
} | |
// recursively browse to children | |
rd.getNodeId().local().ifPresent(nodeId -> browseNode(indent + " ", client, nodeId)); | |
} | |
} catch (InterruptedException | ExecutionException e) { | |
logger.error("Browsing nodeId={} failed: {}", browseRoot, e.getMessage(), e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment