diff --git a/DMS.Application/Interfaces/IEventService.cs b/DMS.Application/Interfaces/IEventService.cs index 76e49b3..76ba66d 100644 --- a/DMS.Application/Interfaces/IEventService.cs +++ b/DMS.Application/Interfaces/IEventService.cs @@ -12,16 +12,16 @@ public interface IEventService #region 设备事件 /// - /// 设备状态改变事件 + /// 设备状态改变事件(统一事件,处理激活状态和连接状态变化) /// - event EventHandler OnDeviceActiveChanged; + event EventHandler OnDeviceStateChanged; /// /// 触发设备状态改变事件 /// /// 事件发送者 /// 设备状态改变事件参数 - void RaiseDeviceActiveChanged(object sender, DeviceActiveChangedEventArgs e); + void RaiseDeviceStateChanged(object sender, DeviceStateChangedEventArgs e); /// /// 设备添加事件 @@ -69,17 +69,7 @@ public interface IEventService #endregion - /// - /// 设备运行改变事件 - /// - event EventHandler OnDeviceConnectChanged; - - /// - /// 触发设备状态改变事件 - /// - /// 事件发送者 - /// 设备状态改变事件参数 - void RaiseDeviceConnectChanged(object sender, DeviceConnectChangedEventArgs e); + /// /// 变量值改变事件 diff --git a/DMS.Application/Services/EventService.cs b/DMS.Application/Services/EventService.cs index 4ff1778..658f42e 100644 --- a/DMS.Application/Services/EventService.cs +++ b/DMS.Application/Services/EventService.cs @@ -20,14 +20,9 @@ public class EventService : IEventService #region 设备事件 /// - /// 设备状态改变事件 + /// 设备状态改变事件(统一事件,处理激活状态和连接状态变化) /// - public event EventHandler OnDeviceActiveChanged; - - /// - /// 设备运行改变事件 - /// - public event EventHandler OnDeviceConnectChanged; + public event EventHandler OnDeviceStateChanged; /// /// 设备添加事件 @@ -47,20 +42,33 @@ public class EventService : IEventService if (device.IsActive != e.NewStatus) { device.IsActive = e.NewStatus; - OnDeviceActiveChanged?.Invoke(sender, e); + // 转发到统一的设备状态事件 + var unifiedEvent = new DeviceStateChangedEventArgs(e.DeviceId, e.DeviceName, e.NewStatus, Core.Enums.DeviceStateType.Active); + OnDeviceStateChanged?.Invoke(sender, unifiedEvent); } } } /// - /// 触发设备状态改变事件 + /// 触发设备连接状态改变事件 /// /// 事件发送者 /// 设备状态改变事件参数 public void RaiseDeviceConnectChanged(object sender, DeviceConnectChangedEventArgs e) { - OnDeviceConnectChanged?.Invoke(sender, e); + // 转发到统一的设备状态事件 + var unifiedEvent = new DeviceStateChangedEventArgs(e.DeviceId, e.DeviceName, e.NewStatus, Core.Enums.DeviceStateType.Connection); + OnDeviceStateChanged?.Invoke(sender, unifiedEvent); + } + /// + /// 触发设备状态改变事件(统一事件) + /// + /// 事件发送者 + /// 设备状态改变事件参数 + public void RaiseDeviceStateChanged(object sender, DeviceStateChangedEventArgs e) + { + OnDeviceStateChanged?.Invoke(sender, e); } /// diff --git a/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs b/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs index 7dd8884..8ea917a 100644 --- a/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs +++ b/DMS.Application/Services/Monitoring/DeviceMonitoringService.cs @@ -33,23 +33,27 @@ public class DeviceMonitoringService : IDeviceMonitoringService, IDisposable _eventService = eventService; _appDataStorageService = appDataStorageService; _appDataCenterService = appDataCenterService; - _eventService.OnDeviceActiveChanged += OnDeviceActiveChanged; + _eventService.OnDeviceStateChanged += OnDeviceStateChanged; } - private void OnDeviceActiveChanged(object? sender, DeviceActiveChangedEventArgs e) + private void OnDeviceStateChanged(object? sender, DeviceStateChangedEventArgs e) { - if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device)) + // 只处理激活状态变化事件 + if (e.StateType == Core.Enums.DeviceStateType.Active) { - // 更新设备激活状态 - 同时更新数据库和内存 - _ = Task.Run(async () => + if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device)) { - await _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device); - }); + // 更新设备激活状态 - 同时更新数据库和内存 + _ = Task.Run(async () => + { + await _appDataCenterService.DeviceManagementService.UpdateDeviceAsync(device); + }); + } } } public void Dispose() { - _eventService.OnDeviceActiveChanged -= OnDeviceActiveChanged; + _eventService.OnDeviceStateChanged -= OnDeviceStateChanged; } } \ No newline at end of file diff --git a/DMS.Core/Enums/DeviceStateType.cs b/DMS.Core/Enums/DeviceStateType.cs new file mode 100644 index 0000000..6c91979 --- /dev/null +++ b/DMS.Core/Enums/DeviceStateType.cs @@ -0,0 +1,18 @@ +namespace DMS.Core.Enums +{ + /// + /// 设备状态类型枚举 + /// + public enum DeviceStateType + { + /// + /// 激活状态 + /// + Active, + + /// + /// 连接状态 + /// + Connection + } +} \ No newline at end of file diff --git a/DMS.Core/Events/DeviceActiveChangedEventArgs.cs b/DMS.Core/Events/DeviceActiveChangedEventArgs.cs index 193cdc0..af20afc 100644 --- a/DMS.Core/Events/DeviceActiveChangedEventArgs.cs +++ b/DMS.Core/Events/DeviceActiveChangedEventArgs.cs @@ -2,7 +2,9 @@ namespace DMS.Core.Events; /// /// 设备状态改变事件参数 +/// 已弃用:请使用 DeviceStateChangedEventArgs 替代 /// +[Obsolete("Use DeviceStateChangedEventArgs instead")] public class DeviceActiveChangedEventArgs : EventArgs { /// diff --git a/DMS.Core/Events/DeviceConnectChangedEventArgs.cs b/DMS.Core/Events/DeviceConnectChangedEventArgs.cs index 581091e..c4cc3b9 100644 --- a/DMS.Core/Events/DeviceConnectChangedEventArgs.cs +++ b/DMS.Core/Events/DeviceConnectChangedEventArgs.cs @@ -1,5 +1,9 @@ namespace DMS.Core.Events; +/// +/// 已弃用:请使用 DeviceStateChangedEventArgs 替代 +/// +[Obsolete("Use DeviceStateChangedEventArgs instead")] public class DeviceConnectChangedEventArgs { /// diff --git a/DMS.Core/Events/DeviceStateChangedEventArgs.cs b/DMS.Core/Events/DeviceStateChangedEventArgs.cs new file mode 100644 index 0000000..b3e21ab --- /dev/null +++ b/DMS.Core/Events/DeviceStateChangedEventArgs.cs @@ -0,0 +1,52 @@ +using DMS.Core.Enums; + +namespace DMS.Core.Events +{ + /// + /// 设备状态改变事件参数 + /// 统一处理设备激活状态和连接状态的变更 + /// + public class DeviceStateChangedEventArgs : System.EventArgs + { + /// + /// 设备ID + /// + public int DeviceId { get; } + + /// + /// 设备名称 + /// + public string DeviceName { get; } + + /// + /// 状态值 + /// + public bool StateValue { get; } + + /// + /// 状态类型 (激活状态或连接状态) + /// + public DeviceStateType StateType { get; } + + /// + /// 状态改变时间 + /// + public DateTime ChangeTime { get; } + + /// + /// 初始化DeviceStateChangedEventArgs类的新实例 + /// + /// 设备ID + /// 设备名称 + /// 状态值 + /// 状态类型 + public DeviceStateChangedEventArgs(int deviceId, string deviceName, bool stateValue, DeviceStateType stateType) + { + DeviceId = deviceId; + DeviceName = deviceName ?? string.Empty; + StateValue = stateValue; + StateType = stateType; + ChangeTime = DateTime.Now; + } + } +} \ No newline at end of file diff --git a/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs b/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs index 8f5c22c..f5d8241 100644 --- a/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs +++ b/DMS.Infrastructure/Services/OpcUa/OpcUaServiceManager.cs @@ -45,19 +45,30 @@ namespace DMS.Infrastructure.Services.OpcUa _deviceContexts = new ConcurrentDictionary(); _semaphore = new SemaphoreSlim(_options.MaxConcurrentConnections, _options.MaxConcurrentConnections); - _eventService.OnDeviceActiveChanged += OnDeviceActiveChanged; + _eventService.OnDeviceStateChanged += OnDeviceStateChanged; _eventService.OnDeviceChanged += OnDeviceChanged; } - private async void OnDeviceActiveChanged(object? sender, DeviceActiveChangedEventArgs e) + private async void OnDeviceStateChanged(object? sender, DeviceStateChangedEventArgs e) { - if (e.NewStatus) + switch (e.StateType) { - await ConnectDeviceAsync(e.DeviceId, CancellationToken.None); - } - else - { - await DisconnectDeviceAsync(e.DeviceId, CancellationToken.None); + case Core.Enums.DeviceStateType.Active: + // 处理激活状态变化 + if (e.StateValue) + { + await ConnectDeviceAsync(e.DeviceId, CancellationToken.None); + } + else + { + await DisconnectDeviceAsync(e.DeviceId, CancellationToken.None); + } + break; + + case Core.Enums.DeviceStateType.Connection: + // 处理连接状态变化(通常由底层连接过程触发) + // 在OPC UA服务中,这可能是内部状态更新 + break; } } @@ -359,8 +370,8 @@ namespace DMS.Infrastructure.Services.OpcUa _logger.LogWarning("设备 {DeviceName} 连接失败", context.Device.Name); } - _eventService.RaiseDeviceConnectChanged( - this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected)); + _eventService.RaiseDeviceStateChanged( + this, new DeviceStateChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected, Core.Enums.DeviceStateType.Connection)); } catch (Exception ex) { @@ -368,8 +379,8 @@ namespace DMS.Infrastructure.Services.OpcUa context.Device.Name, ex.Message); context.IsConnected = false; context.Device.IsRunning = false; - _eventService.RaiseDeviceConnectChanged( - this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, false)); + _eventService.RaiseDeviceStateChanged( + this, new DeviceStateChangedEventArgs(context.Device.Id, context.Device.Name, false, Core.Enums.DeviceStateType.Connection)); } finally { @@ -391,8 +402,8 @@ namespace DMS.Infrastructure.Services.OpcUa await context.OpcUaService.DisconnectAsync(); context.IsConnected = false; context.Device.IsRunning = false; - _eventService.RaiseDeviceConnectChanged( - this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, false)); + _eventService.RaiseDeviceStateChanged( + this, new DeviceStateChangedEventArgs(context.Device.Id, context.Device.Name, false, Core.Enums.DeviceStateType.Connection)); _logger.LogInformation("设备 {DeviceName} 连接已断开", context.Device.Name); } catch (Exception ex) diff --git a/DMS.Infrastructure/Services/S7/S7ServiceManager.cs b/DMS.Infrastructure/Services/S7/S7ServiceManager.cs index a8f7f35..d70f1dd 100644 --- a/DMS.Infrastructure/Services/S7/S7ServiceManager.cs +++ b/DMS.Infrastructure/Services/S7/S7ServiceManager.cs @@ -224,9 +224,9 @@ namespace DMS.Infrastructure.Services.S7 } finally { - _eventService.RaiseDeviceConnectChanged( + _eventService.RaiseDeviceStateChanged( this, - new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected)); + new DeviceStateChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected, Core.Enums.DeviceStateType.Connection)); _semaphore.Release(); } } @@ -245,9 +245,9 @@ namespace DMS.Infrastructure.Services.S7 await context.S7Service.DisconnectAsync(); context.IsConnected = false; - _eventService.RaiseDeviceConnectChanged( + _eventService.RaiseDeviceStateChanged( this, - new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected)); + new DeviceStateChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected, Core.Enums.DeviceStateType.Connection)); _logger.LogInformation("设备 {DeviceName} 连接已断开", context.Device.Name); } catch (Exception ex) diff --git a/DMS.WPF/Services/DeviceDataService.cs b/DMS.WPF/Services/DeviceDataService.cs index 480ed2a..3a2cddb 100644 --- a/DMS.WPF/Services/DeviceDataService.cs +++ b/DMS.WPF/Services/DeviceDataService.cs @@ -49,28 +49,32 @@ public class DeviceDataService : IDeviceDataService _variableDataService = variableDataService; _uiDispatcher = Dispatcher.CurrentDispatcher; - _eventService.OnDeviceConnectChanged += OnDeviceConnectChanged; + _eventService.OnDeviceStateChanged += OnDeviceStateChanged; } - private void OnDeviceConnectChanged(object? sender, DeviceConnectChangedEventArgs e) + private void OnDeviceStateChanged(object? sender, DeviceStateChangedEventArgs e) { - _uiDispatcher.Invoke(() => + // 只处理连接状态变化 + if (e.StateType == Core.Enums.DeviceStateType.Connection) { - - if (_dataStorageService.Devices.TryGetValue(e.DeviceId, out DeviceItemViewModel device)) + _uiDispatcher.Invoke(() => { - device.IsRunning = e.NewStatus; - if (device.IsRunning) + if (_dataStorageService.Devices.TryGetValue(e.DeviceId, out DeviceItemViewModel device)) { - _notificationService.ShowSuccess($"设备:{device.Name},连接成功。"); + + device.IsRunning = e.StateValue; + if (device.IsRunning) + { + _notificationService.ShowSuccess($"设备:{device.Name},连接成功。"); + } + else + { + _notificationService.ShowSuccess($"设备:{device.Name},已断开连接。"); + } } - else - { - _notificationService.ShowSuccess($"设备:{device.Name},已断开连接。"); - } - } - }); + }); + } } /// diff --git a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs index 8ad1c03..7845dbc 100644 --- a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs @@ -91,8 +91,8 @@ public partial class DeviceItemViewModel : ObservableObject // 只有当设备ID有效且事件服务已初始化时才发布事件 if (Id > 0 && EventService != null ) { - // 发布设备状态改变事件 - EventService.RaiseDeviceActiveChanged(this, new DeviceActiveChangedEventArgs(Id, Name, newValue)); + // 发布设备状态改变事件(使用统一的事件类型) + EventService.RaiseDeviceStateChanged(this, new DeviceStateChangedEventArgs(Id, Name, newValue, Core.Enums.DeviceStateType.Active)); } }