跳到主要内容

EventFilter

概述

OPC UA Part 4 §7.18 EventFilter 由两部分组成:

  • selectClauses — 抓哪些字段
  • whereClause — 过滤条件 (V1 暂未实现)

每个 select clause 是 SimpleAttributeOperand:

字段说明
typeDefinitionId事件类型 NodeId
browsePath字段路径 (String[])
attributeId属性 Id, 默认 Value
indexRange数组下标 (一般 null)

不传 EventFilter 时默认 5 字段: EventId / EventType / SourceName / Time / Message。

代码示例

import com.darra.opcua.*;

try (DarraOpcUa ua = new DarraOpcUa("opc.tcp://localhost:4840")) {
ua.connect();
OpcUaSubscription sub = ua.createSubscription(500);

EventFilter filter = EventFilter.builder()
.select(new SimpleAttributeOperand("i=2041", new String[]{"EventId"}))
.select(new SimpleAttributeOperand("i=2041", new String[]{"EventType"}))
.select(new SimpleAttributeOperand("i=2041", new String[]{"SourceName"}))
.select(new SimpleAttributeOperand("i=2041", new String[]{"Time"}))
.select(new SimpleAttributeOperand("i=2041", new String[]{"Message"}))
.select(new SimpleAttributeOperand("i=2041", new String[]{"Severity"}))
.select(new SimpleAttributeOperand("i=2782", new String[]{"ConditionId"}))
.select(new SimpleAttributeOperand("i=2915", new String[]{"ActiveState", "Id"}))
.build();

OpcUaEventSubscription ev = sub.subscribeEvents("i=2253", filter);

ev.addEventArrivedListener(args -> {
// fields 顺序与 selectClauses 一致
byte[] eid = args.fields[0].asByteString();
Instant time = args.fields[3].asDateTime();
String msg = args.fields[4].asString();
int sev = args.fields[5].asUInt16();
boolean active = args.fields[7].asBoolean();
System.out.printf("[%s] sev=%d: %s active=%s%n", time, sev, msg, active);
});
}

字段速查

EventTypeBrowsePath含义
BaseEventType (i=2041)["EventId"]事件唯一 ID (byte[])
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 是否激活

实现说明

  • Stack 不支持时回退默认 5 字段并推 Diagnostic Info 事件

最佳实践

  • 显式列字段, 不要靠默认
  • 顺序对齐: fields[i] 顺序就是 selectClauses 顺序
  • 报警面板必抓 ConditionId

跨语言对照

C#PythonJavaC++RustC
EventFilterEventFilterEventFilterEventFilterEventFilterDarraUa_EventFilter
SimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandDarraUa_SimpleAttributeOperand

下一步