open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
server_valuecallback.cpp
#include <iostream>
int main() {
opcua::Server server;
const opcua::NodeId currentTimeId(1, "CurrentTime");
auto currentTimeNode =
.addVariable(currentTimeId, "CurrentTime")
.writeDisplayName({"en-US", "Current time"})
.writeDescription({"en-US", "Current time"})
.writeDataType<opcua::DateTime>()
.writeValueScalar(opcua::DateTime::now());
// set variable value callback to write current time before every read operation
opcua::ValueCallback valueCallback;
valueCallback.onBeforeRead = [&](const opcua::DataValue& value) {
const auto timeOld = value.getValue().getScalar<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;
currentTimeNode.writeValueScalar(timeNow);
};
server.setVariableNodeValueCallback(currentTimeId, valueCallback);
server.run();
}
UA_DataValue wrapper class.
Definition types.hpp:1478
UA_DateTime wrapper class.
Definition types.hpp:354
static DateTime now() noexcept
Get current DateTime.
Definition types.hpp:366
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:590
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, ValueCallback callback)
Set value callbacks to execute before every read and after every write operation.
Value callbacks for variable nodes.
Definition nodestore.hpp:16
std::function< void(const DataValue &value)> onBeforeRead
Called before the value attribute is read.
Definition nodestore.hpp:26