跳到主要内容

History 历史

Session 提供历史读 5 模式 + 历史写 / 删. 时间一律用 FileTime (UTC 100ns since 1601-01-01), 与 Windows DateTime.ToFileTimeUtc() 一致.

子页跳转

FileTime 常量​

constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;  // 1601 → 1970 间隔

// 当前时刻的 FileTime
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;

// 1 小时前
int64_t one_hour_ago = now_ft - 3600LL * 10000000LL;

5 种 HistoryRead 模式​

模式API说明
Raw 原始数据ReadHistory(...)区间内全部点
Modified 含修改记录ReadHistoryModified(...)区间内含修改记录
AtTime 指定时间点ReadHistoryAtTime(...)给定时间戳的内插值
Processed 聚合ReadHistoryProcessed(...)聚合 (Avg / Min / Max / ...)
Events 事件历史ReadHistoryEvents(...)历史事件 (报警 / 条件)

HistoryUpdate​

API用途
UpdateHistoryInsert / Replace / Update / Remove 历史数据
DeleteHistoryRange按时间段删除

通用前提​

  • 服务端必须支持 Historian。不支持时服务端回 BadHistoryOperationUnsupported, 也可能回 BadNotSupported / BadNotReadable / BadCommunicationError 等状态码。这些是服务端的原始 StatusCode, 以 uint32_t 带出; Status 枚举只收了常用的一部分 (含 BadHistoryOperationUnsupported / BadNotReadable / BadCommunicationError, 不含 BadNotSupported), 没收录的用 e.status_code() 读原始值, 不要写成 Status::BadNotSupported 这种编译不过的枚举名。
  • 除 DeleteHistoryRange 外的调用失败都抛 Exception, 用 e.status_code() (uint32_t) 或 e.status() (Status 枚举) 取状态码; DeleteHistoryRange 直接把 Status 当返回值, 不抛。
  • 节点 AccessLevel Attribute (17) 要有历史权限: 第 2 位 (0x04) = HistoryRead, 第 3 位 (0x08) = HistoryWrite (与 0x01 当前读 / 0x02 当前写 同一套掩码)。
  • 节点 Historizing Attribute (20) = 服务端此刻是否正在记录历史。

下一步​