EventFilter
概要
OPC UA Part 4 §7.18 EventFilter:
- select_clauses — どのフィールドを抽出するか
- where_clause — フィルタ条件(現在未実装のため、空欄のままにしてください)
SimpleAttributeOperand フィールド: type_definition_id / browse_path / attribute_id / index_range。
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() といった値の取得方法もありません:
| フィールド | 種類 | 説明 |
|---|---|---|
| severity | uint16_t | 深刻度 1~1000 |
| message | std::string | LocalizedText.text |
| source_name | std::string | |
| time | int64_t | FileTime(UTC 100ns since 1601) |
| event_type | std::string | 現在、恒は空文字列です(SelectClauses 除く EventType) |
以下は、参照用の OPC UA 標準の BaseEventType / ConditionType フィールド表です。これら 4 つ以外のフィールドは、現時点では SDK では取得できません:
| EventType | BrowsePath | 意味 | 現時点で捕まえることができるか |
|---|---|---|---|
| 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# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|---|
| EventFilter | EventFilter | EventFilter | EventFilterDef.Filter | EventFilter | EventFilter | DarraUa_EventFilter |
| SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | EventFilterDef.Operand | SimpleAttributeOperand | SimpleAttributeOperand | DarraUa_SimpleAttributeOperand |
| filter 付きイベントのサブスクリプション | subscribe(..., filter) | subscribe(..., filter) | subscribe(..., Filter) | なし | subscribe(..., filter) | DarraUa_EventSubscription_SubscribeWithFilter |
Javaの型名は異なります:はい EventFilterDef.Filter / EventFilterDef.Operand(EventFilterDef.java)、いいえ EventFilter / SimpleAttributeOperand。C++ は、「filter 付きのイベントサブスクリプションのエントリポイント」を持たない唯一の言語である。