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)
| EventType | BrowsePath | 意味 |
|---|---|---|
| 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# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| EventFilter | EventFilter | EventFilterDef.Filter | EventFilter | EventFilter | 現在、提供しておりません |
| SimpleAttributeOperand | SimpleAttributeOperand | EventFilterDef.Operand | SimpleAttributeOperand | SimpleAttributeOperand | 現在、提供しておりません |