跳到主要内容

EventFilter

概述​

OPC UA Part 4 §7.18 EventFilter:

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

SimpleAttributeOperand (Part 4 §7.4.4.5): type_definition_id / browse_path / attribute_id / index_range。

当前没有"带 EventFilter 订阅"的公开入口

EventFilter / SimpleAttributeOperand 在 SDK 里只是数据结构, 已从 darra_opcua 顶层导出, 但 Session::subscribe_events 走的是 C 层 DarraUa_Subscription_AddEventItem, 该入口不接受 filter 参数。 EventSubscription::subscribe_with_filter 未实现, 不要照它写代码。

实际订阅入口只有一个 (见 事件订阅):

pub fn Session::subscribe_events<F>(
&self,
node_id: Option<&str>,
existing: Option<Arc<Subscription>>,
callback: F,
) -> Result<EventSubscription, OpcUaError>
where F: FnMut(&EventArrivedEventArgs) + Send + 'static

EventSubscription 唯一公共方法是 monitored_item_handle() -> u32。

API​

类型 / 字段 / 方法类别读写说明
EventFilter { select_clauses, where_clause }struct—有 Default
EventFilter::default_5_fields()方法读Severity / Message / SourceName / Time / EventType
SimpleAttributeOperand { type_definition_id, browse_path, attribute_id, index_range }struct—只 derive Debug + Clone, 无 Default
SimpleAttributeOperand::field(name)方法读单字段, type_definition_id = None
SimpleAttributeOperand::nested(path)方法读嵌套字段, type_definition_id = None

SimpleAttributeOperand 怎么写​

type_definition_id 是 Option<String>, 不是 String —— "i=2041".into() 编译不过 (Option<String> 不实现 From<&str>), 要写 Some("i=2041".to_string())。 结构体没有 impl Default, ..Default::default() 也编译不过, 四个字段必须写全:

use darra_opcua::{SimpleAttributeOperand, AttributeId};

let clause = SimpleAttributeOperand {
type_definition_id: Some("i=2041".to_string()), // Option<String>
browse_path: vec!["EventType".into()],
attribute_id: AttributeId::Value,
index_range: None,
};

不需要指定 EventType 时用构造器更短:

let a = SimpleAttributeOperand::field("EventType");                 // 单字段
let b = SimpleAttributeOperand::nested(["EnabledState", "Id"]); // 嵌套字段

构造器产出的 type_definition_id 是 None (视作通配 BaseEventType)。

EventFilter 组装​

EventFilter 有 Default, where_clause: None 直接写没问题:

use darra_opcua::{EventFilter, SimpleAttributeOperand, AttributeId};

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

回调实际能拿到什么​

订阅走 s.subscribe_events(...), 回调收到的是 EventArrivedEventArgs —— 5 个平铺字段, 没有 fields 数组:

pub struct EventArrivedEventArgs {
pub severity: u16,
pub message: Option<String>,
pub source_name: Option<String>,
pub time: Option<SystemTime>,
pub event_type: Option<String>,
}
use darra_opcua::Session;

let mut s = Session::new("opc.tcp://localhost:4840")?;
s.connect()?;

let ev = s.subscribe_events(Some("i=2253"), None, |args| {
println!("[{:?}] sev={}: {:?} type={:?}",
args.time, args.severity, args.message, args.event_type);
})?;
println!("event mi = {}", ev.monitored_item_handle());
SDK 不按索引暴露 select_clauses 结果

select_clauses 的结果不会以 args.fields[i] 的形式给回调, 因此没有 "fields[i] 顺序就是 select_clauses 顺序" 这回事。EventId / ConditionId 的原始 ByteString 当前不透出到 EventArrivedEventArgs, 只有上面 5 个字段。 Variant 上也没有 as_byte_string / as_uint16 / as_boolean 这些 getter。

字段速查​

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 是否激活

上表是字段语义参考, 当前 SDK 的事件回调只透出 5 个固定字段。

实现限制​

  • 没有"带 EventFilter 订阅"的公开入口, EventFilter 目前只作为数据结构存在
  • 事件 MI 走 C 层 DarraUa_Subscription_AddEventItem, 不下发自定义 SelectClauses
  • where_clause V1 未实现
  • EventId / ConditionId 原始 ByteString 不透出

最佳实践​

  • 要抓事件就用 subscribe_events, 不要试图传 EventFilter
  • 显式列字段的习惯保留, 等 filter 入口开放后可直接复用
  • 报警面板需要 ConditionId 时, 先用 event_type + source_name 定位, 再 call 报警节点的 Acknowledge Method

跨语言对照​

C#PythonJavaC++RustC
EventFilterEventFilterEventFilterEventFilterEventFilterDarraUa_EventFilter
SimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandDarraUa_SimpleAttributeOperand

下一步​