diff --git a/DMS.Application/DTOs/Events/DataChangedEventArgs.cs b/DMS.Application/Events/DataChangedEventArgs.cs similarity index 93% rename from DMS.Application/DTOs/Events/DataChangedEventArgs.cs rename to DMS.Application/Events/DataChangedEventArgs.cs index 8f503b0..29dab72 100644 --- a/DMS.Application/DTOs/Events/DataChangedEventArgs.cs +++ b/DMS.Application/Events/DataChangedEventArgs.cs @@ -1,7 +1,6 @@ -using System; using DMS.Core.Enums; -namespace DMS.Application.DTOs.Events +namespace DMS.Application.Events { /// /// 数据变更事件参数基类 diff --git a/DMS.Application/DTOs/Events/DataLoadCompletedEventArgs.cs b/DMS.Application/Events/DataLoadCompletedEventArgs.cs similarity index 91% rename from DMS.Application/DTOs/Events/DataLoadCompletedEventArgs.cs rename to DMS.Application/Events/DataLoadCompletedEventArgs.cs index b81a3cd..ab6aab1 100644 --- a/DMS.Application/DTOs/Events/DataLoadCompletedEventArgs.cs +++ b/DMS.Application/Events/DataLoadCompletedEventArgs.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; - -namespace DMS.Application.DTOs.Events +namespace DMS.Application.Events { /// /// 数据加载完成事件参数 diff --git a/DMS.Application/DTOs/Events/DeviceChangedEventArgs.cs b/DMS.Application/Events/DeviceChangedEventArgs.cs similarity index 93% rename from DMS.Application/DTOs/Events/DeviceChangedEventArgs.cs rename to DMS.Application/Events/DeviceChangedEventArgs.cs index 2be3d8c..d76c74f 100644 --- a/DMS.Application/DTOs/Events/DeviceChangedEventArgs.cs +++ b/DMS.Application/Events/DeviceChangedEventArgs.cs @@ -1,7 +1,7 @@ -using System; +using DMS.Application.DTOs; using DMS.Core.Enums; -namespace DMS.Application.DTOs.Events +namespace DMS.Application.Events { /// /// 设备变更事件参数 diff --git a/DMS.Application/DTOs/Events/MenuChangedEventArgs.cs b/DMS.Application/Events/MenuChangedEventArgs.cs similarity index 94% rename from DMS.Application/DTOs/Events/MenuChangedEventArgs.cs rename to DMS.Application/Events/MenuChangedEventArgs.cs index d716cb2..c78c7f5 100644 --- a/DMS.Application/DTOs/Events/MenuChangedEventArgs.cs +++ b/DMS.Application/Events/MenuChangedEventArgs.cs @@ -1,7 +1,7 @@ -using System; +using DMS.Application.DTOs; using DMS.Core.Enums; -namespace DMS.Application.DTOs.Events +namespace DMS.Application.Events { /// /// 菜单变更事件参数 diff --git a/DMS.Application/DTOs/Events/MqttServerChangedEventArgs.cs b/DMS.Application/Events/MqttServerChangedEventArgs.cs similarity index 94% rename from DMS.Application/DTOs/Events/MqttServerChangedEventArgs.cs rename to DMS.Application/Events/MqttServerChangedEventArgs.cs index 484a623..c39060d 100644 --- a/DMS.Application/DTOs/Events/MqttServerChangedEventArgs.cs +++ b/DMS.Application/Events/MqttServerChangedEventArgs.cs @@ -1,7 +1,7 @@ -using System; +using DMS.Application.DTOs; using DMS.Core.Enums; -namespace DMS.Application.DTOs.Events +namespace DMS.Application.Events { /// /// MQTT服务器变更事件参数 diff --git a/DMS.Application/DTOs/Events/NlogChangedEventArgs.cs b/DMS.Application/Events/NlogChangedEventArgs.cs similarity index 90% rename from DMS.Application/DTOs/Events/NlogChangedEventArgs.cs rename to DMS.Application/Events/NlogChangedEventArgs.cs index f4798c8..71fa3f1 100644 --- a/DMS.Application/DTOs/Events/NlogChangedEventArgs.cs +++ b/DMS.Application/Events/NlogChangedEventArgs.cs @@ -1,7 +1,7 @@ -using System; +using DMS.Application.DTOs; using DMS.Core.Enums; -namespace DMS.Application.DTOs.Events +namespace DMS.Application.Events { /// /// Nlog日志变更事件参数 diff --git a/DMS.Application/DTOs/Events/VariableChangedEventArgs.cs b/DMS.Application/Events/VariableChangedEventArgs.cs similarity index 95% rename from DMS.Application/DTOs/Events/VariableChangedEventArgs.cs rename to DMS.Application/Events/VariableChangedEventArgs.cs index 9ed030a..a094943 100644 --- a/DMS.Application/DTOs/Events/VariableChangedEventArgs.cs +++ b/DMS.Application/Events/VariableChangedEventArgs.cs @@ -1,7 +1,7 @@ -using System; +using DMS.Application.DTOs; using DMS.Core.Enums; -namespace DMS.Application.DTOs.Events +namespace DMS.Application.Events { /// /// 变量变更事件参数 diff --git a/DMS.Application/DTOs/Events/VariableTableChangedEventArgs.cs b/DMS.Application/Events/VariableTableChangedEventArgs.cs similarity index 95% rename from DMS.Application/DTOs/Events/VariableTableChangedEventArgs.cs rename to DMS.Application/Events/VariableTableChangedEventArgs.cs index 053468d..2dbacfd 100644 --- a/DMS.Application/DTOs/Events/VariableTableChangedEventArgs.cs +++ b/DMS.Application/Events/VariableTableChangedEventArgs.cs @@ -1,7 +1,7 @@ -using System; +using DMS.Application.DTOs; using DMS.Core.Enums; -namespace DMS.Application.DTOs.Events +namespace DMS.Application.Events { /// /// 变量表变更事件参数 diff --git a/DMS.Application/Interfaces/IAppDataCenterService.cs b/DMS.Application/Interfaces/IAppDataCenterService.cs index 40b8560..020a29c 100644 --- a/DMS.Application/Interfaces/IAppDataCenterService.cs +++ b/DMS.Application/Interfaces/IAppDataCenterService.cs @@ -1,6 +1,5 @@ using System.Collections.Concurrent; using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; using DMS.Application.Interfaces.Management; using DMS.Application.Services; using DMS.Core.Models; diff --git a/DMS.Application/Interfaces/IDataLoaderService.cs b/DMS.Application/Interfaces/IDataLoaderService.cs index 425f855..d3ba392 100644 --- a/DMS.Application/Interfaces/IDataLoaderService.cs +++ b/DMS.Application/Interfaces/IDataLoaderService.cs @@ -1,6 +1,6 @@ using System.Collections.Concurrent; using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; namespace DMS.Application.Interfaces; diff --git a/DMS.Application/Interfaces/IDeviceMonitoringService.cs b/DMS.Application/Interfaces/IDeviceMonitoringService.cs index a07229f..0d5b1d1 100644 --- a/DMS.Application/Interfaces/IDeviceMonitoringService.cs +++ b/DMS.Application/Interfaces/IDeviceMonitoringService.cs @@ -1,5 +1,4 @@ using System; -using DMS.Application.Events; namespace DMS.Application.Interfaces; diff --git a/DMS.Application/Interfaces/IEventService.cs b/DMS.Application/Interfaces/IEventService.cs index 6b96af4..b76005f 100644 --- a/DMS.Application/Interfaces/IEventService.cs +++ b/DMS.Application/Interfaces/IEventService.cs @@ -1,5 +1,4 @@ using System; -using DMS.Application.DTOs.Events; using DMS.Application.Events; using DMS.Core.Events; @@ -81,4 +80,11 @@ public interface IEventService /// 事件发送者 /// 变量值改变事件参数 void RaiseVariableChanged(object sender, VariableChangedEventArgs e); + + void RaiseVariableActiveChanged(object sender,VariablesActiveChangedEventArgs e); + + /// + /// 变量启停改变事件 + /// + event EventHandler OnVariableActiveChanged; } \ No newline at end of file diff --git a/DMS.Application/Interfaces/Management/IDeviceManagementService.cs b/DMS.Application/Interfaces/Management/IDeviceManagementService.cs index f97debc..60ae52f 100644 --- a/DMS.Application/Interfaces/Management/IDeviceManagementService.cs +++ b/DMS.Application/Interfaces/Management/IDeviceManagementService.cs @@ -1,5 +1,5 @@ using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; namespace DMS.Application.Interfaces.Management; diff --git a/DMS.Application/Interfaces/Management/ILogManagementService.cs b/DMS.Application/Interfaces/Management/ILogManagementService.cs index f66dc37..d840b4f 100644 --- a/DMS.Application/Interfaces/Management/ILogManagementService.cs +++ b/DMS.Application/Interfaces/Management/ILogManagementService.cs @@ -1,5 +1,5 @@ using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; namespace DMS.Application.Interfaces.Management; diff --git a/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs b/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs index a16ca7b..72b163d 100644 --- a/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs +++ b/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs @@ -1,5 +1,5 @@ using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; namespace DMS.Application.Interfaces.Management; diff --git a/DMS.Application/Services/AppDataCenterService.cs b/DMS.Application/Services/AppDataCenterService.cs index 2ae495e..0954604 100644 --- a/DMS.Application/Services/AppDataCenterService.cs +++ b/DMS.Application/Services/AppDataCenterService.cs @@ -1,6 +1,5 @@ using AutoMapper; using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; using DMS.Core.Models; using DMS.Application.Interfaces; using DMS.Core.Interfaces; @@ -11,6 +10,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using System; using System.Linq; +using DMS.Application.Events; using DMS.Application.Interfaces.Management; using DMS.Core.Events; diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs index 57c46de..6176555 100644 --- a/DMS.Application/Services/DataLoaderService.cs +++ b/DMS.Application/Services/DataLoaderService.cs @@ -3,7 +3,7 @@ using DMS.Application.DTOs; using DMS.Application.Interfaces; using DMS.Core.Interfaces; using System.Collections.Concurrent; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces.Database; using DMS.Core.Models; diff --git a/DMS.Application/Services/EventService.cs b/DMS.Application/Services/EventService.cs index 79b979d..0ef7b7a 100644 --- a/DMS.Application/Services/EventService.cs +++ b/DMS.Application/Services/EventService.cs @@ -1,5 +1,4 @@ using System; -using DMS.Application.DTOs.Events; using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Core.Events; @@ -77,6 +76,16 @@ public class EventService : IEventService OnVariableChanged?.Invoke(sender, e); } + + /// + /// 变量启停改变事件 + /// + public event EventHandler OnVariableActiveChanged; + public void RaiseVariableActiveChanged(object sender, VariablesActiveChangedEventArgs e) + { + OnVariableActiveChanged?.Invoke(sender, e); + } + /// /// 变量值改变事件 diff --git a/DMS.Application/Services/Management/DeviceManagementService.cs b/DMS.Application/Services/Management/DeviceManagementService.cs index e9204e0..c444cf7 100644 --- a/DMS.Application/Services/Management/DeviceManagementService.cs +++ b/DMS.Application/Services/Management/DeviceManagementService.cs @@ -1,5 +1,5 @@ using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Application.Interfaces.Management; diff --git a/DMS.Application/Services/Management/LogManagementService.cs b/DMS.Application/Services/Management/LogManagementService.cs index 2c13dd0..9c88c8c 100644 --- a/DMS.Application/Services/Management/LogManagementService.cs +++ b/DMS.Application/Services/Management/LogManagementService.cs @@ -1,5 +1,5 @@ using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Application.Interfaces.Management; diff --git a/DMS.Application/Services/Management/MenuManagementService.cs b/DMS.Application/Services/Management/MenuManagementService.cs index 7ef02a8..f1efeb0 100644 --- a/DMS.Application/Services/Management/MenuManagementService.cs +++ b/DMS.Application/Services/Management/MenuManagementService.cs @@ -1,5 +1,5 @@ using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Management; using DMS.Core.Enums; diff --git a/DMS.Application/Services/Management/MqttManagementService.cs b/DMS.Application/Services/Management/MqttManagementService.cs index 27d6bbb..331eb3f 100644 --- a/DMS.Application/Services/Management/MqttManagementService.cs +++ b/DMS.Application/Services/Management/MqttManagementService.cs @@ -1,5 +1,5 @@ using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Application.Interfaces.Management; diff --git a/DMS.Application/Services/Management/VariableManagementService.cs b/DMS.Application/Services/Management/VariableManagementService.cs index d87a5fd..6956583 100644 --- a/DMS.Application/Services/Management/VariableManagementService.cs +++ b/DMS.Application/Services/Management/VariableManagementService.cs @@ -1,6 +1,6 @@ using System.Collections.Concurrent; using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Application.Interfaces.Management; diff --git a/DMS.Application/Services/Management/VariableTableManagementService.cs b/DMS.Application/Services/Management/VariableTableManagementService.cs index 0aee0d1..4c5e342 100644 --- a/DMS.Application/Services/Management/VariableTableManagementService.cs +++ b/DMS.Application/Services/Management/VariableTableManagementService.cs @@ -1,6 +1,6 @@ using System.Collections.Concurrent; using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Application.Interfaces.Management; diff --git a/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs b/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs index 868e0d6..2f48abd 100644 --- a/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs +++ b/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs @@ -2,8 +2,8 @@ using System; using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; -using DMS.Application.Events; using DMS.Application.Interfaces; +using DMS.Core.Events; using DMS.Core.Models; using Microsoft.Extensions.Logging; diff --git a/DMS.Application/Services/Processors/HistoryProcessor.cs b/DMS.Application/Services/Processors/HistoryProcessor.cs index 0adf06d..7d5aae9 100644 --- a/DMS.Application/Services/Processors/HistoryProcessor.cs +++ b/DMS.Application/Services/Processors/HistoryProcessor.cs @@ -33,7 +33,7 @@ public class HistoryProcessor : IVariableProcessor, IDisposable // 只有当数据需要保存时才记录历史 if (!context.Data.IsHistoryEnabled) // 如果数据已经被其他处理器处理过或者不需要保存,则跳过 { - _logger.LogDebug("变量 {VariableName} (ID: {VariableId}) 历史记录已禁用,跳过处理", context.Data.Name, context.Data.Id); + // _logger.LogDebug("变量 {VariableName} (ID: {VariableId}) 历史记录已禁用,跳过处理", context.Data.Name, context.Data.Id); return; } diff --git a/DMS.Application/Services/Processors/ValueConvertProcessor.cs b/DMS.Application/Services/Processors/ValueConvertProcessor.cs index ca8cd42..fe3c739 100644 --- a/DMS.Application/Services/Processors/ValueConvertProcessor.cs +++ b/DMS.Application/Services/Processors/ValueConvertProcessor.cs @@ -20,7 +20,7 @@ public class ValueConvertProcessor : IVariableProcessor ConvertS7ValueToStringAndNumeric(context.Data, context.NewValue); context.Data.UpdatedAt = DateTime.Now; // 如何值没有变化则中断处理 - if (context.Data.DataValue!=oldValue) + if (context.Data.DataValue==oldValue) { context.IsHandled = true; } diff --git a/DMS.Application/Events/DeviceActiveChangedEventArgs.cs b/DMS.Core/Events/DeviceActiveChangedEventArgs.cs similarity index 95% rename from DMS.Application/Events/DeviceActiveChangedEventArgs.cs rename to DMS.Core/Events/DeviceActiveChangedEventArgs.cs index 7898904..193cdc0 100644 --- a/DMS.Application/Events/DeviceActiveChangedEventArgs.cs +++ b/DMS.Core/Events/DeviceActiveChangedEventArgs.cs @@ -1,6 +1,4 @@ -using System; - -namespace DMS.Application.Events; +namespace DMS.Core.Events; /// /// 设备状态改变事件参数 diff --git a/DMS.Application/Events/DeviceConnectChangedEventArgs.cs b/DMS.Core/Events/DeviceConnectChangedEventArgs.cs similarity index 95% rename from DMS.Application/Events/DeviceConnectChangedEventArgs.cs rename to DMS.Core/Events/DeviceConnectChangedEventArgs.cs index 028231a..581091e 100644 --- a/DMS.Application/Events/DeviceConnectChangedEventArgs.cs +++ b/DMS.Core/Events/DeviceConnectChangedEventArgs.cs @@ -1,4 +1,4 @@ -namespace DMS.Application.Events; +namespace DMS.Core.Events; public class DeviceConnectChangedEventArgs { diff --git a/DMS.Application/Events/MqttConnectionChangedEventArgs.cs b/DMS.Core/Events/MqttConnectionChangedEventArgs.cs similarity index 96% rename from DMS.Application/Events/MqttConnectionChangedEventArgs.cs rename to DMS.Core/Events/MqttConnectionChangedEventArgs.cs index adfa426..8608861 100644 --- a/DMS.Application/Events/MqttConnectionChangedEventArgs.cs +++ b/DMS.Core/Events/MqttConnectionChangedEventArgs.cs @@ -1,6 +1,4 @@ -using System; - -namespace DMS.Application.Events; +namespace DMS.Core.Events; /// /// MQTT连接状态改变事件参数 diff --git a/DMS.Core/Events/VariableActiveChangedEventArgs.cs b/DMS.Core/Events/VariableActiveChangedEventArgs.cs new file mode 100644 index 0000000..bb12f23 --- /dev/null +++ b/DMS.Core/Events/VariableActiveChangedEventArgs.cs @@ -0,0 +1,20 @@ +namespace DMS.Core.Events; + +public class VariablesActiveChangedEventArgs: EventArgs +{ + + public List VariableIds { get; } + + public int DeviceId{get;} + + public bool NewStatus { get; } + + + + public VariablesActiveChangedEventArgs(List variableIds,int deviceId, bool newStatus) + { + VariableIds = variableIds; + DeviceId=deviceId; + NewStatus = newStatus; + } +} \ No newline at end of file diff --git a/DMS.Infrastructure/Extensions/OpcUaServiceExtensions.cs b/DMS.Infrastructure/Extensions/OpcUaServiceExtensions.cs index d42a867..6c18460 100644 --- a/DMS.Infrastructure/Extensions/OpcUaServiceExtensions.cs +++ b/DMS.Infrastructure/Extensions/OpcUaServiceExtensions.cs @@ -2,6 +2,7 @@ using DMS.Application.Interfaces; using DMS.Infrastructure.Configuration; using DMS.Infrastructure.Interfaces.Services; using DMS.Infrastructure.Services; +using DMS.Infrastructure.Services.OpcUa; using Microsoft.Extensions.DependencyInjection; namespace DMS.Infrastructure.Extensions diff --git a/DMS.Infrastructure/Extensions/S7ServiceExtensions.cs b/DMS.Infrastructure/Extensions/S7ServiceExtensions.cs deleted file mode 100644 index 3ccd511..0000000 --- a/DMS.Infrastructure/Extensions/S7ServiceExtensions.cs +++ /dev/null @@ -1,32 +0,0 @@ -using DMS.Application.Interfaces; -using DMS.Infrastructure.Interfaces; -using DMS.Infrastructure.Interfaces.Services; -using DMS.Infrastructure.Services; -using Microsoft.Extensions.DependencyInjection; - -namespace DMS.Infrastructure.Extensions -{ - /// - /// S7服务扩展方法 - /// - public static class S7ServiceExtensions - { - /// - /// 添加S7服务 - /// - public static IServiceCollection AddS7Services(this IServiceCollection services) - { - // 注册服务 - services.AddSingleton(); - services.AddSingleton(); - - // 注册后台服务 - services.AddHostedService(); - - // 注册优化的后台服务(可选) - // services.AddHostedService(); - - return services; - } - } -} \ No newline at end of file diff --git a/DMS.Infrastructure/Interfaces/Services/IS7ServiceManager.cs b/DMS.Infrastructure/Interfaces/Services/IS7ServiceManager.cs index 6e490c4..ef138cf 100644 --- a/DMS.Infrastructure/Interfaces/Services/IS7ServiceManager.cs +++ b/DMS.Infrastructure/Interfaces/Services/IS7ServiceManager.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using DMS.Application.DTOs; using DMS.Infrastructure.Services; +using DMS.Infrastructure.Services.S7; namespace DMS.Infrastructure.Interfaces.Services { diff --git a/DMS.Infrastructure/Services/MqttBackgroundService.cs b/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs similarity index 98% rename from DMS.Infrastructure/Services/MqttBackgroundService.cs rename to DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs index 5172a0c..1ca41ab 100644 --- a/DMS.Infrastructure/Services/MqttBackgroundService.cs +++ b/DMS.Infrastructure/Services/Mqtt/MqttBackgroundService.cs @@ -1,18 +1,13 @@ +using System.Collections.Concurrent; +using DMS.Application.Events; +using DMS.Application.Interfaces; +using DMS.Core.Interfaces.Services; +using DMS.Core.Models; using DMS.Infrastructure.Interfaces.Services; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using DMS.Application.DTOs.Events; -using DMS.Core.Models; -using DMS.Application.Interfaces; -using DMS.Core.Interfaces.Services; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.Mqtt { /// /// MQTT后台服务,负责管理MQTT连接和数据传输 diff --git a/DMS.Infrastructure/Services/MqttDeviceContext.cs b/DMS.Infrastructure/Services/Mqtt/MqttDeviceContext.cs similarity index 93% rename from DMS.Infrastructure/Services/MqttDeviceContext.cs rename to DMS.Infrastructure/Services/Mqtt/MqttDeviceContext.cs index 84c338b..809d790 100644 --- a/DMS.Infrastructure/Services/MqttDeviceContext.cs +++ b/DMS.Infrastructure/Services/Mqtt/MqttDeviceContext.cs @@ -1,10 +1,8 @@ +using System.Collections.Concurrent; using DMS.Core.Models; using DMS.Infrastructure.Interfaces.Services; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.Mqtt { /// /// MQTT设备上下文,用于存储单个MQTT服务器的连接信息和状态 diff --git a/DMS.Infrastructure/Services/MqttService.cs b/DMS.Infrastructure/Services/Mqtt/MqttService.cs similarity index 98% rename from DMS.Infrastructure/Services/MqttService.cs rename to DMS.Infrastructure/Services/Mqtt/MqttService.cs index f829ac7..0cc023d 100644 --- a/DMS.Infrastructure/Services/MqttService.cs +++ b/DMS.Infrastructure/Services/Mqtt/MqttService.cs @@ -5,11 +5,8 @@ using MQTTnet.Client; using MQTTnet.Client.Connecting; using MQTTnet.Client.Disconnecting; using MQTTnet.Client.Options; -using MQTTnet.Client.Receiving; -using System; -using System.Threading.Tasks; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.Mqtt { /// /// MQTT服务实现类,用于与MQTT服务器进行通信 diff --git a/DMS.Infrastructure/Services/MqttServiceFactory.cs b/DMS.Infrastructure/Services/Mqtt/MqttServiceFactory.cs similarity index 96% rename from DMS.Infrastructure/Services/MqttServiceFactory.cs rename to DMS.Infrastructure/Services/Mqtt/MqttServiceFactory.cs index 8f3b232..18b91f7 100644 --- a/DMS.Infrastructure/Services/MqttServiceFactory.cs +++ b/DMS.Infrastructure/Services/Mqtt/MqttServiceFactory.cs @@ -1,8 +1,7 @@ using DMS.Infrastructure.Interfaces.Services; using Microsoft.Extensions.Logging; -using System; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.Mqtt { /// /// MQTT服务工厂实现类,用于创建MQTT服务实例 diff --git a/DMS.Infrastructure/Services/MqttServiceManager.cs b/DMS.Infrastructure/Services/Mqtt/MqttServiceManager.cs similarity index 98% rename from DMS.Infrastructure/Services/MqttServiceManager.cs rename to DMS.Infrastructure/Services/Mqtt/MqttServiceManager.cs index 93f3f65..58859a0 100644 --- a/DMS.Infrastructure/Services/MqttServiceManager.cs +++ b/DMS.Infrastructure/Services/Mqtt/MqttServiceManager.cs @@ -1,18 +1,13 @@ -using DMS.Infrastructure.Interfaces.Services; -using Microsoft.Extensions.Logging; -using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Text; -using System.Threading; -using System.Threading.Tasks; -using DMS.Core.Models; using DMS.Application.Interfaces; using DMS.Core.Interfaces.Services; +using DMS.Core.Models; +using DMS.Infrastructure.Interfaces.Services; +using Microsoft.Extensions.Logging; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.Mqtt { /// /// MQTT服务管理器,负责管理MQTT连接和变量监控 diff --git a/DMS.Infrastructure/Services/OpcUaService.cs b/DMS.Infrastructure/Services/OpcUa/OpcUaService.cs similarity index 99% rename from DMS.Infrastructure/Services/OpcUaService.cs rename to DMS.Infrastructure/Services/OpcUa/OpcUaService.cs index 9db6ec4..f9ef0cf 100644 --- a/DMS.Infrastructure/Services/OpcUaService.cs +++ b/DMS.Infrastructure/Services/OpcUa/OpcUaService.cs @@ -4,12 +4,8 @@ using DMS.Infrastructure.Models; using Opc.Ua; using Opc.Ua.Client; using Opc.Ua.Configuration; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.OpcUa { public class OpcUaService : IOpcUaService { diff --git a/DMS.Infrastructure/Services/OpcUaServiceManager.cs b/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs similarity index 99% rename from DMS.Infrastructure/Services/OpcUaServiceManager.cs rename to DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs index ad21c9b..8ee5325 100644 --- a/DMS.Infrastructure/Services/OpcUaServiceManager.cs +++ b/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs @@ -1,10 +1,10 @@ using System.Collections.Concurrent; using System.Diagnostics; using DMS.Application.DTOs; -using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Application.Models; using DMS.Core.Enums; +using DMS.Core.Events; using DMS.Infrastructure.Configuration; using DMS.Infrastructure.Interfaces.Services; using DMS.Infrastructure.Models; @@ -12,7 +12,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using VariableValueChangedEventArgs = DMS.Core.Events.VariableValueChangedEventArgs; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.OpcUa { /// /// OPC UA服务管理器,负责管理OPC UA连接、订阅和变量监控 diff --git a/DMS.Infrastructure/Services/OptimizedOpcUaBackgroundService.cs b/DMS.Infrastructure/Services/OpcUa/OptimizedOpcUaBackgroundService.cs similarity index 98% rename from DMS.Infrastructure/Services/OptimizedOpcUaBackgroundService.cs rename to DMS.Infrastructure/Services/OpcUa/OptimizedOpcUaBackgroundService.cs index 49454f5..78238cb 100644 --- a/DMS.Infrastructure/Services/OptimizedOpcUaBackgroundService.cs +++ b/DMS.Infrastructure/Services/OpcUa/OptimizedOpcUaBackgroundService.cs @@ -1,12 +1,12 @@ using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Core.Enums; using DMS.Infrastructure.Interfaces.Services; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.OpcUa { /// /// 优化后的OPC UA后台服务 diff --git a/DMS.Infrastructure/Services/OptimizedS7BackgroundService.cs b/DMS.Infrastructure/Services/S7/OptimizedS7BackgroundService.cs similarity index 95% rename from DMS.Infrastructure/Services/OptimizedS7BackgroundService.cs rename to DMS.Infrastructure/Services/S7/OptimizedS7BackgroundService.cs index 6918c43..1ce3afe 100644 --- a/DMS.Infrastructure/Services/OptimizedS7BackgroundService.cs +++ b/DMS.Infrastructure/Services/S7/OptimizedS7BackgroundService.cs @@ -1,21 +1,17 @@ using System.Collections.Concurrent; -using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; -using DMS.Application.Interfaces; -using DMS.Core.Enums; -using DMS.Core.Models; -using Microsoft.Extensions.Hosting; -using S7.Net; -using S7.Net.Types; -using DateTime = System.DateTime; -using Microsoft.Extensions.Logging; -using DMS.Core.Interfaces; -using DMS.Infrastructure.Interfaces.Services; using System.Diagnostics; -using System.Globalization; +using DMS.Application.DTOs; +using DMS.Application.Events; +using DMS.Application.Interfaces; using DMS.Application.Models; +using DMS.Core.Enums; +using DMS.Core.Events; +using DMS.Infrastructure.Interfaces.Services; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using DateTime = System.DateTime; -namespace DMS.Infrastructure.Services; +namespace DMS.Infrastructure.Services.S7; /// /// 优化的S7后台服务,继承自BackgroundService,用于在后台高效地轮询S7 PLC设备数据。 @@ -24,6 +20,7 @@ public class OptimizedS7BackgroundService : BackgroundService { private readonly IAppDataCenterService _appDataCenterService; private readonly IAppDataStorageService _appDataStorageService; + private readonly IEventService _eventService; private readonly IDataProcessingService _dataProcessingService; private readonly IS7ServiceManager _s7ServiceManager; private readonly ILogger _logger; @@ -42,18 +39,22 @@ public class OptimizedS7BackgroundService : BackgroundService public OptimizedS7BackgroundService( IAppDataCenterService appDataCenterService, IAppDataStorageService appDataStorageService, + IEventService eventService, IDataProcessingService dataProcessingService, IS7ServiceManager s7ServiceManager, ILogger logger) { _appDataCenterService = appDataCenterService; _appDataStorageService = appDataStorageService; + _eventService = eventService; _dataProcessingService = dataProcessingService; _s7ServiceManager = s7ServiceManager; _logger = logger; _appDataCenterService.DataLoaderService.OnLoadDataCompleted += OnLoadDataCompleted; + } + private void OnLoadDataCompleted(object? sender, DataLoadCompletedEventArgs e) { @@ -245,10 +246,10 @@ public class OptimizedS7BackgroundService : BackgroundService // 将更新后的数据推入处理队列。 await _dataProcessingService.EnqueueAsync(new VariableContext(variable, value)); } - else - { - _logger.LogWarning($"未能从设备 {device.Name} 读取变量 {variable.S7Address} 的值"); - } + // else + // { + // _logger.LogWarning($"未能从设备 {device.Name} 读取变量 {variable.S7Address} 的值"); + // } } } catch (Exception ex) diff --git a/DMS.Infrastructure/Services/S7DeviceAgent.cs b/DMS.Infrastructure/Services/S7/S7DeviceAgent.cs similarity index 95% rename from DMS.Infrastructure/Services/S7DeviceAgent.cs rename to DMS.Infrastructure/Services/S7/S7DeviceAgent.cs index 5e1d112..0ac5281 100644 --- a/DMS.Infrastructure/Services/S7DeviceAgent.cs +++ b/DMS.Infrastructure/Services/S7/S7DeviceAgent.cs @@ -1,19 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; using DMS.Application.DTOs; using DMS.Application.Models; -using DMS.Core.Enums; using DMS.Core.Models; using DMS.Infrastructure.Interfaces; -using DMS.Infrastructure.Interfaces.Services; using Microsoft.Extensions.Logging; using S7.Net; using S7.Net.Types; using CpuType = DMS.Core.Enums.CpuType; using DateTime = System.DateTime; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.S7 { /// /// S7设备代理类,专门负责与单个S7 PLC进行所有通信 @@ -57,16 +52,16 @@ namespace DMS.Infrastructure.Services } } - private S7.Net.CpuType ConvertCpuType(CpuType cpuType) + private global::S7.Net.CpuType ConvertCpuType(CpuType cpuType) { return cpuType switch { - CpuType.S7200 => S7.Net.CpuType.S7200, - CpuType.S7300 => S7.Net.CpuType.S7300, - CpuType.S7400 => S7.Net.CpuType.S7400, - CpuType.S71200 => S7.Net.CpuType.S71200, - CpuType.S71500 => S7.Net.CpuType.S71500, - _ => S7.Net.CpuType.S71200 + CpuType.S7200 => global::S7.Net.CpuType.S7200, + CpuType.S7300 => global::S7.Net.CpuType.S7300, + CpuType.S7400 => global::S7.Net.CpuType.S7400, + CpuType.S71200 => global::S7.Net.CpuType.S71200, + CpuType.S71500 => global::S7.Net.CpuType.S71500, + _ => global::S7.Net.CpuType.S71200 }; } diff --git a/DMS.Infrastructure/Services/S7Service.cs b/DMS.Infrastructure/Services/S7/S7Service.cs similarity index 99% rename from DMS.Infrastructure/Services/S7Service.cs rename to DMS.Infrastructure/Services/S7/S7Service.cs index 8da82f6..98e2fae 100644 --- a/DMS.Infrastructure/Services/S7Service.cs +++ b/DMS.Infrastructure/Services/S7/S7Service.cs @@ -1,10 +1,9 @@ using DMS.Infrastructure.Interfaces.Services; using Microsoft.Extensions.Logging; -using NLog; using S7.Net; using S7.Net.Types; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.S7 { /// /// S7服务实现类,用于与S7 PLC进行通信 diff --git a/DMS.Infrastructure/Services/S7ServiceFactory.cs b/DMS.Infrastructure/Services/S7/S7ServiceFactory.cs similarity index 93% rename from DMS.Infrastructure/Services/S7ServiceFactory.cs rename to DMS.Infrastructure/Services/S7/S7ServiceFactory.cs index 978b493..4932a8a 100644 --- a/DMS.Infrastructure/Services/S7ServiceFactory.cs +++ b/DMS.Infrastructure/Services/S7/S7ServiceFactory.cs @@ -1,7 +1,7 @@ using DMS.Infrastructure.Interfaces.Services; using Microsoft.Extensions.Logging; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.S7 { /// /// S7服务工厂实现,用于创建S7Service实例 diff --git a/DMS.Infrastructure/Services/S7ServiceManager.cs b/DMS.Infrastructure/Services/S7/S7ServiceManager.cs similarity index 75% rename from DMS.Infrastructure/Services/S7ServiceManager.cs rename to DMS.Infrastructure/Services/S7/S7ServiceManager.cs index 4cf15d3..a8f7f35 100644 --- a/DMS.Infrastructure/Services/S7ServiceManager.cs +++ b/DMS.Infrastructure/Services/S7/S7ServiceManager.cs @@ -1,18 +1,14 @@ -using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; using DMS.Application.DTOs; -using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Core.Enums; +using DMS.Core.Events; using DMS.Infrastructure.Interfaces.Services; using Microsoft.Extensions.Logging; +using NPOI.HSSF.Record; -namespace DMS.Infrastructure.Services +namespace DMS.Infrastructure.Services.S7 { /// /// S7服务管理器,负责管理S7连接和变量监控 @@ -23,6 +19,7 @@ namespace DMS.Infrastructure.Services private readonly IEventService _eventService; private readonly IDataProcessingService _dataProcessingService; private readonly IAppDataCenterService _appDataCenterService; + private readonly IAppDataStorageService _appDataStorageService; private readonly IS7ServiceFactory _s7ServiceFactory; private readonly ConcurrentDictionary _deviceContexts; private readonly SemaphoreSlim _semaphore; @@ -33,15 +30,44 @@ namespace DMS.Infrastructure.Services IEventService eventService, IDataProcessingService dataProcessingService, IAppDataCenterService appDataCenterService, + IAppDataStorageService appDataStorageService, IS7ServiceFactory s7ServiceFactory) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _eventService = eventService; - _dataProcessingService = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService)); - _appDataCenterService = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService)); + _dataProcessingService + = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService)); + _appDataCenterService + = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService)); + _appDataStorageService = appDataStorageService; _s7ServiceFactory = s7ServiceFactory ?? throw new ArgumentNullException(nameof(s7ServiceFactory)); _deviceContexts = new ConcurrentDictionary(); _semaphore = new SemaphoreSlim(10, 10); // 默认最大并发连接数为10 + + _eventService.OnVariableActiveChanged += OnVariableActiveChanged; + } + + private void OnVariableActiveChanged(object? sender, VariablesActiveChangedEventArgs e) + { + if (_deviceContexts.TryGetValue(e.DeviceId, out var s7DeviceContext)) + { + + var variables = _appDataStorageService.Variables.Values.Where(v => e.VariableIds.Contains(v.Id)) + .ToList(); + foreach (var variable in variables) + { + if (e.NewStatus) + { + // 变量启用,从轮询列表中添加变量 + s7DeviceContext.Variables.AddOrUpdate(variable.S7Address,variable, (key, oldValue) => variable); + } + else + { + // 变量停用,从轮询列表中移除变量 + s7DeviceContext.Variables.Remove(variable.S7Address, out _); + } + } + } } /// @@ -69,12 +95,12 @@ namespace DMS.Infrastructure.Services } var context = new S7DeviceContext - { - Device = device, - S7Service = _s7ServiceFactory.CreateService(), - Variables = new ConcurrentDictionary(), - IsConnected = false - }; + { + Device = device, + S7Service = _s7ServiceFactory.CreateService(), + Variables = new ConcurrentDictionary(), + IsConnected = false + }; _deviceContexts.AddOrUpdate(device.Id, context, (key, oldValue) => context); _logger.LogInformation("已添加设备 {DeviceId} 到监控列表", device.Id); @@ -104,6 +130,7 @@ namespace DMS.Infrastructure.Services { context.Variables.AddOrUpdate(variable.S7Address, variable, (key, oldValue) => variable); } + _logger.LogInformation("已更新设备 {DeviceId} 的变量列表,共 {Count} 个变量", deviceId, variables.Count); } } @@ -135,6 +162,7 @@ namespace DMS.Infrastructure.Services { return _deviceContexts.Keys.ToList(); } + /// /// 获取所有监控的设备ID /// @@ -155,13 +183,14 @@ namespace DMS.Infrastructure.Services try { _logger.LogInformation("正在连接设备 {DeviceName} ({IpAddress}:{Port})", - context.Device.Name, context.Device.IpAddress, context.Device.Port); + context.Device.Name, context.Device.IpAddress, context.Device.Port); var stopwatch = Stopwatch.StartNew(); // 设置连接超时 using var timeoutToken = new CancellationTokenSource(5000); // 5秒超时 - using var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutToken.Token); + using var linkedToken + = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutToken.Token); var cpuType = ConvertCpuType(context.Device.CpuType); await context.S7Service.ConnectAsync( @@ -173,13 +202,13 @@ namespace DMS.Infrastructure.Services stopwatch.Stop(); _logger.LogInformation("设备 {DeviceName} 连接耗时 {ElapsedMs} ms", - context.Device.Name, stopwatch.ElapsedMilliseconds); + context.Device.Name, stopwatch.ElapsedMilliseconds); if (context.S7Service.IsConnected) { context.IsConnected = true; - - + + _logger.LogInformation("设备 {DeviceName} 连接成功", context.Device.Name); } else @@ -190,15 +219,14 @@ namespace DMS.Infrastructure.Services catch (Exception ex) { _logger.LogError(ex, "连接设备 {DeviceName} 时发生错误: {ErrorMessage}", - context.Device.Name, ex.Message); + context.Device.Name, ex.Message); context.IsConnected = false; - - } finally { _eventService.RaiseDeviceConnectChanged( - this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected)); + this, + new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected)); _semaphore.Release(); } } @@ -216,32 +244,33 @@ namespace DMS.Infrastructure.Services _logger.LogInformation("正在断开设备 {DeviceName} 的连接", context.Device.Name); await context.S7Service.DisconnectAsync(); context.IsConnected = false; - + _eventService.RaiseDeviceConnectChanged( - this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected)); + this, + new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected)); _logger.LogInformation("设备 {DeviceName} 连接已断开", context.Device.Name); } catch (Exception ex) { _logger.LogError(ex, "断开设备 {DeviceName} 连接时发生错误: {ErrorMessage}", - context.Device.Name, ex.Message); + context.Device.Name, ex.Message); } } /// /// 将字符串形式的CPU类型转换为S7.Net.CpuType枚举 /// - private S7.Net.CpuType ConvertCpuType(CpuType cpuType) + private global::S7.Net.CpuType ConvertCpuType(CpuType cpuType) { return cpuType switch - { - CpuType.S7200 => S7.Net.CpuType.S7200, - CpuType.S7300 => S7.Net.CpuType.S7300, - CpuType.S7400 => S7.Net.CpuType.S7400, - CpuType.S71200 => S7.Net.CpuType.S71200, - CpuType.S71500 => S7.Net.CpuType.S71500, - _ => S7.Net.CpuType.S71200 // 默认值 - }; + { + CpuType.S7200 => global::S7.Net.CpuType.S7200, + CpuType.S7300 => global::S7.Net.CpuType.S7300, + CpuType.S7400 => global::S7.Net.CpuType.S7400, + CpuType.S71200 => global::S7.Net.CpuType.S71200, + CpuType.S71500 => global::S7.Net.CpuType.S71500, + _ => global::S7.Net.CpuType.S71200 // 默认值 + }; } /// @@ -287,7 +316,8 @@ namespace DMS.Infrastructure.Services /// /// 批量断开设备连接 /// - public async Task DisconnectDevicesAsync(IEnumerable deviceIds, CancellationToken cancellationToken = default) + public async Task DisconnectDevicesAsync(IEnumerable deviceIds, + CancellationToken cancellationToken = default) { var disconnectTasks = new List(); @@ -319,7 +349,8 @@ namespace DMS.Infrastructure.Services // 断开所有设备连接 var deviceIds = _deviceContexts.Keys.ToList(); - DisconnectDevicesAsync(deviceIds).Wait(TimeSpan.FromSeconds(10)); + DisconnectDevicesAsync(deviceIds) + .Wait(TimeSpan.FromSeconds(10)); // 释放其他资源 _semaphore?.Dispose(); diff --git a/DMS.Infrastructure/Services/S7BackgroundService.cs b/DMS.Infrastructure/Services/S7BackgroundService.cs deleted file mode 100644 index 99eddf7..0000000 --- a/DMS.Infrastructure/Services/S7BackgroundService.cs +++ /dev/null @@ -1,241 +0,0 @@ -using System.Collections.Concurrent; -using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; -using DMS.Application.Interfaces; -using DMS.Core.Enums; -using DMS.Core.Models; -using Microsoft.Extensions.Hosting; -using S7.Net; -using S7.Net.Types; -using DateTime = System.DateTime; -using Microsoft.Extensions.Logging; -using DMS.Core.Interfaces; -using DMS.Infrastructure.Interfaces; -using DMS.Infrastructure.Interfaces.Services; - -namespace DMS.Infrastructure.Services; - -/// -/// S7后台服务,继承自BackgroundService,采用"编排者-代理"模式管理所有S7设备。 -/// S7BackgroundService作为编排者,负责创建、管理和销毁每个设备专属的S7DeviceAgent。 -/// 每个S7DeviceAgent作为代理,专门负责与一个S7 PLC进行所有交互。 -/// -public class S7BackgroundService : BackgroundService -{ - private readonly IAppDataCenterService _appDataCenterService; - private readonly IAppDataStorageService _appDataStorageService; - private readonly IDataProcessingService _dataProcessingService; - private readonly IChannelBus _channelBus; - private readonly IMessenger _messenger; - private readonly ILogger _logger; - private readonly SemaphoreSlim _reloadSemaphore = new SemaphoreSlim(0); - - // 存储活动的S7设备代理,键为设备ID,值为代理实例 - private readonly ConcurrentDictionary _activeAgents = new(); - - // S7轮询一遍后的等待时间 - private readonly int _s7PollOnceSleepTimeMs = 100; - - /// - /// 构造函数,注入所需的服务 - /// - public S7BackgroundService( - IAppDataCenterService appDataCenterService, - IAppDataStorageService appDataStorageService, - IDataProcessingService dataProcessingService, - IChannelBus channelBus, - IMessenger messenger, - ILogger logger) - { - _appDataCenterService = appDataCenterService; - _appDataStorageService = appDataStorageService; - _dataProcessingService = dataProcessingService; - _channelBus = channelBus; - _messenger = messenger; - _logger = logger; - - _appDataCenterService.DataLoaderService.OnLoadDataCompleted += OnLoadDataCompleted; - } - - private void OnLoadDataCompleted(object? sender, DataLoadCompletedEventArgs e) - { - _reloadSemaphore.Release(); - } - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - _logger.LogInformation("S7后台服务正在启动。"); - try - { - while (!stoppingToken.IsCancellationRequested) - { - await _reloadSemaphore.WaitAsync(stoppingToken); // Wait for a reload signal - - if (stoppingToken.IsCancellationRequested) - { - break; - } - - if (_appDataStorageService.Devices.IsEmpty) - { - _logger.LogInformation("没有可用的S7设备,等待设备列表更新..."); - continue; - } - - await LoadAndInitializeDevicesAsync(stoppingToken); - _logger.LogInformation("S7后台服务已启动。"); - - // 持续轮询,直到取消请求或需要重新加载 - while (!stoppingToken.IsCancellationRequested && _reloadSemaphore.CurrentCount == 0) - { - await PollAllDevicesAsync(stoppingToken); - await Task.Delay(_s7PollOnceSleepTimeMs, stoppingToken); - } - } - } - catch (OperationCanceledException) - { - _logger.LogInformation("S7后台服务已停止。"); - } - catch (Exception e) - { - _logger.LogError(e, $"S7后台服务运行中发生了错误:{e.Message}"); - } - finally - { - await CleanupAsync(); - } - } - - /// - /// 加载并初始化所有S7设备 - /// - private async Task LoadAndInitializeDevicesAsync(CancellationToken stoppingToken) - { - try - { - _logger.LogInformation("开始加载S7设备...."); - - // 获取所有激活的S7设备 - var s7Devices = _appDataStorageService - .Devices.Values.Where(d => d.Protocol == ProtocolType.S7 && d.IsActive == true) - .ToList(); - - // 清理已不存在的设备代理 - var existingDeviceIds = s7Devices.Select(d => d.Id).ToHashSet(); - var agentKeysToRemove = _activeAgents.Keys.Where(id => !existingDeviceIds.Contains(id)).ToList(); - - foreach (var deviceId in agentKeysToRemove) - { - if (_activeAgents.TryRemove(deviceId, out var agent)) - { - await agent.DisposeAsync(); - _logger.LogInformation($"已移除设备ID {deviceId} 的代理"); - } - } - - // 为每个设备创建或更新代理 - foreach (var deviceDto in s7Devices) - { - if (!_appDataStorageService.Devices.TryGetValue(deviceDto.Id, out var device)) - continue; - - // 创建或更新设备代理 - // await CreateOrUpdateAgentAsync(device, stoppingToken); - } - - _logger.LogInformation($"S7设备加载成功,共加载S7设备:{s7Devices.Count}个"); - } - catch (Exception e) - { - _logger.LogError(e, $"加载S7设备的过程中发生了错误:{e.Message}"); - } - } - - /// - /// 创建或更新设备代理 - /// - private async Task CreateOrUpdateAgentAsync(Device device, CancellationToken stoppingToken) - { - try - { - // 获取设备的变量 - var variables = device.VariableTables? - .SelectMany(vt => vt.Variables) - .Where(v => v.IsActive == true && v.Protocol == ProtocolType.S7) - .ToList() ?? new List(); - - // 检查是否已存在代理 - if (_activeAgents.TryGetValue(device.Id, out var existingAgent)) - { - // 更新现有代理的变量配置 - existingAgent.UpdateVariables(variables); - } - else - { - // 创建新的代理 - // // var agent = new S7DeviceAgent(device, _channelBus, _messenger, _logger); - // _activeAgents.AddOrUpdate(device.Id, agent, (key, oldValue) => agent); - // - // // 连接设备 - // await agent.ConnectAsync(); - // - // // 更新变量配置 - // agent.UpdateVariables(variables); - - _logger.LogInformation($"已为设备 {device.Name} (ID: {device.Id}) 创建代理"); - } - } - catch (Exception ex) - { - _logger.LogError(ex, $"为设备 {device.Name} (ID: {device.Id}) 创建/更新代理时发生错误"); - } - } - - /// - /// 轮询所有设备 - /// - private async Task PollAllDevicesAsync(CancellationToken stoppingToken) - { - try - { - var pollTasks = new List(); - - // 为每个活动代理创建轮询任务 - foreach (var agent in _activeAgents.Values) - { - if (stoppingToken.IsCancellationRequested) - break; - - pollTasks.Add(agent.PollVariablesAsync()); - } - - // 并行执行所有轮询任务 - await Task.WhenAll(pollTasks); - } - catch (Exception ex) - { - _logger.LogError(ex, $"轮询S7设备时发生错误:{ex.Message}"); - } - } - - /// - /// 清理资源 - /// - private async Task CleanupAsync() - { - _logger.LogInformation("正在清理S7后台服务资源..."); - - // 断开所有代理连接并释放资源 - var cleanupTasks = new List(); - foreach (var agent in _activeAgents.Values) - { - cleanupTasks.Add(agent.DisposeAsync().AsTask()); - } - - await Task.WhenAll(cleanupTasks); - _activeAgents.Clear(); - - _logger.LogInformation("S7后台服务资源清理完成"); - } -} \ No newline at end of file diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 4c7ad62..766c3c0 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -21,6 +21,9 @@ using DMS.Infrastructure.Interfaces; using DMS.Infrastructure.Interfaces.Services; using DMS.Infrastructure.Repositories; using DMS.Infrastructure.Services; +using DMS.Infrastructure.Services.Mqtt; +using DMS.Infrastructure.Services.OpcUa; +using DMS.Infrastructure.Services.S7; using DMS.WPF.Converters; using DMS.WPF.Helper; using DMS.WPF.Interfaces; diff --git a/DMS.WPF/Interfaces/ILogDataService.cs b/DMS.WPF/Interfaces/ILogDataService.cs index a8a7264..5903948 100644 --- a/DMS.WPF/Interfaces/ILogDataService.cs +++ b/DMS.WPF/Interfaces/ILogDataService.cs @@ -1,5 +1,5 @@ using System.Collections.ObjectModel; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.WPF.ViewModels.Items; namespace DMS.WPF.Interfaces; diff --git a/DMS.WPF/Services/DataEventService.cs b/DMS.WPF/Services/DataEventService.cs index 64f80f8..ef50d3f 100644 --- a/DMS.WPF/Services/DataEventService.cs +++ b/DMS.WPF/Services/DataEventService.cs @@ -3,7 +3,7 @@ using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Core.Enums; using DMS.Core.Events; diff --git a/DMS.WPF/Services/DeviceDataService.cs b/DMS.WPF/Services/DeviceDataService.cs index 6d758b8..6fbc6fd 100644 --- a/DMS.WPF/Services/DeviceDataService.cs +++ b/DMS.WPF/Services/DeviceDataService.cs @@ -3,8 +3,8 @@ using System.Windows.Threading; using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using DMS.Application.DTOs; -using DMS.Application.Events; using DMS.Application.Interfaces; +using DMS.Core.Events; using DMS.WPF.Interfaces; using DMS.WPF.ViewModels.Items; diff --git a/DMS.WPF/Services/LogDataService.cs b/DMS.WPF/Services/LogDataService.cs index 16153cd..8958f5b 100644 --- a/DMS.WPF/Services/LogDataService.cs +++ b/DMS.WPF/Services/LogDataService.cs @@ -2,7 +2,7 @@ using System.Collections.ObjectModel; using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Core.Enums; using DMS.WPF.Interfaces; diff --git a/DMS.WPF/Services/VariableTableDataService.cs b/DMS.WPF/Services/VariableTableDataService.cs index d6837f0..0861175 100644 --- a/DMS.WPF/Services/VariableTableDataService.cs +++ b/DMS.WPF/Services/VariableTableDataService.cs @@ -1,7 +1,6 @@ using System.Collections.ObjectModel; using AutoMapper; using DMS.Application.DTOs; -using DMS.Application.DTOs.Events; using DMS.Application.Interfaces; using DMS.Core.Enums; using DMS.WPF.Interfaces; diff --git a/DMS.WPF/Services/WPFDataService.cs b/DMS.WPF/Services/WPFDataService.cs index ac78df9..6db65c5 100644 --- a/DMS.WPF/Services/WPFDataService.cs +++ b/DMS.WPF/Services/WPFDataService.cs @@ -1,6 +1,5 @@ using System.Collections.ObjectModel; using AutoMapper; -using DMS.Application.DTOs.Events; using DMS.Application.Interfaces; using DMS.WPF.Interfaces; using DMS.WPF.ViewModels.Items; diff --git a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs index a081d06..8ad1c03 100644 --- a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs @@ -2,9 +2,9 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; -using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Core.Enums; +using DMS.Core.Events; using DMS.WPF.Interfaces; namespace DMS.WPF.ViewModels.Items; diff --git a/DMS.WPF/ViewModels/LogHistoryViewModel.cs b/DMS.WPF/ViewModels/LogHistoryViewModel.cs index 2360b18..28f2e31 100644 --- a/DMS.WPF/ViewModels/LogHistoryViewModel.cs +++ b/DMS.WPF/ViewModels/LogHistoryViewModel.cs @@ -15,8 +15,8 @@ using DMS.WPF.ViewModels.Dialogs; using Microsoft.Extensions.DependencyInjection; using System.Collections.ObjectModel; using System; +using DMS.Application.Events; using DMS.WPF.Services; -using DMS.Application.DTOs.Events; using DMS.Application.Interfaces.Database; using DMS.Core.Enums; diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index 9bf03b4..dbac6c4 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -6,6 +6,7 @@ using DMS.Application.DTOs; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Core.Enums; +using DMS.Core.Events; using DMS.Core.Models; using DMS.WPF.Interfaces; using DMS.WPF.ViewModels.Dialogs; @@ -25,6 +26,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable private readonly IDialogService _dialogService; private readonly IVariableAppService _variableAppService; + private readonly IEventService _eventService; private readonly IMqttAliasAppService _mqttAliasAppService; private readonly IMqttAppService _mqttAppService; @@ -91,12 +93,15 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable private readonly INotificationService _notificationService; public VariableTableViewModel(IMapper mapper, IDialogService dialogService, IVariableAppService variableAppService, + IEventService eventService, IMqttAliasAppService mqttAliasAppService, IMqttAppService mqttAppService, - IWPFDataService wpfDataService,IDataStorageService dataStorageService, INotificationService notificationService) + IWPFDataService wpfDataService, IDataStorageService dataStorageService, + INotificationService notificationService) { _mapper = mapper; _dialogService = dialogService; _variableAppService = variableAppService; + _eventService = eventService; _mqttAliasAppService = mqttAliasAppService; _mqttAppService = mqttAppService; _wpfDataService = wpfDataService; @@ -262,7 +267,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable if (improtVariableDtos.Count != 0) { - var addVariableDtos = await _variableAppService.BatchImportVariablesAsync(improtVariableDtos); + var addVariableDtos = await _variableAppService.BatchImportVariablesAsync(improtVariableDtos); if (addVariableDtos is { Count: > 0 }) { _variableItemList.AddRange(_mapper.Map>(addVariableDtos)); @@ -517,7 +522,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } } - + /// /// 为选定的变量添加MQTT服务器。 /// 此命令通常绑定到UI中的“添加MQTT服务器”按钮。 @@ -658,6 +663,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable if (result > 0) { + _eventService.RaiseVariableActiveChanged(this,new VariablesActiveChangedEventArgs(validVariables.Select(v=>v.Id).ToList(),CurrentVariableTable.DeviceId,newIsActive.Value)); // 显示成功通知 _notificationService.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的启用状态"); } @@ -673,7 +679,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable { // 导航到历史记录视图 var navigationService = App.Current.Services.GetRequiredService(); - MenuItemViewModel viewModel=new MenuItemViewModel(); + MenuItemViewModel viewModel = new MenuItemViewModel(); viewModel.TargetViewKey = "VariableHistoryView"; viewModel.MenuType = MenuType.VariableMenu; viewModel.TargetId = SelectedVariable.Id; @@ -699,10 +705,12 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 显示历史记录设置对话框,并传入第一个变量的当前设置作为默认值 var viewModel = new HistorySettingsDialogViewModel( - validVariables.First().IsHistoryEnabled, - validVariables.First().HistoryDeadband); + validVariables.First() + .IsHistoryEnabled, + validVariables.First() + .HistoryDeadband); var result = await _dialogService.ShowDialogAsync(viewModel); - + if (result != null) { // 更新所有选定变量的历史记录设置 @@ -716,7 +724,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 批量更新数据库中的变量数据 var variableDtos = _mapper.Map>(validVariables); var updateResult = await _variableAppService.UpdateVariablesAsync(variableDtos); - + if (updateResult > 0) { @@ -749,11 +757,14 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 显示报警设置对话框,并传入第一个变量的当前报警设置作为默认值 var viewModel = new AlarmSettingsDialogViewModel( - validVariables.First().IsAlarmEnabled, - validVariables.First().AlarmMinValue, - validVariables.First().AlarmMaxValue); + validVariables.First() + .IsAlarmEnabled, + validVariables.First() + .AlarmMinValue, + validVariables.First() + .AlarmMaxValue); var result = await _dialogService.ShowDialogAsync(viewModel); - + if (result != null) { // 更新所有选定变量的报警设置 @@ -806,7 +817,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable public async Task OnNavigatedToAsync(MenuItemViewModel menu) { - if (_dataStorageService.VariableTables.TryGetValue(menu.TargetId,out var varTable)) + if (_dataStorageService.VariableTables.TryGetValue(menu.TargetId, out var varTable)) { CurrentVariableTable = varTable; // 根据变量表的协议类型设置对应的布尔属性 diff --git a/DMS.WPF/Views/VariableTableView.xaml b/DMS.WPF/Views/VariableTableView.xaml index 82f4b30..1d23201 100644 --- a/DMS.WPF/Views/VariableTableView.xaml +++ b/DMS.WPF/Views/VariableTableView.xaml @@ -240,10 +240,8 @@ - - - - + + @@ -293,6 +291,8 @@ + + @@ -319,7 +319,7 @@ Header="更新时间" IsReadOnly="True" /> - +