open62541pp 0.17.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
server_datasource.cpp
#include <iostream>
/// Templated data source that stores the data of type `T` internally.
template <typename T>
struct DataSource : public opcua::DataSourceBase {
[[maybe_unused]] opcua::Session& session,
[[maybe_unused]] const opcua::NodeId& id,
[[maybe_unused]] const opcua::NumericRange* range,
bool timestamp
) override {
std::cout << "Read value from data source: " << data << "\n";
if (timestamp) {
}
}
[[maybe_unused]] opcua::Session& session,
[[maybe_unused]] const opcua::NodeId& id,
[[maybe_unused]] const opcua::NumericRange* range,
const opcua::DataValue& dv
) override {
data = dv.value().to<T>();
std::cout << "Write value to data source: " << data << "\n";
}
T data{};
};
int main() {
opcua::Server server;
// Add variable node
const auto id =
server,
opcua::ObjectId::ObjectsFolder,
{1, 1000},
"DataSource",
.setAccessLevel(UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE)
.setDataType<int>(),
opcua::VariableTypeId::BaseDataVariableType,
opcua::ReferenceTypeId::HasComponent
).value();
// Define data source
DataSource<int> dataSource;
server.setVariableNodeDataSource(id, dataSource);
server.run();
}
Data source base class for variable nodes.
Definition nodestore.hpp:60
virtual StatusCode read(Session &session, const NodeId &id, const NumericRange *range, DataValue &value, bool timestamp)=0
Callback to set the read value, the result status and optionally a source timestamp.
virtual StatusCode write(Session &session, const NodeId &id, const NumericRange *range, const DataValue &value)=0
Callback to write the value into a data source.
UA_DataValue wrapper class.
Definition types.hpp:1832
void setSourceTimestamp(DateTime sourceTimestamp) noexcept
Set source timestamp for the value.
Definition types.hpp:1897
Variant & value() &noexcept
Get value.
Definition types.hpp:1951
void setValue(const Variant &value)
Set value (copy).
Definition types.hpp:1885
static DateTime now() noexcept
Get current DateTime.
Definition types.hpp:402
UA_NodeId wrapper class.
Definition types.hpp:666
UA_NumericRange wrapper class.
Definition types.hpp:2442
High-level server class.
Definition server.hpp:132
void setVariableNodeDataSource(const NodeId &id, DataSourceBase &source)
Set data source for variable node.
void run()
Run the server's main loop. This method will block until Server::stop is called.
High-level session class to manage client sessions.
Definition session.hpp:20
UA_StatusCode wrapper class.
Definition types.hpp:46
UA_Variant wrapper class.
Definition types.hpp:1176
T to() const
Converts the variant to the specified type T with automatic conversion if required.
Definition types.hpp:1642
UA_VariableAttributes wrapper class.
Definition types.hpp:437
auto & setAccessLevel(Bitmask< AccessLevel > accessLevel) noexcept
Definition types.hpp:479
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