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

EventFilter

概要​

OPC UA Part 4 §7.18 EventFilter:

  • select_clauses — どのフィールドを抽出するか
  • where_clause — フィルタ条件(現在未実装のため、空欄のままにしてください)

SimpleAttributeOperand フィールド: type_definition_id / browse_path / attribute_id / index_range。

これら二つの構造体は、C++側では単なる型定義に過ぎず、サブスクリプション

Session::SubscribeEvents(std::string const& node_id = "", Subscription* existing = nullptr)には接続されていません。引数 EventFilter なし、どのフィールドを取得するかは固定されている4つ SelectClause:Severity / Message / SourceName / Time。

したがって、C++ 側では現在直せないどのフィールドを取得するかは設定できません WhereClause。カスタムフィルタを設定するには、Cの便利レイヤーを経由する必要があり DarraUa_EventSubscription_SubscribeWithFilter(sub, object_node_id, filter, cb, ctx, &out_mi)、NULLフィルターを渡すと、この4つのデフォルトフィールドが返されます。

API​

namespace darra::opcua {

/// SimpleAttributeOperand (Part 4 §7.4.4.5)
struct SimpleAttributeOperand {
std::string type_definition_id; // イベントの種類 NodeId、例:「i=2041」;空 = ワイルドカード BaseEventType
std::vector<std::string> browse_path; // 例: ["Severity"] / ["EnabledState","Id"]; 空 = すべて選択
AttributeId attribute_id = AttributeId::Value;
std::string index_range; // NumericRange リスト; 空 = すべて
};

/// EventFilter (Part 4 §7.18)
struct EventFilter {
std::vector<SimpleAttributeOperand> select_clauses;
std::string where_clause; // V1 未実装、空白のまま
};
}

どちらも struct のフィールド、方法はありません。SDK C++17に準拠しており、例では位置による初期化を使用しています(指定 .field = 初期化は記述しないでください。それはC++20の機能です)。

コード例​

#include <darra/opcua/client.hpp>
#include <darra/opcua/event_filter.hpp>
#include <iostream>

using namespace darra::opcua;

Session s("opc.tcp://localhost:4840");
s.Connect();
Subscription sub = s.CreateSubscription(500.0);

// 1) フィルタータイプの構築(現時点では構築のみ可能で、渡すことはできない SubscribeEvents)
EventFilter filter;
filter.select_clauses = {
SimpleAttributeOperand{ "i=2041", {"EventId"} },
SimpleAttributeOperand{ "i=2041", {"EventType"} },
SimpleAttributeOperand{ "i=2041", {"SourceName"} },
SimpleAttributeOperand{ "i=2041", {"Time"} },
SimpleAttributeOperand{ "i=2041", {"Message"} },
SimpleAttributeOperand{ "i=2041", {"Severity"} },
SimpleAttributeOperand{ "i=2782", {"ConditionId"} },
SimpleAttributeOperand{ "i=2915", {"ActiveState", "Id"} },
};

// 2) 実際のサブスクリプション —— パラメータは二つだけで、filter はありません
EventSubscription ev = s.SubscribeEvents("i=2253", &sub);

// 3) コールバックで受け取るのは、名前付きのフィールドが5つという固定の形式であり、fields[] 配列ではない
ev.on_event() = [](EventArrivedEventArgs const& e) {
std::cout << "sev=" << e.severity << " " << e.message
<< " src=" << e.source_name << "\n";
};

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

コールバックの引数 EventArrivedEventArgs は名前フィールド、配列ではなく、 fields[] また AsByteString() / AsDateTime() / AsUInt16() / AsBoolean() といった値の取得方法もありません:

フィールド種類説明
severityuint16_t深刻度 1~1000
messagestd::stringLocalizedText.text
source_namestd::string
timeint64_tFileTime(UTC 100ns since 1601)
event_typestd::string現在、恒は空文字列です(SelectClauses 除く EventType)

以下は、参照用の OPC UA 標準の BaseEventType / ConditionType フィールド表です。これら 4 つ以外のフィールドは、現時点では SDK では取得できません:

EventTypeBrowsePath意味現時点で捕まえることができるか
BaseEventType (i=2041){"Severity"}深刻度 1~1000可能(固定 SelectClause)
BaseEventType{"Message"}メッセージ本文可能(固定 SelectClause)
BaseEventType{"SourceName"}出典名可能(固定 SelectClause)
BaseEventType{"Time"}発生日時可能(固定 SelectClause)
BaseEventType{"EventId"}イベントの固有IDできない
BaseEventType{"EventType"}イベントの種類 NodeIdできない
ConditionType (i=2782){"ConditionId"}条件 NodeIdできない
AcknowledgeableConditionType (i=2881){"AckedState", "Id"}bool Ackかどうかできない
AlarmConditionType (i=2915){"ActiveState", "Id"}bool 有効かできない

実装の説明​

  • 取得するフィールドは4つに固定 SelectClause:Severity / Message / SourceName / Time、5つではなく――4つを基準とする
  • where_clause 未実装、空欄
  • 把握できない EventId / ConditionId、直接的な影響 アラームと確認 の Acknowledge / Confirm / AddComment(それらは event_id)―― 当該ページの「能力ギャップ」の説明を参照のこと

ベストプラクティス​

  • フィールドのカスタマイズやフィルタリングを行う場合は、C層を利用してください DarraUa_EventSubscription_SubscribeWithFilter
  • コールバックは Publish スレッド上で同期的に実行され、再処理はキューに委ねられる

多言語対照​

項目C#PythonJavaC++RustC
EventFilterEventFilterEventFilterEventFilterDef.FilterEventFilterEventFilterDarraUa_EventFilter
SimpleAttributeOperandSimpleAttributeOperandSimpleAttributeOperandEventFilterDef.OperandSimpleAttributeOperandSimpleAttributeOperandDarraUa_SimpleAttributeOperand
filter 付きイベントのサブスクリプションsubscribe(..., filter)subscribe(..., filter)subscribe(..., Filter)なしsubscribe(..., filter)DarraUa_EventSubscription_SubscribeWithFilter

Javaの型名は異なります:はい EventFilterDef.Filter / EventFilterDef.Operand(EventFilterDef.java)、いいえ EventFilter / SimpleAttributeOperand。C++ は、「filter 付きのイベントサブスクリプションのエントリポイント」を持たない唯一の言語である。

次のステップ​