跳到主要内容

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):

常量NodeId含义
HistoryAggregate::Average"i=2342"算术平均
HistoryAggregate::TimeAverage"i=2343"时间加权平均
HistoryAggregate::Total"i=2344"累计总和
HistoryAggregate::Minimum"i=2346"最小值
HistoryAggregate::Maximum"i=2347"最大值
HistoryAggregate::MinimumActualTime"i=2348"最小值出现时间
HistoryAggregate::MaximumActualTime"i=2349"最大值出现时间
HistoryAggregate::Range"i=2350"Max − Min
HistoryAggregate::Count"i=2352"点数
HistoryAggregate::NumberOfTransitions"i=2355"状态变化次数
HistoryAggregate::Interpolative"i=2341"插值

完整列表见 OPC UA 聚合函数标准.


用法

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 列出支持的)
  • 不支持的聚合返回 Status::BadAggregateNotSupported

下一步