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

EventFilter — カスタムイベントフィールドとフィルタリング

概要​

OPC UA Part 4 §7.18 EventFilter は二つの部分から構成されます:

  • SelectClauses — どのフィールドを取得するか(順に EventArrivedEventArgs に対応)
  • WhereClause — フィルタ条件(ContentFilter 式ツリー) — 現在提供されていません

各 SelectClause は一つの SimpleAttributeOperand(第4部 §7.4.4.5)です:

フィールド説明
TypeDefinitionIdイベントの種類 NodeId (例:i=2041 BaseEventType)
BrowsePathフィールドパス(例:["Severity"] / ["EnabledState", "Id"])
AttributeId属性 Id、デフォルト値 Value
IndexRange配列のインデックス範囲(通常は null)

EventFilter を渡すかどうかにかかわらず、SDK は現在 5 つのフィールドのみを購読しています: Severity / Message / SourceName / Time / EventType(下の「現在の動作」を参照)。

API​

クラス / フィールドカテゴリ読み書き説明
EventFilterクラス—フィルター容器
.SelectClauses属性読み書きSimpleAttributeOperand[]
.WhereClause属性読み書きstring、現在は提供されていません。nullのままにしておいてください。
SimpleAttributeOperandクラス—個々のフィールドの定義
ev.Subscribe(objectNodeId, filter)方法書く古いイベント MI を削除した後、対象ノードに一つ再登録する(filter は現在有効ではありません)

コード例​

using DarraOpcUa_Client;

using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Connect();

using var sub = ua.CreateSubscription(500);

// 1) どのフィールドを取得するかを指定する(デフォルトは 5 + アラート固有の 2)
var filter = new EventFilter
{
SelectClauses = new[]
{
// デフォルトで 5 つのフィールド
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "Severity" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "Message" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "SourceName" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "Time" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2041", BrowsePath = new[] { "EventType" } },
// 警報特有
new SimpleAttributeOperand { TypeDefinitionId = "i=2782", BrowsePath = new[] { "ConditionId" } },
new SimpleAttributeOperand { TypeDefinitionId = "i=2915", BrowsePath = new[] { "ActiveState", "Id" } },
}
};

// 2) まずイベントサブスクリプションを作成し、その後、filter に基づいてそのノードのイベントサブスクリプションを再作成する
using var ev = ua.SubscribeEvents("i=2253", sub);
ev.Subscribe("i=2253", filter);

ev.EventArrived += (s, e) =>
{
// イベントコールバックでは、現在5つの固定フィールドのみが公開されており、カスタムフィールドは現在提供されていません。
Console.WriteLine($"[{e.Time:HH:mm:ss}] sev={e.Severity} {e.SourceName}: {e.Message}");
};

現在の動作 (重要)​

カスタム SelectClauses は現在確実に機能しません

ev.Subscribe(objectNodeId, filter) は SelectClauses をサーバーに送信しません。必ずデフォルトのフィールドで動作します:

  • イベント MI は通常通り登録され、SelectClauses は送信されません
  • Diagnostic カテゴリの Info イベントを一件プッシュします。説明:デフォルトの 5 フィールドに戻りました
  • ですからフィルタ条件もフィールドの選択も反映されていない: サーバーから送信されたものを、そのまま EventArrived で受信する
  • イベントコールバックでは Severity / Message / SourceName / Time / EventType の 5 つのフィールド (EventArrivedEventArgs) が常に返され、カスタムフィールドは受信されない
  • WhereClause は文字列のプレースホルダーで、ContentFilter 解析はありません。いかなる条件によるフィルタリングも行わない
  • Subscribe(objectNodeId, filter) はまず既存の Event MI を削除し、その後、対象ノードに新しい Event を登録します。オブジェクトがコンテナのサブスクリプションに登録済みである必要があります。そうでない場合は InvalidOperationException をスローします。objectNodeId が null の場合は ArgumentNullException をスローします

SelectClauses フィールドパスのクイックリファレンス (よく使う EventType + BrowsePath)​

EventTypeBrowsePath意味
BaseEventType (i=2041)["EventId"]イベントの固有ID (ByteString)
BaseEventType["EventType"]イベントの種類 NodeId
BaseEventType["SourceName"]トリガーソース名
BaseEventType["Time"]発生日時 (UTC)
BaseEventType["Message"]テキストメッセージ
BaseEventType["Severity"]深刻度 1~1000
ConditionType (i=2782)["ConditionId"]条件 NodeId
ConditionType["ConditionName"]条件名
ConditionType["EnabledState", "Id"]Bool 有効か無効か
AcknowledgeableConditionType (i=2881)["AckedState", "Id"]Bool:Ack済みか
AlarmConditionType (i=2915)["ActiveState", "Id"]Bool 有効か
AlarmConditionType["ShelvingState", "CurrentState"]LocalizedText 遮蔽状態

ベストプラクティス​

  • 5つの固定フィールドに基づいてコールバックを記述する — 今は SelectClauses に頼らないでください。送っても効かないから
  • 「アラーム」フィールド(EventId / ConditionId / ActiveState)については、別の方法を考えなければならない — イベントコールバックにはこれらの項目が含まれていないが、ua.Acknowledge / Confirm には conditionId + eventId が必要です。詳細は イベントの購読 の説明を参照
  • フィールドパスの規定(参考用) — ["ActiveState", "Id"] は Bool、["ActiveState"] は LocalizedText。フィルタが有効になったらこの表に基づいて割り当ててください

多言語対照​

C#PythonJavaC++RustC
EventFilterEventFilterEventFilterDef.FilterEventFilterEventFilter現在、提供しておりません
SimpleAttributeOperandSimpleAttributeOperandEventFilterDef.OperandSimpleAttributeOperandSimpleAttributeOperand現在、提供しておりません

次のステップ​