消除所有错误,重新构建
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using DMS.Core.Helper;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Helper;
|
||||
using DMS.WPF.Models;
|
||||
using DMS.WPF.Services;
|
||||
|
||||
namespace DMS.Services.Processors;
|
||||
|
||||
@@ -14,19 +16,19 @@ public class CheckValueChangedProcessor : IVariableProcessor
|
||||
public Task ProcessAsync(VariableContext context)
|
||||
{
|
||||
Variable newVariable = context.Data;
|
||||
if (!_dataServices.AllVariables.TryGetValue(newVariable.Id, out Variable oldVariable))
|
||||
{
|
||||
NlogHelper.Warn($"检查变量值是否改变时在_dataServices.AllVariables中找不到Id:{newVariable.Id},Name:{newVariable.Name}的变量。");
|
||||
context.IsHandled = true;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
// if (!_dataServices.AllVariables.TryGetValue(newVariable.Id, out Variable oldVariable))
|
||||
// {
|
||||
// NlogHelper.Warn($"检查变量值是否改变时在_dataServices.AllVariables中找不到Id:{newVariable.Id},Name:{newVariable.Name}的变量。");
|
||||
// context.IsHandled = true;
|
||||
// return Task.CompletedTask;
|
||||
// }
|
||||
|
||||
if (newVariable.DataValue == oldVariable.DataValue)
|
||||
{
|
||||
// 值没有变化,直接完成
|
||||
context.IsHandled = true;
|
||||
}
|
||||
|
||||
// if (newVariable.DataValue == oldVariable.DataValue)
|
||||
// {
|
||||
// // 值没有变化,直接完成
|
||||
// context.IsHandled = true;
|
||||
// }
|
||||
//
|
||||
// 在这里处理 context.Data
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Infrastructure.Data;
|
||||
using DMS.Infrastructure.Entities;
|
||||
using DMS.Core.Helper;
|
||||
using DMS.WPF.Models;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DMS.WPF.Services.Processors;
|
||||
@@ -17,7 +10,7 @@ public class HistoryProcessor : IVariableProcessor, IDisposable
|
||||
private const int BATCH_SIZE = 50; // 批量写入的阈值
|
||||
private const int TIMER_INTERVAL_MS = 30 * 1000; // 30秒
|
||||
|
||||
private readonly ConcurrentQueue<DbVariableHistory> _queue = new();
|
||||
// private readonly ConcurrentQueue<DbVariableHistory> _queue = new();
|
||||
private readonly Timer _timer;
|
||||
|
||||
public HistoryProcessor()
|
||||
@@ -30,55 +23,55 @@ public class HistoryProcessor : IVariableProcessor, IDisposable
|
||||
public async Task ProcessAsync(VariableContext context)
|
||||
{
|
||||
// 只有当数据发生变化时才记录历史
|
||||
if (!context.Data.IsSave) // 如果数据已经被其他处理器处理过或者不需要保存,则跳过
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 将 Variable 转换为 DbVariableHistory
|
||||
var historyData = new DbVariableHistory
|
||||
{
|
||||
Name = context.Data.Name,
|
||||
NodeId = context.Data.NodeId,
|
||||
DataValue = context.Data.DataValue,
|
||||
VariableId = context.Data.Id,
|
||||
Timestamp = DateTime.Now // 记录当前时间
|
||||
};
|
||||
|
||||
_queue.Enqueue(historyData);
|
||||
|
||||
if (_queue.Count >= BATCH_SIZE)
|
||||
{
|
||||
await FlushQueueToDatabase();
|
||||
}
|
||||
// if (!context.Data.IsSave) // 如果数据已经被其他处理器处理过或者不需要保存,则跳过
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // 将 Variable 转换为 DbVariableHistory
|
||||
// var historyData = new DbVariableHistory
|
||||
// {
|
||||
// Name = context.Data.Name,
|
||||
// NodeId = context.Data.NodeId,
|
||||
// DataValue = context.Data.DataValue,
|
||||
// VariableId = context.Data.Id,
|
||||
// Timestamp = DateTime.Now // 记录当前时间
|
||||
// };
|
||||
//
|
||||
// _queue.Enqueue(historyData);
|
||||
//
|
||||
// if (_queue.Count >= BATCH_SIZE)
|
||||
// {
|
||||
// await FlushQueueToDatabase();
|
||||
// }
|
||||
}
|
||||
|
||||
private async Task FlushQueueToDatabase()
|
||||
{
|
||||
// 停止定时器,防止在写入过程中再次触发
|
||||
_timer.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
|
||||
var itemsToProcess = new List<DbVariableHistory>();
|
||||
while (_queue.TryDequeue(out var item))
|
||||
{
|
||||
itemsToProcess.Add(item);
|
||||
}
|
||||
|
||||
if (itemsToProcess.Any())
|
||||
{
|
||||
try
|
||||
{
|
||||
using var db = DbContext.GetInstance();
|
||||
await db.Insertable(itemsToProcess).ExecuteCommandAsync();
|
||||
NlogHelper.Info($"成功批量插入 {itemsToProcess.Count} 条历史变量数据。");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NlogHelper.Error( $"批量插入历史变量数据时发生错误: {ex.Message}",ex);
|
||||
// 错误处理:可以将未成功插入的数据重新放回队列,或者记录到日志中以便后续处理
|
||||
// 为了简单起见,这里不重新入队,避免无限循环
|
||||
}
|
||||
}
|
||||
// _timer.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
//
|
||||
// var itemsToProcess = new List<DbVariableHistory>();
|
||||
// while (_queue.TryDequeue(out var item))
|
||||
// {
|
||||
// itemsToProcess.Add(item);
|
||||
// }
|
||||
//
|
||||
// if (itemsToProcess.Any())
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// using var db = DbContext.GetInstance();
|
||||
// await db.Insertable(itemsToProcess).ExecuteCommandAsync();
|
||||
// NlogHelper.Info($"成功批量插入 {itemsToProcess.Count} 条历史变量数据。");
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// NlogHelper.Error( $"批量插入历史变量数据时发生错误: {ex.Message}",ex);
|
||||
// // 错误处理:可以将未成功插入的数据重新放回队列,或者记录到日志中以便后续处理
|
||||
// // 为了简单起见,这里不重新入队,避免无限循环
|
||||
// }
|
||||
// }
|
||||
|
||||
// 重新启动定时器
|
||||
_timer.Change(TIMER_INTERVAL_MS, TIMER_INTERVAL_MS);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Threading.Tasks;
|
||||
using DMS.WPF.Models;
|
||||
using DMS.Core.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using DMS.Helper;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Threading.Tasks;
|
||||
using DMS.WPF.Models;
|
||||
using DMS.Core.Models;
|
||||
|
||||
namespace DMS.Services.Processors
|
||||
{
|
||||
@@ -8,12 +8,12 @@ namespace DMS.Services.Processors
|
||||
/// </summary>
|
||||
public class MqttPublishProcessor : IVariableProcessor
|
||||
{
|
||||
private readonly MqttBackgroundService _mqttBackgroundService;
|
||||
|
||||
public MqttPublishProcessor(MqttBackgroundService mqttBackgroundService)
|
||||
{
|
||||
_mqttBackgroundService = mqttBackgroundService;
|
||||
}
|
||||
// private readonly MqttBackgroundService _mqttBackgroundService;
|
||||
//
|
||||
// public MqttPublishProcessor(MqttBackgroundService mqttBackgroundService)
|
||||
// {
|
||||
// _mqttBackgroundService = mqttBackgroundService;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// 处理单个变量上下文,如果有关联的MQTT配置,则将其推送到发送队列。
|
||||
@@ -21,19 +21,19 @@ namespace DMS.Services.Processors
|
||||
/// <param name="context">包含变量及其元数据的上下文对象。</param>
|
||||
public async Task ProcessAsync(VariableContext context)
|
||||
{
|
||||
var variable = context.Data;
|
||||
if (variable?.VariableMqtts == null || variable.VariableMqtts.Count == 0)
|
||||
{
|
||||
return; // 没有关联的MQTT配置,直接返回
|
||||
}
|
||||
|
||||
// 遍历所有关联的MQTT配置,并将其推入发送队列
|
||||
foreach (var variableMqtt in variable.VariableMqtts)
|
||||
{
|
||||
// 确保VariableMqtt对象中包含了最新的Variable数据
|
||||
variableMqtt.Variable = variable;
|
||||
await _mqttBackgroundService.SendVariableAsync(variableMqtt);
|
||||
}
|
||||
// var variable = context.Data;
|
||||
// if (variable?.VariableMqtts == null || variable.VariableMqtts.Count == 0)
|
||||
// {
|
||||
// return; // 没有关联的MQTT配置,直接返回
|
||||
// }
|
||||
//
|
||||
// // 遍历所有关联的MQTT配置,并将其推入发送队列
|
||||
// foreach (var variableMqtt in variable.VariableMqtts)
|
||||
// {
|
||||
// // 确保VariableMqtt对象中包含了最新的Variable数据
|
||||
// variableMqtt.Variable = variable;
|
||||
// await _mqttBackgroundService.SendVariableAsync(variableMqtt);
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Core.Helper;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Helper;
|
||||
using DMS.WPF.Models;
|
||||
using DMS.WPF.Services;
|
||||
|
||||
namespace DMS.Services.Processors
|
||||
{
|
||||
@@ -18,15 +20,15 @@ namespace DMS.Services.Processors
|
||||
try
|
||||
{
|
||||
// 假设 DataServices 有一个方法来更新 Variable
|
||||
await _dataServices.UpdateVariableAsync(context.Data);
|
||||
// await _dataServices.UpdateVariableAsync(context.Data);
|
||||
// NlogHelper.Info($"数据库变量 {context.Data.Name} 更新成功,值为: {context.Data.DataValue}");
|
||||
|
||||
if (!_dataServices.AllVariables.TryGetValue(context.Data.Id, out Variable oldVariable))
|
||||
{
|
||||
NlogHelper.Warn($"数据库更新完成修改变量值是否改变时在_dataServices.AllVariables中找不到Id:{context.Data.Id},Name:{context.Data.Name}的变量。");
|
||||
context.IsHandled = true;
|
||||
}
|
||||
oldVariable.DataValue = context.Data.DataValue;
|
||||
// if (!_dataServices.AllVariables.TryGetValue(context.Data.Id, out Variable oldVariable))
|
||||
// {
|
||||
// NlogHelper.Warn($"数据库更新完成修改变量值是否改变时在_dataServices.AllVariables中找不到Id:{context.Data.Id},Name:{context.Data.Name}的变量。");
|
||||
// context.IsHandled = true;
|
||||
// }
|
||||
// oldVariable.DataValue = context.Data.DataValue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user