2025-06-23 17:01:06 +08:00
|
|
|
|
using System.Windows;
|
2025-06-24 20:48:38 +08:00
|
|
|
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
2025-05-29 08:58:58 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using NLog;
|
|
|
|
|
|
using NLog.Extensions.Logging;
|
|
|
|
|
|
using PMSWPF.Data;
|
|
|
|
|
|
using PMSWPF.Data.Entities;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
using PMSWPF.Enums;
|
2025-06-29 12:37:35 +08:00
|
|
|
|
using PMSWPF.Extensions;
|
|
|
|
|
|
using PMSWPF.Helper;
|
2025-05-29 08:58:58 +08:00
|
|
|
|
using PMSWPF.Services;
|
|
|
|
|
|
using PMSWPF.ViewModels;
|
|
|
|
|
|
using PMSWPF.Views;
|
2025-07-05 02:22:36 +08:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2025-07-05 16:13:46 +08:00
|
|
|
|
using PMSWPF.ViewModels.Dialogs;
|
2025-06-29 12:37:35 +08:00
|
|
|
|
using SqlSugar;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
2025-05-29 08:58:58 +08:00
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
namespace PMSWPF;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Interaction logic for App.xaml
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class App : Application
|
2025-05-29 08:58:58 +08:00
|
|
|
|
{
|
2025-07-05 02:22:36 +08:00
|
|
|
|
public IServiceProvider Services { get; }
|
|
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
public App()
|
2025-05-29 08:58:58 +08:00
|
|
|
|
{
|
2025-07-05 02:22:36 +08:00
|
|
|
|
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
2025-07-05 12:00:46 +08:00
|
|
|
|
.ConfigureServices((context, services) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConfigureServices(services);
|
|
|
|
|
|
})
|
|
|
|
|
|
.ConfigureLogging(loggingBuilder =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConfigureLogging(loggingBuilder);
|
|
|
|
|
|
})
|
2025-07-05 02:22:36 +08:00
|
|
|
|
.Build();
|
|
|
|
|
|
Services = Host.Services;
|
2025-07-02 22:07:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public new static App Current => (App)Application.Current;
|
2025-07-05 02:22:36 +08:00
|
|
|
|
public IHost Host { get; }
|
2025-07-02 22:07:16 +08:00
|
|
|
|
|
2025-07-05 02:22:36 +08:00
|
|
|
|
protected override async void OnStartup(StartupEventArgs e)
|
2025-07-02 22:07:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
base.OnStartup(e);
|
2025-07-05 02:22:36 +08:00
|
|
|
|
await Host.StartAsync();
|
2025-07-02 22:07:16 +08:00
|
|
|
|
InitializeDataBase();
|
2025-07-05 02:22:36 +08:00
|
|
|
|
InitializeMenu()
|
|
|
|
|
|
.Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); },
|
|
|
|
|
|
() => { MessageHelper.SendLoadMessage(LoadTypes.Menu); });
|
2025-07-05 18:15:21 +08:00
|
|
|
|
Host.Services.GetRequiredService<GrowlNotificationService>();
|
2025-07-05 02:22:36 +08:00
|
|
|
|
MainWindow = Host.Services.GetRequiredService<MainView>();
|
2025-07-02 22:07:16 +08:00
|
|
|
|
MainWindow.Show();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-05 02:22:36 +08:00
|
|
|
|
protected override async void OnExit(ExitEventArgs e)
|
2025-07-02 22:07:16 +08:00
|
|
|
|
{
|
2025-07-05 02:22:36 +08:00
|
|
|
|
await Host.StopAsync();
|
|
|
|
|
|
Host.Dispose();
|
2025-07-02 22:07:16 +08:00
|
|
|
|
LogManager.Shutdown();
|
|
|
|
|
|
base.OnExit(e);
|
|
|
|
|
|
}
|
2025-06-23 13:42:02 +08:00
|
|
|
|
|
2025-07-05 02:22:36 +08:00
|
|
|
|
private void ConfigureServices(IServiceCollection services)
|
2025-07-02 22:07:16 +08:00
|
|
|
|
{
|
2025-07-05 02:22:36 +08:00
|
|
|
|
services.AddSingleton<DataServices>();
|
|
|
|
|
|
services.AddSingleton<NavgatorServices>();
|
|
|
|
|
|
services.AddSingleton<IDialogService, DialogService>();
|
|
|
|
|
|
services.AddSingleton<GrowlNotificationService>();
|
|
|
|
|
|
services.AddHostedService<S7BackgroundService>(); // Register as HostedService
|
2025-07-05 21:49:41 +08:00
|
|
|
|
// services.AddHostedService<MqttBackgroundService>();
|
2025-07-05 02:22:36 +08:00
|
|
|
|
services.AddSingleton<MainViewModel>();
|
|
|
|
|
|
services.AddSingleton<HomeViewModel>();
|
|
|
|
|
|
services.AddSingleton<DevicesViewModel>();
|
|
|
|
|
|
services.AddSingleton<DataTransformViewModel>();
|
|
|
|
|
|
services.AddSingleton<SettingViewModel>();
|
|
|
|
|
|
services.AddSingleton<SettingView>();
|
|
|
|
|
|
services.AddSingleton<MainView>();
|
|
|
|
|
|
services.AddSingleton<HomeView>();
|
|
|
|
|
|
services.AddSingleton<DevicesView>();
|
|
|
|
|
|
services.AddSingleton<DataTransformViewModel>();
|
|
|
|
|
|
services.AddTransient<VariableTableViewModel>();
|
|
|
|
|
|
services.AddSingleton<VariableTableView>();
|
|
|
|
|
|
services.AddScoped<DeviceDetailViewModel>();
|
|
|
|
|
|
services.AddScoped<DeviceDetailView>();
|
|
|
|
|
|
services.AddScoped<MqttsViewModel>();
|
|
|
|
|
|
services.AddScoped<MqttsView>();
|
2025-07-05 22:57:54 +08:00
|
|
|
|
services.AddScoped<MqttServerDetailViewModel>();
|
2025-06-23 17:01:06 +08:00
|
|
|
|
}
|
2025-05-29 08:58:58 +08:00
|
|
|
|
|
2025-07-05 02:22:36 +08:00
|
|
|
|
private void ConfigureLogging(ILoggingBuilder loggingBuilder)
|
2025-06-23 17:01:06 +08:00
|
|
|
|
{
|
2025-07-05 12:00:46 +08:00
|
|
|
|
LogManager.Setup().LoadConfigurationFromFile("Config/nlog.config");
|
2025-07-05 02:22:36 +08:00
|
|
|
|
loggingBuilder.ClearProviders();
|
|
|
|
|
|
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
|
|
|
|
|
|
loggingBuilder.AddNLog();
|
2025-07-02 22:07:16 +08:00
|
|
|
|
|
|
|
|
|
|
// 捕获未处理的异常并记录
|
|
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
|
2025-06-29 12:37:35 +08:00
|
|
|
|
{
|
2025-07-02 22:07:16 +08:00
|
|
|
|
var ex = args.ExceptionObject as Exception;
|
|
|
|
|
|
if (ex != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 可以使用一个专用的 Logger 来记录未处理异常
|
2025-07-05 02:22:36 +08:00
|
|
|
|
LogManager.GetLogger("UnhandledExceptionLogger")
|
|
|
|
|
|
.Fatal($"应用程序发生未处理的异常:{ex}");
|
2025-07-02 22:07:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 捕获 Dispatcher 线程上的未处理异常 (UI 线程)
|
|
|
|
|
|
this.DispatcherUnhandledException += (sender, args) =>
|
2025-06-30 13:06:51 +08:00
|
|
|
|
{
|
2025-07-05 02:22:36 +08:00
|
|
|
|
LogManager.GetLogger("DispatcherExceptionLogger")
|
|
|
|
|
|
.Fatal($"UI 线程发生未处理的异常:{args.Exception}");
|
2025-07-02 22:07:16 +08:00
|
|
|
|
// 标记为已处理,防止应用程序崩溃 (生产环境慎用,可能掩盖问题)
|
|
|
|
|
|
// args.Handled = true;
|
|
|
|
|
|
};
|
2025-06-23 13:42:02 +08:00
|
|
|
|
|
2025-07-02 22:07:16 +08:00
|
|
|
|
// 如果您使用 Task (异步方法) 并且没有正确 await,可能会导致异常丢失,
|
|
|
|
|
|
// 可以通过以下方式捕获 Task 中的异常。
|
|
|
|
|
|
TaskScheduler.UnobservedTaskException += (sender, args) =>
|
|
|
|
|
|
{
|
2025-07-05 02:22:36 +08:00
|
|
|
|
LogManager.GetLogger("UnobservedTaskExceptionLogger")
|
|
|
|
|
|
.Fatal($"异步任务发生未观察到的异常:{args.Exception}");
|
2025-07-02 22:07:16 +08:00
|
|
|
|
// args.SetObserved(); // 标记为已观察,防止进程终止
|
|
|
|
|
|
};
|
2025-06-23 17:01:06 +08:00
|
|
|
|
}
|
2025-05-29 08:58:58 +08:00
|
|
|
|
|
2025-07-02 22:07:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化菜单
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private async Task InitializeMenu()
|
2025-06-24 20:48:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
using (var db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-06-29 12:37:35 +08:00
|
|
|
|
var homeMenu = new DbMenu()
|
2025-07-05 02:22:36 +08:00
|
|
|
|
{ Name = "主页", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Home.Glyph, ParentId = 0 };
|
2025-06-29 12:37:35 +08:00
|
|
|
|
|
|
|
|
|
|
var deviceMenu = new DbMenu()
|
2025-07-05 02:22:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
Name = "设备", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Devices3.Glyph,
|
|
|
|
|
|
ParentId = 0
|
|
|
|
|
|
};
|
2025-06-29 12:37:35 +08:00
|
|
|
|
var dataTransfromMenu = new DbMenu()
|
2025-07-05 02:22:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
Name = "数据转换", Type = MenuType.MainMenu,
|
|
|
|
|
|
Icon = SegoeFluentIcons.ChromeSwitch.Glyph, ParentId = 0
|
|
|
|
|
|
};
|
2025-06-29 12:37:35 +08:00
|
|
|
|
var mqttMenu = new DbMenu()
|
2025-07-05 02:22:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
Name = "Mqtt服务器", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Cloud.Glyph,
|
|
|
|
|
|
ParentId = 0
|
|
|
|
|
|
};
|
2025-07-02 22:07:16 +08:00
|
|
|
|
|
2025-06-29 12:37:35 +08:00
|
|
|
|
var settingMenu = new DbMenu()
|
2025-07-05 02:22:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
Name = "设置", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Settings.Glyph,
|
|
|
|
|
|
ParentId = 0
|
|
|
|
|
|
};
|
2025-06-29 12:37:35 +08:00
|
|
|
|
var aboutMenu = new DbMenu()
|
2025-07-05 02:22:36 +08:00
|
|
|
|
{ Name = "关于", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Info.Glyph, ParentId = 0 };
|
2025-06-29 12:37:35 +08:00
|
|
|
|
await CheckMainMenuExist(db, homeMenu);
|
|
|
|
|
|
await CheckMainMenuExist(db, deviceMenu);
|
|
|
|
|
|
await CheckMainMenuExist(db, dataTransfromMenu);
|
|
|
|
|
|
await CheckMainMenuExist(db, mqttMenu);
|
|
|
|
|
|
await CheckMainMenuExist(db, settingMenu);
|
|
|
|
|
|
await CheckMainMenuExist(db, aboutMenu);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static async Task CheckMainMenuExist(SqlSugarClient db, DbMenu menu)
|
|
|
|
|
|
{
|
2025-07-05 02:22:36 +08:00
|
|
|
|
var homeMenuExist = await db.Queryable<DbMenu>()
|
|
|
|
|
|
.FirstAsync(dm => dm.Name == menu.Name);
|
2025-06-29 12:37:35 +08:00
|
|
|
|
if (homeMenuExist == null)
|
|
|
|
|
|
{
|
2025-07-05 02:22:36 +08:00
|
|
|
|
await db.Insertable<DbMenu>(menu)
|
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-06-24 20:48:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-02 22:07:16 +08:00
|
|
|
|
private void InitializeDataBase()
|
2025-06-23 17:01:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
var _db = DbContext.GetInstance();
|
|
|
|
|
|
_db.DbMaintenance.CreateDatabase();
|
|
|
|
|
|
_db.CodeFirst.InitTables<DbNlog>();
|
|
|
|
|
|
_db.CodeFirst.InitTables<DbNlog>();
|
|
|
|
|
|
_db.CodeFirst.InitTables<DbDevice>();
|
|
|
|
|
|
_db.CodeFirst.InitTables<DbVariableTable>();
|
2025-07-02 22:07:16 +08:00
|
|
|
|
_db.CodeFirst.InitTables<DbVariableData>();
|
|
|
|
|
|
_db.CodeFirst.InitTables<DbVariableS7Data>();
|
2025-06-23 17:01:06 +08:00
|
|
|
|
_db.CodeFirst.InitTables<DbUser>();
|
|
|
|
|
|
_db.CodeFirst.InitTables<DbMqtt>();
|
2025-07-05 19:31:36 +08:00
|
|
|
|
_db.CodeFirst.InitTables<DbVariableDataMqtt>();
|
2025-06-24 20:48:38 +08:00
|
|
|
|
_db.CodeFirst.InitTables<DbMenu>();
|
2025-06-10 20:55:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|