open62541pp 0.15.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
server_instantiation.cpp
int main() {
opcua::Server server;
// Create object types `MammalType` and `DogType`:
// (ObjectType) BaseObjectType
// └─ (ObjectType) MammalType
// ├─ (Variable) Age
// └─ (ObjectType) DogType
// └─ (Variable) Name
opcua::Node nodeBaseObjectType(server, opcua::ObjectTypeId::BaseObjectType);
auto nodeMammalType = nodeBaseObjectType.addObjectType(
{1, 10000},
"MammalType",
.setDisplayName({"en-US", "MammalType"})
.setDescription({"en-US", "A mammal"})
);
nodeMammalType
{1, 10001},
"Age",
.setDisplayName({"en-US", "Age"})
.setDescription({"en-US", "This mammals age in months"})
.setValueScalar(0U) // default age
)
auto nodeDogType = nodeMammalType.addObjectType(
{1, 10002},
"DogType",
.setDisplayName({"en-US", "DogType"})
.setDescription({"en-US", "A dog, subtype of mammal"})
);
nodeDogType
{1, 10003},
"Name",
.setDisplayName({"en-US", "Name"})
.setDescription({"en-US", "This dogs name"})
.setValueScalar("unnamed dog") // default name
)
// Instantiate a dog named Bello:
// (Object) Objects
// └─ (Object) Bello <DogType>
// ├─ (Variable) Age
// └─ (Variable) Name
auto nodeBello = nodeObjects.addObject(
{1, 20000},
"Bello",
.setDisplayName({"en-US", "Bello"})
.setDescription({"en-US", "A dog named Bello"}),
nodeDogType.id()
);
// Set variables Age and Name
nodeBello.browseChild({{1, "Age"}}).writeValueScalar(3U);
nodeBello.browseChild({{1, "Name"}}).writeValueScalar("Bello");
server.run();
}
High-level node class to access node attribute, browse and populate address space.
Definition server.hpp:27
Node & addModellingRule(ModellingRule rule)
Add modelling rule.
Definition node.hpp:222
Node addObjectType(const NodeId &id, std::string_view browseName, const ObjectTypeAttributes &attributes={}, const NodeId &referenceType=ReferenceTypeId::HasSubtype)
Add object type.
Definition node.hpp:150
Node addObject(const NodeId &id, std::string_view browseName, const ObjectAttributes &attributes={}, const NodeId &objectType=ObjectTypeId::BaseObjectType, const NodeId &referenceType=ReferenceTypeId::HasComponent)
Add object.
Definition node.hpp:88
Node addVariable(const NodeId &id, std::string_view browseName, const VariableAttributes &attributes={}, const NodeId &variableType=VariableTypeId::BaseDataVariableType, const NodeId &referenceType=ReferenceTypeId::HasComponent)
Add variable.
Definition node.hpp:102
Node browseChild(Span< const QualifiedName > path)
Browse child node specified by its relative path from this node (only local nodes).
Definition node.hpp:301
UA_ObjectAttributes wrapper class.
auto & setDisplayName(const LocalizedText &displayName)
UA_ObjectTypeAttributes wrapper class.
auto & setDisplayName(const LocalizedText &displayName)
High-level server class.
Definition server.hpp:63
void run()
Run the server's main loop. This method will block until Server::stop is called.
UA_VariableAttributes wrapper class.
auto & setDisplayName(const LocalizedText &displayName)