diff --git a/DMS.Application/Events/VariableValueChangedEventArgs.cs b/DMS.Application/Events/VariableValueChangedEventArgs.cs new file mode 100644 index 0000000..299c42c --- /dev/null +++ b/DMS.Application/Events/VariableValueChangedEventArgs.cs @@ -0,0 +1,29 @@ +using DMS.Application.DTOs; + +namespace DMS.Application.Events +{ + /// + /// 变量值变更事件参数 + /// + public class VariableValueChangedEventArgs : EventArgs + { + /// + /// 变量DTO对象 + /// + public VariableDto Variable { get; set; } + + /// + /// 旧值 + /// + public string? OldValue { get; set; } + + /// + /// 构造函数 + /// + public VariableValueChangedEventArgs(VariableDto variable, string? oldValue) + { + Variable = variable; + OldValue = oldValue; + } + } +} \ No newline at end of file diff --git a/DMS.Application/Services/EventService.cs b/DMS.Application/Services/EventService.cs index 51fb2ff..0f48bde 100644 --- a/DMS.Application/Services/EventService.cs +++ b/DMS.Application/Services/EventService.cs @@ -30,36 +30,7 @@ public class EventService : IEventService public event EventHandler OnDeviceChanged; - /// - /// 触发设备状态改变事件 - /// - /// 事件发送者 - /// 设备状态改变事件参数 - 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); - } - } - } - /// - /// 触发设备连接状态改变事件 - /// - /// 事件发送者 - /// 设备状态改变事件参数 - 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); - } /// /// 触发设备状态改变事件(统一事件) diff --git a/DMS.Application/Services/Processors/UpdateViewProcessor.cs b/DMS.Application/Services/Processors/UpdateViewProcessor.cs index ae45de2..45afaeb 100644 --- a/DMS.Application/Services/Processors/UpdateViewProcessor.cs +++ b/DMS.Application/Services/Processors/UpdateViewProcessor.cs @@ -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); } } \ No newline at end of file diff --git a/DMS.Core/Events/DeviceActiveChangedEventArgs.cs b/DMS.Core/Events/DeviceActiveChangedEventArgs.cs deleted file mode 100644 index af20afc..0000000 --- a/DMS.Core/Events/DeviceActiveChangedEventArgs.cs +++ /dev/null @@ -1,45 +0,0 @@ -namespace DMS.Core.Events; - -/// -/// 设备状态改变事件参数 -/// 已弃用:请使用 DeviceStateChangedEventArgs 替代 -/// -[Obsolete("Use DeviceStateChangedEventArgs instead")] -public class DeviceActiveChangedEventArgs : EventArgs -{ - /// - /// 设备ID - /// - public int DeviceId { get; } - - /// - /// 设备名称 - /// - public string DeviceName { get; } - - - /// - /// 新状态 - /// - public bool NewStatus { get; } - - /// - /// 状态改变时间 - /// - public DateTime ChangeTime { get; } - - /// - /// 初始化DeviceStatusChangedEventArgs类的新实例 - /// - /// 设备ID - /// 设备名称 - /// 旧状态 - /// 新状态 - public DeviceActiveChangedEventArgs(int deviceId, string deviceName, bool newStatus) - { - DeviceId = deviceId; - DeviceName = deviceName; - NewStatus = newStatus; - ChangeTime = DateTime.Now; - } -} \ No newline at end of file diff --git a/DMS.Core/Events/DeviceConnectChangedEventArgs.cs b/DMS.Core/Events/DeviceConnectChangedEventArgs.cs deleted file mode 100644 index c4cc3b9..0000000 --- a/DMS.Core/Events/DeviceConnectChangedEventArgs.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace DMS.Core.Events; - -/// -/// 已弃用:请使用 DeviceStateChangedEventArgs 替代 -/// -[Obsolete("Use DeviceStateChangedEventArgs instead")] -public class DeviceConnectChangedEventArgs -{ - /// - /// 设备ID - /// - public int DeviceId { get; } - - /// - /// 设备名称 - /// - public string DeviceName { get; } - - /// - /// 新状态 - /// - public bool NewStatus { get; } - - /// - /// 初始化DeviceStatusChangedEventArgs类的新实例 - /// - /// 设备ID - /// 设备名称 - /// 旧状态 - /// 新状态 - public DeviceConnectChangedEventArgs(int deviceId, string deviceName, bool newStatus) - { - DeviceId = deviceId; - DeviceName = deviceName; - NewStatus = newStatus; - } -} \ No newline at end of file diff --git a/DMS.Core/Events/VariableValueChangedEventArgs.cs b/DMS.Core/Events/VariableValueChangedEventArgs.cs deleted file mode 100644 index ed9067e..0000000 --- a/DMS.Core/Events/VariableValueChangedEventArgs.cs +++ /dev/null @@ -1,45 +0,0 @@ -namespace DMS.Core.Events -{ - /// - /// 变量值变更事件参数 - /// - public class VariableValueChangedEventArgs : EventArgs - { - /// - /// 变量ID - /// - public int VariableId { get; set; } - - /// - /// 变量名称 - /// - public string VariableName { get; set; } - - /// - /// 旧值 - /// - public string OldValue { get; set; } - - /// - /// 新值 - /// - public string NewValue { get; set; } - - /// - /// 更新时间 - /// - public DateTime UpdateTime { get; set; } - - /// - /// 构造函数 - /// - public VariableValueChangedEventArgs(int variableId, string variableName, string oldValue, string newValue, DateTime updateTime) - { - VariableId = variableId; - VariableName = variableName; - OldValue = oldValue; - NewValue = newValue; - UpdateTime = updateTime; - } - } -} \ No newline at end of file diff --git a/DMS.WPF/Services/DataEventService.cs b/DMS.WPF/Services/DataEventService.cs index a706ddc..b54898f 100644 --- a/DMS.WPF/Services/DataEventService.cs +++ b/DMS.WPF/Services/DataEventService.cs @@ -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); } })); } diff --git a/DMS.WPF/ViewModels/VariableHistoryViewModel.cs b/DMS.WPF/ViewModels/VariableHistoryViewModel.cs index d711e05..e55597a 100644 --- a/DMS.WPF/ViewModels/VariableHistoryViewModel.cs +++ b/DMS.WPF/ViewModels/VariableHistoryViewModel.cs @@ -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);