open62541pp 0.19.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
server_valuecallback.cpp
#include <iostream>
// Variable value callback to write current time before every read operation
class CurrentTimeCallback : public opcua::ValueCallbackBase {
void onRead(
opcua::Session& session,
const opcua::NodeId& id,
[[maybe_unused]] const opcua::NumericRange* range,
const opcua::DataValue& value
) override {
opcua::Node node{session.connection(), id};
const auto timeOld = value.value().scalar<opcua::DateTime>();
const auto timeNow = opcua::DateTime::now();
std::cout << "Time before read: " << timeOld.format("%Y-%m-%d %H:%M:%S") << std::endl;
std::cout << "Set current time: " << timeNow.format("%Y-%m-%d %H:%M:%S") << std::endl;
node.writeValue(opcua::Variant{timeNow});
}
void onWrite(
[[maybe_unused]] opcua::Session& session,
[[maybe_unused]] const opcua::NodeId& id,
[[maybe_unused]] const opcua::NumericRange* range,
[[maybe_unused]] const opcua::DataValue& value
) override {}
};
int main() {
opcua::Server server;
const opcua::NodeId currentTimeId{1, "CurrentTime"};
opcua::Node{server, opcua::ObjectId::ObjectsFolder}
.addVariable(currentTimeId, "CurrentTime")
.writeDisplayName({"en-US", "Current time"})
.writeDescription({"en-US", "Current time"})
.writeDataType<opcua::DateTime>()
CurrentTimeCallback currentTimeCallback;
server.setVariableNodeValueCallback(currentTimeId, currentTimeCallback);
server.run();
}
UA_DataValue wrapper class.
Definition types.hpp:1568
Variant & value() &noexcept
Get value.
Definition types.hpp:1668
UA_DateTime wrapper class.
Definition types.hpp:381
String format(std::string_view format, bool localtime=false) const
Convert to string with given format (same format codes as strftime).
static DateTime now() noexcept
Get current DateTime.
Definition types.hpp:393
UA_NodeId wrapper class.
Definition types.hpp:641
High-level node class to access node attribute, browse and populate address space.
Definition node.hpp:45
UA_NumericRange wrapper class.
Definition types.hpp:1997
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.
void setVariableNodeValueCallback(const NodeId &id, ValueCallbackBase &callback)
Set value callback for variable node.
High-level session class to manage client sessions.
Definition session.hpp:20
Server & connection() noexcept
Get the server instance.
Definition session.hpp:28
Value callback base class for variable nodes.
Definition nodestore.hpp:18
virtual void onRead(Session &session, const NodeId &id, const NumericRange *range, const DataValue &value)=0
Called before the value attribute is read.
virtual void onWrite(Session &session, const NodeId &id, const NumericRange *range, const DataValue &value)=0
Called after writing the value attribute.
UA_Variant wrapper class.
Definition types.hpp:1048
T & scalar() &
Get scalar value with given template type (only native or wrapper types).
Definition types.hpp:1311
StatusCode writeDescription(T &connection, const NodeId &id, const LocalizedText &description) noexcept
Write the AttributeId::Description attribute of a node.