open62541pp 0.15.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
method/server_method.cpp
#include <algorithm>
int main() {
opcua::Server server;
// Add a method to return "Hello " + provided name.
objectsNode.addMethod(
{1, 1000},
"Greet",
const auto& name = input[0].getScalar<opcua::String>();
const auto greeting = std::string("Hello ").append(name);
output[0].setScalarCopy(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",
auto array = input[0].getArrayCopy<int32_t>();
const auto delta = input[1].getScalarCopy<int32_t>();
std::for_each(array.begin(), array.end(), [&](auto& v) { v += delta; });
output[0].setArrayCopy(array);
},
{
"int32 array",
{"en-US", "int32[5] array"},
{5}
),
"int32 delta",
{"en-US", "int32 delta"},
),
},
{
"each entry is incremented by the delta",
{"en-US", "int32[5] array"},
{5}
),
}
);
server.run();
}
UA_Argument wrapper class.
High-level node class to access node attribute, browse and populate address space.
Definition server.hpp:27
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:125
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.
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:26
UA_String wrapper class.
Definition types.hpp:251