ReadHistoryAtTime
给一组离散时间点 (FileTime), 服务端按 "前一个 / 后一个 / 线性插值" 算法返回那个时刻的值. 用于"对齐时序":
前置 / 配套
- 想拉聚合统计请用 ReadHistoryProcessed.
- 想拉区间所有原始点请用 ReadHistory (Raw).
签名
std::vector<DataValue> ReadHistoryAtTime(
std::string const& node_id,
std::vector<int64_t> const& req_times,
bool use_simple_bounds = true);
| 参数 | 说明 |
|---|---|
node_id | 目标 NodeId |
req_times | 离散时间点列表 (FileTime) |
use_simple_bounds | true = 阶梯型 (用 ≤ t 的最近点); false = 线性插值 |
用法
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
// 拉过去 24 小时, 每整点的温度
std::vector<int64_t> hours;
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
int64_t now_ft = WIN_EPOCH_OFFSET + now_ns / 100;
// 取整到当前小时
int64_t hour_ft = now_ft - (now_ft % (3600LL * 10000000LL));
for (int h = -24; h <= 0; ++h) {
hours.push_back(hour_ft + h * 3600LL * 10000000LL);
}
auto values = s.ReadHistoryAtTime("ns=2;s=Temperature", hours);
for (size_t i = 0; i < hours.size(); ++i) {
std::cout << "ft=" << hours[i] << " val=" << values[i].Value().AsString() << "\n";
}
与 ReadHistoryProcessed 的区别
| API | 返回 |
|---|---|
ReadHistoryAtTime | 每个时间点对应一个内插值 (不聚合) |
ReadHistoryProcessed | 每个子区间一个聚合值 (Avg / Min / Max / ...) |
例 "拉 24 小时, 每小时一个值":
ReadHistoryAtTime: 输入 25 个整点, 返回 25 个内插点ReadHistoryProcessed: aggregate=Average, interval=1 h, 返回 24 个区间的平均值
应用场景
- 时序对齐: 不同传感器采样时间不同, 要拉到同一时刻才能比较
- 数据库导出: 整点抽样
- 报表: 每天 24 行