跳到主要内容

History 历史访问

OPC UA 定义了 5 种 HistoryRead 模式, 本 SDK 全部实现.

服务端能力

服务端必须实现 Historian (历史档案) 才能返回数据, 否则统一返回 BadHistoryOperationUnsupported.

配套阅读

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(nodeId, startTime, endTime, maxValues)时间区间内全部原始数据
ReadHistoryModified(nodeId, start, end, maxValues, returnBounds)含修改记录的历史 (修改元信息当前不暴露)
ReadHistoryAtTime(nodeId, times, useSimpleBounds)指定离散时间点的内插值
ReadHistoryProcessed(nodeId, start, end, processingInterval, aggregateNodeId)聚合 (Average / Minimum / Maximum / Count / Total / TimeAverage / ...)
ReadHistoryEvents(nodeId, start, end, maxEvents)事件历史

详细参数与示例见 SDK History 章节.

HistoryUpdate — 写历史数据​

DataValue 的构造函数不对外暴露, 提交历史时把读回来的实例原样放进 values:

var values = ua.ReadHistory(
nodeId: "ns=2;s=Temperature",
startTime: DateTime.UtcNow.AddHours(-1),
endTime: DateTime.UtcNow);

// 原样提交 (Insert / Replace / Update)
var codes = ua.UpdateHistory(
"ns=2;s=Temperature", HistoryUpdateType.Update, values);

// 按 SourceTimestamp 删除 (Remove)
var del = ua.UpdateHistory(
"ns=2;s=Temperature", HistoryUpdateType.Remove, values);

DeleteHistoryRange — 删历史数据​

var st = ua.DeleteHistoryRange(
"ns=2;s=Temperature",
start: DateTime.UtcNow.AddDays(-30),
end: DateTime.UtcNow.AddDays(-7),
deleteModifiedToo: false);
HistoryUpdate / Delete 默认禁用

大多数生产 Server 默认禁止 HistoryUpdate / Delete, 需要管理员明确开启权限.

相关链接​