2025-07-19 09:24:35 +08:00
|
|
|
|
using System.Windows;
|
2025-09-06 12:03:39 +08:00
|
|
|
|
using AutoMapper;
|
2025-07-26 13:35:53 +08:00
|
|
|
|
using AutoMapper.Internal;
|
2025-10-04 18:42:12 +08:00
|
|
|
|
using DMS.Application.Configurations;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
|
using DMS.Application.Interfaces.Database;
|
|
|
|
|
|
using DMS.Application.Interfaces.Management;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Application.Services;
|
2025-09-16 12:29:09 +08:00
|
|
|
|
using DMS.Application.Services.Database;
|
|
|
|
|
|
using DMS.Application.Services.Management;
|
2025-09-04 13:40:07 +08:00
|
|
|
|
using DMS.Application.Services.Processors;
|
2025-09-14 19:13:40 +08:00
|
|
|
|
using DMS.Application.Services.Triggers;
|
|
|
|
|
|
using DMS.Application.Services.Triggers.Impl;
|
2025-07-26 13:35:53 +08:00
|
|
|
|
using DMS.Core.Interfaces;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Core.Interfaces.Repositories;
|
2025-09-14 19:13:40 +08:00
|
|
|
|
using DMS.Core.Interfaces.Repositories.Triggers;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.Core.Interfaces.Services;
|
2025-09-04 21:38:56 +08:00
|
|
|
|
using DMS.Infrastructure.Configuration;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Infrastructure.Data;
|
2025-09-05 15:59:14 +08:00
|
|
|
|
using DMS.Infrastructure.Interfaces;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.Infrastructure.Interfaces.Services;
|
2025-07-26 11:20:03 +08:00
|
|
|
|
using DMS.Infrastructure.Repositories;
|
2025-08-22 20:24:09 +08:00
|
|
|
|
using DMS.Infrastructure.Services;
|
2025-09-16 14:42:23 +08:00
|
|
|
|
using DMS.Infrastructure.Services.Mqtt;
|
|
|
|
|
|
using DMS.Infrastructure.Services.OpcUa;
|
|
|
|
|
|
using DMS.Infrastructure.Services.S7;
|
2025-09-14 19:13:40 +08:00
|
|
|
|
using DMS.WPF.Converters;
|
2025-10-04 20:46:39 +08:00
|
|
|
|
using DMS.WPF.Factories;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.WPF.Helper;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
|
|
|
|
|
using DMS.WPF.Logging;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.WPF.Services;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.WPF.ViewModels;
|
2025-08-22 20:46:23 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
2025-10-06 18:17:56 +08:00
|
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using DMS.WPF.Views;
|
2025-10-03 12:11:14 +08:00
|
|
|
|
using LiveChartsCore;
|
|
|
|
|
|
using LiveChartsCore.SkiaSharpView;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using NLog;
|
2025-09-05 16:18:01 +08:00
|
|
|
|
using NLog.Web;
|
2025-10-03 12:11:14 +08:00
|
|
|
|
using SkiaSharp;
|
2025-09-14 19:13:40 +08:00
|
|
|
|
using SqlSugar;
|
2025-09-04 14:46:50 +08:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
2025-07-18 22:21:16 +08:00
|
|
|
|
|
2025-09-04 14:46:50 +08:00
|
|
|
|
namespace DMS.WPF;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-09-04 21:04:27 +08:00
|
|
|
|
/// Interaction logic for App.xaml
|
2025-07-19 09:24:35 +08:00
|
|
|
|
/// </summary>
|
2025-07-19 09:25:01 +08:00
|
|
|
|
public partial class App : System.Windows.Application
|
2025-07-18 22:21:16 +08:00
|
|
|
|
{
|
2025-07-19 09:24:35 +08:00
|
|
|
|
public IServiceProvider Services { get; }
|
2025-09-04 21:04:27 +08:00
|
|
|
|
public new static App Current => (App)System.Windows.Application.Current;
|
|
|
|
|
|
public IHost Host { get; }
|
2025-09-06 19:59:21 +08:00
|
|
|
|
private readonly ILogger<App> _logger;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
public App()
|
|
|
|
|
|
{
|
|
|
|
|
|
Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
2025-09-12 14:59:32 +08:00
|
|
|
|
.ConfigureServices((context, services) => { ConfigureServices(services); })
|
|
|
|
|
|
.ConfigureLogging(loggingBuilder => { ConfigureLogging(loggingBuilder); })
|
|
|
|
|
|
.Build();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
Services = Host.Services;
|
2025-09-06 19:59:21 +08:00
|
|
|
|
_logger = Host.Services.GetRequiredService<ILogger<App>>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async void OnStartup(StartupEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnStartup(e);
|
2025-10-03 12:11:14 +08:00
|
|
|
|
LiveCharts.Configure(config => config.HasGlobalSKTypeface(SKFontManager.Default.MatchCharacter('汉')));
|
2025-07-26 11:51:09 +08:00
|
|
|
|
ShutdownMode = ShutdownMode.OnLastWindowClose;
|
2025-07-19 09:24:35 +08:00
|
|
|
|
ThemeHelper.InitializeTheme();
|
|
|
|
|
|
await Host.StartAsync();
|
2025-10-03 12:11:14 +08:00
|
|
|
|
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Host.Services.GetRequiredService<GrowlNotificationService>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
Host.Services.GetRequiredService<IDeviceMonitoringService>();
|
2025-10-06 18:17:56 +08:00
|
|
|
|
DeviceItem.EventService = Host.Services.GetRequiredService<IEventService>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
// 初始化数据处理链
|
|
|
|
|
|
var dataProcessingService = Host.Services.GetRequiredService<IDataProcessingService>();
|
2025-09-15 20:54:32 +08:00
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<ValueConvertProcessor>());
|
|
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<UpdateViewProcessor>());
|
2025-07-19 09:24:35 +08:00
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<MqttPublishProcessor>());
|
|
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<UpdateDbVariableProcessor>());
|
2025-10-02 22:30:24 +08:00
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<HistoryProcessor>());
|
2025-09-13 12:30:12 +08:00
|
|
|
|
// 添加报警处理器
|
|
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<DMS.Application.Services.Processors.AlarmProcessor>());
|
2025-09-15 13:12:14 +08:00
|
|
|
|
// 添加触发器处理器
|
|
|
|
|
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<TriggerProcessor>());
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
|
{
|
2025-09-04 19:59:35 +08:00
|
|
|
|
var notificationService = Host.Services.GetRequiredService<INotificationService>();
|
2025-09-04 17:29:24 +08:00
|
|
|
|
notificationService.ShowError($"加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception);
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
2025-09-13 12:30:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 订阅报警事件
|
|
|
|
|
|
var alarmService = Host.Services.GetRequiredService<DMS.Application.Interfaces.IAlarmService>();
|
|
|
|
|
|
var alarmEventHandler = Host.Services.GetRequiredService<DMS.Application.EventHandlers.AlarmEventHandler>();
|
|
|
|
|
|
alarmService.OnAlarmTriggered += alarmEventHandler.HandleAlarm;
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-07-26 11:51:09 +08:00
|
|
|
|
var splashWindow = Host.Services.GetRequiredService<SplashWindow>();
|
|
|
|
|
|
splashWindow.Show();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async void OnExit(ExitEventArgs e)
|
|
|
|
|
|
{
|
2025-09-06 19:59:21 +08:00
|
|
|
|
_logger.LogInformation("应用程序正在关闭,开始清理资源...");
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取服务管理器
|
|
|
|
|
|
var opcUaServiceManager = Host.Services.GetService<IOpcUaServiceManager>();
|
|
|
|
|
|
var s7ServiceManager = Host.Services.GetService<IS7ServiceManager>();
|
|
|
|
|
|
var mqttServiceManager = Host.Services.GetService<IMqttServiceManager>();
|
|
|
|
|
|
|
|
|
|
|
|
// 优雅地关闭OPC UA服务
|
|
|
|
|
|
if (opcUaServiceManager != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("正在关闭OPC UA服务...");
|
|
|
|
|
|
opcUaServiceManager.Dispose();
|
|
|
|
|
|
_logger.LogInformation("OPC UA服务已关闭");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 优雅地关闭S7服务
|
|
|
|
|
|
if (s7ServiceManager != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("正在关闭S7服务...");
|
|
|
|
|
|
s7ServiceManager.Dispose();
|
|
|
|
|
|
_logger.LogInformation("S7服务已关闭");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 优雅地关闭MQTT服务
|
|
|
|
|
|
if (mqttServiceManager != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogInformation("正在关闭MQTT服务...");
|
|
|
|
|
|
mqttServiceManager.Dispose();
|
|
|
|
|
|
_logger.LogInformation("MQTT服务已关闭");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 停止后台服务
|
|
|
|
|
|
_logger.LogInformation("正在停止后台服务...");
|
|
|
|
|
|
await Host.StopAsync();
|
|
|
|
|
|
_logger.LogInformation("后台服务已停止");
|
|
|
|
|
|
|
|
|
|
|
|
// 释放Host资源
|
|
|
|
|
|
_logger.LogInformation("正在释放Host资源...");
|
|
|
|
|
|
Host.Dispose();
|
|
|
|
|
|
_logger.LogInformation("Host资源已释放");
|
|
|
|
|
|
|
|
|
|
|
|
// 关闭NLog
|
|
|
|
|
|
_logger.LogInformation("正在关闭NLog...");
|
|
|
|
|
|
LogManager.Shutdown();
|
|
|
|
|
|
_logger.LogInformation("NLog已关闭");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(ex, "关闭应用程序时发生错误: {ErrorMessage}", ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("应用程序已完全关闭");
|
2025-07-19 09:24:35 +08:00
|
|
|
|
base.OnExit(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
2025-09-04 14:46:50 +08:00
|
|
|
|
// 注册NLogLogger作为Microsoft.Extensions.Logging.ILogger的实现
|
2025-09-05 16:18:01 +08:00
|
|
|
|
services.AddSingleton<ILogger, NLogLogger>();
|
2025-09-04 14:46:50 +08:00
|
|
|
|
services.AddSingleton<ILoggerFactory, NLogLoggerFactory>();
|
2025-07-28 13:06:36 +08:00
|
|
|
|
services.AddSingleton<GrowlNotificationService>();
|
2025-09-04 19:59:35 +08:00
|
|
|
|
services.AddSingleton<INotificationService, NotificationService>();
|
2025-10-04 18:42:12 +08:00
|
|
|
|
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-04 21:04:27 +08:00
|
|
|
|
// 注册核心服务
|
2025-07-26 13:35:53 +08:00
|
|
|
|
services.AddAutoMapper(cfg =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 最终解决方案:根据异常信息的建议,设置此标记以忽略重复的Profile加载。
|
|
|
|
|
|
// 注意:此属性位于 Internal() 方法下。
|
2025-09-12 14:59:32 +08:00
|
|
|
|
cfg.Internal()
|
|
|
|
|
|
.AllowAdditiveTypeMapCreation = true;
|
2025-07-26 13:35:53 +08:00
|
|
|
|
|
|
|
|
|
|
cfg.AddProfile(new DMS.Application.Profiles.MappingProfile());
|
|
|
|
|
|
cfg.AddProfile(new DMS.Infrastructure.Profiles.MappingProfile());
|
2025-07-27 21:08:58 +08:00
|
|
|
|
cfg.AddProfile(new DMS.WPF.Profiles.MappingProfile());
|
2025-07-26 13:35:53 +08:00
|
|
|
|
});
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 注册数据处理服务和处理器
|
2025-09-04 21:38:56 +08:00
|
|
|
|
// services.AddHostedService<OpcUaBackgroundService>();
|
2025-09-05 15:59:14 +08:00
|
|
|
|
//注册OpcUa相关的服务
|
2025-09-04 21:38:56 +08:00
|
|
|
|
services.Configure<OpcUaServiceOptions>(options => { });
|
2025-09-04 21:04:27 +08:00
|
|
|
|
services.AddSingleton<IOpcUaServiceManager, OpcUaServiceManager>();
|
2025-09-04 21:38:56 +08:00
|
|
|
|
services.AddHostedService<OptimizedOpcUaBackgroundService>();
|
2025-09-05 15:59:14 +08:00
|
|
|
|
// 注册S7相关的服务
|
|
|
|
|
|
services.AddSingleton<IS7ServiceFactory, S7ServiceFactory>();
|
|
|
|
|
|
services.AddSingleton<IS7ServiceManager, S7ServiceManager>();
|
|
|
|
|
|
services.AddSingleton<IChannelBus, ChannelBus>();
|
|
|
|
|
|
services.AddSingleton<IMessenger, Messenger>();
|
|
|
|
|
|
services.AddHostedService<OptimizedS7BackgroundService>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<IDataProcessingService, DataProcessingService>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
services.AddHostedService(provider =>
|
|
|
|
|
|
(DataProcessingService)provider.GetRequiredService<IDataProcessingService>());
|
2025-09-15 20:54:32 +08:00
|
|
|
|
services.AddSingleton<ValueConvertProcessor>();
|
|
|
|
|
|
services.AddSingleton<UpdateViewProcessor>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<UpdateDbVariableProcessor>();
|
|
|
|
|
|
services.AddSingleton<HistoryProcessor>();
|
|
|
|
|
|
services.AddSingleton<MqttPublishProcessor>();
|
2025-09-13 12:30:12 +08:00
|
|
|
|
// 注册报警服务和处理器
|
|
|
|
|
|
services.AddSingleton<DMS.Application.Interfaces.IAlarmService, DMS.Application.Services.AlarmService>();
|
|
|
|
|
|
services.AddSingleton<DMS.Application.Services.Processors.AlarmProcessor>();
|
|
|
|
|
|
services.AddSingleton<DMS.Application.EventHandlers.AlarmEventHandler>();
|
2025-09-15 13:12:14 +08:00
|
|
|
|
services.AddSingleton<TriggerProcessor>(); // 注册触发器处理器
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
// 注册Core中的仓库
|
|
|
|
|
|
services.AddSingleton<AppSettings>();
|
2025-10-04 18:42:12 +08:00
|
|
|
|
services.AddSingleton<SqlSugarDbContext>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-14 19:13:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<IInitializeRepository, InitializeRepository>();
|
2025-09-04 17:29:24 +08:00
|
|
|
|
services.AddSingleton<IDeviceRepository, DeviceRepository>();
|
|
|
|
|
|
services.AddSingleton<IVariableTableRepository, VariableTableRepository>();
|
|
|
|
|
|
services.AddSingleton<IVariableRepository, VariableRepository>();
|
|
|
|
|
|
services.AddSingleton<IMqttServerRepository, MqttServerRepository>();
|
|
|
|
|
|
services.AddSingleton<IVariableMqttAliasRepository, VariableMqttAliasRepository>();
|
|
|
|
|
|
services.AddSingleton<IMenuRepository, MenuRepository>();
|
|
|
|
|
|
services.AddSingleton<IVariableHistoryRepository, VariableHistoryRepository>();
|
|
|
|
|
|
services.AddSingleton<IUserRepository, UserRepository>();
|
2025-09-07 19:01:58 +08:00
|
|
|
|
services.AddSingleton<INlogRepository, NlogRepository>();
|
2025-07-26 13:35:53 +08:00
|
|
|
|
services.AddSingleton<IRepositoryManager, RepositoryManager>();
|
2025-09-13 12:30:12 +08:00
|
|
|
|
services.AddSingleton<IAlarmHistoryRepository, AlarmHistoryRepository>(); // 添加这行
|
2025-09-14 19:13:40 +08:00
|
|
|
|
services.AddSingleton<ITriggerRepository, TriggerRepository>(); // 注册触发器仓储
|
2025-09-13 19:08:43 +08:00
|
|
|
|
services.AddSingleton<IEmailAccountRepository, EmailAccountRepository>();
|
|
|
|
|
|
services.AddSingleton<IEmailMessageRepository, EmailMessageRepository>();
|
|
|
|
|
|
services.AddSingleton<IEmailTemplateRepository, EmailTemplateRepository>();
|
|
|
|
|
|
services.AddSingleton<IEmailLogRepository, EmailLogRepository>();
|
2025-08-22 20:24:09 +08:00
|
|
|
|
services.AddSingleton<IExcelService, ExcelService>();
|
2025-08-25 20:16:57 +08:00
|
|
|
|
|
2025-09-01 21:03:34 +08:00
|
|
|
|
services.AddTransient<IOpcUaService, OpcUaService>();
|
2025-09-06 15:19:04 +08:00
|
|
|
|
services.AddTransient<IMqttService, MqttService>();
|
|
|
|
|
|
services.AddTransient<IMqttServiceFactory, MqttServiceFactory>();
|
2025-09-13 19:08:43 +08:00
|
|
|
|
services.AddTransient<IEmailService, EmailService>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-14 16:16:10 +08:00
|
|
|
|
// 注册App服务
|
2025-09-04 21:04:27 +08:00
|
|
|
|
services.AddSingleton<IInitializeService, InitializeService>();
|
|
|
|
|
|
services.AddSingleton<IDeviceAppService, DeviceAppService>();
|
|
|
|
|
|
services.AddSingleton<IVariableAppService, VariableAppService>();
|
2025-09-11 18:09:35 +08:00
|
|
|
|
services.AddSingleton<IHistoryAppService, HistoryAppService>();
|
2025-09-04 21:04:27 +08:00
|
|
|
|
services.AddSingleton<IVariableTableAppService, VariableTableAppService>();
|
2025-10-05 17:07:17 +08:00
|
|
|
|
services.AddSingleton<IMenuAppService, MenuAppService>();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
services.AddSingleton<IAppDataCenterService, AppDataCenterService>();
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<INavigationService, NavigationService>();
|
2025-07-26 18:58:52 +08:00
|
|
|
|
services.AddSingleton<IDialogService, DialogService>();
|
2025-09-09 15:28:07 +08:00
|
|
|
|
services.AddSingleton<IAppDataStorageService, AppDataStorageService>();
|
|
|
|
|
|
services.AddSingleton<IDeviceManagementService, DeviceManagementService>();
|
|
|
|
|
|
services.AddSingleton<IVariableTableManagementService, VariableTableManagementService>();
|
|
|
|
|
|
services.AddSingleton<IVariableManagementService, VariableManagementService>();
|
|
|
|
|
|
services.AddSingleton<IMenuManagementService, MenuManagementService>();
|
|
|
|
|
|
services.AddSingleton<IMqttManagementService, MqttManagementService>();
|
2025-10-11 18:07:01 +08:00
|
|
|
|
services.AddSingleton<IMqttAliasAppService, MqttAliasAppService>();
|
|
|
|
|
|
services.AddSingleton<IMqttAliasManagementService, MqttAliasManagementService>();
|
2025-09-09 15:28:07 +08:00
|
|
|
|
services.AddSingleton<ILogManagementService, LogManagementService>();
|
2025-09-14 16:16:10 +08:00
|
|
|
|
services.AddSingleton<ITriggerManagementService, TriggerManagementService>(); // 注册触发器管理服务
|
|
|
|
|
|
services.AddSingleton<ITriggerEvaluationService, TriggerEvaluationService>(); // 注册触发器评估服务
|
|
|
|
|
|
services.AddSingleton<ITriggerActionExecutor, TriggerActionExecutor>(); // 注册触发器动作执行器
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
services.AddSingleton<IDataLoaderService, DataLoaderService>();
|
2025-09-07 19:48:48 +08:00
|
|
|
|
services.AddSingleton<INlogAppService, NlogAppService>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
services.AddSingleton<IDeviceMonitoringService, DeviceMonitoringService>();
|
|
|
|
|
|
|
2025-09-06 15:19:04 +08:00
|
|
|
|
// 注册MQTT服务管理器
|
|
|
|
|
|
services.AddSingleton<IMqttServiceManager, MqttServiceManager>();
|
2025-09-06 16:12:30 +08:00
|
|
|
|
services.AddSingleton<IMqttAliasAppService, MqttAliasAppService>();
|
2025-09-06 15:19:04 +08:00
|
|
|
|
services.AddHostedService<MqttBackgroundService>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-04 21:04:27 +08:00
|
|
|
|
// 注册WPF中的服务
|
2025-09-06 12:03:39 +08:00
|
|
|
|
services.AddSingleton<IMqttAppService, MqttAppService>();
|
2025-09-14 16:16:10 +08:00
|
|
|
|
services.AddSingleton<IEmailAppService, EmailAppService>();
|
|
|
|
|
|
services.AddSingleton<EmailFunctionalityTestService, EmailFunctionalityTestService>();
|
|
|
|
|
|
|
|
|
|
|
|
// 注册转换器 (Converters)
|
|
|
|
|
|
services.AddTransient<EnumToVisibilityConverter>();
|
|
|
|
|
|
services.AddTransient<NullableTimeSpanToSecondsConverter>();
|
|
|
|
|
|
services.AddTransient<BooleanToBrushConverter>();
|
2025-09-14 20:46:31 +08:00
|
|
|
|
services.AddTransient<BoolToVisibilityConverter>();
|
|
|
|
|
|
services.AddTransient<CountToVisibilityConverter>();
|
|
|
|
|
|
services.AddTransient<BoolToStringConverter>();
|
|
|
|
|
|
services.AddTransient<BoolToColorConverter>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-12 13:25:39 +08:00
|
|
|
|
// 注册事件服务
|
2025-09-12 14:59:32 +08:00
|
|
|
|
services.AddSingleton<DMS.Application.Interfaces.IEventService, DMS.Application.Services.EventService>();
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
// 注册新的数据服务
|
|
|
|
|
|
services.AddSingleton<IDeviceDataService, DeviceDataService>();
|
|
|
|
|
|
services.AddSingleton<IVariableDataService, VariableDataService>();
|
|
|
|
|
|
services.AddSingleton<IVariableTableDataService, VariableTableDataService>();
|
|
|
|
|
|
services.AddSingleton<IMenuDataService, MenuDataService>();
|
|
|
|
|
|
services.AddSingleton<IMqttDataService, MqttDataService>();
|
2025-10-11 18:07:01 +08:00
|
|
|
|
services.AddSingleton<IMqttAliasDataService, MqttAliasDataService>();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
services.AddSingleton<ILogDataService, LogDataService>();
|
2025-09-23 06:51:29 +08:00
|
|
|
|
services.AddSingleton<ITriggerDataService, TriggerDataService>(); // 注册触发器数据服务
|
2025-09-09 13:35:16 +08:00
|
|
|
|
services.AddSingleton<IDataEventService, DataEventService>();
|
|
|
|
|
|
services.AddSingleton<IDataStorageService, DataStorageService>();
|
2025-09-13 19:39:18 +08:00
|
|
|
|
services.AddSingleton<IEmailDataService, EmailDataService>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
// 注册主数据服务
|
2025-09-12 14:59:32 +08:00
|
|
|
|
services.AddSingleton<IWPFDataService, WPFDataService>();
|
2025-09-13 19:08:43 +08:00
|
|
|
|
services.AddSingleton<IEmailAppService, EmailAppService>();
|
|
|
|
|
|
services.AddSingleton<EmailFunctionalityTestService, EmailFunctionalityTestService>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-19 09:24:35 +08:00
|
|
|
|
// 注册视图模型
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<SplashViewModel>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<MainViewModel>();
|
|
|
|
|
|
services.AddSingleton<HomeViewModel>();
|
|
|
|
|
|
services.AddSingleton<DevicesViewModel>();
|
|
|
|
|
|
services.AddSingleton<DataTransformViewModel>();
|
|
|
|
|
|
services.AddSingleton<SettingViewModel>();
|
2025-09-07 19:48:48 +08:00
|
|
|
|
services.AddSingleton<LogHistoryViewModel>();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
services.AddTransient<VariableTableViewModel>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<DeviceDetailViewModel>();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
services.AddSingleton<MqttsViewModel>();
|
|
|
|
|
|
services.AddSingleton<LogHistoryViewModel>();
|
2025-09-06 12:03:39 +08:00
|
|
|
|
services.AddScoped<MqttServerDetailViewModel>();
|
2025-09-11 11:04:07 +08:00
|
|
|
|
services.AddSingleton<VariableHistoryViewModel>();
|
2025-09-13 19:08:43 +08:00
|
|
|
|
services.AddSingleton<EmailManagementViewModel>();
|
2025-09-14 16:16:10 +08:00
|
|
|
|
services.AddSingleton<TriggersViewModel>(); // 注册 TriggersViewModel
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-06 19:10:25 +08:00
|
|
|
|
// 注册对话框视图模型
|
|
|
|
|
|
services.AddTransient<DeviceDialogViewModel>();
|
|
|
|
|
|
services.AddTransient<ConfirmDialogViewModel>();
|
|
|
|
|
|
services.AddTransient<VariableTableDialogViewModel>();
|
2025-08-22 20:46:23 +08:00
|
|
|
|
services.AddTransient<ImportExcelDialogViewModel>();
|
2025-08-25 20:16:57 +08:00
|
|
|
|
services.AddTransient<ImportOpcUaDialogViewModel>();
|
2025-08-24 14:42:31 +08:00
|
|
|
|
services.AddTransient<VariableDialogViewModel>();
|
2025-09-06 19:10:25 +08:00
|
|
|
|
services.AddTransient<PollLevelDialogViewModel>();
|
|
|
|
|
|
services.AddTransient<IsActiveDialogViewModel>();
|
|
|
|
|
|
services.AddTransient<MqttDialogViewModel>();
|
|
|
|
|
|
services.AddTransient<MqttSelectionDialogViewModel>();
|
|
|
|
|
|
services.AddTransient<MqttAliasBatchEditDialogViewModel>();
|
2025-09-12 08:57:41 +08:00
|
|
|
|
services.AddTransient<HistorySettingsDialogViewModel>();
|
2025-09-12 12:26:57 +08:00
|
|
|
|
services.AddTransient<AlarmSettingsDialogViewModel>();
|
2025-09-13 19:08:43 +08:00
|
|
|
|
services.AddTransient<EmailAccountDialogViewModel>();
|
|
|
|
|
|
services.AddTransient<EmailTemplateDialogViewModel>();
|
2025-09-14 19:13:40 +08:00
|
|
|
|
services.AddTransient<TriggerDialogViewModel>(); // 注册 TriggerEditorViewModel
|
2025-10-04 20:46:39 +08:00
|
|
|
|
|
|
|
|
|
|
// 注册工厂
|
|
|
|
|
|
services.AddTransient<IVariableItemViewModelFactory, VariableItemViewModelFactory>();
|
2025-09-12 14:59:32 +08:00
|
|
|
|
|
2025-09-04 21:04:27 +08:00
|
|
|
|
// 注册View视图
|
2025-07-26 11:20:03 +08:00
|
|
|
|
services.AddSingleton<SplashWindow>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddSingleton<SettingView>();
|
|
|
|
|
|
services.AddSingleton<MainView>();
|
|
|
|
|
|
services.AddSingleton<HomeView>();
|
|
|
|
|
|
services.AddSingleton<DevicesView>();
|
|
|
|
|
|
services.AddSingleton<VariableTableView>();
|
2025-09-11 11:04:07 +08:00
|
|
|
|
services.AddSingleton<VariableHistoryView>();
|
2025-09-07 19:48:48 +08:00
|
|
|
|
services.AddSingleton<LogHistoryView>();
|
2025-07-19 09:24:35 +08:00
|
|
|
|
services.AddScoped<DeviceDetailView>();
|
|
|
|
|
|
services.AddScoped<MqttsView>();
|
2025-09-13 19:08:43 +08:00
|
|
|
|
services.AddSingleton<EmailManagementView>();
|
2025-09-14 19:13:40 +08:00
|
|
|
|
services.AddSingleton<TriggersView>(); // 注册 TriggersView
|
2025-09-13 19:08:43 +08:00
|
|
|
|
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ConfigureLogging(ILoggingBuilder loggingBuilder)
|
|
|
|
|
|
{
|
2025-09-12 14:59:32 +08:00
|
|
|
|
LogManager.Setup()
|
|
|
|
|
|
.LoadConfigurationFromFile("Configurations/nlog.config");
|
2025-07-19 09:24:35 +08:00
|
|
|
|
loggingBuilder.ClearProviders();
|
|
|
|
|
|
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
|
2025-09-05 16:18:01 +08:00
|
|
|
|
// loggingBuilder.addn; // 添加NLog作为日志提供者
|
2025-07-19 09:24:35 +08:00
|
|
|
|
|
2025-09-09 15:28:07 +08:00
|
|
|
|
// // 捕获未处理的异常并记录
|
|
|
|
|
|
// AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var ex = args.ExceptionObject as Exception;
|
|
|
|
|
|
// if (ex != null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// // 可以使用一个专用的 Logger 来记录未处理异常
|
|
|
|
|
|
// LogManager.GetLogger("UnhandledExceptionLogger")
|
|
|
|
|
|
// .Fatal($"应用程序发生未处理的异常:{ex}");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// };
|
|
|
|
|
|
//
|
|
|
|
|
|
// // 捕获 Dispatcher 线程上的未处理异常 (UI 线程)
|
|
|
|
|
|
// this.DispatcherUnhandledException += (sender, args) =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// LogManager.GetLogger("DispatcherExceptionLogger")
|
|
|
|
|
|
// .Fatal($"UI 线程发生未处理的异常:{args.Exception}");
|
|
|
|
|
|
// // 标记为已处理,防止应用程序崩溃 (生产环境慎用,可能掩盖问题)
|
|
|
|
|
|
// // args.Handled = true;
|
|
|
|
|
|
// };
|
|
|
|
|
|
//
|
|
|
|
|
|
// // 如果您使用 Task (异步方法) 并且没有正确 await,可能会导致异常丢失,
|
|
|
|
|
|
// // 可以通过以下方式捕获 Task 中的异常。
|
|
|
|
|
|
// TaskScheduler.UnobservedTaskException += (sender, args) =>
|
|
|
|
|
|
// {
|
|
|
|
|
|
// LogManager.GetLogger("UnobservedTaskExceptionLogger")
|
|
|
|
|
|
// .Fatal($"异步任务发生未观察到的异常:{args.Exception}");
|
|
|
|
|
|
// // args.SetObserved(); // 标记为已观察,防止进程终止
|
|
|
|
|
|
// };
|
2025-07-19 09:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|