重构项目,将项目拆分(临时提交)
This commit is contained in:
33
DMS.WPF/Services/Processors/CheckValueChangedProcessor.cs
Normal file
33
DMS.WPF/Services/Processors/CheckValueChangedProcessor.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using DMS.Helper;
|
||||
using DMS.Models;
|
||||
|
||||
namespace DMS.Services.Processors;
|
||||
|
||||
public class CheckValueChangedProcessor : IVariableProcessor
|
||||
{
|
||||
private readonly DataServices _dataServices;
|
||||
|
||||
public CheckValueChangedProcessor(DataServices dataServices)
|
||||
{
|
||||
_dataServices = dataServices;
|
||||
}
|
||||
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 (newVariable.DataValue == oldVariable.DataValue)
|
||||
{
|
||||
// 值没有变化,直接完成
|
||||
context.IsHandled = true;
|
||||
}
|
||||
|
||||
// 在这里处理 context.Data
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
94
DMS.WPF/Services/Processors/HistoryProcessor.cs
Normal file
94
DMS.WPF/Services/Processors/HistoryProcessor.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Data;
|
||||
using DMS.Data.Entities;
|
||||
using DMS.Helper;
|
||||
using DMS.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using DMS.Services;
|
||||
|
||||
namespace DMS.Services.Processors;
|
||||
|
||||
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 Timer _timer;
|
||||
|
||||
public HistoryProcessor()
|
||||
{
|
||||
|
||||
_timer = new Timer(async _ => await FlushQueueToDatabase(), null, Timeout.Infinite, Timeout.Infinite);
|
||||
_timer.Change(TIMER_INTERVAL_MS, TIMER_INTERVAL_MS); // 启动定时器
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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(TIMER_INTERVAL_MS, TIMER_INTERVAL_MS);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
// 在 Dispose 时,尝试将剩余数据写入数据库
|
||||
FlushQueueToDatabase().Wait();
|
||||
}
|
||||
}
|
||||
23
DMS.WPF/Services/Processors/LoggingProcessor.cs
Normal file
23
DMS.WPF/Services/Processors/LoggingProcessor.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using DMS.Helper;
|
||||
|
||||
namespace DMS.Services.Processors;
|
||||
|
||||
/// <summary>
|
||||
/// 一个简单的数据处理器实现,用于演示。
|
||||
/// 其主要功能是记录接收到的变量数据的名称和值。
|
||||
/// </summary>
|
||||
public class LoggingProcessor : IVariableProcessor
|
||||
{
|
||||
public LoggingProcessor()
|
||||
{
|
||||
}
|
||||
|
||||
public Task ProcessAsync(VariableContext context)
|
||||
{
|
||||
// NlogHelper.Info($"处理数据: {context.Data.Name}, 值: {context.Data.DataValue}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
39
DMS.WPF/Services/Processors/MqttPublishProcessor.cs
Normal file
39
DMS.WPF/Services/Processors/MqttPublishProcessor.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Models;
|
||||
|
||||
namespace DMS.Services.Processors
|
||||
{
|
||||
/// <summary>
|
||||
/// 负责将变量数据发布到MQTT的处理器。
|
||||
/// </summary>
|
||||
public class MqttPublishProcessor : IVariableProcessor
|
||||
{
|
||||
private readonly MqttBackgroundService _mqttBackgroundService;
|
||||
|
||||
public MqttPublishProcessor(MqttBackgroundService mqttBackgroundService)
|
||||
{
|
||||
_mqttBackgroundService = mqttBackgroundService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理单个变量上下文,如果有关联的MQTT配置,则将其推送到发送队列。
|
||||
/// </summary>
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
37
DMS.WPF/Services/Processors/UpdateDbVariableProcessor.cs
Normal file
37
DMS.WPF/Services/Processors/UpdateDbVariableProcessor.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Helper;
|
||||
using DMS.Models;
|
||||
|
||||
namespace DMS.Services.Processors
|
||||
{
|
||||
public class UpdateDbVariableProcessor : IVariableProcessor
|
||||
{
|
||||
private readonly DataServices _dataServices;
|
||||
|
||||
public UpdateDbVariableProcessor(DataServices dataServices)
|
||||
{
|
||||
_dataServices = dataServices;
|
||||
}
|
||||
|
||||
public async Task ProcessAsync(VariableContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 假设 DataServices 有一个方法来更新 Variable
|
||||
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;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NlogHelper.Error($"更新数据库变量 {context.Data.Name} 失败: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user