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

EventFilter

概要​

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

  • SelectClauses — どのフィールドを取得したいかを指定する
  • WhereClause — フィルタ条件 (V1 未実装)

各 SelectClause は SimpleAttributeOperand:

フィールド種類デフォルト説明
type_definition_idOptional[str]Noneイベントタイプ NodeId 文字列(例: "i=2041");None = ワイルドカード BaseEventType
browse_pathTuple[str, ...]()フィールドの参照パス(各セグメント BrowseName);空のタプルはその EventType 全体を選択することを示す
attribute_idAttributeIdAttributeId.Value属性 ID
index_rangeOptional[str]None配列のインデックス範囲 (NumericRange 文字列)

EventFilter それ自体も frozen dataclass です:

フィールド種類デフォルト説明
select_clausesTuple[SimpleAttributeOperand, ...]()どのフィールドを取得するか
where_clauseOptional[str]Noneフィルタ式 (ContentFilter)。 V1 未実現, None または "" を指定すればよい
現在のバージョンでは EventFilter は実際に有効になりません

EventSubscription.subscribe(node_id, event_filter) は EventFilter を受け付けますが、カスタマイズした SelectClauses / WhereClause はいずれも配布されません。イベントはすべて以下の 5 つの固定フィールドに従って event_arrived コールバックに返されます。

実際に取得できるフィールド​

伝えない EventFilter 時(および送っても同じこと) コールバックで取得した固定の5つのフィールド:

#フィールド対応 EventArrivedEventArgs
1Severity (UInt16)severity
2Message (String)message
3SourceName (String)source_name
4Time (DateTime)time
5EventType (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)
コールバック内には args.fields

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_idbrowse_path意味
BaseEventTypei=2041("EventId",)イベントの固有ID
BaseEventTypei=2041("Severity",)深刻度
BaseEventTypei=2041("Message",)事件の本文
BaseEventTypei=2041("Time",)発生日時
ConditionTypei=2782("ConditionId",)条件 NodeId
ConditionTypei=2782("EnabledState", "Id")bool 有効にするかどうか
AcknowledgeableConditionTypei=2881("AckedState", "Id")bool Ackかどうか
AlarmConditionTypei=2915("ActiveState", "Id")bool 有効か
ShelvedConditionTypei=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#PythonJavaC++RustC
EventFilterEventFilterEventFilterEventFilterEventFilterDarraUa_EventFilter
SimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandDarraUa_SimpleAttributeOperand

次のステップ​