跳到主要内容

ReadHistoryProcessed

给区间 + 聚合函数 + 子区间宽度, 服务端返回每个子区间的聚合结果.

前置 / 配套

签名​

public IReadOnlyList<DataValue> ReadHistoryProcessed(
string nodeId,
DateTime start,
DateTime end,
TimeSpan processingInterval,
string aggregateNodeId);
参数类型默认说明
nodeIdstring—目标 NodeId;null / 空串抛 ArgumentNullException
startDateTime—区间起 (UTC)
endDateTime—区间止 (UTC)
processingIntervalTimeSpan—单个聚合区间长度;<= TimeSpan.Zero 抛 ArgumentOutOfRangeException
aggregateNodeIdstring—聚合函数 NodeId, 从 HistoryAggregate 常量里选;null / 空串抛 ArgumentNullException

返回 IReadOnlyList<DataValue>:每个子区间一个聚合值,条数由区间长度 ÷ processingInterval 决定,服务端可以少给。每个 DataValue 是 IDisposable, 必须 using 释放。

取数值用 dv.Value.AsDouble, 返回 double?;直接传 double 形参 = CS1503 编译不过。

HistoryAggregate 常量​

常量NodeId含义
HistoryAggregate.Averagei=2342算术平均
HistoryAggregate.Minimumi=2346最小值
HistoryAggregate.Maximumi=2347最大值
HistoryAggregate.Counti=2352区间内 Good 状态的数据点数量
HistoryAggregate.Totali=2348累加 (代数和)
HistoryAggregate.StandardDeviationSamplei=11427样本标准差 (n-1 分母)
HistoryAggregate.TimeAveragei=11285时间加权平均 (按 SamplingInterval 占比)
HistoryAggregate.Rangei=2353区间内 (最大值 − 最小值)
HistoryAggregate.Deltai=11286区间末 − 区间始

服务端不支持某个聚合时, 调用以 OpcUaException 失败返回, 状态码见 OpcUaException.StatusCode —— SDK 不改写服务端的码。

用法​

24 小时温度的每小时平均​

using System;
using DarraOpcUa_Client;

using var ua = new DarraOpcUa("opc.tcp://localhost:4840");
ua.Connect();

var values = ua.ReadHistoryProcessed(
nodeId: "ns=2;s=Temperature",
start: DateTime.UtcNow.Date.AddDays(-1),
end: DateTime.UtcNow.Date,
processingInterval: TimeSpan.FromHours(1),
aggregateNodeId: HistoryAggregate.Average);

foreach (var dv in values) using (dv) // 每个子区间一个聚合值 (此例 24 段)
Console.WriteLine($"{dv.SourceTimestamp:HH:mm}: {dv.Value}");

一周每天的最大温度​

var today = DateTime.UtcNow.Date;
var maxes = ua.ReadHistoryProcessed("ns=2;s=Temperature",
start: today.AddDays(-7), end: today,
processingInterval: TimeSpan.FromDays(1),
aggregateNodeId: HistoryAggregate.Maximum);

foreach (var dv in maxes) using (dv)
Console.WriteLine($"{dv.SourceTimestamp:yyyy-MM-dd}: {dv.Value}");

一小时累计流量​

var now = DateTime.UtcNow;
var totals = ua.ReadHistoryProcessed("ns=2;s=FlowRate",
start: now.AddHours(-1), end: now,
processingInterval: TimeSpan.FromHours(1),
aggregateNodeId: HistoryAggregate.Total);

foreach (var dv in totals) using (dv)
Console.WriteLine($"{dv.SourceTimestamp:HH:mm}: {dv.Value}");

异常​

异常触发条件
ArgumentNullExceptionnodeId 或 aggregateNodeId 为 null / 空串
ArgumentOutOfRangeExceptionprocessingInterval <= TimeSpan.Zero
OpcUaException服务端返回非 Good;ex.StatusCode 是服务端原样返回的码

实现限制​

  • 没有 CancellationToken —— 只有 ReadHistoryStreamAsync 带取消令牌。
  • 同步阻塞, 没有 Async 变体。
  • 会话级故障不自动重试。
  • 聚合由服务端算, 客户端不做二次计算;服务端是否真支持某个 Aggregate 由服务端决定。

性能优势​

  • 避免拉大量原始点再在客户端算
  • 跨日 / 跨周报表的标准方案

下一步​