メインコンテンツまでスキップ

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。

現在 SDK 、「EventFilter 付きの購読」の公開エントリポイントはありません

EventFilter / SimpleAttributeOperand 内単なるデータ構造, トップ darra_opcua レベルからエクスポートされていますが、Cレイヤーを経由 Session::subscribe_events しています 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 ゲッターもありません。

フィールドのクイックリファレンス​

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 有効か

上記の表はフィールドの意味に関する参考情報, 現在のイベントコールバックでは、5つの固定フィールドのみが公開 SDK されています。

実装上の制約​

  • 「サブスクリプション EventFilter 付き」への公開アクセスポイントはなく、現時点ではデータ構造として EventFilter のみ存在しています
  • イベント C層を経由 MI し 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

次のステップ​