open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
client_browse.cpp
#include <iomanip>
#include <iostream>
#include <string_view>
/// Get name of node class.
constexpr std::string_view getEnumName(opcua::NodeClass nodeClass) {
switch (nodeClass) {
return "Object";
return "Variable";
return "Method";
return "ObjectType";
return "VariableType";
return "ReferenceType";
return "DataType";
return "View";
default:
return "Unknown";
}
}
// Separate definition for recursion
void printNodeTree(opcua::Node<opcua::Client>& node, int indent);
// Browse and print the server's node tree recursively:
// - Objects (Object)
// - Server (Object)
// - Auditing (Variable)
// - ServiceLevel (Variable)
// - NamespaceArray (Variable)
// - ServerArray (Variable)
// - ServerRedundancy (Object)
// - RedundancySupport (Variable)
// - VendorServerInfo (Object)
// ...
void printNodeTree(opcua::Node<opcua::Client>& node, int indent) { // NOLINT
for (auto&& child : node.browseChildren()) {
std::cout << std::setw(indent) << "- " << child.readBrowseName().getName() << " ("
<< getEnumName(child.readNodeClass()) << ")\n";
printNodeTree(child, indent + 2);
}
}
int main() {
opcua::Client client;
client.connect("opc.tcp://localhost:4840");
// Browse all nodes recursively and print node tree to console
printNodeTree(nodeRoot, 0);
// Browse a child node by its relative path using browse names
auto nodeServer = nodeRoot.browseChild({{0, "Objects"}, {0, "Server"}});
// Browse the parent node
auto nodeServerParent = nodeServer.browseParent();
std::cout << nodeServer.readDisplayName().getText() << "'s parent node is "
<< nodeServerParent.readDisplayName().getText() << "\n";
}
High-level client class.
Definition client.hpp:121
void connect(std::string_view endpointUrl)
Connect to the selected server.
High-level node class to access node attribute, browse and populate address space.
Definition server.hpp:30
NodeClass
Node class.
Definition common.hpp:138