主要变更:
1. 创建了 DeviceStateType 枚举来区分状态类型(激活/连接) 2. 创建了 DeviceStateChangedEventArgs 统一事件类 3. 更新了 IEventService 接口,将两个事件合并为一个 4. 更新了 EventService 实现以支持新事件 5. 更新了所有相关服务(DeviceMonitoringService, OpcUaServiceManager, S7ServiceManager, DeviceDataService, DeviceItemViewModel) 关键优点: - 减少了事件类型的数量,简化了事件系统 - 为设备状态变化提供了一致的处理方式 - 保持了向后兼容性,将旧类标记为 [Obsolete]
This commit is contained in:
@@ -12,16 +12,16 @@ public interface IEventService
|
|||||||
#region 设备事件
|
#region 设备事件
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备状态改变事件
|
/// 设备状态改变事件(统一事件,处理激活状态和连接状态变化)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
event EventHandler<DeviceActiveChangedEventArgs> OnDeviceActiveChanged;
|
event EventHandler<DeviceStateChangedEventArgs> OnDeviceStateChanged;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 触发设备状态改变事件
|
/// 触发设备状态改变事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender">事件发送者</param>
|
/// <param name="sender">事件发送者</param>
|
||||||
/// <param name="e">设备状态改变事件参数</param>
|
/// <param name="e">设备状态改变事件参数</param>
|
||||||
void RaiseDeviceActiveChanged(object sender, DeviceActiveChangedEventArgs e);
|
void RaiseDeviceStateChanged(object sender, DeviceStateChangedEventArgs e);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备添加事件
|
/// 设备添加事件
|
||||||
@@ -69,17 +69,7 @@ public interface IEventService
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 设备运行改变事件
|
|
||||||
/// </summary>
|
|
||||||
event EventHandler<DeviceConnectChangedEventArgs> OnDeviceConnectChanged;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 触发设备状态改变事件
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sender">事件发送者</param>
|
|
||||||
/// <param name="e">设备状态改变事件参数</param>
|
|
||||||
void RaiseDeviceConnectChanged(object sender, DeviceConnectChangedEventArgs e);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 变量值改变事件
|
/// 变量值改变事件
|
||||||
|
|||||||
@@ -20,14 +20,9 @@ public class EventService : IEventService
|
|||||||
#region 设备事件
|
#region 设备事件
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备状态改变事件
|
/// 设备状态改变事件(统一事件,处理激活状态和连接状态变化)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public event EventHandler<DeviceActiveChangedEventArgs> OnDeviceActiveChanged;
|
public event EventHandler<DeviceStateChangedEventArgs> OnDeviceStateChanged;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 设备运行改变事件
|
|
||||||
/// </summary>
|
|
||||||
public event EventHandler<DeviceConnectChangedEventArgs> OnDeviceConnectChanged;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备添加事件
|
/// 设备添加事件
|
||||||
@@ -47,20 +42,33 @@ public class EventService : IEventService
|
|||||||
if (device.IsActive != e.NewStatus)
|
if (device.IsActive != e.NewStatus)
|
||||||
{
|
{
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 触发设备状态改变事件
|
/// 触发设备连接状态改变事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="sender">事件发送者</param>
|
/// <param name="sender">事件发送者</param>
|
||||||
/// <param name="e">设备状态改变事件参数</param>
|
/// <param name="e">设备状态改变事件参数</param>
|
||||||
public void RaiseDeviceConnectChanged(object sender, DeviceConnectChangedEventArgs e)
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发设备状态改变事件(统一事件)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender">事件发送者</param>
|
||||||
|
/// <param name="e">设备状态改变事件参数</param>
|
||||||
|
public void RaiseDeviceStateChanged(object sender, DeviceStateChangedEventArgs e)
|
||||||
|
{
|
||||||
|
OnDeviceStateChanged?.Invoke(sender, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -33,10 +33,13 @@ public class DeviceMonitoringService : IDeviceMonitoringService, IDisposable
|
|||||||
_eventService = eventService;
|
_eventService = eventService;
|
||||||
_appDataStorageService = appDataStorageService;
|
_appDataStorageService = appDataStorageService;
|
||||||
_appDataCenterService = appDataCenterService;
|
_appDataCenterService = appDataCenterService;
|
||||||
_eventService.OnDeviceActiveChanged += OnDeviceActiveChanged;
|
_eventService.OnDeviceStateChanged += OnDeviceStateChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDeviceActiveChanged(object? sender, DeviceActiveChangedEventArgs e)
|
private void OnDeviceStateChanged(object? sender, DeviceStateChangedEventArgs e)
|
||||||
|
{
|
||||||
|
// 只处理激活状态变化事件
|
||||||
|
if (e.StateType == Core.Enums.DeviceStateType.Active)
|
||||||
{
|
{
|
||||||
if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device))
|
if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device))
|
||||||
{
|
{
|
||||||
@@ -47,9 +50,10 @@ public class DeviceMonitoringService : IDeviceMonitoringService, IDisposable
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_eventService.OnDeviceActiveChanged -= OnDeviceActiveChanged;
|
_eventService.OnDeviceStateChanged -= OnDeviceStateChanged;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
18
DMS.Core/Enums/DeviceStateType.cs
Normal file
18
DMS.Core/Enums/DeviceStateType.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
namespace DMS.Core.Enums
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 设备状态类型枚举
|
||||||
|
/// </summary>
|
||||||
|
public enum DeviceStateType
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 激活状态
|
||||||
|
/// </summary>
|
||||||
|
Active,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 连接状态
|
||||||
|
/// </summary>
|
||||||
|
Connection
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,9 @@ namespace DMS.Core.Events;
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备状态改变事件参数
|
/// 设备状态改变事件参数
|
||||||
|
/// 已弃用:请使用 DeviceStateChangedEventArgs 替代
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete("Use DeviceStateChangedEventArgs instead")]
|
||||||
public class DeviceActiveChangedEventArgs : EventArgs
|
public class DeviceActiveChangedEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
namespace DMS.Core.Events;
|
namespace DMS.Core.Events;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 已弃用:请使用 DeviceStateChangedEventArgs 替代
|
||||||
|
/// </summary>
|
||||||
|
[Obsolete("Use DeviceStateChangedEventArgs instead")]
|
||||||
public class DeviceConnectChangedEventArgs
|
public class DeviceConnectChangedEventArgs
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
52
DMS.Core/Events/DeviceStateChangedEventArgs.cs
Normal file
52
DMS.Core/Events/DeviceStateChangedEventArgs.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using DMS.Core.Enums;
|
||||||
|
|
||||||
|
namespace DMS.Core.Events
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 设备状态改变事件参数
|
||||||
|
/// 统一处理设备激活状态和连接状态的变更
|
||||||
|
/// </summary>
|
||||||
|
public class DeviceStateChangedEventArgs : System.EventArgs
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 设备ID
|
||||||
|
/// </summary>
|
||||||
|
public int DeviceId { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设备名称
|
||||||
|
/// </summary>
|
||||||
|
public string DeviceName { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态值
|
||||||
|
/// </summary>
|
||||||
|
public bool StateValue { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态类型 (激活状态或连接状态)
|
||||||
|
/// </summary>
|
||||||
|
public DeviceStateType StateType { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 状态改变时间
|
||||||
|
/// </summary>
|
||||||
|
public DateTime ChangeTime { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化DeviceStateChangedEventArgs类的新实例
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="deviceId">设备ID</param>
|
||||||
|
/// <param name="deviceName">设备名称</param>
|
||||||
|
/// <param name="stateValue">状态值</param>
|
||||||
|
/// <param name="stateType">状态类型</param>
|
||||||
|
public DeviceStateChangedEventArgs(int deviceId, string deviceName, bool stateValue, DeviceStateType stateType)
|
||||||
|
{
|
||||||
|
DeviceId = deviceId;
|
||||||
|
DeviceName = deviceName ?? string.Empty;
|
||||||
|
StateValue = stateValue;
|
||||||
|
StateType = stateType;
|
||||||
|
ChangeTime = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,13 +45,17 @@ namespace DMS.Infrastructure.Services.OpcUa
|
|||||||
_deviceContexts = new ConcurrentDictionary<int, DeviceContext>();
|
_deviceContexts = new ConcurrentDictionary<int, DeviceContext>();
|
||||||
_semaphore = new SemaphoreSlim(_options.MaxConcurrentConnections, _options.MaxConcurrentConnections);
|
_semaphore = new SemaphoreSlim(_options.MaxConcurrentConnections, _options.MaxConcurrentConnections);
|
||||||
|
|
||||||
_eventService.OnDeviceActiveChanged += OnDeviceActiveChanged;
|
_eventService.OnDeviceStateChanged += OnDeviceStateChanged;
|
||||||
_eventService.OnDeviceChanged += OnDeviceChanged;
|
_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)
|
||||||
|
{
|
||||||
|
case Core.Enums.DeviceStateType.Active:
|
||||||
|
// 处理激活状态变化
|
||||||
|
if (e.StateValue)
|
||||||
{
|
{
|
||||||
await ConnectDeviceAsync(e.DeviceId, CancellationToken.None);
|
await ConnectDeviceAsync(e.DeviceId, CancellationToken.None);
|
||||||
}
|
}
|
||||||
@@ -59,6 +63,13 @@ namespace DMS.Infrastructure.Services.OpcUa
|
|||||||
{
|
{
|
||||||
await DisconnectDeviceAsync(e.DeviceId, CancellationToken.None);
|
await DisconnectDeviceAsync(e.DeviceId, CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Core.Enums.DeviceStateType.Connection:
|
||||||
|
// 处理连接状态变化(通常由底层连接过程触发)
|
||||||
|
// 在OPC UA服务中,这可能是内部状态更新
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDeviceChanged(object? sender, DeviceChangedEventArgs e)
|
private void OnDeviceChanged(object? sender, DeviceChangedEventArgs e)
|
||||||
@@ -359,8 +370,8 @@ namespace DMS.Infrastructure.Services.OpcUa
|
|||||||
_logger.LogWarning("设备 {DeviceName} 连接失败", context.Device.Name);
|
_logger.LogWarning("设备 {DeviceName} 连接失败", context.Device.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
_eventService.RaiseDeviceConnectChanged(
|
_eventService.RaiseDeviceStateChanged(
|
||||||
this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected));
|
this, new DeviceStateChangedEventArgs(context.Device.Id, context.Device.Name, context.IsConnected, Core.Enums.DeviceStateType.Connection));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -368,8 +379,8 @@ namespace DMS.Infrastructure.Services.OpcUa
|
|||||||
context.Device.Name, ex.Message);
|
context.Device.Name, ex.Message);
|
||||||
context.IsConnected = false;
|
context.IsConnected = false;
|
||||||
context.Device.IsRunning = false;
|
context.Device.IsRunning = false;
|
||||||
_eventService.RaiseDeviceConnectChanged(
|
_eventService.RaiseDeviceStateChanged(
|
||||||
this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, false));
|
this, new DeviceStateChangedEventArgs(context.Device.Id, context.Device.Name, false, Core.Enums.DeviceStateType.Connection));
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -391,8 +402,8 @@ namespace DMS.Infrastructure.Services.OpcUa
|
|||||||
await context.OpcUaService.DisconnectAsync();
|
await context.OpcUaService.DisconnectAsync();
|
||||||
context.IsConnected = false;
|
context.IsConnected = false;
|
||||||
context.Device.IsRunning = false;
|
context.Device.IsRunning = false;
|
||||||
_eventService.RaiseDeviceConnectChanged(
|
_eventService.RaiseDeviceStateChanged(
|
||||||
this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, false));
|
this, new DeviceStateChangedEventArgs(context.Device.Id, context.Device.Name, false, Core.Enums.DeviceStateType.Connection));
|
||||||
_logger.LogInformation("设备 {DeviceName} 连接已断开", context.Device.Name);
|
_logger.LogInformation("设备 {DeviceName} 连接已断开", context.Device.Name);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -224,9 +224,9 @@ namespace DMS.Infrastructure.Services.S7
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
_eventService.RaiseDeviceConnectChanged(
|
_eventService.RaiseDeviceStateChanged(
|
||||||
this,
|
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();
|
_semaphore.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,9 +245,9 @@ namespace DMS.Infrastructure.Services.S7
|
|||||||
await context.S7Service.DisconnectAsync();
|
await context.S7Service.DisconnectAsync();
|
||||||
context.IsConnected = false;
|
context.IsConnected = false;
|
||||||
|
|
||||||
_eventService.RaiseDeviceConnectChanged(
|
_eventService.RaiseDeviceStateChanged(
|
||||||
this,
|
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);
|
_logger.LogInformation("设备 {DeviceName} 连接已断开", context.Device.Name);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -49,10 +49,13 @@ public class DeviceDataService : IDeviceDataService
|
|||||||
_variableDataService = variableDataService;
|
_variableDataService = variableDataService;
|
||||||
_uiDispatcher = Dispatcher.CurrentDispatcher;
|
_uiDispatcher = Dispatcher.CurrentDispatcher;
|
||||||
|
|
||||||
_eventService.OnDeviceConnectChanged += OnDeviceConnectChanged;
|
_eventService.OnDeviceStateChanged += OnDeviceStateChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDeviceConnectChanged(object? sender, DeviceConnectChangedEventArgs e)
|
private void OnDeviceStateChanged(object? sender, DeviceStateChangedEventArgs e)
|
||||||
|
{
|
||||||
|
// 只处理连接状态变化
|
||||||
|
if (e.StateType == Core.Enums.DeviceStateType.Connection)
|
||||||
{
|
{
|
||||||
_uiDispatcher.Invoke(() =>
|
_uiDispatcher.Invoke(() =>
|
||||||
{
|
{
|
||||||
@@ -60,7 +63,7 @@ public class DeviceDataService : IDeviceDataService
|
|||||||
if (_dataStorageService.Devices.TryGetValue(e.DeviceId, out DeviceItemViewModel device))
|
if (_dataStorageService.Devices.TryGetValue(e.DeviceId, out DeviceItemViewModel device))
|
||||||
{
|
{
|
||||||
|
|
||||||
device.IsRunning = e.NewStatus;
|
device.IsRunning = e.StateValue;
|
||||||
if (device.IsRunning)
|
if (device.IsRunning)
|
||||||
{
|
{
|
||||||
_notificationService.ShowSuccess($"设备:{device.Name},连接成功。");
|
_notificationService.ShowSuccess($"设备:{device.Name},连接成功。");
|
||||||
@@ -72,6 +75,7 @@ public class DeviceDataService : IDeviceDataService
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 加载所有设备数据。
|
/// 加载所有设备数据。
|
||||||
|
|||||||
@@ -91,8 +91,8 @@ public partial class DeviceItemViewModel : ObservableObject
|
|||||||
// 只有当设备ID有效且事件服务已初始化时才发布事件
|
// 只有当设备ID有效且事件服务已初始化时才发布事件
|
||||||
if (Id > 0 && EventService != null )
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user