EventFilter
概述
OPC UA Part 4 §7.18 EventFilter 由两部分组成:
- SelectClauses — 声明想抓哪些字段
- WhereClause — 过滤条件 (V1 暂未实现)
每个 SelectClause 是 SimpleAttributeOperand:
| 字段 | 类型 | 默认 | 说明 |
|---|---|---|---|
| type_definition_id | Optional[str] | None | 事件类型 NodeId 字符串 (如 "i=2041"); None = 通配 BaseEventType |
| browse_path | Tuple[str, ...] | () | 字段浏览路径 (各段 BrowseName); 空 tuple 表示选取该 EventType 整体 |
| attribute_id | AttributeId | AttributeId.Value | 属性 Id |
| index_range | Optional[str] | None | 数组下标范围 (NumericRange 字符串) |
EventFilter 本身也是 frozen dataclass:
| 字段 | 类型 | 默认 | 说明 |
|---|---|---|---|
| select_clauses | Tuple[SimpleAttributeOperand, ...] | () | 抓哪些字段 |
| where_clause | Optional[str] | None | 过滤表达式 (ContentFilter). V1 未实现, 留 None 或 "" 即可 |
EventSubscription.subscribe(node_id, event_filter) 接受 EventFilter, 但自定义 SelectClauses / WhereClause 都不下发, 事件一律按下面那 5 个固定字段回给 event_arrived 回调。
实际能拿到的字段
不传 EventFilter 时 (以及传了也一样) 回调拿到的固定 5 字段:
| # | 字段 | 对应 EventArrivedEventArgs |
|---|---|---|
| 1 | Severity (UInt16) | severity |
| 2 | Message (String) | message |
| 3 | SourceName (String) | source_name |
| 4 | Time (DateTime) | time |
| 5 | EventType (String) | event_type |
没有 EventId / ConditionId / ActiveState / 任何自定义字段 —— 这些当前取不到, 也没法通过 SelectClauses 要出来。需要 ConditionId 之类的信息, 请在收到事件后用 ua.read(...) 主动读报警节点的属性。
代码示例
EventFilter 只有 EventSubscription.subscribe(node_id, event_filter) 吃; 便捷入口 ua.subscribe_events(...) 不收 filter。所以要传 filter 就得自己挂容器:
from darra_opcua import (
DarraOpcUa, EventFilter, SimpleAttributeOperand, EventSubscription,
)
with DarraOpcUa("opc.tcp://localhost:4840") as ua:
ua.connect()
# 1) 容器订阅 (承载 Publish)
with ua.create_subscription(500) as sub:
# 2) 声明想要的字段 (browse_path 用 tuple)
f = EventFilter(select_clauses=(
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("Severity",)),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("Message",)),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("Time",)),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("SourceName",)),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("EventType",)),
))
# 3) 自建 EventSubscription, 挂容器后 subscribe
es = EventSubscription(ua)
es.attach_container_subscription(sub, owns=False) # owns=False: es.dispose() 不销毁 sub
es.subscribe("i=2253", event_filter=f)
# 4) 回调参数固定是 EventArrivedEventArgs, 不是 select_clauses 顺序的 fields 列表
def on_event(args):
if args.time is not None:
print(f"[{args.time:%H:%M:%S}] sev={args.severity}: {args.message}")
print(f" source={args.source_name} type={args.event_type}")
es.event_arrived.append(on_event)
EventArrivedEventArgs 只有 severity / message / source_name / time / event_type 五个字段。不存在 args.fields[...], 也没有 as_byte_string / as_uint16 这类字段访问器 —— 想要更多字段请在回调里 ua.read(...) 主动读。
字段速查 (browse_path 参考)
下表是 OPC UA 标准地址空间 (Part 3 / Part 9) 里的事件字段, 给你构造 SimpleAttributeOperand.browse_path 时对号用。当前 SDK 不下发 EventFilter, 选了这些字段也不会多给你数据 —— 它们只在 EventFilter 真生效后才有意义。
| 事件类型 | type_definition_id | browse_path | 含义 |
|---|---|---|---|
| BaseEventType | i=2041 | ("EventId",) | 事件唯一 ID |
| BaseEventType | i=2041 | ("Severity",) | 严重度 |
| BaseEventType | i=2041 | ("Message",) | 事件文本 |
| BaseEventType | i=2041 | ("Time",) | 事件时间 |
| ConditionType | i=2782 | ("ConditionId",) | 条件 NodeId |
| ConditionType | i=2782 | ("EnabledState", "Id") | bool 是否启用 |
| AcknowledgeableConditionType | i=2881 | ("AckedState", "Id") | bool 是否 Ack |
| AlarmConditionType | i=2915 | ("ActiveState", "Id") | bool 是否激活 |
| ShelvedConditionType | i=2929 | ("ShelvingState", "CurrentState") | LocalizedText 屏蔽态 |
行为与限制
- 自定义 SelectClauses / WhereClause 恒不生效, 事件字段固定那 5 个。
- 不生效时推一条
OpcUaEventCategory.Diagnostic/OpcUaEventSeverity.Info事件, 消息「当前版本不提供自定义 EventFilter 应用 (...), 已回退到默认 5 字段」。注意级别是 Info 不是 Warn。 - filter 只在当前订阅内保留, 订阅重建时不会重新挂。
where_clause目前只是字符串占位, 真正的 ContentFilter (ElementOperand 树) 未实现, 传什么都不起作用。
最佳实践
- 要更多字段, 在事件回调里用
ua.read(...)主动读报警节点属性 - 嵌套字段用 tuple 多段写:
("ActiveState", "Id") type_definition_id用事件类型的 NodeId 字符串, 与browse_path配对声明
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| EventFilter | EventFilter | EventFilter | EventFilter | EventFilter | DarraUa_EventFilter |
| SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | DarraUa_SimpleAttributeOperand |