EventFilter
概述
OPC UA Part 4 §7.18 EventFilter:
- select_clauses — 抓哪些字段
- where_clause — 过滤条件(当前未实现, 留空即可)
SimpleAttributeOperand 字段: type_definition_id / browse_path / attribute_id / index_range。
Session::SubscribeEvents(std::string const& node_id = "", Subscription* existing = nullptr)没有 EventFilter 参数,抓什么字段是写死的 4 个 SelectClause:Severity / Message / SourceName / Time。
所以 C++ 侧目前改不了抓哪些字段,也设不了 WhereClause。要自定义过滤得走 C 便利层 DarraUa_EventSubscription_SubscribeWithFilter(sub, object_node_id, filter, cb, ctx, &out_mi),传 NULL filter 就退回这 4 个默认字段。
API
namespace darra::opcua {
/// SimpleAttributeOperand (Part 4 §7.4.4.5)
struct SimpleAttributeOperand {
std::string type_definition_id; // 事件类型 NodeId, 如 "i=2041"; 空 = 通配 BaseEventType
std::vector<std::string> browse_path; // 如 ["Severity"] / ["EnabledState","Id"]; 空 = 选整体
AttributeId attribute_id = AttributeId::Value;
std::string index_range; // NumericRange 串; 空 = 全部
};
/// EventFilter (Part 4 §7.18)
struct EventFilter {
std::vector<SimpleAttributeOperand> select_clauses;
std::string where_clause; // V1 未实现, 留空
};
}
两个都是 struct 字段,没有方法。SDK 保证 C++17,示例用位置初始化(不要写 .field = 的指定初始化,那是 C++20)。
代码示例
#include <darra/opcua/client.hpp>
#include <darra/opcua/event_filter.hpp>
#include <iostream>
using namespace darra::opcua;
Session s("opc.tcp://localhost:4840");
s.Connect();
Subscription sub = s.CreateSubscription(500.0);
// 1) 构造过滤器类型 (目前只能构造, 传不进 SubscribeEvents)
EventFilter filter;
filter.select_clauses = {
SimpleAttributeOperand{ "i=2041", {"EventId"} },
SimpleAttributeOperand{ "i=2041", {"EventType"} },
SimpleAttributeOperand{ "i=2041", {"SourceName"} },
SimpleAttributeOperand{ "i=2041", {"Time"} },
SimpleAttributeOperand{ "i=2041", {"Message"} },
SimpleAttributeOperand{ "i=2041", {"Severity"} },
SimpleAttributeOperand{ "i=2782", {"ConditionId"} },
SimpleAttributeOperand{ "i=2915", {"ActiveState", "Id"} },
};
// 2) 实际订阅 —— 只有两个参数, 没有 filter
EventSubscription ev = s.SubscribeEvents("i=2253", &sub);
// 3) 回调拿到的是固定 5 个具名字段, 不是 fields[] 数组
ev.on_event() = [](EventArrivedEventArgs const& e) {
std::cout << "sev=" << e.severity << " " << e.message
<< " src=" << e.source_name << "\n";
};
字段速查
回调形参 EventArrivedEventArgs 是具名字段,不是 fields[] 数组,也没有 AsByteString() / AsDateTime() / AsUInt16() / AsBoolean() 这些取值方法:
| 字段 | 类型 | 说明 |
|---|---|---|
| severity | uint16_t | 严重度 1-1000 |
| message | std::string | LocalizedText.text |
| source_name | std::string | |
| time | int64_t | FileTime(UTC 100ns since 1601) |
| event_type | std::string | 当前恒为空串(SelectClauses 不含 EventType) |
下面是 OPC UA 标准的 BaseEventType / ConditionType 字段表,供对照;这 4 个以外的字段当前 SDK 抓不到:
| EventType | BrowsePath | 含义 | 当前能否抓到 |
|---|---|---|---|
| BaseEventType (i=2041) | {"Severity"} | 严重度 1-1000 | 能(固定 SelectClause) |
| BaseEventType | {"Message"} | 消息文本 | 能(固定 SelectClause) |
| BaseEventType | {"SourceName"} | 来源名 | 能(固定 SelectClause) |
| BaseEventType | {"Time"} | 事件时间 | 能(固定 SelectClause) |
| BaseEventType | {"EventId"} | 事件唯一 ID | 不能 |
| BaseEventType | {"EventType"} | 事件类型 NodeId | 不能 |
| ConditionType (i=2782) | {"ConditionId"} | 条件 NodeId | 不能 |
| AcknowledgeableConditionType (i=2881) | {"AckedState", "Id"} | bool 是否 Ack | 不能 |
| AlarmConditionType (i=2915) | {"ActiveState", "Id"} | bool 是否激活 | 不能 |
实现说明
- 抓取字段固定 4 个 SelectClause:Severity / Message / SourceName / Time,不是 5 个 —— 以 4 个为准
where_clause未实现,留空- 抓不到 EventId / ConditionId,直接影响 报警与确认 的
Acknowledge/Confirm/AddComment(它们要event_id)—— 见该页的能力缺口说明
最佳实践
- 要自定义字段 / 过滤就走 C 层
DarraUa_EventSubscription_SubscribeWithFilter - 回调在 Publish 线程上同步执行,重活丢给队列
跨语言对照
| 项 | C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|---|
| EventFilter | EventFilter | EventFilter | EventFilterDef.Filter | EventFilter | EventFilter | DarraUa_EventFilter |
| SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | EventFilterDef.Operand | SimpleAttributeOperand | SimpleAttributeOperand | DarraUa_SimpleAttributeOperand |
| 带 filter 的事件订阅 | subscribe(..., filter) | subscribe(..., filter) | subscribe(..., Filter) | 无 | subscribe(..., filter) | DarraUa_EventSubscription_SubscribeWithFilter |
Java 的类型名不一样:是 EventFilterDef.Filter / EventFilterDef.Operand(EventFilterDef.java),不是 EventFilter / SimpleAttributeOperand。C++ 是唯一没有「带 filter 的事件订阅入口」的一门语言。