40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Windows;
|
|||
|
|
using MembranePoreTester.Communication;
|
|||
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
|
|||
|
|
namespace MembranePoreTester
|
|||
|
|
{
|
|||
|
|
public partial class App : Application
|
|||
|
|
{
|
|||
|
|
public static IPlcService PlcService { get; private set; }
|
|||
|
|
public static PlcConfiguration PlcConfig { get; private set; }
|
|||
|
|
|
|||
|
|
protected override void OnStartup(StartupEventArgs e)
|
|||
|
|
{
|
|||
|
|
base.OnStartup(e);
|
|||
|
|
|
|||
|
|
using var db = new AppDbContext();
|
|||
|
|
db.Database.EnsureCreated(); // 自动建表
|
|||
|
|
|
|||
|
|
var builder = new ConfigurationBuilder()
|
|||
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|||
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
|||
|
|
|
|||
|
|
var configuration = builder.Build();
|
|||
|
|
PlcConfig = configuration.GetSection("PlcSettings").Get<PlcConfiguration>();
|
|||
|
|
|
|||
|
|
if (PlcConfig == null)
|
|||
|
|
throw new InvalidOperationException("PLC settings missing in appsettings.json");
|
|||
|
|
|
|||
|
|
PlcService = new ModbusTcpPlcService(PlcConfig);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected override void OnExit(ExitEventArgs e)
|
|||
|
|
{
|
|||
|
|
(PlcService as IDisposable)?.Dispose();
|
|||
|
|
base.OnExit(e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|