EventFilter
概要
OPC UA Part 4 §7.18 EventFilter:
- select_clauses — どのフィールドを抽出するか
- where_clause — フィルタ条件(V1 NULLのままにしておいてください)
DarraUa_SimpleAttributeOperand フィールド: type_definition_id / browse_path / attribute_id / index_range。
イベントフィルタリング API は SDK 拡張ヘッダー(プロトコルスタック ABI ヘッダーではない)で行われます:
#include <opcua/opcua.h>
#include "../include/opcua_filter.h"
API
typedef struct {
/** イベントタイプ NodeId 文字列(例:「i=2041」などBaseEventType)。 NULL = ワイルドカード BaseEventType. */
const char* type_definition_id;
/** フィールド BrowsePath (各セグメント BrowseName). NULL または count == 0 は、イベント全体を選択することを意味します。 */
const char* const* browse_path;
size_t browse_path_count;
/** 属性 ID、デフォルト UA_ATTR_VALUE. */
DarraUa_AttributeId attribute_id;
/** 配列のインデックス範囲 (NumericRange 文字列). NULL = 設定なし. */
const char* index_range;
} DarraUa_SimpleAttributeOperand;
typedef struct {
const DarraUa_SimpleAttributeOperand* select_clauses;
size_t select_clauses_count;
const char* where_clause; /* NULL = WHERE なし */
} DarraUa_EventFilter;
| 関数 | カテゴリ | 読み書き | 説明 |
|---|---|---|---|
| DarraUa_EventSubscription_SubscribeWithFilter(sub, object_node_id, filter, cb, user_context, out_mi) | 関数 | 書く | カスタムフィルターを使用してイベント MI を作成する |
UA_SDK_API DarraUa_Status UA_SDK_CALL DarraUa_EventSubscription_SubscribeWithFilter(
DarraUa_SubscriptionHandle sub,
const char* object_node_id, /* NULL = "i=2253" */
const DarraUa_EventFilter* filter, /* NULL = デフォルトの4つのフィールドを使用する */
DarraUa_OnEvent cb,
void* user_context,
DarraUa_MonitoredItemHandle* out_mi);
イベントコールバックの署名については、以下を参照してください。 イベントの購読 の DarraUa_OnEvent。
コード例
#include <opcua/opcua.h>
#include "../include/opcua_filter.h"
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://localhost:4840";
cfg.security_mode = UA_MSG_SECURITY_MODE_NONE;
cfg.security_policy_uri = "http://opcfoundation.org/UA/SecurityPolicy#None";
cfg.user_token_type = UA_USER_TOKEN_ANONYMOUS;
DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
DarraUa_Session_Create(&cfg, &h);
DarraUa_Session_Connect(h);
DarraUa_SubscriptionConfig sc;
DarraUa_SubscriptionConfig_Init(&sc);
sc.publishing_interval_ms = 500.0;
sc.publishing_enabled = 1;
DarraUa_SubscriptionHandle sub = UA_INVALID_SUBSCRIPTION_HANDLE;
DarraUa_Subscription_Create(h, &sc, &sub);
// filter の構築
const char* event_id_path[] = { "EventId" };
const char* source_path[] = { "SourceName" };
const char* time_path[] = { "Time" };
const char* msg_path[] = { "Message" };
const char* sev_path[] = { "Severity" };
const char* cond_id_path[] = { "ConditionId" };
const char* active_state_path[] = { "ActiveState", "Id" };
DarraUa_SimpleAttributeOperand selects[7] = {
{ "i=2041", event_id_path, 1, UA_ATTR_VALUE, NULL },
{ "i=2041", source_path, 1, UA_ATTR_VALUE, NULL },
{ "i=2041", time_path, 1, UA_ATTR_VALUE, NULL },
{ "i=2041", msg_path, 1, UA_ATTR_VALUE, NULL },
{ "i=2041", sev_path, 1, UA_ATTR_VALUE, NULL },
{ "i=2782", cond_id_path, 1, UA_ATTR_VALUE, NULL },
{ "i=2915", active_state_path, 2, UA_ATTR_VALUE, NULL },
};
DarraUa_EventFilter filter = {
.select_clauses = selects,
.select_clauses_count = 7,
.where_clause = NULL,
};
DarraUa_MonitoredItemHandle ev_mi = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_Status st = DarraUa_EventSubscription_SubscribeWithFilter(
sub, "i=2253", &filter, on_event, NULL, &ev_mi);
on_event の DarraUa_Event ペイロードフィールドは SDK が固定デコードする
severity / message / source_name / time / event_type、各項 select_clauses
の展開には依存しない。カスタム select_clauses はサーバーがどのフィールドを送信するかを決定する。
フィールドのクイックリファレンス
| EventType | BrowsePath | 意味 |
|---|---|---|
| BaseEventType (i=2041) | ["EventId"] | イベントの固有ID |
| BaseEventType | ["Severity"] | 深刻度 1~1000 |
| ConditionType (i=2782) | ["ConditionId"] | 条件 NodeId |
| AcknowledgeableConditionType (i=2881) | ["AckedState", "Id"] | Ack かどうか |
| AlarmConditionType (i=2915) | ["ActiveState", "Id"] | 有効化されていますか |
実装の説明
- すべてのフィールドは borrowed、RPC エントリ前に SDK がディープコピーする。
SubscribeWithFilterが戻った後、struct を解放するか、スコープ外に出す。 - Stack はカスタム SelectClauses 非対応時にグレースフルに降格: デフォルト 4 フィールド (Severity / Message / SourceName / Time) にフォールバックし、info ログを出力する
ベストプラクティス
- フィールドを明示的に列挙する
- アラームパネルでは必ず ConditionId を取得すること
多言語対照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| EventFilter | EventFilter | EventFilter | EventFilter | EventFilter | DarraUa_EventFilter |
| SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | SimpleAttributeOperand | DarraUa_SimpleAttributeOperand |