Activates the Phase 7 engines in production. Loads Script + VirtualTag + ScriptedAlarm rows from the bootstrapped generation, wires the engines through the Phase7EngineComposer kernel (#243), starts the DriverSubscriptionBridge feed (#244), and late-binds the resulting IReadable sources to OpcUaApplicationHost before OPC UA server start. ## Phase7Composer (Server.Phase7) Singleton orchestrator. PrepareAsync loads the three Phase 7 row sets in one DB scope, builds CachedTagUpstreamSource, calls Phase7EngineComposer.Compose, constructs DriverSubscriptionBridge with one DriverFeed per registered ISubscribable driver (path-to-fullRef map built from EquipmentNamespaceContent via MapPathsToFullRefs), starts the bridge. DisposeAsync tears down in the right order: bridge first (no more events fired into the cache), then engines (cascades + timers stop), then any disposable sink. MapPathsToFullRefs: deterministic path convention is /{areaName}/{lineName}/{equipmentName}/{tagName} matching exactly what EquipmentNodeWalker emits into the OPC UA browse tree, so script literals against the operator-visible UNS tree work without translation. Tags missing EquipmentId or pointing at unknown Equipment are skipped silently (Galaxy SystemPlatform-style tags + dangling references handled). ## OpcUaApplicationHost.SetPhase7Sources New late-bind setter. Throws InvalidOperationException if called after StartAsync because OtOpcUaServer + DriverNodeManagers capture the field values at construction; mutation post-start would silently fail. ## OpcUaServerService After bootstrap loads the current generation, calls phase7Composer.PrepareAsync + applicationHost.SetPhase7Sources before applicationHost.StartAsync. StopAsync disposes Phase7Composer first so the bridge stops feeding the cache before the OPC UA server tears down its node managers (avoids in-flight cascades surfacing as noisy shutdown warnings). ## Program.cs Registers IAlarmHistorianSink as NullAlarmHistorianSink.Instance (task #247 swaps in the real Galaxy.Host-writer-backed SqliteStoreAndForwardSink), Serilog root logger, and Phase7Composer singleton. ## Tests — 5 new Phase7ComposerMappingTests = 34 Phase 7 tests total Maps tag → walker UNS path, skips null EquipmentId, skips unknown Equipment reference, multiple tags under same equipment map distinctly, empty content yields empty map. Pure functions; no DI/DB needed. The real PrepareAsync DB query path can't be exercised without SQL Server in the test environment — it's exercised by the live E2E smoke (task #240) which unblocks once #247 lands. ## Phase 7 production wiring chain status - ✅ #243 composition kernel - ✅ #245 scripted-alarm IReadable adapter - ✅ #244 driver bridge - ✅ #246 this — Program.cs wire-in - 🟡 #247 — Galaxy.Host SqliteStoreAndForwardSink writer adapter (replaces NullSink) - 🟡 #240 — live E2E smoke (unblocks once #247 lands)
128 lines
6.3 KiB
C#
128 lines
6.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Serilog;
|
|
using Serilog.Formatting.Compact;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.LocalCache;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Hosting;
|
|
using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;
|
|
using ZB.MOM.WW.OtOpcUa.Server;
|
|
using ZB.MOM.WW.OtOpcUa.Server.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Server.Phase7;
|
|
using ZB.MOM.WW.OtOpcUa.Server.Security;
|
|
|
|
var builder = Host.CreateApplicationBuilder(args);
|
|
|
|
// Per Phase 6.1 Stream C.3: SIEMs (Splunk, Datadog) ingest the JSON file without a
|
|
// regex parser. Plain-text rolling file stays on by default for human readability;
|
|
// JSON file is opt-in via appsetting `Serilog:WriteJson = true`.
|
|
var writeJson = builder.Configuration.GetValue<bool>("Serilog:WriteJson");
|
|
var loggerBuilder = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(builder.Configuration)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console()
|
|
.WriteTo.File("logs/otopcua-.log", rollingInterval: RollingInterval.Day);
|
|
|
|
if (writeJson)
|
|
{
|
|
loggerBuilder = loggerBuilder.WriteTo.File(
|
|
new CompactJsonFormatter(),
|
|
"logs/otopcua-.json.log",
|
|
rollingInterval: RollingInterval.Day);
|
|
}
|
|
|
|
Log.Logger = loggerBuilder.CreateLogger();
|
|
|
|
builder.Services.AddSerilog();
|
|
builder.Services.AddWindowsService(o => o.ServiceName = "OtOpcUa");
|
|
|
|
var nodeSection = builder.Configuration.GetSection(NodeOptions.SectionName);
|
|
var options = new NodeOptions
|
|
{
|
|
NodeId = nodeSection.GetValue<string>("NodeId")
|
|
?? throw new InvalidOperationException("Node:NodeId not configured"),
|
|
ClusterId = nodeSection.GetValue<string>("ClusterId")
|
|
?? throw new InvalidOperationException("Node:ClusterId not configured"),
|
|
ConfigDbConnectionString = nodeSection.GetValue<string>("ConfigDbConnectionString")
|
|
?? throw new InvalidOperationException("Node:ConfigDbConnectionString not configured"),
|
|
LocalCachePath = nodeSection.GetValue<string>("LocalCachePath") ?? "config_cache.db",
|
|
};
|
|
|
|
var opcUaSection = builder.Configuration.GetSection(OpcUaServerOptions.SectionName);
|
|
var ldapSection = opcUaSection.GetSection("Ldap");
|
|
var ldapOptions = new LdapOptions
|
|
{
|
|
Enabled = ldapSection.GetValue<bool?>("Enabled") ?? false,
|
|
Server = ldapSection.GetValue<string>("Server") ?? "localhost",
|
|
Port = ldapSection.GetValue<int?>("Port") ?? 3893,
|
|
UseTls = ldapSection.GetValue<bool?>("UseTls") ?? false,
|
|
AllowInsecureLdap = ldapSection.GetValue<bool?>("AllowInsecureLdap") ?? true,
|
|
SearchBase = ldapSection.GetValue<string>("SearchBase") ?? "dc=lmxopcua,dc=local",
|
|
ServiceAccountDn = ldapSection.GetValue<string>("ServiceAccountDn") ?? string.Empty,
|
|
ServiceAccountPassword = ldapSection.GetValue<string>("ServiceAccountPassword") ?? string.Empty,
|
|
GroupToRole = ldapSection.GetSection("GroupToRole").Get<Dictionary<string, string>>() ?? new(StringComparer.OrdinalIgnoreCase),
|
|
};
|
|
|
|
var opcUaOptions = new OpcUaServerOptions
|
|
{
|
|
EndpointUrl = opcUaSection.GetValue<string>("EndpointUrl") ?? "opc.tcp://0.0.0.0:4840/OtOpcUa",
|
|
ApplicationName = opcUaSection.GetValue<string>("ApplicationName") ?? "OtOpcUa Server",
|
|
ApplicationUri = opcUaSection.GetValue<string>("ApplicationUri") ?? "urn:OtOpcUa:Server",
|
|
PkiStoreRoot = opcUaSection.GetValue<string>("PkiStoreRoot")
|
|
?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "OtOpcUa", "pki"),
|
|
AutoAcceptUntrustedClientCertificates = opcUaSection.GetValue<bool?>("AutoAcceptUntrustedClientCertificates") ?? true,
|
|
SecurityProfile = Enum.TryParse<OpcUaSecurityProfile>(opcUaSection.GetValue<string>("SecurityProfile"), true, out var p)
|
|
? p : OpcUaSecurityProfile.None,
|
|
Ldap = ldapOptions,
|
|
};
|
|
|
|
builder.Services.AddSingleton(options);
|
|
builder.Services.AddSingleton(opcUaOptions);
|
|
builder.Services.AddSingleton(ldapOptions);
|
|
builder.Services.AddSingleton<IUserAuthenticator>(sp => ldapOptions.Enabled
|
|
? new LdapUserAuthenticator(ldapOptions, sp.GetRequiredService<ILogger<LdapUserAuthenticator>>())
|
|
: new DenyAllUserAuthenticator());
|
|
builder.Services.AddSingleton<ILocalConfigCache>(_ => new LiteDbConfigCache(options.LocalCachePath));
|
|
builder.Services.AddSingleton<DriverHost>();
|
|
builder.Services.AddSingleton<NodeBootstrap>();
|
|
|
|
// ADR-001 Option A wiring — the registry is the handoff between OpcUaServerService's
|
|
// bootstrap-time population pass + OpcUaApplicationHost's StartAsync walker invocation.
|
|
// DriverEquipmentContentRegistry.Get is the equipmentContentLookup delegate that PR #155
|
|
// added to OpcUaApplicationHost's ctor seam.
|
|
builder.Services.AddSingleton<DriverEquipmentContentRegistry>();
|
|
builder.Services.AddScoped<EquipmentNamespaceContentLoader>();
|
|
|
|
builder.Services.AddSingleton<OpcUaApplicationHost>(sp =>
|
|
{
|
|
var registry = sp.GetRequiredService<DriverEquipmentContentRegistry>();
|
|
return new OpcUaApplicationHost(
|
|
sp.GetRequiredService<OpcUaServerOptions>(),
|
|
sp.GetRequiredService<DriverHost>(),
|
|
sp.GetRequiredService<IUserAuthenticator>(),
|
|
sp.GetRequiredService<ILoggerFactory>(),
|
|
sp.GetRequiredService<ILogger<OpcUaApplicationHost>>(),
|
|
equipmentContentLookup: registry.Get);
|
|
});
|
|
builder.Services.AddHostedService<OpcUaServerService>();
|
|
|
|
// Central-config DB access for the host-status publisher (LMX follow-up #7). Scoped context
|
|
// so per-heartbeat change-tracking stays isolated; publisher opens one scope per tick.
|
|
builder.Services.AddDbContext<OtOpcUaConfigDbContext>(opt =>
|
|
opt.UseSqlServer(options.ConfigDbConnectionString));
|
|
builder.Services.AddHostedService<HostStatusPublisher>();
|
|
|
|
// Phase 7 follow-up #246 — historian sink + engine composer. NullAlarmHistorianSink
|
|
// is the default until the Galaxy.Host SqliteStoreAndForwardSink writer adapter
|
|
// lands (task #248). The composer reads Script/VirtualTag/ScriptedAlarm rows on
|
|
// generation bootstrap, builds the engines, and starts the driver-bridge feed.
|
|
builder.Services.AddSingleton<IAlarmHistorianSink>(NullAlarmHistorianSink.Instance);
|
|
builder.Services.AddSingleton(Log.Logger); // Serilog root for ScriptLoggerFactory
|
|
builder.Services.AddSingleton<Phase7Composer>();
|
|
|
|
var host = builder.Build();
|
|
await host.RunAsync();
|