Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/AdvertisedHistoryCapabilities.cs
T
Joseph Doherty b27b5b9cc1
v2-ci / unit-tests (push) Has been cancelled
fix(history): advertise HistoryServerCapabilities from the wired backend (#881)
Every Boolean under Server/ServerCapabilities/HistoryServerCapabilities read
false (the SDK default) even though data and event history were served, so a
spec-following client skipped HistoryRead entirely.

Each backend now declares what it serves through its own interface:
- IHistorianDataSource.ReadCapabilities (Data / Events / ServerTimestamps):
  Gateway Data|Events; SqlServer + QuestDb add ServerTimestamps (they store
  ServerTimeUtc, the Gateway stamps read time); Null declares None. A
  reflection guard requires every production source to declare it itself.
- IHistorianDataUpdater.Capabilities (required member): SqlServer and QuestDb
  declare Insert/Replace/Update/DeleteRaw/DeleteAtTime; the Gateway provider
  has no updater.

The node manager writes the flags on every source/updater assignment
(AdvertisedHistoryCapabilities), and the same declarations drive dispatch:
an update the updater does not declare is BadHistoryOperationUnsupported
without reaching it, the HistoryWrite AccessLevel bit follows a non-empty
declaration, and event-history notifier promotion follows the Events flag.
Event and annotation update flags stay false (nothing dispatches them).

Tests: booted-server wire test per backend + Null + a SqlServer->Gateway
rewire pairing on one session; dispatch gating unit test; the #801 live
gate now asserts the Gateway flags (not run here).

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
2026-11-12 10:31:29 -05:00

108 lines
5.7 KiB
C#

using Opc.Ua;
using Opc.Ua.Server;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
/// <summary>
/// The Boolean <c>Server/ServerCapabilities/HistoryServerCapabilities</c> properties (Part 11 §5.4.2) as the server
/// advertises them for one wired historian (lmxopcua#881).
/// </summary>
/// <param name="AccessHistoryData"><c>AccessHistoryDataCapability</c>.</param>
/// <param name="AccessHistoryEvents"><c>AccessHistoryEventsCapability</c>.</param>
/// <param name="ServerTimestampSupported"><c>ServerTimestampSupported</c>.</param>
/// <param name="InsertData"><c>InsertDataCapability</c>.</param>
/// <param name="ReplaceData"><c>ReplaceDataCapability</c>.</param>
/// <param name="UpdateData"><c>UpdateDataCapability</c>.</param>
/// <param name="DeleteRaw"><c>DeleteRawCapability</c>.</param>
/// <param name="DeleteAtTime"><c>DeleteAtTimeCapability</c>.</param>
internal sealed record HistoryCapabilityFlags(
bool AccessHistoryData,
bool AccessHistoryEvents,
bool ServerTimestampSupported,
bool InsertData,
bool ReplaceData,
bool UpdateData,
bool DeleteRaw,
bool DeleteAtTime)
{
// Insert/Replace/Update/DeleteEvent and InsertAnnotation have no members: the node manager dispatches no event or
// annotation HistoryUpdate to any backend (the SDK base leaves UpdateEventDetails / UpdateStructureDataDetails
// BadHistoryOperationUnsupported), so they are always advertised false.
}
/// <summary>
/// Derive and write the <c>HistoryServerCapabilities</c> Booleans from what the wired backend DECLARES — the
/// source's <see cref="IHistorianDataSource.ReadCapabilities"/> and the updater's
/// <see cref="IHistorianDataUpdater.Capabilities"/>. The same declarations gate HistoryRead-Events (notifier
/// registration) and HistoryUpdate dispatch, so the advertisement cannot claim an operation the server refuses, or
/// hide one it serves. <c>AggregateFunctions</c> (<see cref="AdvertisedAggregateFunctions"/>) and
/// <c>MaxReturnDataValues</c> (<see cref="HistoryPaging.AdvertiseMaxReturnDataValues"/>) have their own sync;
/// <c>MaxReturnEventValues</c> stays the SDK's 0 (no limit: <c>NumValuesPerNode = 0</c> returns every event).
/// </summary>
internal static class AdvertisedHistoryCapabilities
{
/// <summary>The flags for <paramref name="source"/> + <paramref name="updater"/> — pure, unit-testable.</summary>
/// <param name="source">The wired data source (the Null source declares nothing).</param>
/// <param name="updater">The wired updater, or <c>null</c> when the backend has no HistoryUpdate path.</param>
/// <returns>The flags to advertise.</returns>
public static HistoryCapabilityFlags For(IHistorianDataSource source, IHistorianDataUpdater? updater)
{
var read = source.ReadCapabilities;
var update = updater?.Capabilities ?? HistoryUpdateCapabilities.None;
bool Has(HistoryUpdateCapabilities op) => (update & op) == op;
return new HistoryCapabilityFlags(
AccessHistoryData: (read & HistorianReadCapabilities.Data) != 0,
AccessHistoryEvents: (read & HistorianReadCapabilities.Events) != 0,
ServerTimestampSupported: (read & HistorianReadCapabilities.ServerTimestamps) != 0,
InsertData: Has(HistoryUpdateCapabilities.InsertData),
ReplaceData: Has(HistoryUpdateCapabilities.ReplaceData),
UpdateData: Has(HistoryUpdateCapabilities.UpdateData),
DeleteRaw: Has(HistoryUpdateCapabilities.DeleteRaw),
DeleteAtTime: Has(HistoryUpdateCapabilities.DeleteAtTime));
}
/// <summary>
/// Write <paramref name="flags"/> onto the server's <c>HistoryServerCapabilities</c> node (the event / annotation
/// properties are written false). Idempotent; a re-sync overwrites. Never throws.
/// </summary>
/// <param name="server">The running server.</param>
/// <param name="flags">The flags.</param>
/// <returns><c>true</c> when written; <c>false</c> when the diagnostics manager or node is missing, or the SDK failed.</returns>
public static bool Apply(IServerInternal? server, HistoryCapabilityFlags flags)
{
if (server?.DiagnosticsNodeManager is not DiagnosticsNodeManager diagnostics) return false;
try
{
var caps = diagnostics.GetDefaultHistoryCapabilitiesAsync().AsTask().GetAwaiter().GetResult();
if (caps is null) return false;
void Set(PropertyState<bool>? property, bool value)
{
if (property is null) return; // optional in the nodeset — nothing to advertise
property.Value = value;
property.ClearChangeMasks(diagnostics.SystemContext, false);
}
Set(caps.AccessHistoryDataCapability, flags.AccessHistoryData);
Set(caps.AccessHistoryEventsCapability, flags.AccessHistoryEvents);
Set(caps.ServerTimestampSupported, flags.ServerTimestampSupported);
Set(caps.InsertDataCapability, flags.InsertData);
Set(caps.ReplaceDataCapability, flags.ReplaceData);
Set(caps.UpdateDataCapability, flags.UpdateData);
Set(caps.DeleteRawCapability, flags.DeleteRaw);
Set(caps.DeleteAtTimeCapability, flags.DeleteAtTime);
Set(caps.InsertEventCapability, false);
Set(caps.ReplaceEventCapability, false);
Set(caps.UpdateEventCapability, false);
Set(caps.DeleteEventCapability, false);
Set(caps.InsertAnnotationCapability, false);
return true;
}
catch (Exception)
{
return false; // advisory: the caller logs; dispatch enforces the declarations regardless
}
}
}