open62541pp 0.17.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.writeValueScalar(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>()
.writeValueScalar(opcua::DateTime::now());
CurrentTimeCallback currentTimeCallback;
server.setVariableNodeValueCallback(currentTimeId, currentTimeCallback);
server.run();
}
UA_DataValue wrapper class.
Definition types.hpp:1832
Variant & value() &noexcept
Get value.
Definition types.hpp:1951
UA_DateTime wrapper class.
Definition types.hpp:390
static DateTime now() noexcept
Get current DateTime.
Definition types.hpp:402
std::string format(std::string_view format, bool localtime=false) const
Convert to string with given format (same format codes as strftime).
UA_NodeId wrapper class.
Definition types.hpp:666
High-level node class to access node attribute, browse and populate address space.
Definition server.hpp:30
Node & writeValueScalar(const T &value)
Write scalar to variable node.
Definition node.hpp:1168
Node & writeDisplayName(const LocalizedText &displayName)
Write the AttributeId::DisplayName attribute of a node.
Definition node.hpp:997
Node addVariable(const NodeId &id, std::string_view browseName, const VariableAttributes &attributes={}, const NodeId &variableType=VariableTypeId::BaseDataVariableType, const NodeId &referenceType=ReferenceTypeId::HasComponent)
Add variable.
Definition node.hpp:156
UA_NumericRange wrapper class.
Definition types.hpp:2442
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.
void setVariableNodeValueCallback(const NodeId &id, ValueCallbackBase &callback)
Set value callbacks to execute before every read and after every write operation.
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.
T & scalar() &
Get reference to scalar value with given template type (only native or wrapper types).
Definition types.hpp:1528
StatusCode writeDescription(T &connection, const NodeId &id, const LocalizedText &description) noexcept
Write the AttributeId::Description attribute of a node.