EventFilter
概要
OPC UA Part 4 §7.18 EventFilter 二つの部分から構成される:
- SelectClauses — どのフィールドを取得したいかを指定する
- WhereClause — フィルタ条件 (V1 未実装)
各 SelectClause は SimpleAttributeOperand:
| フィールド | 種類 | デフォルト | 説明 |
|---|---|---|---|
| type_definition_id | Optional[str] | None | イベントタイプ NodeId 文字列(例: "i=2041");None = ワイルドカード BaseEventType |
| browse_path | Tuple[str, ...] | () | フィールドの参照パス(各セグメント BrowseName);空のタプルはその EventType 全体を選択することを示す |
| attribute_id | AttributeId | AttributeId.Value | 属性 ID |
| index_range | Optional[str] | None | 配列のインデックス範囲 (NumericRange 文字列) |
EventFilter それ自体も frozen dataclass です:
| フィールド | 種類 | デフォルト | 説明 |
|---|---|---|---|
| select_clauses | Tuple[SimpleAttributeOperand, ...] | () | どのフィールドを取得するか |
| where_clause | Optional[str] | None | フィルタ式 (ContentFilter)。 V1 未実現, None または "" を指定すればよい |
EventSubscription.subscribe(node_id, event_filter) は EventFilter を受け付けますが、カスタマイズした SelectClauses / WhereClause はいずれも配布されません。イベントはすべて以下の 5 つの固定フィールドに従って event_arrived コールバックに返されます。
実際に取得できるフィールド
伝えない EventFilter 時(および送っても同じこと) コールバックで取得した固定の5つのフィールド:
| # | フィールド | 対応 EventArrivedEventArgs |
|---|---|---|
| 1 | Severity (UInt16) | severity |
| 2 | Message (String) | message |
| 3 | SourceName (String) | source_name |
| 4 | Time (DateTime) | time |
| 5 | EventType (String) | event_type |
ないEventId / ConditionId / ActiveState / 任意のカスタムフィールド —— これらは現在取得できず、 を通じて取得 SelectClauses することもできません。 などの ConditionId 情報が必要な場合は、イベントを受信した後、 ua.read(...) を使用してアラームノードの属性を能動的に読み取ってください。
コード例
EventFilter ただ EventSubscription.subscribe(node_id, event_filter) 食べる; 便利なエントリ filter は ua.subscribe_events(...) 受け付けない。したがって、filter を渡すには、自分でコンテナを接続する必要がある:
from darra_opcua import (
DarraOpcUa, EventFilter, SimpleAttributeOperand, EventSubscription,
)
with DarraOpcUa("opc.tcp://localhost:4840") as ua:
ua.connect()
# 1) コンテナサブスクリプション(Publishを受け付ける)
with ua.create_subscription(500) as sub:
# 2) 必要なフィールドを宣言する(browse_path タプルを使用)
f = EventFilter(select_clauses=(
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("Severity",)),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("Message",)),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("Time",)),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("SourceName",)),
SimpleAttributeOperand(type_definition_id="i=2041", browse_path=("EventType",)),
))
# 3) 自前構築 EventSubscription、コンテナを起動した後、subscribe を実行する
es = EventSubscription(ua)
es.attach_container_subscription(sub, owns=False) # owns=False: es.dispose() はサブを破棄しない
es.subscribe("i=2253", event_filter=f)
# 4) コールバック引数は固定であり EventArrivedEventArgs、順序 select_clauses 付きフィールドのリストではない
def on_event(args):
if args.time is not None:
print(f"[{args.time:%H:%M:%S}] sev={args.severity}: {args.message}")
print(f" source={args.source_name} type={args.event_type}")
es.event_arrived.append(on_event)
EventArrivedEventArgs がなく、五 severity / message / source_name / time / event_type つのフィールドしかありません。存在しない args.fields[...], また、 as_byte_string / as_uint16 のようなフィールドアクセサもありません。―― さらに多くのフィールドが必要な場合は、コールバック内で ua.read(...) 能動的に読み込んでください。
フィールドのクイックリファレンス(browse_path 参照)
以下の表は、「標準 OPC UA アドレス空間(第3部/第9部)」に記載されているイベントフィールドの一覧であり、これを構築する SimpleAttributeOperand.browse_path 際の参照用です。現時点では 配布 SDK されません EventFilter, これらのフィールドを選択しても、追加のデータが得られるわけではありません――これらは、 が EventFilter 真になった場合にのみ意味を持ちます。
| イベントの種類 | type_definition_id | browse_path | 意味 |
|---|---|---|---|
| BaseEventType | i=2041 | ("EventId",) | イベントの固有ID |
| BaseEventType | i=2041 | ("Severity",) | 深刻度 |
| BaseEventType | i=2041 | ("Message",) | 事件の本文 |
| BaseEventType | i=2041 | ("Time",) | 発生日時 |
| ConditionType | i=2782 | ("ConditionId",) | 条件 NodeId |
| ConditionType | i=2782 | ("EnabledState", "Id") | bool 有効にするかどうか |
| AcknowledgeableConditionType | i=2881 | ("AckedState", "Id") | bool Ackかどうか |
| AlarmConditionType | i=2915 | ("ActiveState", "Id") | bool 有効か |
| ShelvedConditionType | i=2929 | ("ShelvingState", "CurrentState") | LocalizedText 遮蔽状態 |
行動と制限
- カスタム SelectClauses / WhereClause 適用されない。イベントフィールドは常にその5つに固定される。
- 有効でない場合は、
OpcUaEventCategory.Diagnostic/OpcUaEventSeverity.Infoイベントのメッセージ「現在のバージョンではカスタム EventFilter アプリ (...) は提供されていません。デフォルトの 5 フィールドにロールバックされました」を 一件送信します。注意レベルは Info 「Warn」ではありません。 - filter は現在のサブスクリプション内でのみ保持され、サブスクリプションが再構築されても再設定されることはありません。
where_clause現時点では単なる文字列のプレースホルダーであり、実際の ContentFilter(ElementOperand ツリー)は実装されていないため、何を渡しても機能しません。
ベストプラクティス
- さらに多くのフィールドが必要な場合は、イベントコールバック内でアラームノードの属性を能動
ua.read(...)的に読み取ってください。 - ネストされたフィールドは、タプルを使って複数行で記述します:
("ActiveState", "Id") type_definition_idイベントタイプの文字 NodeId 列と、ペアを組む宣言browse_path
多言語対照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| EventFilter | EventFilter | EventFilter | EventFilter | EventFilter | DarraUa_EventFilter |
| SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | DarraUa_SimpleAttributeOperand |