简单实现变量的历史记录功能
This commit is contained in:
@@ -66,6 +66,7 @@ public partial class App : Application
|
|||||||
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<CheckValueChangedProcessor>());
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<CheckValueChangedProcessor>());
|
||||||
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<LoggingDataProcessor>());
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<LoggingDataProcessor>());
|
||||||
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<UpdateDbVariableProcessor>());
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<UpdateDbVariableProcessor>());
|
||||||
|
dataProcessingService.AddProcessor(Host.Services.GetRequiredService<HistoryDataProcessor>());
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
@@ -116,6 +117,7 @@ public partial class App : Application
|
|||||||
services.AddSingleton<CheckValueChangedProcessor>();
|
services.AddSingleton<CheckValueChangedProcessor>();
|
||||||
services.AddSingleton<LoggingDataProcessor>();
|
services.AddSingleton<LoggingDataProcessor>();
|
||||||
services.AddSingleton<UpdateDbVariableProcessor>();
|
services.AddSingleton<UpdateDbVariableProcessor>();
|
||||||
|
services.AddSingleton<HistoryDataProcessor>();
|
||||||
|
|
||||||
// 注册数据仓库
|
// 注册数据仓库
|
||||||
services.AddSingleton<DeviceRepository>();
|
services.AddSingleton<DeviceRepository>();
|
||||||
@@ -245,6 +247,7 @@ public partial class App : Application
|
|||||||
_db.CodeFirst.InitTables<DbDevice>();
|
_db.CodeFirst.InitTables<DbDevice>();
|
||||||
_db.CodeFirst.InitTables<DbVariableTable>();
|
_db.CodeFirst.InitTables<DbVariableTable>();
|
||||||
_db.CodeFirst.InitTables<DbVariableData>();
|
_db.CodeFirst.InitTables<DbVariableData>();
|
||||||
|
_db.CodeFirst.InitTables<DbVariableDataHistory>();
|
||||||
_db.CodeFirst.InitTables<DbVariableS7Data>();
|
_db.CodeFirst.InitTables<DbVariableS7Data>();
|
||||||
_db.CodeFirst.InitTables<DbUser>();
|
_db.CodeFirst.InitTables<DbUser>();
|
||||||
_db.CodeFirst.InitTables<DbMqtt>();
|
_db.CodeFirst.InitTables<DbMqtt>();
|
||||||
|
|||||||
@@ -163,4 +163,10 @@ public class DbVariableData
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[Navigate(typeof(DbVariableDataMqtt), "VariableDataId", "MqttId")]
|
[Navigate(typeof(DbVariableDataMqtt), "VariableDataId", "MqttId")]
|
||||||
public List<DbMqtt>? Mqtts { get; set; }
|
public List<DbMqtt>? Mqtts { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关联的历史记录列表。
|
||||||
|
/// </summary>
|
||||||
|
[Navigate(NavigateType.OneToMany, nameof(DbVariableDataHistory.VariableDataId))]
|
||||||
|
public List<DbVariableDataHistory>? HistoryRecords { get; set; }
|
||||||
}
|
}
|
||||||
49
Data/Entities/DbVariableDataHistory.cs
Normal file
49
Data/Entities/DbVariableDataHistory.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
|
namespace PMSWPF.Data.Entities;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 表示数据库中的变量数据历史实体。
|
||||||
|
/// </summary>
|
||||||
|
[SugarTable("VarDataHistory")]
|
||||||
|
public class DbVariableDataHistory
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 历史记录唯一标识符。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变量名称。
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 节点ID,用于标识变量在设备或系统中的唯一路径。
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(IsNullable = true)]
|
||||||
|
public string NodeId { get; set; } = String.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 变量当前原始数据值。
|
||||||
|
/// </summary>
|
||||||
|
public string DataValue { get; set; } = String.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关联的DbVariableData的ID。
|
||||||
|
/// </summary>
|
||||||
|
public int VariableDataId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关联的DbVariableData实体。
|
||||||
|
/// </summary>
|
||||||
|
[Navigate(NavigateType.ManyToOne, nameof(VariableDataId))]
|
||||||
|
public DbVariableData? VariableData { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 历史记录的时间戳。
|
||||||
|
/// </summary>
|
||||||
|
public DateTime Timestamp { get; set; } = DateTime.Now;
|
||||||
|
}
|
||||||
94
Services/Processors/HistoryDataProcessor.cs
Normal file
94
Services/Processors/HistoryDataProcessor.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 Microsoft.Extensions.Logging;
|
||||||
|
using PMSWPF.Data;
|
||||||
|
using PMSWPF.Data.Entities;
|
||||||
|
using PMSWPF.Helper;
|
||||||
|
using PMSWPF.Models;
|
||||||
|
using PMSWPF.Services;
|
||||||
|
|
||||||
|
namespace PMSWPF.Services.Processors;
|
||||||
|
|
||||||
|
public class HistoryDataProcessor : IVariableDataProcessor, IDisposable
|
||||||
|
{
|
||||||
|
private const int BATCH_SIZE = 50; // 批量写入的阈值
|
||||||
|
private const int TIMER_INTERVAL_MS = 30 * 1000; // 30秒
|
||||||
|
|
||||||
|
private readonly ConcurrentQueue<DbVariableDataHistory> _queue = new();
|
||||||
|
private readonly Timer _timer;
|
||||||
|
|
||||||
|
public HistoryDataProcessor()
|
||||||
|
{
|
||||||
|
|
||||||
|
_timer = new Timer(async _ => await FlushQueueToDatabase(), null, Timeout.Infinite, Timeout.Infinite);
|
||||||
|
_timer.Change(TIMER_INTERVAL_MS, TIMER_INTERVAL_MS); // 启动定时器
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ProcessAsync(VariableDataContext context)
|
||||||
|
{
|
||||||
|
// 只有当数据发生变化时才记录历史
|
||||||
|
if (!context.Data.IsSave) // 如果数据已经被其他处理器处理过或者不需要保存,则跳过
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 VariableData 转换为 DbVariableDataHistory
|
||||||
|
var historyData = new DbVariableDataHistory
|
||||||
|
{
|
||||||
|
Name = context.Data.Name,
|
||||||
|
NodeId = context.Data.NodeId,
|
||||||
|
DataValue = context.Data.DataValue,
|
||||||
|
VariableDataId = 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<DbVariableDataHistory>();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
33
ValueConverts/NullableBooleanConverter.cs
Normal file
33
ValueConverts/NullableBooleanConverter.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Windows.Data;
|
||||||
|
|
||||||
|
namespace PMSWPF.ValueConverts
|
||||||
|
{
|
||||||
|
public class NullableBooleanConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is bool b && parameter is string paramString)
|
||||||
|
{
|
||||||
|
if (bool.TryParse(paramString, out bool paramBool))
|
||||||
|
{
|
||||||
|
return b == paramBool;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Binding.DoNothing;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is bool b && b && parameter is string paramString)
|
||||||
|
{
|
||||||
|
if (bool.TryParse(paramString, out bool paramBool))
|
||||||
|
{
|
||||||
|
return paramBool;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Binding.DoNothing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user