using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using ZB.MOM.WW.ScadaBridge.Communication.Grpc; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; using ZB.MOM.WW.ScadaBridge.SiteRuntime.Persistence; using ZB.MOM.WW.ScadaBridge.SiteRuntime.Repositories; using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts; using ZB.MOM.WW.ScadaBridge.SiteRuntime.Streaming; namespace ZB.MOM.WW.ScadaBridge.SiteRuntime; public static class ServiceCollectionExtensions { /// /// Registers Site Runtime services including SiteStorageService for SQLite persistence. /// The caller must register an or call the /// overload with an explicit connection string. /// /// The DI service collection to register services into. /// The same to allow chaining. public static IServiceCollection AddSiteRuntime(this IServiceCollection services) { // SiteStorageService is registered by the Host using AddSiteRuntime(connectionString) // This overload is for backward compatibility / skeleton placeholder return services; } /// /// Registers Site Runtime services with an explicit SQLite connection string. /// /// The DI service collection to register services into. /// The SQLite connection string for the site local storage database. /// The same to allow chaining. public static IServiceCollection AddSiteRuntime(this IServiceCollection services, string siteDbConnectionString) { services.AddSingleton(sp => { var logger = sp.GetRequiredService>(); return new SiteStorageService(siteDbConnectionString, logger); }); services.AddHostedService(); // WP-19: Script compilation service services.AddSingleton(); // WP-17: Shared script library services.AddSingleton(); // WP-23: Site stream manager — registered as singleton and exposed as ISiteStreamSubscriber // so the gRPC server can subscribe relay actors to instance events. // ActorSystem is injected later via Initialize() after AkkaHostedService starts. services.AddSingleton(sp => { var options = sp.GetRequiredService>().Value; var logger = sp.GetRequiredService>(); return new SiteStreamManager(options, logger); }); services.AddSingleton(sp => sp.GetRequiredService()); // Site-local repository implementations backed by SQLite services.AddScoped(); services.AddScoped(); return services; } /// Registers any additional DI services needed by the Site Runtime Akka actors. /// The DI service collection to register services into. /// The same to allow chaining. public static IServiceCollection AddSiteRuntimeActors(this IServiceCollection services) { // Actor registration is handled by AkkaHostedService.RegisterSiteActors() // which creates the DeploymentManager singleton and proxy return services; } }