事件订阅
OPC UA 事件 (Alarms & Conditions) 通过 s.SubscribeEvents(...) 订阅. 这是 OPC UA 协议事件, 与 SDK 内部 s.Events().* 不是一回事.
s.SubscribeEvents(...)— OPC UA 协议事件 (报警 / 条件, 服务端推送, 本节)s.Events().on_*— SDK 内部事件 (会话生命周期, 与协议无关)
签名
EventSubscription SubscribeEvents(
std::string const& node_id = "",
Subscription* existing = nullptr);
| 参数 | 默认 | 说明 |
|---|---|---|
| node_id | "" (Server, i=2253) | 事件源节点 |
| existing | nullptr | 复用已存在的 Subscription, nullptr 则自动新建一个 1000ms 容器 |
EventSubscription
EventSubscription 是 Move-only 栈对象, 析构时:
- 如果是自动新建的容器: 销毁容器 (
DarraUa_Subscription_Delete) - 如果是借用外部容器: 仅移除该 Event MI
| 成员 | 说明 |
|---|---|
| SetOnEvent(cb) | 设置事件回调 |
| on_event() = cb | 同 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; // 当前恒为空 (见下)
};
Event 订阅固定只选 4 个 SelectClause: Severity / Message / SourceName / Time
(opcua_event.h)。EventType 不在选句里, 解码侧直接置 NULL, 所以 event_type 恒为空串。
同理 EventArrivedEventArgs 里没有 EventId 字段, 报警确认要用的 EventId 也拿不到。
用法
默认订阅 Server 节点
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 个选句字段, event_type 恒空不打印
});
std::this_thread::sleep_for(std::chrono::minutes(5));
// ev_sub 离开作用域: 自动移除 Event MI + 销毁自动创建的容器
订阅指定节点的事件
auto ev_sub = s.SubscribeEvents("ns=2;s=Line1");
// Line1 必须是 EventNotifier
复用已有 Subscription 容器
auto sub = s.CreateSubscription(500.0);
sub.AddNode("ns=2;s=Counter", [](DataValue const& dv) { /* 数据订阅 */ });
// 把 Event MI 挂到同一个容器 (借用)
auto ev_sub = s.SubscribeEvents("ns=2;s=Line1", &sub);
ev_sub.SetOnEvent([](EventArrivedEventArgs const& e) { /* ... */ });
// ev_sub 析构: 只移除 Event MI, 不销毁 sub
EventNotifier
只有 EventNotifier Attribute (12) 非 0 的节点才能产生事件. 默认 Server (i=2253) 是 EventNotifier. 自定义节点是否有事件源能力, 看服务端实现.
EventFilter (高级)
SubscribeEvents 不接过滤器 —— 固定 4 个 SelectClause (Severity / Message / SourceName / Time),
whereClause 为空, 服务端所有事件都上报。
要自己决定抓哪些字段, 只有 C 便利层 C/include/opcua_filter.h 声明的
DarraUa_EventSubscription_SubscribeWithFilter: 传 DarraUa_EventFilter
(select_clauses 数组 + where_clause), filter 传 NULL 时退化为默认 4 字段过滤器。
where_clause (ContentFilter 表达式) 当前未实现, 传 NULL 即可。C++ 侧没有包装这个入口,
event_filter.hpp 里的 EventFilter / SimpleAttributeOperand 只是类型定义,
SubscribeEvents 也没接它们。
报警操作 (Acknowledge / Confirm)
收到 AlarmConditionType 事件后, 操作员通常要 Acknowledge (确认)。标准 Method 的 NodeId 是固定的
(Part 9), 不要自己拼 condition_id + ".Acknowledge" —— 直接用
alarm_condition.hpp 封装好的自由函数:
#include <darra/opcua/alarm_condition.hpp>
std::vector<uint8_t> event_id = {/* 事件的 EventId 字节 */};
std::string condition_id = "ns=2;s=Line1.Alarm1";
// Acknowledge (i=9111) / Confirm (i=9113) / AddComment (i=9029) 同族
Status st = darra::opcua::alarm::Acknowledge(s, condition_id, event_id, "Operator confirmed");
if (st != Status::Good) { /* 提示用户重试 */ }
入参 EventId 的类型是 ByteString。C++ Variant 没有 SetByteString,
alarm_condition.hpp 按 ISO-8859-1 编码保留全字节
(detail::BuildEventId)。
EventArrivedEventArgs 里没有 EventId 字段 (见上), 所以回调给不了 event_id。
要用 Acknowledge / Confirm, 得从服务端别处拿 EventId, 或改用 C 层
DarraUa_EventSubscription_SubscribeWithFilter 把 EventId 加进 SelectClauses。