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) | 函数 | 写 | 带自定义 filter 建事件 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, SDK 在 RPC 入口前深拷贝;
SubscribeWithFilter返回后 struct 即可释放 / 出作用域 - Stack 不支持自定义 SelectClauses 时 graceful 降级: 回退默认 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 |