跳到主要内容

EventFilter

概述

OPC UA Part 4 §7.18 EventFilter:

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

SimpleAttributeOperand: type_definition_id / browse_path / attribute_id / index_range。

不传 EventFilter 时默认 5 字段。

代码示例

use opcua::{DarraOpcUa, EventFilter, SimpleAttributeOperand, AttributeId};

let ua = DarraOpcUa::new("opc.tcp://localhost:4840")?;
ua.connect()?;
let sub = ua.create_subscription(500)?;

let filter = EventFilter {
select_clauses: vec![
SimpleAttributeOperand {
type_definition_id: "i=2041".into(),
browse_path: vec!["EventId".into()],
attribute_id: AttributeId::Value,
index_range: None,
},
SimpleAttributeOperand {
type_definition_id: "i=2041".into(),
browse_path: vec!["EventType".into()],
..Default::default()
},
SimpleAttributeOperand {
type_definition_id: "i=2041".into(),
browse_path: vec!["SourceName".into()],
..Default::default()
},
SimpleAttributeOperand {
type_definition_id: "i=2041".into(),
browse_path: vec!["Time".into()],
..Default::default()
},
SimpleAttributeOperand {
type_definition_id: "i=2041".into(),
browse_path: vec!["Message".into()],
..Default::default()
},
SimpleAttributeOperand {
type_definition_id: "i=2041".into(),
browse_path: vec!["Severity".into()],
..Default::default()
},
SimpleAttributeOperand {
type_definition_id: "i=2782".into(),
browse_path: vec!["ConditionId".into()],
..Default::default()
},
SimpleAttributeOperand {
type_definition_id: "i=2915".into(),
browse_path: vec!["ActiveState".into(), "Id".into()],
..Default::default()
},
],
where_clause: None,
};

let ev = sub.subscribe_events_with_filter("i=2253", &filter)?;

ev.on_event_arrived(|args| {
let _eid = args.fields[0].as_byte_string()?;
let time = args.fields[3].as_datetime()?;
let msg = args.fields[4].as_string()?;
let sev = args.fields[5].as_uint16()?;
let active = args.fields[7].as_boolean()?;
println!("[{}] sev={}: {} active={}", time, sev, msg, active);
Ok(())
})?;

字段速查

EventTypeBrowsePath含义
BaseEventType (i=2041)["EventId"]事件唯一 ID
BaseEventType["Severity"]严重度 1-1000
ConditionType (i=2782)["ConditionId"]条件 NodeId
AcknowledgeableConditionType (i=2881)["AckedState", "Id"]bool 是否 Ack
AlarmConditionType (i=2915)["ActiveState", "Id"]bool 是否激活

实现说明

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

最佳实践

  • 显式列字段
  • 顺序对齐: fields[i] 顺序就是 select_clauses 顺序
  • 报警面板必抓 ConditionId

跨语言对照

C#PythonJavaC++RustC
EventFilterEventFilterEventFilterEventFilterEventFilterDarraUa_EventFilter
SimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandDarraUa_SimpleAttributeOperand

下一步