open62541pp 0.17.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
method/server_method.cpp
#include <algorithm>
int main() {
opcua::Server server;
opcua::Node objectsNode(server, opcua::ObjectId::ObjectsFolder);
// Add a method to return "Hello " + provided name.
objectsNode.addMethod(
{1, 1000},
"Greet",
const auto& name = input.at(0).scalar<opcua::String>();
const auto greeting = std::string("Hello ").append(name);
output.at(0) = greeting;
},
{{"name", {"en-US", "your name"}, opcua::DataTypeId::String, opcua::ValueRank::Scalar}},
{{"greeting", {"en-US", "greeting"}, opcua::DataTypeId::String, opcua::ValueRank::Scalar}}
);
// Add a method that takes an array of 5 integers and a scalar as input.
// It returns a copy of the array with every entry increased by the scalar.
objectsNode.addMethod(
{1, 1001},
"IncInt32ArrayValues",
const auto values = input.at(0).array<int32_t>();
const auto delta = input.at(0).scalar<int32_t>();
std::vector<int32_t> incremented(values.size());
std::transform(values.begin(), values.end(), incremented.begin(), [&](auto v) {
return v + delta;
});
output.at(0) = incremented;
},
{
"int32 array",
{"en-US", "int32[5] array"},
opcua::DataTypeId::Int32,
{5}
),
"int32 delta",
{"en-US", "int32 delta"},
opcua::DataTypeId::Int32,
),
},
{
"each entry is incremented by the delta",
{"en-US", "int32[5] array"},
opcua::DataTypeId::Int32,
{5}
),
}
);
server.run();
}
High-level node class to access node attribute, browse and populate address space.
Definition server.hpp:30
Node addMethod(const NodeId &id, std::string_view browseName, services::MethodCallback callback, Span< const Argument > inputArguments, Span< const Argument > outputArguments, const MethodAttributes &attributes={}, const NodeId &referenceType=ReferenceTypeId::HasComponent)
Add method.
Definition node.hpp:227
High-level server class.
Definition server.hpp:132
void run()
Run the server's main loop. This method will block until Server::stop is called.
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:28
constexpr reference at(size_t index) const
Access element by index with bounds checking.
Definition span.hpp:123
UA_String wrapper class.
Definition types.hpp:256
UA_Argument wrapper class.
Definition types.hpp:1553