1. 向 VariableTableManagementService 添加了 IEventService 依赖
2. 将内存操作和事件触发逻辑合并到数据库操作方法中:
- CreateVariableTableAsync 现在会在数据库创建后自动添加到内存并触发事件
- UpdateVariableTableAsync 现在会在数据库更新后自动更新内存并触发事件
- DeleteVariableTableAsync 现在会在数据库删除后自动从内存移除并触发事件
3. 从类中删除了独立的内存操作方法
4. 从接口中移除了内存操作方法
5. 更新了所有调用这些服务的代码,使它们不再调用已删除的内存方法
6. 扩展了 IEventService 以支持 VariableTableChanged 事件
163 lines
5.1 KiB
C#
163 lines
5.1 KiB
C#
using System;
|
|
using DMS.Application.Events;
|
|
using DMS.Application.Interfaces;
|
|
using DMS.Core.Events;
|
|
|
|
namespace DMS.Application.Services;
|
|
|
|
/// <summary>
|
|
/// 事件服务实现类,用于统一管理应用程序中的各种事件
|
|
/// </summary>
|
|
public class EventService : IEventService
|
|
{
|
|
private readonly IAppDataStorageService _appDataStorageService;
|
|
|
|
public EventService(IAppDataStorageService appDataStorageService)
|
|
{
|
|
_appDataStorageService = appDataStorageService;
|
|
}
|
|
|
|
#region 设备事件
|
|
|
|
/// <summary>
|
|
/// 设备状态改变事件(统一事件,处理激活状态和连接状态变化)
|
|
/// </summary>
|
|
public event EventHandler<DeviceStateChangedEventArgs> OnDeviceStateChanged;
|
|
|
|
/// <summary>
|
|
/// 设备添加事件
|
|
/// </summary>
|
|
public event EventHandler<DeviceChangedEventArgs> OnDeviceChanged;
|
|
|
|
|
|
/// <summary>
|
|
/// 触发设备状态改变事件
|
|
/// </summary>
|
|
/// <param name="sender">事件发送者</param>
|
|
/// <param name="e">设备状态改变事件参数</param>
|
|
public void RaiseDeviceActiveChanged(object sender, DeviceActiveChangedEventArgs e)
|
|
{
|
|
if (_appDataStorageService.Devices.TryGetValue(e.DeviceId, out var device))
|
|
{
|
|
if (device.IsActive != e.NewStatus)
|
|
{
|
|
device.IsActive = e.NewStatus;
|
|
// 转发到统一的设备状态事件
|
|
var unifiedEvent = new DeviceStateChangedEventArgs(e.DeviceId, e.DeviceName, e.NewStatus, Core.Enums.DeviceStateType.Active);
|
|
OnDeviceStateChanged?.Invoke(sender, unifiedEvent);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 触发设备连接状态改变事件
|
|
/// </summary>
|
|
/// <param name="sender">事件发送者</param>
|
|
/// <param name="e">设备状态改变事件参数</param>
|
|
public void RaiseDeviceConnectChanged(object sender, DeviceConnectChangedEventArgs 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>
|
|
/// <param name="sender">事件发送者</param>
|
|
/// <param name="e">设备变更事件参数</param>
|
|
public void RaiseDeviceChanged(object sender, DeviceChangedEventArgs e)
|
|
{
|
|
OnDeviceChanged?.Invoke(sender, e);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 变量事件
|
|
/// <summary>
|
|
/// 变量表改变事件
|
|
/// </summary>
|
|
public event EventHandler<VariableTableChangedEventArgs> OnVariableTableChanged;
|
|
|
|
/// <summary>
|
|
/// 触发变量表改变事件
|
|
/// </summary>
|
|
/// <param name="sender">事件发送者</param>
|
|
/// <param name="e">变量表改变事件参数</param>
|
|
public void RaiseVariableTableChanged(object sender, VariableTableChangedEventArgs e)
|
|
{
|
|
OnVariableTableChanged?.Invoke(sender, e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 变量值改变事件
|
|
/// </summary>
|
|
public event EventHandler<VariableChangedEventArgs> OnVariableChanged;
|
|
|
|
/// <summary>
|
|
/// 触发变量值改变事件
|
|
/// </summary>
|
|
/// <param name="sender">事件发送者</param>
|
|
/// <param name="e">变量值改变事件参数</param>
|
|
public void RaiseVariableChanged(object sender, VariableChangedEventArgs e)
|
|
{
|
|
OnVariableChanged?.Invoke(sender, e);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 变量启停改变事件
|
|
/// </summary>
|
|
public event EventHandler<VariablesActiveChangedEventArgs> OnVariableActiveChanged;
|
|
public void RaiseVariableActiveChanged(object sender, VariablesActiveChangedEventArgs e)
|
|
{
|
|
OnVariableActiveChanged?.Invoke(sender, e);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 变量值改变事件
|
|
/// </summary>
|
|
public event EventHandler<VariableValueChangedEventArgs> OnVariableValueChanged;
|
|
|
|
/// <summary>
|
|
/// 触发变量值改变事件
|
|
/// </summary>
|
|
/// <param name="sender">事件发送者</param>
|
|
/// <param name="e">变量值改变事件参数</param>
|
|
public void RaiseVariableValueChanged(object sender, VariableValueChangedEventArgs e)
|
|
{
|
|
OnVariableValueChanged?.Invoke(sender, e);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region MQTT事件
|
|
|
|
/// <summary>
|
|
/// MQTT连接状态改变事件
|
|
/// </summary>
|
|
public event EventHandler<MqttConnectionChangedEventArgs> MqttConnectionChanged;
|
|
|
|
/// <summary>
|
|
/// 触发MQTT连接状态改变事件
|
|
/// </summary>
|
|
/// <param name="sender">事件发送者</param>
|
|
/// <param name="e">MQTT连接状态改变事件参数</param>
|
|
public void RaiseMqttConnectionChanged(object sender, MqttConnectionChangedEventArgs e)
|
|
{
|
|
MqttConnectionChanged?.Invoke(sender, e);
|
|
}
|
|
|
|
#endregion
|
|
} |