Skip to main content

Event Subscription

OPC UA events (Alarms & Conditions) are subscribed to through s.SubscribeEvents(...). These are OPC UA protocol events and are not the same thing as the SDK's internal s.Events().*.

Difference
  • s.SubscribeEvents(...) — OPC UA Protocol Events (Alarms / Conditions, Server-Side Push Notifications, This Section)
  • s.Events().on_* — SDK Internal Events (Session Lifecycle, Protocol-Independent)

Signature​

EventSubscription SubscribeEvents(
std::string const& node_id = "",
Subscription* existing = nullptr);
ParametersDefaultNote
node_id"" (Server, i=2253)Event Source Node
existingnullptrReuse an existing subscription; if nullptr, automatically create a new 1000ms container.

EventSubscription​

EventSubscription This is a "move-only" stack object. Upon destruction:

  • If the container was created automatically: destroyContainer(DarraUa_Subscription_Delete)
  • If using an external container: Remove only that event MI
MembersNote
SetOnEvent(cb)Set Up Event Callbacks
on_event() = cbSyntactic sugar similar to SetOnEvent

EventArrivedEventArgs​

struct EventArrivedEventArgs {
uint16_t severity = 0; // 1-1000 (OPC UA Part 9 §5.4)
std::string message;
std::string source_name;
int64_t time = 0; // FileTime (UTC 100ns since 1601)
std::string event_type; // Currently set to null (see below)
};
event_type cannot be retrieved at this time

Event SubscriptionSelect exactly 4 SelectClause: Severity / Message / SourceName / Time (opcua_event.h).EventType Since it is not included in the selected sentences, the decoding side directly sets it to NULL, so event_type is always an empty string. Similarly, in EventArrivedEventArgsNo fields EventId, and I can't EventId even get the one I need to confirm the alarm.

Usage​

Default Subscription Server Node​

auto ev_sub = s.SubscribeEvents();
ev_sub.SetOnEvent([](EventArrivedEventArgs const& e) {
std::cout << "[" << e.severity << "] " << e.source_name << ": " << e.message
<< " ft=" << e.time << "\n"; // 4 sentence selection fields; event_type Hengkong does not print them
});

std::this_thread::sleep_for(std::chrono::minutes(5));
// ev_sub Out of scope: Automatically remove the event MI and destroy the automatically created container

Subscribe to events from a specific node​

auto ev_sub = s.SubscribeEvents("ns=2;s=Line1");
// Line1 It must be EventNotifier

Reusing an Existing Subscription Container​

auto sub = s.CreateSubscription(500.0);
sub.AddNode("ns=2;s=Counter", [](DataValue const& dv) { /* Data Subscriptions */ });

// MI Attach the Event to the same container (borrow)
auto ev_sub = s.SubscribeEvents("ns=2;s=Line1", &sub);
ev_sub.SetOnEvent([](EventArrivedEventArgs const& e) { /* ... */ });

// ev_sub Destructor: Only removes the Event MI; does not destroy the sub

EventNotifier​

Only nodes whose EventNotifier attribute (12) is not 0 can generate events. By default, Server(i=2253) acts as an EventNotifier. Whether a custom node can act as an event source depends on the server implementation.

EventFilter (Advanced)​

SubscribeEvents Do not connect a filter — Fixed at 4 SelectClause (Severity / Message / SourceName / Time), whereClause If left blank, all server events are reported.

To decide which fields to select, use only those declared C/include/opcua_filter.h by the C convenience layer: DarraUa_EventSubscription_SubscribeWithFilter Pass DarraUa_EventFilter (an array of select_clauses + where_clause); passing filter NULL falls back to the default 4-field filter. where_clause (ContentFilter expression) Currently not implemented; pass NULL. The C++ side does not wrap this entry point; event_filter.hpp the EventFilter / SimpleAttributeOperand inside are merely type definitions, SubscribeEvents and they are not connected to anything.

Alarm Operations (Acknowledge / Confirm)​

Upon receiving an AlarmConditionType event, the operator is typically required to acknowledge it. The standard method is NodeId fixed (Part 9), Don't try to put it together yourself condition_id + ".Acknowledge" — Use alarm_condition.hpp the pre-wrapped free function directly:

#include <darra/opcua/alarm_condition.hpp>

std::vector<uint8_t> event_id = {/* Bytes EventId of the event */};
std::string condition_id = "ns=2;s=Line1.Alarm1";

// Acknowledge (i=9111) / Confirm (i=9113) / AddComment (i=9029) Same family
Status st = darra::opcua::alarm::Acknowledge(s, condition_id, event_id, "Operator confirmed");
if (st != Status::Good) { /* Prompt the user to try again */ }

The type of EventId the input parameter is ByteString. C++ Variant None SetByteString, alarm_condition.hpp Encoded as ISO-8859-1 to preserve full bytes (detail::BuildEventId).

EventId Cannot retrieve at this time.

EventArrivedEventArgs There is no EventId field (see above), so the callback cannot be provided event_id EventId. To use Acknowledge / Confirm, you must retrieve it from elsewhere on the server or use the C layer DarraUa_EventSubscription_SubscribeWithFilter to EventId add to it SelectClauses.

Next Step​