Skip to main content

Add MonitoredItem

Prerequisite Reading

sub.AddNode(...)​

DarraUa_MonitoredItemHandle AddNode(
std::string const& node_id,
OnDataChange cb,
AttributeId attr = AttributeId::Value,
double sampling_ms = -1.0);

// using OnDataChange = std::function<void(DataValue const&)>;
ParametersDefaultNote
node_id—Monitored NodeId
cb—Data Change Callbacks (lambda / std::function)
attrValue (13)Monitored Attributes
sampling_ms-1 (Subscribe)Server sampling interval. 0 = As soon as possible

Returns: DarraUa_MonitoredItemHandle (local handle, corresponding to the server MonitoredItemId; used for subsequent Modify / Remove operations)

Exception: Exception (Status codes such as Status::BadNodeIdUnknown, can be ex.status() retrieved using )

auto h  = sub.AddNode("ns=2;s=Temperature", [](DataValue const& dv) {
std::cout << "T = " << dv.Value().AsString() << "\n";
});

auto h2 = sub.AddNode("ns=2;s=Pressure", [](DataValue const& dv) {
std::cout << "P = " << dv.Value().AsString() << "\n";
}, AttributeId::Value, /*sampling_ms=*/100.0);

sub.AddMany(node_ids, cb, attr)​

Add RPC multiple nodes at once MI; if you use a loop with AddNode, each node is added one at a time MI RPC:

std::vector<std::pair<DarraUa_MonitoredItemHandle, Status>> AddMany(
std::vector<std::string> const& node_ids,
OnDataChange cb,
AttributeId attr = AttributeId::Value);

Returns one callback per item (handle, status)(when differentiated NodeId by, handle the branching within the callback itself).

std::vector<std::string> nodes;
for (int i = 1; i <= 100; ++i) nodes.push_back("ns=2;s=T" + std::to_string(i));

auto results = sub.AddMany(nodes, [](DataValue const& dv) {
std::cout << "value = " << dv.Value().AsString() << "\n";
});

for (size_t i = 0; i < nodes.size(); ++i) {
auto [h, st] = results[i];
if (st == Status::Good) {
std::cout << " " << nodes[i] << " -> handle=" << h << "\n";
} else {
std::cout << " " << nodes[i] << " FAILED: 0x"
<< std::hex << static_cast<uint32_t>(st) << "\n";
}
}

Performance: AddMany One-time RPC creation of N items MI (DarraUa_Subscription_AddNodes a single CreateMonitoredItems request); Iteration AddNode is performed once per node RPC (DarraUa_Subscription_AddNode created one by one). This is preferred when there are many nodes AddMany.

Data Callback Signature​

The callback type is std::function<void(DataValue const&)>. The in the callback is DataValueBorrow, Its lifecycle is limited to the callback, Don't save pointers:

sub.AddNode("ns=2;s=T1", [](DataValue const& dv) {
// OK: Process synchronously within the callback
if (!dv.IsGood()) return;
double t = 0; dv.Value().TryGetDouble(t);
LogToFile(t);

// Don't do this: The dv becomes invalid after the callback ends
// s_global_dv = &dv;
});

For more information, see Data Callback.

Remove​

sub.Remove(h);                              // Press the handle
sub.RemoveByNodeId("ns=2;s=Temperature"); // Press NodeId to remove all matching items

Monitoring Different Attributes​

attr Decide which attribute to monitor; the value is AttributeId (client.hpp). The default is AttributeId::Value (13), but you can also choose other attributes, such as AccessLevel (17) / DisplayName (4) / NodeClass (2):

Subscription::OnDataChange cb = [](DataValue const& dv) {
std::cout << "value = " << dv.Value().AsString() << "\n";
};
sub.AddNode("ns=2;s=T1", cb, AttributeId::Value);
sub.AddNode("ns=2;s=T1", cb, AttributeId::AccessLevel);

Note: AttributeId InsideNoneStatus / Quality Item — Status codes and timestamps are not subscribable properties; they are returned with DataValue each callback (dv.StatusCode() / dv.SourceTimestamp()).

DataChangeFilter (Dead Zone)​

Subscription::AddNode / AddMany Without the filter parameter, For structures built on the C++ side, the default behavior is always MI "push any changes." To implement Absolute / Percent dead zones, build them on the C layer MI: DarraUa_MonitoredItemConfig use the deadband_type / deadband_value fields (opcua_subscribe.h), and pass them to DarraUa_MonitoredItem_Create — this structure does not have a trigger field, so trigger conditions can currently only use default values.

monitoring_filter.hpp Inside DataChangeFilter / DataChangeTrigger / DeadbandType It's just a type definition; there's no connection Subscription::AddNode.

Next Step​