EventFilter — 自定义事件字段与过滤
概述
OPC UA Part 4 §7.18 EventFilter 由两部分组成:
- SelectClauses — 抓哪些字段 (按顺序对应到
EventArrivedEventArgs) - WhereClause — 过滤条件 (
ContentFilter表达式树) — 当前不提供
每个 SelectClause 是一个 SimpleAttributeOperand (Part 4 §7.4.4.5):
| 字段 | 说明 |
|---|---|
| TypeDefinitionId | 事件类型 NodeId (如 i=2041 BaseEventType) |
| BrowsePath | 字段路径 (如 ["Severity"] / ["EnabledState", "Id"]) |
| AttributeId | 属性 Id, 默认 Value |
| IndexRange | 数组下标范围 (一般 null) |
不管传不传 EventFilter, SDK 当前都只订阅 5 个字段: Severity / Message / SourceName / Time / EventType (见下面"当前行为")。
API
| 类 / 字段 | 类别 | 读写 | 说明 |
|---|---|---|---|
| EventFilter | 类 | — | 过滤器容器 |
| .SelectClauses | 属性 | 读写 | SimpleAttributeOperand[] |
| .WhereClause | 属性 | 读写 | string, 当前不提供, 留 null 即可 |
| SimpleAttributeOperand | 类 | — | 单个字段定义 |
| ev.Subscribe(objectNodeId, filter) | 方法 | 写 | 删旧 Event MI 后按目标节点重挂一个 (filter 当前不生效) |
代码示例
using DarraOpcUa_Client;
using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Connect();
using var sub = ua.CreateSubscription(500);
// 1) 声明要抓哪些字段 (默认 5 + 报警特有 2)
var filter = new EventFilter
{
SelectClauses = new[]
{
// 默认 5 字段
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "Severity" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "Message" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "SourceName" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "Time" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "EventType" } },
// 报警特有
new SimpleAttributeOperand { TypeDefinitionId = "i=2782", BrowsePath = new[] { "ConditionId" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2915", BrowsePath = new[] { "ActiveState", "Id" } },
}
};
// 2) 先建事件订阅, 再按 filter 重建该节点的事件订阅
using var ev = ua.SubscribeEvents("i=2253", sub);
ev.Subscribe("i=2253", filter);
ev.EventArrived += (s, e) =>
{
// 事件回调当前只透出 5 个固定字段, 自定义字段当前不提供
Console.WriteLine($"[{e.Time:HH:mm:ss}] sev={e.Severity} {e.SourceName}: {e.Message}");
};
当前行为 (重要)
自定义 SelectClauses 当前一定不生效
ev.Subscribe(objectNodeId, filter) 不会把 SelectClauses 发给服务端, 一定走默认字段:
- 事件 MI 照常注册, SelectClauses 不下发
- 会推一条
Diagnostic类别 Info 事件, 说明已回到默认 5 字段 - 所以过滤条件和字段选择都没生效: 服务端发什么,
EventArrived就收什么
- 事件回调固定给出
Severity / Message / SourceName / Time / EventType5 个字段 (EventArrivedEventArgs), 自定义字段收不到 - WhereClause 是字符串占位, 没有 ContentFilter 解析, 不做任何条件过滤
Subscribe(objectNodeId, filter)会先移除已有 Event MI, 再按目标节点挂一个新的; 需要对象已挂接容器订阅, 否则抛InvalidOperationException;objectNodeId为空抛ArgumentNullException
SelectClauses 字段路径速查 (常用 EventType + BrowsePath)
| EventType | BrowsePath | 含义 |
|---|---|---|
| BaseEventType (i=2041) | ["EventId"] | 事件唯一 ID (ByteString) |
| BaseEventType | ["EventType"] | 事件类型 NodeId |
| BaseEventType | ["SourceName"] | 触发源名 |
| BaseEventType | ["Time"] | 事件时间 (UTC) |
| BaseEventType | ["Message"] | 文本消息 |
| BaseEventType | ["Severity"] | 严重度 1-1000 |
| ConditionType (i=2782) | ["ConditionId"] | 条件 NodeId |
| ConditionType | ["ConditionName"] | 条件名 |
| ConditionType | ["EnabledState", "Id"] | Bool 是否启用 |
| AcknowledgeableConditionType (i=2881) | ["AckedState", "Id"] | Bool 是否已 Ack |
| AlarmConditionType (i=2915) | ["ActiveState", "Id"] | Bool 是否激活 |
| AlarmConditionType | ["ShelvingState", "CurrentState"] | LocalizedText 屏蔽态 |
最佳实践
- 按 5 个固定字段写回调 — 现在别依赖 SelectClauses, 传了也不生效
- 要报警字段 (EventId / ConditionId / ActiveState) 得另想办法 — 事件回调里没有这几项, 而
ua.Acknowledge/Confirm又要conditionId+eventId, 见 事件订阅 的说明 - 字段路径规范备查 —
["ActiveState", "Id"]是 Bool,["ActiveState"]是 LocalizedText, 过滤生效后按这张表配
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| EventFilter | EventFilter | EventFilterDef.Filter | EventFilter | EventFilter | 当前不提供 |
| SimpleAttributeOperand | SimpleAttributeOperand | EventFilterDef.Operand | SimpleAttributeOperand | SimpleAttributeOperand | 当前不提供 |