重构:修改 VariableValueChangedEventArgs 以包含 VariableDto 对象
- 修改 VariableValueChangedEventArgs 类:
- 移除了 VariableId、VariableName、NewValue 和 UpdateTime 属性
- 添加了 Variable(VariableDto 类型)和 OldValue 属性
- 更新了构造函数以接收 VariableDto 对象和旧值
- 更新事件触发代码:
- 修改了 UpdateViewProcessor.cs 中的事件触发代码,改为传递 VariableDto 对象和旧值
- 更新事件处理代码:
- 修改了 DataEventService.cs 中的事件处理逻辑,改为访问 e.Variable 属性
- 修改了 VariableHistoryViewModel.cs 中的事件处理逻辑,改为使用 e.Variable 属性
This commit is contained in:
29
DMS.Application/Events/VariableValueChangedEventArgs.cs
Normal file
29
DMS.Application/Events/VariableValueChangedEventArgs.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using DMS.Application.DTOs;
|
||||
|
||||
namespace DMS.Application.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 变量值变更事件参数
|
||||
/// </summary>
|
||||
public class VariableValueChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 变量DTO对象
|
||||
/// </summary>
|
||||
public VariableDto Variable { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 旧值
|
||||
/// </summary>
|
||||
public string? OldValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VariableValueChangedEventArgs(VariableDto variable, string? oldValue)
|
||||
{
|
||||
Variable = variable;
|
||||
OldValue = oldValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,36 +30,7 @@ public class EventService : IEventService
|
||||
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>
|
||||
/// 触发设备状态改变事件(统一事件)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Models;
|
||||
using DMS.Core.Events;
|
||||
|
||||
@@ -17,13 +18,10 @@ public class UpdateViewProcessor: IVariableProcessor
|
||||
{
|
||||
// 触发变量值变更事件
|
||||
var eventArgs = new VariableValueChangedEventArgs(
|
||||
context.Data.Id,
|
||||
context.Data.Name,
|
||||
context.Data.DataValue,
|
||||
context.NewValue.ToString()??"",
|
||||
DateTime.Now);
|
||||
context.Data,
|
||||
context.Data.DataValue);
|
||||
|
||||
_eventService.RaiseVariableValueChanged(this,eventArgs);
|
||||
_eventService.RaiseVariableValueChanged(this, eventArgs);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
namespace DMS.Core.Events;
|
||||
|
||||
/// <summary>
|
||||
/// 设备状态改变事件参数
|
||||
/// 已弃用:请使用 DeviceStateChangedEventArgs 替代
|
||||
/// </summary>
|
||||
[Obsolete("Use DeviceStateChangedEventArgs instead")]
|
||||
public class DeviceActiveChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备ID
|
||||
/// </summary>
|
||||
public int DeviceId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备名称
|
||||
/// </summary>
|
||||
public string DeviceName { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 新状态
|
||||
/// </summary>
|
||||
public bool NewStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态改变时间
|
||||
/// </summary>
|
||||
public DateTime ChangeTime { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化DeviceStatusChangedEventArgs类的新实例
|
||||
/// </summary>
|
||||
/// <param name="deviceId">设备ID</param>
|
||||
/// <param name="deviceName">设备名称</param>
|
||||
/// <param name="oldStatus">旧状态</param>
|
||||
/// <param name="newStatus">新状态</param>
|
||||
public DeviceActiveChangedEventArgs(int deviceId, string deviceName, bool newStatus)
|
||||
{
|
||||
DeviceId = deviceId;
|
||||
DeviceName = deviceName;
|
||||
NewStatus = newStatus;
|
||||
ChangeTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
namespace DMS.Core.Events;
|
||||
|
||||
/// <summary>
|
||||
/// 已弃用:请使用 DeviceStateChangedEventArgs 替代
|
||||
/// </summary>
|
||||
[Obsolete("Use DeviceStateChangedEventArgs instead")]
|
||||
public class DeviceConnectChangedEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备ID
|
||||
/// </summary>
|
||||
public int DeviceId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备名称
|
||||
/// </summary>
|
||||
public string DeviceName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 新状态
|
||||
/// </summary>
|
||||
public bool NewStatus { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 初始化DeviceStatusChangedEventArgs类的新实例
|
||||
/// </summary>
|
||||
/// <param name="deviceId">设备ID</param>
|
||||
/// <param name="deviceName">设备名称</param>
|
||||
/// <param name="oldStatus">旧状态</param>
|
||||
/// <param name="newStatus">新状态</param>
|
||||
public DeviceConnectChangedEventArgs(int deviceId, string deviceName, bool newStatus)
|
||||
{
|
||||
DeviceId = deviceId;
|
||||
DeviceName = deviceName;
|
||||
NewStatus = newStatus;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
namespace DMS.Core.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// 变量值变更事件参数
|
||||
/// </summary>
|
||||
public class VariableValueChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// 变量ID
|
||||
/// </summary>
|
||||
public int VariableId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 变量名称
|
||||
/// </summary>
|
||||
public string VariableName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 旧值
|
||||
/// </summary>
|
||||
public string OldValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 新值
|
||||
/// </summary>
|
||||
public string NewValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 更新时间
|
||||
/// </summary>
|
||||
public DateTime UpdateTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public VariableValueChangedEventArgs(int variableId, string variableName, string oldValue, string newValue, DateTime updateTime)
|
||||
{
|
||||
VariableId = variableId;
|
||||
VariableName = variableName;
|
||||
OldValue = oldValue;
|
||||
NewValue = newValue;
|
||||
UpdateTime = updateTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,27 +95,21 @@ public class DataEventService : IDataEventService
|
||||
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
|
||||
{
|
||||
_logger?.LogDebug("接收到变量值变更事件,变量ID: {VariableId}, 新值: {NewValue}, 更新时间: {UpdateTime}",
|
||||
e.VariableId, e.NewValue, e.UpdateTime);
|
||||
e.Variable.Id, e.Variable.DataValue, e.Variable.UpdatedAt);
|
||||
|
||||
// 在UI线程上更新变量值
|
||||
App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
// 查找并更新对应的变量
|
||||
if (_dataStorageService.Variables.TryGetValue(e.VariableId,out var variableToUpdate))
|
||||
if (_dataStorageService.Variables.TryGetValue(e.Variable.Id,out var variableToUpdate))
|
||||
{
|
||||
_logger?.LogDebug("更新变量 {VariableId} ({VariableName}) 的值: {NewValue}",
|
||||
e.VariableId, variableToUpdate.Name, e.NewValue);
|
||||
|
||||
variableToUpdate.DataValue = e.NewValue;
|
||||
variableToUpdate.DisplayValue = e.NewValue;
|
||||
variableToUpdate.UpdatedAt = e.UpdateTime;
|
||||
|
||||
_logger?.LogDebug("变量 {VariableId} ({VariableName}) 的值已更新",
|
||||
e.VariableId, variableToUpdate.Name);
|
||||
variableToUpdate.DataValue = e.Variable.DataValue;
|
||||
variableToUpdate.DisplayValue = e.Variable.DisplayValue;
|
||||
variableToUpdate.UpdatedAt = e.Variable.UpdatedAt;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger?.LogWarning("在变量存储中找不到ID为 {VariableId} 的变量,无法更新值", e.VariableId);
|
||||
_logger?.LogWarning("在变量存储中找不到ID为 {VariableId} 的变量,无法更新值", e.Variable.Id);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using AutoMapper;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Interfaces.Database;
|
||||
using DMS.Core.Events;
|
||||
@@ -107,7 +108,7 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
|
||||
|
||||
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
|
||||
{
|
||||
if (e.VariableId != CurrentVariable.Id)
|
||||
if (e.Variable.Id != CurrentVariable.Id)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -116,7 +117,7 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
|
||||
{
|
||||
VariableId = CurrentVariable.Id,
|
||||
Timestamp = DateTime.Now,
|
||||
Value = e.NewValue
|
||||
Value = e.Variable.DataValue
|
||||
};
|
||||
_variableHistoryList.Add(variableHistory);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user