using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using ZB.MOM.WW.OtOpcUa.Configuration.LocalCache; using ZB.MOM.WW.OtOpcUa.Core.Hosting; using ZB.MOM.WW.OtOpcUa.Server; using ZB.MOM.WW.OtOpcUa.Server.OpcUa; var builder = Host.CreateApplicationBuilder(args); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) .WriteTo.Console() .WriteTo.File("logs/otopcua-.log", rollingInterval: RollingInterval.Day) .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("NodeId") ?? throw new InvalidOperationException("Node:NodeId not configured"), ClusterId = nodeSection.GetValue("ClusterId") ?? throw new InvalidOperationException("Node:ClusterId not configured"), ConfigDbConnectionString = nodeSection.GetValue("ConfigDbConnectionString") ?? throw new InvalidOperationException("Node:ConfigDbConnectionString not configured"), LocalCachePath = nodeSection.GetValue("LocalCachePath") ?? "config_cache.db", }; var opcUaSection = builder.Configuration.GetSection(OpcUaServerOptions.SectionName); var opcUaOptions = new OpcUaServerOptions { EndpointUrl = opcUaSection.GetValue("EndpointUrl") ?? "opc.tcp://0.0.0.0:4840/OtOpcUa", ApplicationName = opcUaSection.GetValue("ApplicationName") ?? "OtOpcUa Server", ApplicationUri = opcUaSection.GetValue("ApplicationUri") ?? "urn:OtOpcUa:Server", PkiStoreRoot = opcUaSection.GetValue("PkiStoreRoot") ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "OtOpcUa", "pki"), AutoAcceptUntrustedClientCertificates = opcUaSection.GetValue("AutoAcceptUntrustedClientCertificates") ?? true, }; builder.Services.AddSingleton(options); builder.Services.AddSingleton(opcUaOptions); builder.Services.AddSingleton(_ => new LiteDbConfigCache(options.LocalCachePath)); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHostedService(); var host = builder.Build(); await host.RunAsync();