Data Callback
Subscription::AddNode / AddMany Accept std::function<void(DataValue const&)> callbacks, which are triggered by server-side push notifications when data MI changes.
using OnDataChange = std::function<void(DataValue const&)>;
- See the Unified Channel for Global Session Events
s.Events().*. - For OPC UA alarms / conditional events (protocol events), see Event Subscription.
DataValue Field Access
In the callback, is DataValueBorrow, The lifecycle is limited to the callback. Direct access to fields:
| Accessor | Type | Note |
|---|---|---|
| dv.Value() | Variant | Actual Value (Borrowed) |
| dv.StatusCode() | uint32_t | Original 32-bit status code |
| dv.StatusEnum() | Status | Strongly Typed Enumerations |
| dv.IsGood() | bool | Equivalent StatusCode() == 0 |
| dv.SourceTimestamp() | int64_t | FileTime |
| dv.ServerTimestamp() | int64_t | FileTime |
Examples
Simple Log
sub.AddNode("ns=2;s=T1", [](DataValue const& dv) {
std::cout << "ft=" << dv.SourceTimestamp()
<< " val=" << dv.Value().AsString()
<< " status=0x" << std::hex << dv.StatusCode() << "\n";
});
Write a document
std::mutex write_lock;
sub.AddNode("ns=2;s=T1", [&](DataValue const& dv) {
std::lock_guard<std::mutex> lock(write_lock);
std::ofstream out("data.csv", std::ios::app);
out << dv.SourceTimestamp() << ","
<< dv.Value().AsString() << "\n";
});
UI Binding (Qt)
sub.AddNode("ns=2;s=T1", [&](DataValue const& dv) {
// Switch Back to Thread UI (Qt)
QMetaObject::invokeMethod(qApp, [&, val = dv.Value().AsString()]() {
labelTemp->setText(QString::fromStdString(val));
});
});
Otherwise, cross-thread UI operations will cause a crash.
Thread Model
- AutoPublish Pattern (
Session::StartAutoPublish(),ConnectionConfig::auto_publish_on_connectEnabled by default): Shared across the entire sessionOne Background Publish thread, The data callback runs on this thread.Synchronous Serial Call — Callbacks MI for all subscriptions and all items within the same session They share this thread and do not run concurrently with each other. - Manual Mode (Do not open AutoPublish): The callback is called when
Session::Publish()/Subscription::Pump()is called Executed synchronously on that thread - In the callbackDon't hold long positions. —— Callbacks are called directly within the Publish loop; if one takes too long, it will block the rest of this session. Distribution of all notifications; offloading work to one's own worker threads or queues
- An exception thrown in the callback is SDK swallowed, It does not spread, nor will it affect the distribution of subsequent notifications
- In the callback, you UI must manually switch back to the thread UI — operations UI involving this must be marshaled to the thread UI by the caller.
DataValue Loan Notice
DataValue Expires after the callback ends, Do not store pointers or references:
DataValue* g_last = nullptr; // ❌ Don't do that
sub.AddNode("ns=2;s=T1", [&](DataValue const& dv) {
g_last = const_cast<DataValue*>(&dv); // ❌ use-after-free
});
If you need to pass data to a callback, Synchronous Value Retrieval:
struct Snapshot { int64_t ft; double val; uint32_t status; };
std::atomic<Snapshot> latest;
sub.AddNode("ns=2;s=T1", [&](DataValue const& dv) {
Snapshot s;
s.ft = dv.SourceTimestamp();
s.status = dv.StatusCode();
dv.Value().TryGetDouble(s.val);
latest.store(s);
});
Relationship with s.Events()
s.Events().* The route isSession-level Channel, cannot obtain data point values — only on_any
OpcUaEventCategory::StateChanged and are automatically SDK assigned
OpcUaEventCategory::KeepAlive (client.hpp OnStateChange / StartKeepAlive). Data point valuesOnly Submit via the / AddNode callback in
AddMany this section.
// Data Point Value: This is the only way
sub.AddNode("ns=2;s=T1", [](DataValue const& dv) { /* ... */ });
// Session-level: State / Keep-alive, excluding data values
s.Events().on_any = [](OpcUaEventEntry const& e) {
if (e.category == OpcUaEventCategory::StateChanged) { /* ... */ }
};
OpcUaEventCategory::DataChange This enumeration value isLeave it to the higher-ups to post themselves Tags will SDK not be automatically pushed.
To attach data points to the unified event stream, the business side must call (AddNode) itself in the callback PushEvent(OpcUaEventCategory::DataChange, ...) above,
events.hpp and then use
EventBus::Instance().Subscribe(...) to retrieve them.