LIN报文和高速数据服务功能

This commit is contained in:
2025-10-28 23:09:10 +08:00
parent 3b0eb86eb2
commit 46d086e74a
2 changed files with 98 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
using CapMachine.Wpf.ChannelModel;
using CapMachine.Wpf.ChannelModel;
using CapMachine.Wpf.Models;
using CapMachine.Wpf.Models.HighSpeed;
using CapMachine.Wpf.PrismEvent;
@@ -37,8 +37,8 @@ namespace CapMachine.Wpf.Services
ContainerProvider = containerProvider;
CacheHighFragMsg = new List<CommMsg>();
//100ms 触发一次
CycleTimer = new System.Timers.Timer(100);
//500ms 触发一次
CycleTimer = new System.Timers.Timer(500);
CycleTimer.Elapsed += CycleAction;
CycleTimer.AutoReset = true;
CycleTimer.Enabled = true;
@@ -59,12 +59,26 @@ namespace CapMachine.Wpf.Services
/// <param name="sender"></param>
/// <param name="e"></param>
/// <exception cref="NotImplementedException"></exception>
// 保护 CurListMsg 的并发锁
private readonly object _curListMsgLock = new object();
/// <summary>
/// 触发刷盘的记录条数阈值可根据业务容量调整默认1万条
/// </summary>
public int FlushRecordThreshold { get; set; } = 10000;
private void CycleAction(object? sender, ElapsedEventArgs e)
{
if (IsEnable)
{
//本体数据记录
CycleChannelInfo.Writer.WriteAsync(CurListMsg);
// 将当前快照写入通道,避免共享同一 List 引用导致的并发访问问题
List<CommMsg> snapshot;
lock (_curListMsgLock)
{
snapshot = new List<CommMsg>(CurListMsg);
}
// 非阻塞写入通道;若通道满则丢弃最老数据(由通道配置控制)
CycleChannelInfo.Writer.TryWrite(snapshot);
}
}
@@ -73,19 +87,22 @@ namespace CapMachine.Wpf.Services
/// </summary>
public void AppendOrUpdateMsg(CommMsg commMsg)
{
if (!CurListMsg.Exists(a => a.MsgInfo == commMsg.MsgInfo))
lock (_curListMsgLock)
{
//不存在则新增
CurListMsg.Add(commMsg);
}
else
{
//存在则更新数据
var Data = CurListMsg.Find(a => a.MsgInfo == commMsg.MsgInfo);
//Data!.MsgInfo = commMsg.MsgInfo;
//Data!.Category = commMsg.Category;
Data!.MsgData = commMsg.MsgData;
Data!.Time = commMsg.Time;
// 以 Category+MsgInfo 作为唯一键,避免不同总线类别之间发生键冲突
int idx = CurListMsg.FindIndex(a => a.MsgInfo == commMsg.MsgInfo && a.Category == commMsg.Category);
if (idx < 0)
{
//不存在则新增
CurListMsg.Add(commMsg);
}
else
{
//存在则更新数据
var Data = CurListMsg[idx];
Data.MsgData = commMsg.MsgData;
Data.Time = commMsg.Time;
}
}
}
@@ -180,11 +197,11 @@ namespace CapMachine.Wpf.Services
////第一次计时
//stopwatch.Start(); //启动Stopwatch
//新增数据
// 新增数据,按条数累积以控制刷盘频率
CacheHighFragMsg.AddRange(CycleChannelData);
MaxCacheCellCount++;
MaxCacheCellCount += CycleChannelData.Count;
if (MaxCacheCellCount >= 600)
if (MaxCacheCellCount >= FlushRecordThreshold)
{
//CSV文件保存
SaveToCsv(CacheHighFragMsg);