History 历史访问
OPC UA 定义了 5 种 HistoryRead 模式, 本 SDK 全部实现.
服务端能力
服务端必须实现 Historian (历史档案) 才能返回数据, 否则统一返回 BadHistoryOperationUnsupported.
配套阅读
- 基础知识 — 历史模型 — 概念
- 配套规范 — HA Historical Access — 聚合函数 NodeId
- 示例 — 历史数据导出 — 完整代码
ReadHistory — 时间区间内的原始数据
var dvs = ua.ReadHistory(
nodeId: "ns=2;s=Temperature",
startTime: DateTime.UtcNow.AddHours(-1),
endTime: DateTime.UtcNow,
maxValues: 1000);
foreach (var dv in dvs)
Console.WriteLine($"{dv.SourceTimestamp}: {dv.Value} ({dv.Status})");
5 种模式速览
| API | 用途 |
|---|---|
ReadHistory(...) 或 History.ReadRaw(...) | 时间区间内全部原始数据 |
History.ReadModified(...) | 含修改记录 (谁改的, 何时改的) |
History.ReadAtTime(times[]) | 指定离散时间点的内插值 |
History.ReadProcessed(aggregate, interval, ...) | 聚合 (Avg/Min/Max/Sum/Count/...) |
History.ReadEvents(filter, ...) | 事件历史 |
详细参数与示例见 SDK History 章节.
HistoryUpdate — 写历史数据
ua.History.UpdateData("ns=2;s=Temperature", new[]
{
new DataValue(new Variant(25.3), StatusCode.Good, sourceTimestamp: DateTime.UtcNow.AddSeconds(-10)),
new DataValue(new Variant(25.7), StatusCode.Good, sourceTimestamp: DateTime.UtcNow.AddSeconds(-5)),
});
DeleteHistoryRange — 删历史数据
ua.History.DeleteRange("ns=2;s=Temperature",
startTime: DateTime.UtcNow.AddDays(-30),
endTime: DateTime.UtcNow.AddDays(-7));
HistoryUpdate / Delete 默认禁用
大多数生产 Server 默认禁止 HistoryUpdate / Delete, 需要管理员明确开启权限.