主要变更:

1. 创建了 DeviceStateType 枚举来区分状态类型(激活/连接)
   2. 创建了 DeviceStateChangedEventArgs 统一事件类
   3. 更新了 IEventService 接口,将两个事件合并为一个
   4. 更新了 EventService 实现以支持新事件
   5. 更新了所有相关服务(DeviceMonitoringService, OpcUaServiceManager, S7ServiceManager, DeviceDataService, DeviceItemViewModel)

  关键优点:
   - 减少了事件类型的数量,简化了事件系统
   - 为设备状态变化提供了一致的处理方式
   - 保持了向后兼容性,将旧类标记为 [Obsolete]
This commit is contained in:
2025-10-01 18:41:05 +08:00
parent c0223b083a
commit e84a202438
11 changed files with 159 additions and 66 deletions

View File

@@ -45,19 +45,30 @@ namespace DMS.Infrastructure.Services.OpcUa
_deviceContexts = new ConcurrentDictionary<int, DeviceContext>();
_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)

View File

@@ -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)