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