ReadHistoryEvents
如果服务端记录事件历史, 客户端可以按时间区间拉历史事件.
配套
- 实时事件订阅请用 事件订阅.
- 历史变量数据请用 ReadHistory (Raw) / ReadHistoryProcessed.
签名
以下原型是 Session 的成员方法。
std::vector<EventArrivedEventArgs> ReadHistoryEvents(
std::string const& node_id,
int64_t start_ft, int64_t end_ft,
uint32_t max_events = 0);
| 参数 | 默认 | 说明 |
|---|---|---|
| node_id | — | 事件源节点 NodeId, 通常是 Server i=2253 |
| start_ft | — | 区间起 (FileTime) |
| end_ft | — | 区间止 (FileTime) |
| max_events | 0 | 最多返回多少条; 0 = 不限 (服务端仍可自行设上限) |
EventArrivedEventArgs
struct EventArrivedEventArgs {
uint16_t severity = 0; // 1-1000
std::string message;
std::string source_name;
int64_t time = 0; // FileTime (UTC 100ns since 1601)
std::string event_type;
};
服务端返回的每条事件已经在 SDK 层抽取成扁平结构。内置 SelectClause 固定 4 个字段: Severity / Message / SourceName / Time; 过滤条件留空 = 区间内全部事件都返回。
event_type 在历史读里恒为空串 —— 它不在这 4 个 SelectClause 里, 历史路径不解 EventType (默认事件订阅同样不解, 见 事件订阅)。
用法
#include <darra/opcua/client.hpp>
#include <chrono>
#include <iostream>
using namespace darra::opcua;
Session s("opc.tcp://localhost:4840");
s.Connect();
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int64_t end_ft = WIN_EPOCH_OFFSET + now_ns / 100;
int64_t start_ft = end_ft - 24LL * 3600 * 10000000;
auto events = s.ReadHistoryEvents("i=2253", start_ft, end_ft);
for (auto const& e : events) {
std::cout << "[" << e.severity << "] " << e.source_name
<< ": " << e.message
<< " ft=" << e.time << "\n";
// e.event_type 历史读下恒为空串, 见上文
}
自定义过滤
ReadHistoryEvents 没有 EventFilter 参数 —— 抓哪些字段、过滤什么都是固定的: SelectClause 4 字段, 过滤条件留空 (全部事件)。区间内的报警 / 条件都回来, 业务侧自己再按 Severity / SourceName 筛。
要自定义 SelectClause 或过滤条件, 走事件订阅那条路: C API DarraUa_EventSubscription_SubscribeWithFilter 传自定义 EventFilter。C++ 侧 darra/opcua/event_filter.hpp 提供 EventFilter / SimpleAttributeOperand 类型定义, 但 Session 目前不接收这些类型, 该文件的 WhereClause 也未实现 (留空即可)。
服务端要求
- 服务端必须支持 HistoryRead Events (ReadEventsDetails)
- 事件源节点的
EventNotifierAttribute (12) 第 1 位 (0x02, HistoryRead) 必须置位
与实时事件订阅的关系
| API | 拿什么 |
|---|---|
| s.SubscribeEvents(...) | 实时事件 (订阅期间发生的) |
| s.ReadHistoryEvents(...) | 历史事件 (服务端归档的) |
通常组合用: 启动时 ReadHistoryEvents 拉过去 24 小时, 然后 SubscribeEvents 接管实时.