From 1dbae9c2080b27edf618e9bd515cf36e42befd5d Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Fri, 3 Oct 2025 20:13:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(HistoryProcessor):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=AD=BB=E5=8C=BA=E6=A3=80=E6=9F=A5=E5=8A=9F=E8=83=BD=E4=BB=A5?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E5=BA=93=E5=86=99=E5=85=A5?= =?UTF-8?q?=20-=20=E5=BC=95=E5=85=A5=20ConcurrentDictionary=20=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=AE=89=E5=85=A8=E5=AD=97=E5=85=B8=E6=9D=A5=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=9C=80=E5=90=8E=E5=86=99=E5=85=A5=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E5=80=BC=20-=20=E5=AE=9E=E7=8E=B0=E6=AD=BB=E5=8C=BA=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BB=85=E5=BD=93=E6=95=B0?= =?UTF-8?q?=E5=80=BC=E5=8F=98=E5=8C=96=E8=B6=85=E8=BF=87=20HistoryDeadband?= =?UTF-8?q?=20=E6=97=B6=E6=89=8D=E5=86=99=E5=85=A5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=20-=20=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=EF=BC=8C=E6=98=BE=E7=A4=BA=E5=9B=A0=E6=9C=AA=E8=B6=85?= =?UTF-8?q?=E8=BF=87=E6=AD=BB=E5=8C=BA=E5=80=BC=E8=80=8C=E8=B7=B3=E8=BF=87?= =?UTF-8?q?=E5=86=99=E5=85=A5=E7=9A=84=E6=83=85=E5=86=B5=20-=20=E4=BF=9D?= =?UTF-8?q?=E6=8C=81=E5=8E=9F=E6=9C=89=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E6=80=A7=EF=BC=8C=E4=BB=85=E4=BC=98=E5=8C=96=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93=E5=86=99=E5=85=A5?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Processors/HistoryProcessor.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/DMS.Application/Services/Processors/HistoryProcessor.cs b/DMS.Application/Services/Processors/HistoryProcessor.cs index c854f70..35ce8fe 100644 --- a/DMS.Application/Services/Processors/HistoryProcessor.cs +++ b/DMS.Application/Services/Processors/HistoryProcessor.cs @@ -16,6 +16,8 @@ public class HistoryProcessor : IVariableProcessor, IDisposable private readonly Timer _timer; private readonly IRepositoryManager _repositoryManager; private readonly ILogger _logger; + private readonly ConcurrentDictionary _lastWrittenValues = new(); // 安全字典,缓存最后写入的数值 + public HistoryProcessor(IRepositoryManager repositoryManager, ILogger logger) { @@ -37,6 +39,23 @@ public class HistoryProcessor : IVariableProcessor, IDisposable return; } + // 检查数值变化是否超过死区值 + double currentValue = context.Data.NumericValue; + double historyDeadband = context.Data.HistoryDeadband; + int variableId = context.Data.Id; + + // 获取上次写入的值,如果不存在则使用当前值(第一次写入) + if (_lastWrittenValues.TryGetValue(variableId, out double lastWrittenValue)) + { + // 如果当前值与上次写入值的差值小于等于死区值,则跳过写入 + if (Math.Abs(currentValue - lastWrittenValue) <= historyDeadband) + { + _logger.LogDebug("变量 {VariableName} (ID: {VariableId}) 数值变化未超过死区值 {Deadband},跳过写入", + context.Data.Name, context.Data.Id, historyDeadband); + return; + } + } + // 将 VariableDto 转换为 VariableHistory var historyData = new VariableHistory { @@ -46,6 +65,9 @@ public class HistoryProcessor : IVariableProcessor, IDisposable Timestamp = DateTime.Now // 记录当前时间 }; + // 更新缓存中的最后写入值 + _lastWrittenValues[variableId] = currentValue; + _queue.Enqueue(historyData); _logger.LogDebug("变量 {VariableName} (ID: {VariableId}) 历史数据已入队,队列数量: {QueueCount}", context.Data.Name, context.Data.Id, _queue.Count);