本次提交对变量管理和数据处理进行了多项重构和优化:

1.  重构变量管理服务:
      *   统一了单个和批量变量的更新逻辑,并引入 AutoMapper 进行对象映射,提高了代码的可维护性。
      *   增加了对 IsHistoryEnabled 属性变更的跟踪,允许系统对此类变更做出响应。

  2.  改进历史记录:
      *   HistoryProcessor 现在记录变量的 DisplayValue,使得历史数据与用户界面显示的值保持一致。

  3.  修正值转换逻辑:
      *   ValueConvertProcessor 在应用转换公式后,会同时更新变量的 NumericValue 和 DisplayValue,确保了数据在处理链中的一致性。
This commit is contained in:
2025-10-02 23:14:40 +08:00
parent 745cd43513
commit f330f67767
4 changed files with 22 additions and 73 deletions

View File

@@ -83,43 +83,7 @@ public class VariableManagementService : IVariableManagementService
/// </summary>
public async Task<int> 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>() { variableDto});
}
/// <summary>
@@ -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;
}
/// <summary>
/// 更新内存中的变量
/// </summary>
/// <param name="oldVariable">内存中的变量</param>
/// <param name="newVariable">更新后的变量</param>
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;
}
/// <summary>
/// 异步批量删除变量。

View File

@@ -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 // 记录当前时间
};

View File

@@ -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}'");

View File

@@ -113,6 +113,7 @@ namespace DMS.Core.Enums
/// <summary>
/// 所有属性
/// </summary>
All
All,
IsHistoryEnabled
}
}