From 6796a06325ad0ec0f8f1d6b3589ee045f87d78dc Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Fri, 12 Sep 2025 14:59:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=EF=BC=8C=E5=B9=B6=E5=AE=8C=E6=88=90=E4=BA=86?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=8A=B6=E6=80=81=E6=94=B9=E5=8F=98=E5=90=8E?= =?UTF-8?q?=E5=86=99=E5=85=A5=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Events/DeviceActiveChangedEventArgs.cs | 2 +- .../Events/MqttConnectionChangedEventArgs.cs | 2 +- .../Events/VariableValueChangedEventArgs.cs | 2 +- .../Interfaces/IDeviceMonitoringService.cs | 11 +++ .../Interfaces/IEventService.cs | 6 +- .../Services/EventService.cs | 10 +- .../Monitoring/DeviceMonitoringService.cs | 55 +++++++++++ DMS.WPF/App.xaml.cs | 91 +++++++------------ DMS.WPF/Interfaces/IWPFDataService.cs | 7 +- DMS.WPF/Services/DeviceMonitoringService.cs | 74 --------------- DMS.WPF/Services/WPFDataService.cs | 9 +- DMS.WPF/ViewModels/DevicesViewModel.cs | 3 - .../ViewModels/Items/DeviceItemViewModel.cs | 5 +- 13 files changed, 113 insertions(+), 164 deletions(-) rename {DMS.WPF => DMS.Application}/Events/DeviceActiveChangedEventArgs.cs (97%) rename {DMS.WPF => DMS.Application}/Events/MqttConnectionChangedEventArgs.cs (97%) rename {DMS.WPF => DMS.Application}/Events/VariableValueChangedEventArgs.cs (97%) create mode 100644 DMS.Application/Interfaces/IDeviceMonitoringService.cs rename {DMS.WPF => DMS.Application}/Interfaces/IEventService.cs (91%) rename {DMS.WPF => DMS.Application}/Services/EventService.cs (87%) create mode 100644 DMS.Application/Services/Monitoring/DeviceMonitoringService.cs delete mode 100644 DMS.WPF/Services/DeviceMonitoringService.cs diff --git a/DMS.WPF/Events/DeviceActiveChangedEventArgs.cs b/DMS.Application/Events/DeviceActiveChangedEventArgs.cs similarity index 97% rename from DMS.WPF/Events/DeviceActiveChangedEventArgs.cs rename to DMS.Application/Events/DeviceActiveChangedEventArgs.cs index 8bd1fa8..3b22cd1 100644 --- a/DMS.WPF/Events/DeviceActiveChangedEventArgs.cs +++ b/DMS.Application/Events/DeviceActiveChangedEventArgs.cs @@ -1,6 +1,6 @@ using System; -namespace DMS.WPF.Events; +namespace DMS.Application.Events; /// /// 设备状态改变事件参数 diff --git a/DMS.WPF/Events/MqttConnectionChangedEventArgs.cs b/DMS.Application/Events/MqttConnectionChangedEventArgs.cs similarity index 97% rename from DMS.WPF/Events/MqttConnectionChangedEventArgs.cs rename to DMS.Application/Events/MqttConnectionChangedEventArgs.cs index 50f5206..adfa426 100644 --- a/DMS.WPF/Events/MqttConnectionChangedEventArgs.cs +++ b/DMS.Application/Events/MqttConnectionChangedEventArgs.cs @@ -1,6 +1,6 @@ using System; -namespace DMS.WPF.Events; +namespace DMS.Application.Events; /// /// MQTT连接状态改变事件参数 diff --git a/DMS.WPF/Events/VariableValueChangedEventArgs.cs b/DMS.Application/Events/VariableValueChangedEventArgs.cs similarity index 97% rename from DMS.WPF/Events/VariableValueChangedEventArgs.cs rename to DMS.Application/Events/VariableValueChangedEventArgs.cs index 7c69e50..e72e3f2 100644 --- a/DMS.WPF/Events/VariableValueChangedEventArgs.cs +++ b/DMS.Application/Events/VariableValueChangedEventArgs.cs @@ -1,6 +1,6 @@ using System; -namespace DMS.WPF.Events; +namespace DMS.Application.Events; /// /// 变量值改变事件参数 diff --git a/DMS.Application/Interfaces/IDeviceMonitoringService.cs b/DMS.Application/Interfaces/IDeviceMonitoringService.cs new file mode 100644 index 0000000..a07229f --- /dev/null +++ b/DMS.Application/Interfaces/IDeviceMonitoringService.cs @@ -0,0 +1,11 @@ +using System; +using DMS.Application.Events; + +namespace DMS.Application.Interfaces; + +/// +/// 设备监视服务接口,用于监视设备的状态变化 +/// +public interface IDeviceMonitoringService +{ +} \ No newline at end of file diff --git a/DMS.WPF/Interfaces/IEventService.cs b/DMS.Application/Interfaces/IEventService.cs similarity index 91% rename from DMS.WPF/Interfaces/IEventService.cs rename to DMS.Application/Interfaces/IEventService.cs index 05967a1..e0b6425 100644 --- a/DMS.WPF/Interfaces/IEventService.cs +++ b/DMS.Application/Interfaces/IEventService.cs @@ -1,7 +1,7 @@ using System; -using DMS.WPF.Events; +using DMS.Application.Events; -namespace DMS.WPF.Interfaces; +namespace DMS.Application.Interfaces; /// /// 事件服务接口,用于统一管理应用程序中的各种事件 @@ -13,7 +13,7 @@ public interface IEventService /// /// 设备状态改变事件 /// - event EventHandler DeviceStatusChanged; + event EventHandler OnDeviceActiveChanged; /// /// 触发设备状态改变事件 diff --git a/DMS.WPF/Services/EventService.cs b/DMS.Application/Services/EventService.cs similarity index 87% rename from DMS.WPF/Services/EventService.cs rename to DMS.Application/Services/EventService.cs index cc6a0e6..d750cb6 100644 --- a/DMS.WPF/Services/EventService.cs +++ b/DMS.Application/Services/EventService.cs @@ -1,8 +1,8 @@ using System; -using DMS.WPF.Events; -using DMS.WPF.Interfaces; +using DMS.Application.Events; +using DMS.Application.Interfaces; -namespace DMS.WPF.Services; +namespace DMS.Application.Services; /// /// 事件服务实现类,用于统一管理应用程序中的各种事件 @@ -14,7 +14,7 @@ public class EventService : IEventService /// /// 设备状态改变事件 /// - public event EventHandler DeviceStatusChanged; + public event EventHandler OnDeviceActiveChanged; /// /// 触发设备状态改变事件 @@ -23,7 +23,7 @@ public class EventService : IEventService /// 设备状态改变事件参数 public void RaiseDeviceStatusChanged(object sender, DeviceActiveChangedEventArgs e) { - DeviceStatusChanged?.Invoke(sender, e); + OnDeviceActiveChanged?.Invoke(sender, e); } #endregion diff --git a/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs b/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs new file mode 100644 index 0000000..1f6629c --- /dev/null +++ b/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Concurrent; +using System.Threading; +using System.Threading.Tasks; +using DMS.Application.Events; +using DMS.Application.Interfaces; +using DMS.Core.Models; +using Microsoft.Extensions.Logging; + +namespace DMS.Application.Services; + +/// +/// 设备监视服务实现类,用于监视设备的状态变化 +/// +public class DeviceMonitoringService : IDeviceMonitoringService, IDisposable +{ + private readonly ILogger _logger; + private readonly IEventService _eventService; + private readonly IAppDataStorageService _appDataStorageService; + private readonly IAppDataCenterService _appDataCenterService; + + + /// + /// 初始化DeviceMonitoringService类的新实例 + /// + /// 日志记录器 + /// 设备应用服务 + public DeviceMonitoringService(ILogger logger, IEventService eventService, + IAppDataStorageService appDataStorageService, + IAppDataCenterService appDataCenterService) + { + _logger = logger; + _eventService = eventService; + _appDataStorageService = appDataStorageService; + _appDataCenterService = appDataCenterService; + _eventService.OnDeviceActiveChanged += OnDeviceActiveChanged; + } + + private void OnDeviceActiveChanged(object? sender, DeviceActiveChangedEventArgs e) + { + if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device)) + { + if (device.IsActive != e.NewStatus) + { + device.IsActive = e.NewStatus; + _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device); + } + } + } + + public void Dispose() + { + _eventService.OnDeviceActiveChanged -= OnDeviceActiveChanged; + } +} \ No newline at end of file diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index ec46eb5..37c9112 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -20,6 +20,7 @@ using DMS.WPF.Logging; using DMS.WPF.Services; using DMS.WPF.ViewModels; using DMS.WPF.ViewModels.Dialogs; +using DMS.WPF.ViewModels.Items; using DMS.WPF.Views; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -44,15 +45,9 @@ public partial class App : System.Windows.Application public App() { Host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder() - .ConfigureServices((context, services) => - { - ConfigureServices(services); - }) - .ConfigureLogging(loggingBuilder => - { - ConfigureLogging(loggingBuilder); - }) - .Build(); + .ConfigureServices((context, services) => { ConfigureServices(services); }) + .ConfigureLogging(loggingBuilder => { ConfigureLogging(loggingBuilder); }) + .Build(); Services = Host.Services; _logger = Host.Services.GetRequiredService>(); } @@ -67,10 +62,8 @@ public partial class App : System.Windows.Application try { Host.Services.GetRequiredService(); - - // 初始化设备监控服务 - Host.Services.GetRequiredService(); - + Host.Services.GetRequiredService(); + DeviceItemViewModel.EventService = Host.Services.GetRequiredService(); // 初始化数据处理链 var dataProcessingService = Host.Services.GetRequiredService(); dataProcessingService.AddProcessor(Host.Services.GetRequiredService()); @@ -84,7 +77,7 @@ public partial class App : System.Windows.Application var notificationService = Host.Services.GetRequiredService(); notificationService.ShowError($"加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}", exception); } - + var splashWindow = Host.Services.GetRequiredService(); splashWindow.Show(); } @@ -155,13 +148,14 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - + // 注册核心服务 services.AddAutoMapper(cfg => { // 最终解决方案:根据异常信息的建议,设置此标记以忽略重复的Profile加载。 // 注意:此属性位于 Internal() 方法下。 - cfg.Internal().AllowAdditiveTypeMapCreation = true; + cfg.Internal() + .AllowAdditiveTypeMapCreation = true; cfg.AddProfile(new DMS.Application.Profiles.MappingProfile()); cfg.AddProfile(new DMS.Infrastructure.Profiles.MappingProfile()); @@ -180,16 +174,17 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); services.AddHostedService(); - - + + services.AddSingleton(); - services.AddHostedService(provider => (DataProcessingService)provider.GetRequiredService()); + services.AddHostedService(provider => + (DataProcessingService)provider.GetRequiredService()); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - + // 注册Core中的仓库 services.AddSingleton(); services.AddSingleton(_ => @@ -197,7 +192,7 @@ public partial class App : System.Windows.Application var appSettings = new AppSettings { Database = { Database = "dms_test" } }; return new SqlSugarDbContext(appSettings); }); - + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -214,14 +209,14 @@ public partial class App : System.Windows.Application services.AddTransient(); services.AddTransient(); services.AddTransient(); - + // 注册App服务\r\n services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -232,24 +227,22 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - + services.AddSingleton(); services.AddSingleton(); - + services.AddSingleton(); + // 注册MQTT服务管理器 services.AddSingleton(); services.AddSingleton(); services.AddHostedService(); - + // 注册WPF中的服务 services.AddSingleton(); - + // 注册事件服务 - services.AddSingleton(); - - // 注册设备监控服务 - services.AddSingleton(); - + services.AddSingleton(); + // 注册新的数据服务 services.AddSingleton(); services.AddSingleton(); @@ -259,34 +252,11 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); - + // 注册主数据服务 - services.AddSingleton(provider => - new WPFDataService( - provider.GetRequiredService(), - provider.GetRequiredService(), - provider.GetRequiredService(), - provider.GetRequiredService(), - provider.GetRequiredService(), - provider.GetRequiredService(), - provider.GetRequiredService(), - provider.GetRequiredService(), - provider.GetRequiredService() - )); - - // 保留原DataServices以保证现有代码兼容性(可选,建议逐步移除) - // services.AddSingleton(provider => - // new DataServices( - // provider.GetRequiredService(), - // provider.GetRequiredService(), - // provider.GetRequiredService() - // ) - // ); - + services.AddSingleton(); - - // 注册视图模型 services.AddSingleton(); services.AddSingleton(); @@ -301,7 +271,7 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddScoped(); services.AddSingleton(); - + // 注册对话框视图模型 services.AddTransient(); services.AddTransient(); @@ -316,7 +286,7 @@ public partial class App : System.Windows.Application services.AddTransient(); services.AddTransient(); services.AddTransient(); - + // 注册View视图 services.AddSingleton(); services.AddSingleton(); @@ -332,7 +302,8 @@ public partial class App : System.Windows.Application private void ConfigureLogging(ILoggingBuilder loggingBuilder) { - LogManager.Setup().LoadConfigurationFromFile("Configurations/nlog.config"); + LogManager.Setup() + .LoadConfigurationFromFile("Configurations/nlog.config"); loggingBuilder.ClearProviders(); loggingBuilder.SetMinimumLevel(LogLevel.Trace); // loggingBuilder.addn; // 添加NLog作为日志提供者 diff --git a/DMS.WPF/Interfaces/IWPFDataService.cs b/DMS.WPF/Interfaces/IWPFDataService.cs index 0073b8f..72df67e 100644 --- a/DMS.WPF/Interfaces/IWPFDataService.cs +++ b/DMS.WPF/Interfaces/IWPFDataService.cs @@ -1,4 +1,4 @@ -using DMS.WPF.Interfaces; +using DMS.Application.Interfaces; namespace DMS.WPF.Interfaces; @@ -35,10 +35,5 @@ public interface IWPFDataService /// 日志数据服务。 /// ILogDataService LogDataService { get; } - - /// - /// 事件服务。 - /// - IEventService EventService { get; } } \ No newline at end of file diff --git a/DMS.WPF/Services/DeviceMonitoringService.cs b/DMS.WPF/Services/DeviceMonitoringService.cs deleted file mode 100644 index 19b7740..0000000 --- a/DMS.WPF/Services/DeviceMonitoringService.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using DMS.WPF.Events; -using DMS.WPF.Interfaces; -using Microsoft.Extensions.Logging; - -namespace DMS.WPF.Services; - -/// -/// 设备监控服务,用于监听设备状态改变事件并进行相应处理 -/// -public class DeviceMonitoringService -{ - private readonly ILogger _logger; - private readonly IEventService _eventService; - private readonly INotificationService _notificationService; - - /// - /// 初始化DeviceMonitoringService类的新实例 - /// - /// 日志服务 - /// 事件服务 - /// 通知服务 - public DeviceMonitoringService( - ILogger logger, - IEventService eventService, - INotificationService notificationService) - { - _logger = logger; - _eventService = eventService; - _notificationService = notificationService; - - // 订阅设备状态改变事件 - _eventService.DeviceStatusChanged += OnDeviceStatusChanged; - } - - /// - /// 处理设备状态改变事件 - /// - /// 事件发送者 - /// 设备状态改变事件参数 - private void OnDeviceStatusChanged(object sender, DeviceActiveChangedEventArgs e) - { - try - { - // 记录设备状态改变日志 - _logger.LogInformation($"设备 {e.DeviceName}(ID: {e.DeviceId}) 状态改变: {e.OldStatus} -> {e.NewStatus}"); - - // 根据设备状态改变发送通知 - string message = e.NewStatus - ? $"设备 {e.DeviceName} 已启动" - : $"设备 {e.DeviceName} 已停止"; - - _notificationService.ShowInfo(message); - - // 在这里可以添加更多处理逻辑,例如: - // 1. 更新数据库中的设备状态历史记录 - // 2. 发送邮件或短信通知 - // 3. 触发其他相关操作 - } - catch (Exception ex) - { - _logger.LogError(ex, $"处理设备 {e.DeviceName} 状态改变事件时发生错误"); - } - } - - /// - /// 释放资源 - /// - public void Dispose() - { - // 取消订阅事件 - _eventService.DeviceStatusChanged -= OnDeviceStatusChanged; - } -} \ No newline at end of file diff --git a/DMS.WPF/Services/WPFDataService.cs b/DMS.WPF/Services/WPFDataService.cs index ba6c025..ac78df9 100644 --- a/DMS.WPF/Services/WPFDataService.cs +++ b/DMS.WPF/Services/WPFDataService.cs @@ -44,11 +44,6 @@ public class WPFDataService : IWPFDataService /// 日志数据服务。 /// public ILogDataService LogDataService { get; } - - /// - /// 事件服务。 - /// - public IEventService EventService { get; } /// /// WPFDataService 构造函数。 @@ -61,8 +56,7 @@ public class WPFDataService : IWPFDataService IMenuDataService menuDataService, IMqttDataService mqttDataService, ILogDataService logDataService, - IVariableTableDataService variableTableDataService, - IEventService eventService) + IVariableTableDataService variableTableDataService) { _mapper = mapper; _appDataCenterService = appDataCenterService; @@ -72,6 +66,5 @@ public class WPFDataService : IWPFDataService MqttDataService = mqttDataService; LogDataService = logDataService; VariableTableDataService = variableTableDataService; - EventService = eventService; } } diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index bc6d54a..0b9c67f 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -64,9 +64,6 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable _deviceAppService = deviceAppService; _notificationService = notificationService; Devices = _dataStorageService.Devices; - - // 设置DeviceItemViewModel的静态服务引用 - DeviceItemViewModel.EventService = wpfDataService.EventService; } diff --git a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs index 5a32d3c..4818ba4 100644 --- a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs @@ -2,8 +2,9 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; +using DMS.Application.Events; +using DMS.Application.Interfaces; using DMS.Core.Enums; -using DMS.WPF.Events; using DMS.WPF.Interfaces; namespace DMS.WPF.ViewModels.Items; @@ -83,7 +84,7 @@ public partial class DeviceItemViewModel : ObservableObject partial void OnIsActiveChanged(bool oldValue, bool newValue) { // 只有当设备ID有效且事件服务已初始化时才发布事件 - if (Id > 0 && EventService != null) + if (Id > 0 && EventService != null ) { // 发布设备状态改变事件 EventService.RaiseDeviceStatusChanged(this, new DeviceActiveChangedEventArgs(Id, Name, oldValue, newValue));