跳到主要内容

ReadHistoryProcessed (聚合)

HistoryRead 的聚合模式, 配合标准聚合函数. 给区间 + 聚合函数 + 子区间宽度, 服务端返回每个子区间的聚合结果.

前置 / 配套
  • 拉所有原始点请用 ReadHistory (Raw).
  • 指定时间点的内插值请用 ReadHistoryAtTime.
  • 聚合函数 NodeId 常量见 darra/opcua/history.hpp 中的 HistoryAggregate.

签名​

std::vector<DataValue> ReadHistoryProcessed(
std::string const& node_id,
int64_t start_ft, int64_t end_ft,
double processing_interval_ms,
std::string const& aggregate_node_id);

标准聚合函数 NodeId (ns=0)​

HistoryAggregate 常量 (在 darra/opcua/history.hpp), 共 9 个:

常量NodeId含义
HistoryAggregate::Average"i=2342"区间内值的算术平均
HistoryAggregate::Minimum"i=2346"区间内最小值
HistoryAggregate::Maximum"i=2347"区间内最大值
HistoryAggregate::Count"i=2352"区间内 Good 状态的数据点数量
HistoryAggregate::Total"i=2348"区间内值的累加 (代数和)
HistoryAggregate::StandardDeviationSample"i=11427"样本标准差 (Sample), n-1 分母
HistoryAggregate::TimeAverage"i=11285"时间加权平均 (按 SamplingInterval 占比)
HistoryAggregate::Range"i=2353"区间内 (Max - Min)
HistoryAggregate::Delta"i=11286"区间末 − 区间始

常量类型是 inline constexpr const char*, 直接作为 aggregate_node_id 传入。

用法​

24 小时温度的每小时平均​

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 values = s.ReadHistoryProcessed(
"ns=2;s=Temperature",
start_ft, end_ft,
/*processing_interval_ms=*/3600000.0, // 1 hour
HistoryAggregate::Average);
// 返回 24 个 DataValue, 每个是该小时的平均值

for (size_t i = 0; i < values.size(); ++i) {
std::cout << "hour " << i << " avg = " << values[i].Value().AsString() << "\n";
}

一周每天的最大温度​

auto maxes = s.ReadHistoryProcessed("ns=2;s=Temperature",
start_ft, end_ft,
/*processing_interval_ms=*/86400000.0, // 1 day
HistoryAggregate::Maximum);

一小时累计流量​

auto totals = s.ReadHistoryProcessed("ns=2;s=FlowRate",
start_ft, end_ft,
/*processing_interval_ms=*/3600000.0,
HistoryAggregate::Total);

性能优势​

  • 省掉原始点的传输与客户端二次计算
  • 服务端直接在历史库上做聚合
  • 跨日 / 跨周报表的标准方案

服务端要求​

  • Historian 必须实现该聚合函数 (服务端 ServerCapabilities/AggregateFunctions 列出支持的)
  • 不支持的聚合不走返回值: ReadHistoryProcessed 整体失败一律抛 darra::opcua::Exception, 用 ex.status() 取码 (如 Status::BadHistoryOperationUnsupported)

下一步​