From b85ffdc21b1d0f2d6667c030a78a7923b098a42d Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Sun, 5 Oct 2025 11:21:05 +0800 Subject: [PATCH] =?UTF-8?q?=20=20=20=201=20feat:=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=88=90=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=A4=84=E7=90=86=E6=9C=BA=E5=88=B6=20=20=20?= =?UTF-8?q?=20=202=20=20=20=20=203=20-=20=E4=BB=8EIDataLoaderService?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=B8=AD=E7=A7=BB=E9=99=A4OnLoadDataComplete?= =?UTF-8?q?d=E4=BA=8B=E4=BB=B6=20=20=20=20=204=20-=20=E5=9C=A8IEventServic?= =?UTF-8?q?e=E6=8E=A5=E5=8F=A3=E4=B8=AD=E6=96=B0=E5=A2=9EOnLoadDataComplet?= =?UTF-8?q?ed=E4=BA=8B=E4=BB=B6=E5=92=8CRaiseLoadDataCompleted=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20=20=20=20=205=20-=20=E5=9C=A8EventService=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=B1=BB=E4=B8=AD=E5=AE=9E=E7=8E=B0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=88=90=E4=BA=8B=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E5=8A=9F=E8=83=BD=20=20=20=20=206=20-=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9DataLoaderService=E4=B8=8D=E5=86=8D=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=A7=A6=E5=8F=91=E4=BA=8B=E4=BB=B6=EF=BC=8C=E8=80=8C?= =?UTF-8?q?=E6=98=AF=E9=80=9A=E8=BF=87IEventService=E6=9D=A5=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=20=20=20=20=207=20-=20=E6=9B=B4=E6=96=B0MQTT=E3=80=81?= =?UTF-8?q?OPC=20UA=E5=92=8CS7=E5=90=8E=E5=8F=B0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E4=BB=A5=E8=AE=A2=E9=98=85=E4=BA=8B=E4=BB=B6=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=95=B0=E6=8D=AE=E5=8A=A0=E8=BD=BD=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E4=BA=8B=E4=BB=B6=20=20=20=20=208=20-=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=95=B0=E6=8D=AE=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E7=9A=84=E7=9B=91=E5=90=AC=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E8=BF=9B=E8=A1=8C=E7=AE=A1=E7=90=86=20=20=20?= =?UTF-8?q?=20=209=20=20=20=2010=20=E6=AD=A4=E9=87=8D=E6=9E=84=E6=94=B9?= =?UTF-8?q?=E8=BF=9B=E4=BA=86=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86=E7=9A=84?= =?UTF-8?q?=E6=9E=B6=E6=9E=84=E8=AE=BE=E8=AE=A1=EF=BC=8C=E4=BD=BF=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E7=AE=A1=E7=90=86=E6=9B=B4=E5=8A=A0=E9=9B=86=E4=B8=AD?= =?UTF-8?q?=E5=92=8C=E4=B8=80=E8=87=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interfaces/IDataLoaderService.cs | 5 +---- DMS.Application/Interfaces/IEventService.cs | 12 ++++++++++++ DMS.Application/Services/DataLoaderService.cs | 12 ++++++------ DMS.Application/Services/EventService.cs | 19 +++++++++++++++++++ .../Services/Mqtt/MqttBackgroundService.cs | 7 +++++-- .../OpcUa/OptimizedOpcUaBackgroundService.cs | 7 ++++--- .../S7/OptimizedS7BackgroundService.cs | 2 +- DMS.WPF/Services/DataEventService.cs | 2 +- 8 files changed, 49 insertions(+), 17 deletions(-) diff --git a/DMS.Application/Interfaces/IDataLoaderService.cs b/DMS.Application/Interfaces/IDataLoaderService.cs index d3ba392..851ca72 100644 --- a/DMS.Application/Interfaces/IDataLoaderService.cs +++ b/DMS.Application/Interfaces/IDataLoaderService.cs @@ -44,8 +44,5 @@ public interface IDataLoaderService /// Task LoadAllNlogsAsync(int count); - /// - /// 当数据加载完成时触发 - /// - event EventHandler OnLoadDataCompleted; + } \ No newline at end of file diff --git a/DMS.Application/Interfaces/IEventService.cs b/DMS.Application/Interfaces/IEventService.cs index 0b152da..e8a9b15 100644 --- a/DMS.Application/Interfaces/IEventService.cs +++ b/DMS.Application/Interfaces/IEventService.cs @@ -125,4 +125,16 @@ public interface IEventService /// 变量启停改变事件 /// event EventHandler OnVariableActiveChanged; + + /// + /// 数据加载完成事件 + /// + event EventHandler OnLoadDataCompleted; + + /// + /// 触发数据加载完成事件 + /// + /// 事件发送者 + /// 数据加载完成事件参数 + void RaiseLoadDataCompleted(object sender, DataLoadCompletedEventArgs e); } \ No newline at end of file diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs index b9b472e..ddc7cd2 100644 --- a/DMS.Application/Services/DataLoaderService.cs +++ b/DMS.Application/Services/DataLoaderService.cs @@ -28,11 +28,9 @@ public class DataLoaderService : IDataLoaderService private readonly IMqttAppService _mqttAppService; private readonly INlogAppService _nlogAppService; private readonly ITriggerManagementService _triggerManagementService; // 添加触发器管理服务 + private readonly IEventService _eventService; // 添加事件服务 + - /// - /// 当数据加载完成时触发 - /// - public event EventHandler OnLoadDataCompleted; public const int LoadLogCount =100; public DataLoaderService( @@ -45,7 +43,8 @@ public class DataLoaderService : IDataLoaderService IMenuService menuService, IMqttAppService mqttAppService, INlogAppService nlogAppService, - ITriggerManagementService triggerManagementService) // 添加触发器管理服务参数 + ITriggerManagementService triggerManagementService, // 添加触发器管理服务参数 + IEventService eventService) // 添加事件服务参数 { _repositoryManager = repositoryManager; _mapper = mapper; @@ -57,6 +56,7 @@ public class DataLoaderService : IDataLoaderService _mqttAppService = mqttAppService; _nlogAppService = nlogAppService; _triggerManagementService = triggerManagementService; // 初始化触发器管理服务 + _eventService = eventService; // 初始化事件服务 } @@ -86,7 +86,7 @@ public class DataLoaderService : IDataLoaderService // 加载所有触发器 await LoadAllTriggersAsync(); - OnLoadDataCompleted?.Invoke(this, new DataLoadCompletedEventArgs(true, "数据加载成功")); + _eventService.RaiseLoadDataCompleted(this, new DataLoadCompletedEventArgs(true, "数据加载成功")); } /// diff --git a/DMS.Application/Services/EventService.cs b/DMS.Application/Services/EventService.cs index 4f487e2..205fe22 100644 --- a/DMS.Application/Services/EventService.cs +++ b/DMS.Application/Services/EventService.cs @@ -161,4 +161,23 @@ public class EventService : IEventService } #endregion + + #region 数据加载事件 + + /// + /// 数据加载完成事件 + /// + public event EventHandler OnLoadDataCompleted; + + /// + /// 触发数据加载完成事件 + /// + /// 事件发送者 + /// 数据加载完成事件参数 + public void RaiseLoadDataCompleted(object sender, DataLoadCompletedEventArgs e) + { + OnLoadDataCompleted?.Invoke(sender, e); + } + + #endregion } \ No newline at end of file diff --git a/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs b/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs index ef4d9a9..3aea17e 100644 --- a/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs +++ b/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs @@ -16,6 +16,7 @@ namespace DMS.Infrastructure.Services.Mqtt { private readonly ILogger _logger; private readonly IMqttServiceManager _mqttServiceManager; + private readonly IEventService _eventService; private readonly IAppDataStorageService _appDataStorageService; private readonly IAppDataCenterService _appDataCenterService; private readonly ConcurrentDictionary _mqttServers; @@ -24,16 +25,18 @@ namespace DMS.Infrastructure.Services.Mqtt public MqttBackgroundService( ILogger logger, IMqttServiceManager mqttServiceManager, + IEventService eventService, IAppDataStorageService appDataStorageService, IAppDataCenterService appDataCenterService) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _mqttServiceManager = mqttServiceManager ?? throw new ArgumentNullException(nameof(mqttServiceManager)); + _eventService = eventService; _appDataStorageService = appDataStorageService; _appDataCenterService = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService)); _mqttServers = new ConcurrentDictionary(); - _appDataCenterService.DataLoaderService.OnLoadDataCompleted += OnLoadDataCompleted; + _eventService.OnLoadDataCompleted += OnLoadDataCompleted; } private void OnLoadDataCompleted(object? sender, DataLoadCompletedEventArgs e) @@ -250,7 +253,7 @@ namespace DMS.Infrastructure.Services.Mqtt { _logger.LogInformation("正在释放MQTT后台服务资源..."); - _appDataCenterService.DataLoaderService.OnLoadDataCompleted -= OnLoadDataCompleted; + _eventService.OnLoadDataCompleted -= OnLoadDataCompleted; _reloadSemaphore?.Dispose(); base.Dispose(); diff --git a/DMS.Infrastructure/Services/OpcUa/OptimizedOpcUaBackgroundService.cs b/DMS.Infrastructure/Services/OpcUa/OptimizedOpcUaBackgroundService.cs index 78238cb..18e26ea 100644 --- a/DMS.Infrastructure/Services/OpcUa/OptimizedOpcUaBackgroundService.cs +++ b/DMS.Infrastructure/Services/OpcUa/OptimizedOpcUaBackgroundService.cs @@ -23,6 +23,7 @@ namespace DMS.Infrastructure.Services.OpcUa public OptimizedOpcUaBackgroundService( IAppDataCenterService appDataCenterService, IAppDataStorageService appDataStorageService, + IEventService eventService, IOpcUaServiceManager opcUaServiceManager, ILogger logger) { @@ -30,8 +31,8 @@ namespace DMS.Infrastructure.Services.OpcUa _appDataStorageService = appDataStorageService; _opcUaServiceManager = opcUaServiceManager ?? throw new ArgumentNullException(nameof(opcUaServiceManager)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - - _appDataCenterService.DataLoaderService.OnLoadDataCompleted += OnLoadDataCompleted; + _eventService = eventService; + _eventService.OnLoadDataCompleted += OnLoadDataCompleted; } @@ -146,7 +147,7 @@ namespace DMS.Infrastructure.Services.OpcUa { _logger.LogInformation("正在释放OPC UA后台服务资源..."); - _appDataCenterService.DataLoaderService.OnLoadDataCompleted -= OnLoadDataCompleted; + _eventService.OnLoadDataCompleted -= OnLoadDataCompleted; _reloadSemaphore?.Dispose(); base.Dispose(); diff --git a/DMS.Infrastructure/Services/S7/OptimizedS7BackgroundService.cs b/DMS.Infrastructure/Services/S7/OptimizedS7BackgroundService.cs index f24624b..0473856 100644 --- a/DMS.Infrastructure/Services/S7/OptimizedS7BackgroundService.cs +++ b/DMS.Infrastructure/Services/S7/OptimizedS7BackgroundService.cs @@ -51,7 +51,7 @@ public class OptimizedS7BackgroundService : BackgroundService _s7ServiceManager = s7ServiceManager; _logger = logger; - _appDataCenterService.DataLoaderService.OnLoadDataCompleted += OnLoadDataCompleted; + _eventService.OnLoadDataCompleted += OnLoadDataCompleted; } diff --git a/DMS.WPF/Services/DataEventService.cs b/DMS.WPF/Services/DataEventService.cs index 2325507..b789a7f 100644 --- a/DMS.WPF/Services/DataEventService.cs +++ b/DMS.WPF/Services/DataEventService.cs @@ -49,7 +49,7 @@ public class DataEventService : IDataEventService // 监听变量值变更事件 _eventService.OnVariableValueChanged += OnVariableValueChanged; _eventService.OnMqttServerChanged += OnMqttServerChanged; - _appDataCenterService.DataLoaderService.OnLoadDataCompleted += OnLoadDataCompleted; + _eventService.OnLoadDataCompleted += OnLoadDataCompleted; // 监听日志变更事件 // _appDataCenterService.OnLogChanged += _logDataService.OnNlogChanged;