Repository navigation
Expand file tree
/
Copy pathHistoryReader.cs
More file actions
425 lines (411 loc) · 15.5 KB
/
Copy pathHistoryReader.cs
File metadata and controls
425 lines (411 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// SPDX-FileCopyrightText: Copyright (c) 2005-2026, OPC Federation AISBL
// SPDX-License-Identifier: MIT
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client;
namespace UaLens.Plugins.Historian;
/// <summary>
/// Thin façade over <see cref="ISession.HistoryReadAsync"/> for the three
/// flavours of history read used by the Historian tab. Each method
/// loops on the returned continuation point until the server signals
/// the end of the dataset (CP is null/empty).
/// </summary>
/// <remarks>
/// On failure or cancellation we issue one final HistoryRead with
/// <c>releaseContinuationPoints: true</c> so any outstanding server-side
/// cursor is freed. Failures of that release call are intentionally
/// swallowed to preserve the original outcome. Cleanup has an independent
/// five-second cancellation deadline.
/// </remarks>
internal sealed class HistoryReader
{
private readonly ISession m_session;
private readonly TimeSpan m_cleanupTimeout;
public HistoryReader(ISession session, TimeSpan? cleanupTimeout = null)
{
m_session = session ?? throw new ArgumentNullException(nameof(session));
m_cleanupTimeout = cleanupTimeout ?? TimeSpan.FromSeconds(5);
}
/// <summary>Reads raw or modified history. See Part 11 §6.4.3.</summary>
public Task<List<HistoryRow>> ReadRawAsync(
NodeId nodeId,
DateTime startUtc,
DateTime endUtc,
bool returnBounds,
bool isReadModified,
uint numValuesPerNode,
CancellationToken ct)
{
var details = new ReadRawModifiedDetails
{
StartTime = startUtc,
EndTime = endUtc,
NumValuesPerNode = numValuesPerNode,
IsReadModified = isReadModified,
ReturnBounds = returnBounds
};
return ReadLoopAsync(nodeId, new ExtensionObject(details), ct);
}
/// <summary>Reads aggregated (processed) history. See Part 11 §6.4.4.</summary>
public Task<List<HistoryRow>> ReadProcessedAsync(
NodeId nodeId,
NodeId aggregateType,
DateTime startUtc,
DateTime endUtc,
double processingIntervalMs,
CancellationToken ct)
{
var aggregateArray = new NodeId[] { aggregateType };
var details = new ReadProcessedDetails
{
StartTime = startUtc,
EndTime = endUtc,
ProcessingInterval = processingIntervalMs,
AggregateType = aggregateArray,
AggregateConfiguration = new AggregateConfiguration()
};
return ReadLoopAsync(nodeId, new ExtensionObject(details), ct);
}
/// <summary>Reads values at specified timestamps. See Part 11 §6.4.5.</summary>
public Task<List<HistoryRow>> ReadAtTimeAsync(
NodeId nodeId,
IReadOnlyList<DateTime> reqTimes,
CancellationToken ct)
{
var timesArray = new DateTimeUtc[reqTimes.Count];
for (int i = 0; i < reqTimes.Count; i++)
{
timesArray[i] = reqTimes[i];
}
var details = new ReadAtTimeDetails
{
ReqTimes = timesArray,
UseSimpleBounds = true
};
return ReadLoopAsync(nodeId, new ExtensionObject(details), ct);
}
/// <summary>
/// Best-effort: resolves the standard <c>Annotations</c> property of
/// the historizing variable (Part 11 §5.4.5) and, when present, reads
/// the annotation history covering the source timestamps of the given
/// rows. Each <see cref="HistoryRow.Annotation"/> is populated with
/// the first annotation whose <see cref="Annotation.AnnotationTime"/>
/// matches the row's <see cref="HistoryRow.SourceTimestamp"/> (UTC,
/// millisecond precision). Servers that do not expose the
/// <c>Annotations</c> property or that fail the secondary read are
/// skipped — annotations are optional. Cancellation is not an optional-feature
/// failure and always propagates to the caller.
/// </summary>
public async Task AttachAnnotationsAsync(
NodeId variableNodeId,
IReadOnlyList<HistoryRow> rows,
CancellationToken ct)
{
ct.ThrowIfCancellationRequested();
if (variableNodeId.IsNull || rows.Count == 0)
{
return;
}
NodeId annotationsNodeId = await ResolveAnnotationsNodeIdAsync(variableNodeId, ct).ConfigureAwait(false);
ct.ThrowIfCancellationRequested();
if (annotationsNodeId.IsNull)
{
return;
}
NodeId annNode = annotationsNodeId;
DateTime minTs = DateTime.MaxValue;
DateTime maxTs = DateTime.MinValue;
foreach (HistoryRow r in rows)
{
DateTime ts = r.SourceTimestamp.Kind == DateTimeKind.Utc
? r.SourceTimestamp
: r.SourceTimestamp.ToUniversalTime();
if (ts < minTs)
{
minTs = ts;
}
if (ts > maxTs)
{
maxTs = ts;
}
}
if (minTs == DateTime.MaxValue)
{
return;
}
// Pad the range by one millisecond on each side so servers that
// treat StartTime/EndTime as exclusive still return the bounding
// annotations.
DateTime start = minTs.AddMilliseconds(-1);
DateTime end = maxTs.AddMilliseconds(1);
List<DataValue> annotationValues;
try
{
annotationValues = await ReadAnnotationDataValuesAsync(annNode, start, end, ct)
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception)
{
// Best-effort — server doesn't support annotations or read
// failed for an unrelated reason. Annotation column simply
// stays empty for this batch.
ct.ThrowIfCancellationRequested();
return;
}
ct.ThrowIfCancellationRequested();
if (annotationValues.Count == 0)
{
return;
}
// Build a lookup keyed by 100ns-truncated source ticks so an
// ordinary equality match suffices. Annotations whose
// AnnotationTime aligns with a known row are projected onto the
// row; orphan annotations are ignored.
var byTick = new Dictionary<long, HistoryRow>(rows.Count);
foreach (HistoryRow r in rows)
{
DateTime ts = r.SourceTimestamp.Kind == DateTimeKind.Utc
? r.SourceTimestamp
: r.SourceTimestamp.ToUniversalTime();
byTick[ts.Ticks] = r;
}
foreach (DataValue dv in annotationValues)
{
if (dv.WrappedValue.IsNull)
{
continue;
}
Annotation? ann = ExtractAnnotation(dv.WrappedValue);
if (ann is null)
{
continue;
}
// The DataValue's SourceTimestamp identifies the *data point*
// being annotated (per Part 11 §5.4.5); the inner
// Annotation.AnnotationTime is when the annotation itself
// was created, which is not what we want to match on.
DateTime key = (DateTime)dv.SourceTimestamp;
if (key.Kind != DateTimeKind.Utc)
{
key = key.ToUniversalTime();
}
if (byTick.TryGetValue(key.Ticks, out HistoryRow? matched))
{
matched.Annotation = ann;
}
}
}
private async Task<NodeId> ResolveAnnotationsNodeIdAsync(
NodeId variableNodeId, CancellationToken ct)
{
try
{
var element = new RelativePathElement
{
ReferenceTypeId = ReferenceTypeIds.HasProperty,
IsInverse = false,
IncludeSubtypes = false,
TargetName = new QualifiedName(BrowseNames.Annotations)
};
var browsePath = new BrowsePath
{
StartingNode = variableNodeId,
RelativePath = new RelativePath { Elements = [element] }
};
var browsePaths = new List<BrowsePath> { browsePath }.ToArrayOf();
TranslateBrowsePathsToNodeIdsResponse resp = await m_session
.TranslateBrowsePathsToNodeIdsAsync(null, browsePaths, ct)
.ConfigureAwait(false);
if (resp.Results.Count == 0)
{
return NodeId.Null;
}
BrowsePathResult r = resp.Results[0];
if (StatusCode.IsBad(r.StatusCode) || r.Targets is not { Count: > 0 } targets)
{
return NodeId.Null;
}
NodeId mapped = ExpandedNodeId.ToNodeId(targets[0].TargetId, m_session.NamespaceUris);
return mapped;
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception)
{
ct.ThrowIfCancellationRequested();
return NodeId.Null;
}
}
private async Task<List<DataValue>> ReadAnnotationDataValuesAsync(
NodeId annotationsNodeId,
DateTime startUtc,
DateTime endUtc,
CancellationToken ct)
{
var details = new ReadRawModifiedDetails
{
StartTime = startUtc,
EndTime = endUtc,
NumValuesPerNode = 0,
IsReadModified = false,
ReturnBounds = false
};
var detailsObject = new ExtensionObject(details);
var results = new List<DataValue>();
await foreach (HistoryReadResult result in ReadPagesAsync(
annotationsNodeId, detailsObject, TimestampsToReturn.Source, ct).ConfigureAwait(false))
{
if (result.HistoryData.TryGetValue(out HistoryData? hd) && hd is not null)
{
foreach (DataValue dv in hd.DataValues)
{
results.Add(dv);
}
}
}
return results;
}
private static Annotation? ExtractAnnotation(Variant v)
{
if (v.TryGetValue(out ExtensionObject extension) &&
extension.TryGetValue(out Annotation? annotation))
{
return annotation;
}
if (v.TryGetValue(out ArrayOf<ExtensionObject> extensions))
{
foreach (ExtensionObject item in extensions)
{
if (item.TryGetValue(out Annotation? match) && match is not null)
{
return match;
}
}
}
return null;
}
private async Task<List<HistoryRow>> ReadLoopAsync(
NodeId nodeId,
ExtensionObject historyReadDetails,
CancellationToken ct)
{
var rows = new List<HistoryRow>();
await foreach (HistoryReadResult result in ReadPagesAsync(
nodeId, historyReadDetails, TimestampsToReturn.Both, ct).ConfigureAwait(false))
{
ArrayOf<DataValue> values = default;
if (result.HistoryData.TryGetValue(out HistoryModifiedData? modified) && modified is not null)
{
values = modified.DataValues;
}
else if (result.HistoryData.TryGetValue(out HistoryData? data) && data is not null)
{
values = data.DataValues;
}
foreach (DataValue value in values)
{
rows.Add(new HistoryRow(
(DateTime)value.SourceTimestamp, (DateTime)value.ServerTimestamp,
value.WrappedValue, value.StatusCode));
}
}
return rows;
}
private async IAsyncEnumerable<HistoryReadResult> ReadPagesAsync(
NodeId nodeId,
ExtensionObject historyReadDetails,
TimestampsToReturn timestamps,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
var outstanding = new List<ByteString>();
ByteString continuationPoint = default;
try
{
do
{
cancellationToken.ThrowIfCancellationRequested();
HistoryReadResponse response = await m_session.HistoryReadAsync(
null, historyReadDetails, timestamps, false,
[new HistoryReadValueId { NodeId = nodeId, ContinuationPoint = continuationPoint }],
cancellationToken).ConfigureAwait(false);
bool good = response.ResponseHeader is not null &&
StatusCode.IsGood(response.ResponseHeader.ServiceResult) && response.Results.Count == 1 &&
StatusCode.IsGood(response.Results[0].StatusCode);
if (good)
{
outstanding.Clear();
}
foreach (HistoryReadResult result in response.Results)
{
if (!result.ContinuationPoint.IsNull && result.ContinuationPoint.Length != 0 &&
!outstanding.Contains(result.ContinuationPoint))
{
outstanding.Add(result.ContinuationPoint);
}
}
cancellationToken.ThrowIfCancellationRequested();
if (!good)
{
StatusCode status = response.ResponseHeader is not null &&
StatusCode.IsBad(response.ResponseHeader.ServiceResult)
? response.ResponseHeader.ServiceResult
: response.Results.Count == 1
? response.Results[0].StatusCode
: StatusCodes.BadUnexpectedError;
throw new ServiceResultException(
StatusCode.IsBad(status) ? status : StatusCodes.BadUnexpectedError);
}
HistoryReadResult page = response.Results[0];
continuationPoint = page.ContinuationPoint;
yield return page;
}
while (!continuationPoint.IsNull && continuationPoint.Length != 0);
}
finally
{
await TryReleaseAsync(nodeId, historyReadDetails, timestamps, outstanding).ConfigureAwait(false);
}
}
private async Task TryReleaseAsync(
NodeId nodeId,
ExtensionObject historyReadDetails,
TimestampsToReturn timestamps,
List<ByteString> continuationPoints)
{
if (continuationPoints.Count == 0)
{
return;
}
try
{
using var cleanup = new CancellationTokenSource(m_cleanupTimeout);
var nodesToRead = new List<HistoryReadValueId>(continuationPoints.Count);
foreach (ByteString continuationPoint in continuationPoints)
{
nodesToRead.Add(new HistoryReadValueId { NodeId = nodeId, ContinuationPoint = continuationPoint });
}
await m_session.HistoryReadAsync(
requestHeader: null,
historyReadDetails: historyReadDetails,
timestampsToReturn: timestamps,
releaseContinuationPoints: true,
nodesToRead: [.. nodesToRead],
ct: cleanup.Token).AsTask().WaitAsync(cleanup.Token).ConfigureAwait(false);
}
catch (Exception)
{
// Best-effort release — the server will GC the cursor on its
// own timeout if we fail to free it explicitly.
}
}
}