open62541pp 0.19.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
.addVariable(
{1, 10001},
"Age",
.setDisplayName({"en-US", "Age"})
.setDescription({"en-US", "This mammals age in months"})
.setValue(opcua::Variant{0U}) // default age
)
.addModellingRule(opcua::ModellingRule::Mandatory); // create on new instance
auto nodeDogType = nodeMammalType.addObjectType(
{1, 10002},
"DogType",
.setDisplayName({"en-US", "DogType"})
.setDescription({"en-US", "A dog, subtype of mammal"})
);
nodeDogType
.addVariable(
{1, 10003},
"Name",
.setDisplayName({"en-US", "Name"})
.setDescription({"en-US", "This dogs name"})
.setValue(opcua::Variant{"unnamed dog"}) // default name
)
.addModellingRule(opcua::ModellingRule::Mandatory); // create on new instance
// Instantiate a dog named Bello:
// (Object) Objects
// └─ (Object) Bello <DogType>
// ├─ (Variable) Age
// └─ (Variable) Name
opcua::Node nodeObjects{server, opcua::ObjectId::ObjectsFolder};
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"}}).writeValue(opcua::Variant{3U});
nodeBello.browseChild({{1, "Name"}}).writeValue(opcua::Variant{"Bello"});
server.run();
}
High-level node class to access node attribute, browse and populate address space.
Definition node.hpp:45
High-level server class.
Definition server.hpp:142
void run()
Run the server's main loop. This method will block until Server::stop is called.
UA_Variant wrapper class.
Definition types.hpp:1048
UA_ObjectAttributes wrapper class.
Definition types.hpp:400
auto & setDisplayName(LocalizedText displayName)
Definition types.hpp:408
UA_ObjectTypeAttributes wrapper class.
Definition types.hpp:501
auto & setDisplayName(LocalizedText displayName)
Definition types.hpp:509
UA_VariableAttributes wrapper class.
Definition types.hpp:419
auto & setDisplayName(LocalizedText displayName)
Definition types.hpp:427