54 lines
2.5 KiB
C#
54 lines
2.5 KiB
C#
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<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 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,
|
|
};
|
|
|
|
builder.Services.AddSingleton(options);
|
|
builder.Services.AddSingleton(opcUaOptions);
|
|
builder.Services.AddSingleton<ILocalConfigCache>(_ => new LiteDbConfigCache(options.LocalCachePath));
|
|
builder.Services.AddSingleton<DriverHost>();
|
|
builder.Services.AddSingleton<NodeBootstrap>();
|
|
builder.Services.AddSingleton<OpcUaApplicationHost>();
|
|
builder.Services.AddHostedService<OpcUaServerService>();
|
|
|
|
var host = builder.Build();
|
|
await host.RunAsync();
|