From f330f677678cbcf7a0da67c22487381859f974ab Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Thu, 2 Oct 2025 23:14:40 +0800 Subject: [PATCH] =?UTF-8?q?=20=E6=9C=AC=E6=AC=A1=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E5=AF=B9=E5=8F=98=E9=87=8F=E7=AE=A1=E7=90=86=E5=92=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=A4=84=E7=90=86=E8=BF=9B=E8=A1=8C=E4=BA=86=E5=A4=9A?= =?UTF-8?q?=E9=A1=B9=E9=87=8D=E6=9E=84=E5=92=8C=E4=BC=98=E5=8C=96=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 重构变量管理服务: * 统一了单个和批量变量的更新逻辑,并引入 AutoMapper 进行对象映射,提高了代码的可维护性。 * 增加了对 IsHistoryEnabled 属性变更的跟踪,允许系统对此类变更做出响应。 2. 改进历史记录: * HistoryProcessor 现在记录变量的 DisplayValue,使得历史数据与用户界面显示的值保持一致。 3. 修正值转换逻辑: * ValueConvertProcessor 在应用转换公式后,会同时更新变量的 NumericValue 和 DisplayValue,确保了数据在处理链中的一致性。 --- .../Management/VariableManagementService.cs | 65 ++----------------- .../Services/Processors/HistoryProcessor.cs | 2 +- .../Processors/ValueConvertProcessor.cs | 25 ++++--- DMS.Core/Enums/VariablePropertyType.cs | 3 +- 4 files changed, 22 insertions(+), 73 deletions(-) diff --git a/DMS.Application/Services/Management/VariableManagementService.cs b/DMS.Application/Services/Management/VariableManagementService.cs index 7bdac17..8185d3b 100644 --- a/DMS.Application/Services/Management/VariableManagementService.cs +++ b/DMS.Application/Services/Management/VariableManagementService.cs @@ -83,43 +83,7 @@ public class VariableManagementService : IVariableManagementService /// public async Task UpdateVariableAsync(VariableDto variableDto) { - var result = await _variableAppService.UpdateVariableAsync(variableDto); - - // 更新成功后,更新内存中的变量 - if (result > 0 && variableDto != null) - { - if (_appDataStorageService.Variables.TryGetValue(variableDto.Id, out var mVariableDto)) - { - // 比较旧值和新值,确定哪个属性发生了变化 - var changedProperties = GetChangedProperties(mVariableDto, variableDto); - - // 更新内存中的变量 - UpdateVariableInMemory(mVariableDto, variableDto); - - // 为每个发生变化的属性触发事件 - foreach (var property in changedProperties) - { - _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(ActionChangeType.Updated, variableDto, property)); - } - - // 如果没有任何属性发生变化,至少触发一次更新事件 - if (changedProperties.Count == 0) - { - _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(ActionChangeType.Updated, variableDto, VariablePropertyType.All)); - } - } - else - { - // 如果内存中不存在该变量,则直接添加 - _appDataStorageService.Variables.TryAdd(variableDto.Id, variableDto); - _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(ActionChangeType.Added, variableDto, VariablePropertyType.All)); - } - } - - return result; + return await UpdateVariablesAsync(new List() { variableDto}); } /// @@ -140,7 +104,7 @@ public class VariableManagementService : IVariableManagementService var changedProperties = GetChangedProperties(mVariableDto, variableDto); // 更新内存中的变量 - UpdateVariableInMemory(mVariableDto, variableDto); + _mapper.Map(variableDto,mVariableDto); // 为每个发生变化的属性触发事件 foreach (var property in changedProperties) @@ -266,6 +230,8 @@ public class VariableManagementService : IVariableManagementService if (oldVariable.IsActive != newVariable.IsActive) changedProperties.Add(VariablePropertyType.IsActive); + if (oldVariable.IsHistoryEnabled != newVariable.IsHistoryEnabled) + changedProperties.Add(VariablePropertyType.IsHistoryEnabled); if (oldVariable.OpcUaNodeId != newVariable.OpcUaNodeId) changedProperties.Add(VariablePropertyType.OpcUaNodeId); @@ -282,29 +248,6 @@ public class VariableManagementService : IVariableManagementService return changedProperties; } - /// - /// 更新内存中的变量 - /// - /// 内存中的变量 - /// 更新后的变量 - private void UpdateVariableInMemory(VariableDto oldVariable, VariableDto newVariable) - { - oldVariable.Name = newVariable.Name; - oldVariable.S7Address = newVariable.S7Address; - oldVariable.DataType = newVariable.DataType; - oldVariable.ConversionFormula = newVariable.ConversionFormula; - oldVariable.OpcUaUpdateType = newVariable.OpcUaUpdateType; - oldVariable.MqttAliases = newVariable.MqttAliases; - oldVariable.Description = newVariable.Description; - oldVariable.VariableTableId = newVariable.VariableTableId; - oldVariable.DataValue = newVariable.DataValue; - oldVariable.UpdatedAt = newVariable.UpdatedAt; - oldVariable.IsActive = newVariable.IsActive; - oldVariable.OpcUaNodeId = newVariable.OpcUaNodeId; - oldVariable.PollingInterval = newVariable.PollingInterval; - oldVariable.SignalType = newVariable.SignalType; - oldVariable.Protocol = newVariable.Protocol; - } /// /// 异步批量删除变量。 diff --git a/DMS.Application/Services/Processors/HistoryProcessor.cs b/DMS.Application/Services/Processors/HistoryProcessor.cs index 7d5aae9..8ac9c02 100644 --- a/DMS.Application/Services/Processors/HistoryProcessor.cs +++ b/DMS.Application/Services/Processors/HistoryProcessor.cs @@ -41,7 +41,7 @@ public class HistoryProcessor : IVariableProcessor, IDisposable var historyData = new VariableHistory { VariableId = context.Data.Id, - Value = context.Data.DataValue?.ToString() ?? string.Empty, + Value = context.Data.DisplayValue?.ToString() ?? string.Empty, Timestamp = DateTime.Now // 记录当前时间 }; diff --git a/DMS.Application/Services/Processors/ValueConvertProcessor.cs b/DMS.Application/Services/Processors/ValueConvertProcessor.cs index 83abf68..01d2695 100644 --- a/DMS.Application/Services/Processors/ValueConvertProcessor.cs +++ b/DMS.Application/Services/Processors/ValueConvertProcessor.cs @@ -62,16 +62,21 @@ public class ValueConvertProcessor : IVariableProcessor // 使用 DataTable.Compute 来安全地计算表达式 var result = new DataTable().Compute(expression, null); - // 将计算结果格式化后赋给 DisplayValue - if (result is double || result is decimal || result is float) - { - variable.DisplayValue = string.Format("{0:F2}", result); // 默认格式化为两位小数,可根据需要调整 - } - else - { - variable.DisplayValue = result.ToString(); - } - } + // 将计算结果格式化后赋给 DisplayValue + if (result is double || result is decimal || result is float) + { + variable.DisplayValue = string.Format("{0:F2}", result); // 默认格式化为两位小数,可根据需要调整 + variable.NumericValue = Convert.ToDouble(result); // 更新NumericValue为计算后的值 + } + else + { + variable.DisplayValue = result.ToString(); + // 尝试将字符串结果解析回double,以更新NumericValue + if (double.TryParse(result.ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedResult)) + { + variable.NumericValue = parsedResult; + } + } } catch (Exception ex) { _logger.LogError(ex, $"为变量 {variable.Name} (ID: {variable.Id}) 计算DisplayValue时出错。公式: '{variable.ConversionFormula}'"); diff --git a/DMS.Core/Enums/VariablePropertyType.cs b/DMS.Core/Enums/VariablePropertyType.cs index 0fc5fea..720f3b1 100644 --- a/DMS.Core/Enums/VariablePropertyType.cs +++ b/DMS.Core/Enums/VariablePropertyType.cs @@ -113,6 +113,7 @@ namespace DMS.Core.Enums /// /// 所有属性 /// - All + All, + IsHistoryEnabled } } \ No newline at end of file