using Opc.Ua;
using Opc.Ua.Server;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
///
/// The Boolean Server/ServerCapabilities/HistoryServerCapabilities properties (Part 11 ยง5.4.2) as the server
/// advertises them for one wired historian (lmxopcua#881).
///
/// AccessHistoryDataCapability.
/// AccessHistoryEventsCapability.
/// ServerTimestampSupported.
/// InsertDataCapability.
/// ReplaceDataCapability.
/// UpdateDataCapability.
/// DeleteRawCapability.
/// DeleteAtTimeCapability.
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.
}
///
/// Derive and write the HistoryServerCapabilities Booleans from what the wired backend DECLARES โ the
/// source's and the updater's
/// . 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. AggregateFunctions () and
/// MaxReturnDataValues () have their own sync;
/// MaxReturnEventValues stays the SDK's 0 (no limit: NumValuesPerNode = 0 returns every event).
///
internal static class AdvertisedHistoryCapabilities
{
/// The flags for + โ pure, unit-testable.
/// The wired data source (the Null source declares nothing).
/// The wired updater, or null when the backend has no HistoryUpdate path.
/// The flags to advertise.
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));
}
///
/// Write onto the server's HistoryServerCapabilities node (the event / annotation
/// properties are written false). Idempotent; a re-sync overwrites. Never throws.
///
/// The running server.
/// The flags.
/// true when written; false when the diagnostics manager or node is missing, or the SDK failed.
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? 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
}
}
}