EventFilter
概述
OPC UA Part 4 §7.18 EventFilter 由两部分组成:
- SelectClauses — 抓哪些字段 (按顺序对应到事件 fields)
- WhereClause — 过滤条件 (V1 暂未实现)
每个 SelectClause 是 SimpleAttributeOperand:
| 字段 | 说明 |
|---|---|
| type_definition_id | 事件类型 NodeId |
| browse_path | 字段路径 (list[str]) |
| attribute_id | 属性 Id, 默认 VALUE |
| index_range | 数组下标 (一般 None) |
不传 EventFilter 时默认抓 5 字段: EventId / EventType / SourceName / Time / Message。
代码示例
from opcua import (
DarraOpcUa, EventFilter, SimpleAttributeOperand, AttributeId
)
with DarraOpcUa("opc.tcp://localhost:4840") as ua:
ua.connect()
sub = ua.create_subscription(500)
filter = EventFilter(select_clauses=[
# 默认 5 字段
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=["EventId"]),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=["EventType"]),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=["SourceName"]),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=["Time"]),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=["Message"]),
# 报警特有
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=["Severity"]),
SimpleAttributeOperand(type_definition_id="i=2782", browse_path=["ConditionId"]),
SimpleAttributeOperand(type_definition_id="i=2915", browse_path=["ActiveState", "Id"]),
])
ev = sub.subscribe_events("i=2253", filter)
@ev.on_event_arrived
def on_event(args):
# fields 顺序与 select_clauses 一致
eid = args.fields[0].as_byte_string
time = args.fields[3].as_datetime
msg = args.fields[4].as_string
sev = args.fields[5].as_uint16
active = args.fields[7].as_boolean
print(f"[{time:%H:%M:%S}] sev={sev}: {msg} active={active}")
字段速查
| EventType | BrowsePath | 含义 |
|---|---|---|
| BaseEventType (i=2041) | ["EventId"] | 事件唯一 ID (bytes) |
| BaseEventType | ["Severity"] | 严重度 1-1000 |
| ConditionType (i=2782) | ["ConditionId"] | 条件 NodeId |
| ConditionType | ["EnabledState", "Id"] | bool 是否启用 |
| AcknowledgeableConditionType (i=2881) | ["AckedState", "Id"] | bool 是否 Ack |
| AlarmConditionType (i=2915) | ["ActiveState", "Id"] | bool 是否激活 |
| AlarmConditionType | ["ShelvingState", "CurrentState"] | LocalizedText 屏蔽态 |
实现说明
- Stack 不支持时回退默认 5 字段并推 Diagnostic Info 事件
- WhereClause V2 接通后支持完整 ContentFilter
最佳实践
- 显式列字段, 不要靠默认
- 顺序对齐: fields[i] 顺序就是 select_clauses 顺序
- 报警面板必抓 ConditionId
- ActiveState 用嵌套 path:
["ActiveState", "Id"]
跨语言对照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| EventFilter | EventFilter | EventFilter | EventFilter | EventFilter | DarraUa_EventFilter |
| SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | DarraUa_SimpleAttributeOperand |