Files
histsdk/src/AVEVA.Historian.Client/Protocol/Historian2020ProtocolDialect.cs
T
dohertj2 c95824a65d Initial commit: managed .NET 10 AVEVA Historian SDK + reverse-engineering toolkit
Full read-only SDK (src/AVEVA.Historian.Client) implementing the CLAUDE.md required
surface against AVEVA Historian's binary WCF protocol — no native AVEVA runtime
dependency. All operations live-verified against a local Historian:

- ProbeAsync, ReadRawAsync, ReadAggregateAsync, ReadAtTimeAsync, ReadEventsAsync
- BrowseTagNamesAsync, GetTagMetadataAsync (17 native data-type codes mapped)
- GetConnectionStatusAsync, GetStoreForwardStatusAsync, GetSystemParameterAsync
- 108/108 unit + integration tests pass

Includes the reverse-engineering toolkit (tools/AVEVA.Historian.ReverseEngineering)
used to decode the protocol: WCF probes, IL inspection via dnlib, and IL-rewrite
instrumentation (instrument-wcf-{write,read}message etc.) plus the .NET Framework
trace harness (tools/AVEVA.Historian.NativeTraceHarness) for parity testing.

Sanitized handoff evidence under docs/reverse-engineering/. Native AVEVA binaries
(current/, aveva-install-x64/, aveva-install-x86/) are gitignored — fetch separately
from the AVEVA installer.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 06:31:48 -04:00

164 lines
7.1 KiB
C#

using System.Runtime.InteropServices;
using AVEVA.Historian.Client.Models;
using AVEVA.Historian.Client.Wcf;
namespace AVEVA.Historian.Client.Protocol;
internal sealed class Historian2020ProtocolDialect
{
private readonly HistorianClientOptions _options;
public Historian2020ProtocolDialect(HistorianClientOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
public IAsyncEnumerable<HistorianSample> ReadRawAsync(string tag, DateTime startUtc, DateTime endUtc, int maxValues, CancellationToken cancellationToken)
{
if (_options.Transport != HistorianTransport.LocalPipe)
{
return Missing<HistorianSample>($"StartDataRetrievalQuery/Full over {_options.Transport}", cancellationToken);
}
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Missing<HistorianSample>("StartDataRetrievalQuery/Full requires Windows for the LocalPipe + SSPI path", cancellationToken);
}
return ReadRawWindowsAsync(tag, startUtc, endUtc, maxValues, cancellationToken);
}
private IAsyncEnumerable<HistorianSample> ReadRawWindowsAsync(string tag, DateTime startUtc, DateTime endUtc, int maxValues, CancellationToken cancellationToken)
{
#pragma warning disable CA1416 // Validated by RuntimeInformation.IsOSPlatform check above.
HistorianWcfReadOrchestrator orchestrator = new(_options);
return orchestrator.ReadRawAsync(tag, startUtc, endUtc, maxValues, cancellationToken);
#pragma warning restore CA1416
}
public IAsyncEnumerable<HistorianAggregateSample> ReadAggregateAsync(string tag, DateTime startUtc, DateTime endUtc, RetrievalMode mode, TimeSpan interval, CancellationToken cancellationToken)
{
if (_options.Transport != HistorianTransport.LocalPipe)
{
return Missing<HistorianAggregateSample>($"StartDataRetrievalQuery/{mode} over {_options.Transport}", cancellationToken);
}
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Missing<HistorianAggregateSample>($"StartDataRetrievalQuery/{mode} requires Windows for the LocalPipe + SSPI path", cancellationToken);
}
return ReadAggregateWindowsAsync(tag, startUtc, endUtc, mode, interval, cancellationToken);
}
private IAsyncEnumerable<HistorianAggregateSample> ReadAggregateWindowsAsync(string tag, DateTime startUtc, DateTime endUtc, RetrievalMode mode, TimeSpan interval, CancellationToken cancellationToken)
{
#pragma warning disable CA1416
HistorianWcfReadOrchestrator orchestrator = new(_options);
return orchestrator.ReadAggregateAsync(tag, startUtc, endUtc, mode, interval, cancellationToken);
#pragma warning restore CA1416
}
public Task<IReadOnlyList<HistorianSample>> ReadAtTimeAsync(string tag, IReadOnlyList<DateTime> timestampsUtc, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (_options.Transport != HistorianTransport.LocalPipe)
{
throw new ProtocolEvidenceMissingException($"StartDataRetrievalQuery/Interpolated at-time over {_options.Transport}");
}
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new ProtocolEvidenceMissingException("StartDataRetrievalQuery/Interpolated at-time requires Windows for the LocalPipe + SSPI path");
}
#pragma warning disable CA1416
HistorianWcfReadOrchestrator orchestrator = new(_options);
return orchestrator.ReadAtTimeAsync(tag, timestampsUtc, cancellationToken);
#pragma warning restore CA1416
}
public IAsyncEnumerable<HistorianBlock> ReadBlocksAsync(string tag, DateTime startUtc, DateTime endUtc, CancellationToken cancellationToken)
{
return Missing<HistorianBlock>("StartBlockRetrievalQuery", cancellationToken);
}
public IAsyncEnumerable<HistorianEvent> ReadEventsAsync(DateTime startUtc, DateTime endUtc, CancellationToken cancellationToken)
{
if (_options.Transport != HistorianTransport.LocalPipe)
{
return Missing<HistorianEvent>($"StartEventDataRetrievalQuery over {_options.Transport}", cancellationToken);
}
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Missing<HistorianEvent>("StartEventDataRetrievalQuery requires Windows for the LocalPipe + SSPI path", cancellationToken);
}
return ReadEventsWindowsAsync(startUtc, endUtc, cancellationToken);
}
private IAsyncEnumerable<HistorianEvent> ReadEventsWindowsAsync(DateTime startUtc, DateTime endUtc, CancellationToken cancellationToken)
{
#pragma warning disable CA1416
HistorianWcfEventOrchestrator orchestrator = new(_options);
return orchestrator.ReadEventsAsync(startUtc, endUtc, cancellationToken);
#pragma warning restore CA1416
}
public IAsyncEnumerable<string> BrowseTagNamesAsync(string filter, CancellationToken cancellationToken)
{
return Missing<string>("StartLikeTagNameSearch/GetLikeTagnames", cancellationToken);
}
public Task<HistorianTagMetadata?> GetTagMetadataAsync(string tag, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
throw new ProtocolEvidenceMissingException("GetTagInfoByName/GetTagInfos");
}
public Task<HistorianConnectionStatus> GetConnectionStatusAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (!OperatingSystem.IsWindows())
{
throw new ProtocolEvidenceMissingException("GetConnectionStatus on non-Windows");
}
return Wcf.HistorianWcfStatusClient.GetConnectionStatusAsync(_options, cancellationToken);
}
public Task<HistorianStoreForwardStatus> GetStoreForwardStatusAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (!OperatingSystem.IsWindows())
{
throw new ProtocolEvidenceMissingException("GetStoreForwardStatus on non-Windows");
}
return Wcf.HistorianWcfStatusClient.GetStoreForwardStatusAsync(_options, cancellationToken);
}
public Task<string?> GetSystemParameterAsync(string name, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
ArgumentException.ThrowIfNullOrWhiteSpace(name);
if (!OperatingSystem.IsWindows())
{
throw new ProtocolEvidenceMissingException("GetSystemParameter on non-Windows");
}
return Wcf.HistorianWcfStatusClient.GetSystemParameterAsync(_options, name, cancellationToken);
}
private static async IAsyncEnumerable<T> Missing<T>(
string operation,
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
{
await Task.Yield();
cancellationToken.ThrowIfCancellationRequested();
throw new ProtocolEvidenceMissingException(operation);
#pragma warning disable CS0162
yield break;
#pragma warning restore CS0162
}
}