using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using ZB.MOM.WW.OtOpcUa.Core.Hosting; using ZB.MOM.WW.OtOpcUa.Server.OpcUa; namespace ZB.MOM.WW.OtOpcUa.Server; /// /// BackgroundService that owns the OPC UA server lifecycle (decision #30, replacing TopShelf). /// Bootstraps config, starts the , starts the OPC UA server via /// , drives each driver's discovery into the address space, /// runs until stopped. /// public sealed class OpcUaServerService( NodeBootstrap bootstrap, DriverHost driverHost, OpcUaApplicationHost applicationHost, DriverEquipmentContentRegistry equipmentContentRegistry, IServiceScopeFactory scopeFactory, ILogger logger) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { logger.LogInformation("OtOpcUa.Server starting"); var result = await bootstrap.LoadCurrentGenerationAsync(stoppingToken); logger.LogInformation("Bootstrap complete: source={Source} generation={Gen}", result.Source, result.GenerationId); // ADR-001 Option A — populate per-driver Equipment namespace snapshots into the // registry before StartAsync walks the address space. The walker on the OPC UA side // reads synchronously from the registry; pre-loading here means the hot path stays // non-blocking + each driver pays at most one Config-DB query at bootstrap time. // Skipped when no generation is Published yet — the fleet boots into a UNS-less // address space until the first publish, then the registry fills on next restart. if (result.GenerationId is { } gen) await PopulateEquipmentContentAsync(gen, stoppingToken); // PR 17: stand up the OPC UA server + drive discovery per registered driver. Driver // registration itself (RegisterAsync on DriverHost) happens during an earlier DI // extension once the central config DB query + per-driver factory land; for now the // server comes up with whatever drivers are in DriverHost at start time. await applicationHost.StartAsync(stoppingToken); logger.LogInformation("OtOpcUa.Server running. Hosted drivers: {Count}", driverHost.RegisteredDriverIds.Count); try { await Task.Delay(Timeout.InfiniteTimeSpan, stoppingToken); } catch (OperationCanceledException) { logger.LogInformation("OtOpcUa.Server stopping"); } } public override async Task StopAsync(CancellationToken cancellationToken) { await base.StopAsync(cancellationToken); await applicationHost.DisposeAsync(); await driverHost.DisposeAsync(); } /// /// Pre-load an EquipmentNamespaceContent snapshot for each registered driver at /// the bootstrapped generation. Null results (driver has no Equipment rows — /// Modbus/AB CIP/TwinCAT/FOCAS today per decisions #116–#121) are skipped: the walker /// wire-in sees Get(driverId) return null + falls back to DiscoverAsync-owns-it. /// Opens one scope so the scoped OtOpcUaConfigDbContext is shared across all /// per-driver queries rather than paying scope-setup overhead per driver. /// private async Task PopulateEquipmentContentAsync(long generationId, CancellationToken ct) { using var scope = scopeFactory.CreateScope(); var loader = scope.ServiceProvider.GetRequiredService(); var loaded = 0; foreach (var driverId in driverHost.RegisteredDriverIds) { var content = await loader.LoadAsync(driverId, generationId, ct).ConfigureAwait(false); if (content is null) continue; equipmentContentRegistry.Set(driverId, content); loaded++; } logger.LogInformation( "Equipment namespace snapshots loaded for {Count}/{Total} driver(s) at generation {Gen}", loaded, driverHost.RegisteredDriverIds.Count, generationId); } }