using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Recorder;
using ZB.MOM.WW.OtOpcUa.Runtime.Historian;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway;
///
/// for the HistorianGateway sidecar. Numeric-only
/// (Double/Int64/Boolean) and read/alarm/provisioning only — no HistoryUpdate.
///
public sealed class GatewayHistorianProvider : IHistorianBackendProvider
{
private readonly ServerHistorianOptions _options;
/// Creates the provider over the bound ServerHistorian options.
/// The gateway connection options.
public GatewayHistorianProvider(ServerHistorianOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
///
public string Name => "Gateway";
// The recorder's own default set (ContinuousHistorizationRecorder.DefaultSupportedKinds), so the
// gateway's kinds and the recorder's fallback can never drift apart.
///
public IReadOnlySet SupportedKinds => ContinuousHistorizationRecorder.DefaultSupportedKinds;
///
public IHistorianDataSource CreateDataSource(IServiceProvider sp) => GatewayHistorian.CreateDataSource(_options, sp);
///
// Built only when continuous historization is enabled on this backend, so this is where its startup Warning lives:
// the gaps are silent at run time (the gateway accepts a quality it then discards).
public IHistorianValueWriter CreateValueWriter(IServiceProvider sp)
{
ArgumentNullException.ThrowIfNull(sp);
var loggerFactory = sp.GetService() ?? NullLoggerFactory.Instance;
var logger = sp.GetService>()
?? NullLogger.Instance;
logger.LogWarning(ContinuousHistorizationWarning);
return new GatewayHistorianValueWriter(HistorianGatewayClientAdapter.Create(_options, loggerFactory), logger);
}
/// The startup Warning for continuous historization on this backend (see docs/Historian.md, the capability matrix).
internal const string ContinuousHistorizationWarning =
"Continuous historization is enabled on the Gateway historian backend, which stores less than the SQL backends: " +
"the gateway discards each sample's quality (AVEVA stores every recorded value Good, so a driver's Bad or Uncertain " +
"publish is historized as Good), comms-loss outage markers are dropped (reads interpolate across an outage), and " +
"non-numeric values (String, DateTime) are not recorded. Use the SqlServer or QuestDb backend where stored status, " +
"outage markers or non-numeric history matter.";
///
public IAlarmHistorianWriter CreateAlarmWriter(IServiceProvider sp) => GatewayHistorian.CreateAlarmWriter(_options, sp);
///
public IHistorianProvisioning CreateProvisioning(IServiceProvider sp) => GatewayHistorian.CreateProvisioner(_options, sp);
///
public IHistorianDataUpdater? CreateUpdater(IServiceProvider sp) => null;
}