跳到主要内容

历史流式读

概述​

IDE 趋势图拉历史时, 一次性把万点装进 std::vector 会让进程卡顿 + OOM。ReadHistoryStream 走回调模式 — 每读到一条同步回调一次, 业务侧拿一条处理一条; 需要整批结果时用 ReadHistoryStreamAsync 拿 std::future。两者都是 darra::opcua 命名空间的自由函数, 第一个参数传 Session&, 不是 Session 的成员方法。

对应规范段: OPC UA Part 11(Historical Access)§6.5。

API​

函数类别读写说明
ReadHistoryStream历史读回调式流式读, 每条同步回调一次
ReadHistoryStreamAsync历史读整批异步读, 返回 std::future<std::vector<DataValue>>
namespace darra::opcua {

// 回调式. on_value 对每条 DataValue 同步触发 (读取线程). 返回整体状态, 单点好坏由 dv.IsGood() 自查.
Status ReadHistoryStream(
Session& s,
std::string const& nid,
int64_t start_ft, int64_t end_ft,
std::function<void(DataValue const&)> on_value,
uint32_t batch_size = 1000,
std::shared_ptr<std::atomic<bool>> cancel = {});

// 异步整批. 在 future.get() 上一次性拿全部.
std::future<std::vector<DataValue>> ReadHistoryStreamAsync(
Session& s,
std::string const& nid,
int64_t start_ft, int64_t end_ft,
uint32_t batch_size = 1000);
}
参数默认说明
s—已连接的 Session
nid—历史节点 NodeId 字符串
start_ft—区间起 (FileTime, UTC 100ns since 1601)
end_ft—区间止; <= start_ft 时直接返回 Status::Good, 不回调
on_value—void(DataValue const&) 回调; 传空函数对象返回 Status::DarraInvalidArgument
batch_size1000一次最多拿多少点 (提示值, 见"实现限制")
cancel空std::shared_ptr<std::atomic<bool>>; 置 true 后停止回调

返回值: ReadHistoryStream 返回 Status (Status::Good / Status::DarraInvalidArgument), ReadHistoryStreamAsync 返回 std::future<std::vector<DataValue>>。回调里抛的异常被逐条吞掉、继续下一条, 业务侧要自查 dv.IsGood()。

代码示例​

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

using namespace darra::opcua;
using namespace std::chrono;

Session s("opc.tcp://localhost:4840");
s.Connect();

// 时间用 FileTime (UTC 100ns since 1601-01-01)
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
auto now_ns = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
int64_t t1 = WIN_EPOCH_OFFSET + now_ns / 100;
int64_t t0 = t1 - 24LL * 3600 * 10000000LL; // 24h 前

// 1) 边拉边渲染
ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) {
double d = 0;
if (dv.Value().TryGetDouble(d))
chartSeries->AddPoint(dv.SourceTimestamp(), d);
},
/*batch_size=*/1000);

// 2) 提前退出 — 别处置 cancel 标志, 读取循环逐条检查后停止
auto cancel = std::make_shared<std::atomic<bool>>(false);
stopButton->OnClicked = [cancel] { cancel->store(true); };

ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) { Process(dv); },
/*batch_size=*/1000, cancel);

// 3) 计数坏值
size_t bad = 0;
ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) {
if (!dv.IsGood()) ++bad;
},
/*batch_size=*/1000);
std::cout << "24h 内 " << bad << " 个坏值\n";

// 4) 需要整批时用 future 版
auto fut = ReadHistoryStreamAsync(s, "ns=2;s=Temp", t0, t1);
auto values = fut.get(); // 阻塞拿全部

实现限制​

  • 当前 Session::ReadHistory 不暴露 ContinuationPoint, 一次 RPC 返回区间内所有点 (受 batch_size 限)
  • 大区间业务侧自行切片
  • batch_size 只是"调用方一次最多拿多少"的提示, 一次调用仍取回所有点
  • 提前退出 = 把 cancel 置 true, 循环停止回调后返回 Status::Good; 回调签名是 void, 没有"返回 false 退出"这回事

最佳实践​

  • 回调在读取线程上同步执行, 回调里的长操作会拖慢整轮读取 — 重活丢给工作线程或队列
  • 区间 > 1 小时建议外层切片
  • UI 渲染节流: 累积 100 条再 refresh

跨语言对照​

C#PythonJavaC++RustC
ReadHistoryStreamAsyncread_history_streamreadHistoryStreamReadHistoryStreamread_history_streamDarraUa_HistoryStream_New/_Next/_IsEnd/_Free

下一步​