feat(处理器): 增强变量处理链并实现批量更新

- 新增 UpdateDbVariableProcessor 处理器,通过队列和定时器实现数据库的批量更新,以降低负载。
  - 重构 ValueConvertProcessor 处理器,使其能够解析 ConversionFormula 公式,计算出最终的 DisplayValue。
  - 扩展 IVariableRepository 仓储接口,添加 UpdateBatchAsync 方法,并使用SqlSugar实现高效的批量更新。
  - 优化 VariableContext 模型,将 NewValue 类型统一为 string,简化了数据流并提升了类型安全。
This commit is contained in:
2025-10-02 17:35:35 +08:00
parent 2a98b40bfe
commit cdfb906112
8 changed files with 169 additions and 50 deletions

View File

@@ -170,4 +170,20 @@ public class VariableRepository : BaseRepository<DbVariable>, IVariableRepositor
.ToListAsync();
return _mapper.Map<List<Variable>>(dbVariables);
}
/// <summary>
/// 异步批量更新变量。
/// </summary>
/// <param name="variables">要更新的变量实体集合。</param>
/// <returns>受影响的行数。</returns>
public async Task<int> UpdateBatchAsync(IEnumerable<Variable> variables)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var dbVariables = _mapper.Map<List<DbVariable>>(variables);
var result = await _dbContext.GetInstance().Updateable(dbVariables).ExecuteCommandAsync();
stopwatch.Stop();
_logger.LogInformation($"Batch update {typeof(DbVariable)}, Count={dbVariables.Count}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}

View File

@@ -561,7 +561,7 @@ namespace DMS.Infrastructure.Services.OpcUa
variable.Name, variable.Id, opcUaNode.NodeId, context.Device.Name);
// 推送到数据处理队列
await _dataProcessingService.EnqueueAsync(new VariableContext(variable, opcUaNode.Value));
await _dataProcessingService.EnqueueAsync(new VariableContext(variable, opcUaNode.Value?.ToString()));
_logger.LogDebug("HandleDataChanged: 变量 {VariableName} 的值已推送到数据处理队列", variable.Name);
break;

View File

@@ -242,9 +242,8 @@ public class OptimizedS7BackgroundService : BackgroundService
{
if (readResults.TryGetValue(variable.S7Address, out var value))
{
// 将更新后的数据推入处理队列。
await _dataProcessingService.EnqueueAsync(new VariableContext(variable, value));
await _dataProcessingService.EnqueueAsync(new VariableContext(variable, value?.ToString()));
}
// else
// {