open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
server_datasource.cpp
#include <iostream>
int main() {
opcua::Server server;
// Counter variable as an example of a simple data source
int counter = 0;
// Add variable node
const auto counterId = opcua::services::addVariable(
server,
{1, 1000},
"Counter",
.setDataType<int>()
.setValueScalar(counter),
).value();
// Define data source with its read and write callbacks
dataSource.read = [&](opcua::DataValue& value, const opcua::NumericRange&, bool timestamp) {
// Increment counter before every read
counter++;
value.getValue().setScalarCopy(counter);
if (timestamp) {
}
std::cout << "Read counter from data source: " << counter << "\n";
};
dataSource.write = [&](const opcua::DataValue& value, const opcua::NumericRange&) {
counter = value.getValue().getScalarCopy<int>();
std::cout << "Write counter to data source: " << counter << "\n";
};
// Define data source as variable node backend
server.setVariableNodeValueBackend(counterId, dataSource);
server.run();
}
UA_DataValue wrapper class.
Definition types.hpp:1478
void setSourceTimestamp(DateTime sourceTimestamp) noexcept
Set source timestamp for the value.
Definition types.hpp:1607
Variant & getValue() &noexcept
Get value.
Definition types.hpp:1550
static DateTime now() noexcept
Get current DateTime.
Definition types.hpp:366
Numeric range to indicate subsets of (multidimensional) arrays.
Definition types.hpp:1893
High-level server class.
Definition server.hpp:132
void setVariableNodeValueBackend(const NodeId &id, ValueBackendDataSource backend)
Set data source backend for variable node.
void run()
Run the server's main loop. This method will block until Server::stop is called.
UA_VariableAttributes wrapper class.
auto & setAccessLevel(Bitmask< AccessLevel > accessLevel) noexcept
T getScalarCopy() const
Get copy of scalar value with given template type.
Definition types.hpp:1050
void setScalarCopy(const T &value)
Copy scalar value to variant.
Definition types.hpp:1108
#define UA_ACCESSLEVELMASK_READ
#define UA_ACCESSLEVELMASK_WRITE
Result< NodeId > addVariable(T &connection, const NodeId &parentId, const NodeId &id, std::string_view browseName, const VariableAttributes &attributes, const NodeId &variableType, const NodeId &referenceType) noexcept
Add variable.
#define UA_STATUSCODE_GOOD
Data source backend for variable nodes.
Definition nodestore.hpp:45
std::function< StatusCode(DataValue &value, const NumericRange &range, bool timestamp)> read
Callback to set the read value, the result status and optionally a source timestamp.
Definition nodestore.hpp:63
std::function< StatusCode(const DataValue &value, const NumericRange &range)> write
Callback to write the value into a data source.
Definition nodestore.hpp:74