using DW5S.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace DW5S.Service
{
///
/// 任务参考信号缓存
///
public class TaskRefCache
{
public static TaskRefCache Instance { get; private set; } = new TaskRefCache();
///
/// 缓存参考信号信息
///
private Dictionary> _cahceRefs = new Dictionary>();
public void AddRange(int taskId, List cgItems)
{
lock (this)
{
if (!_cahceRefs.ContainsKey(taskId))
_cahceRefs[taskId] = new List();
_cahceRefs[taskId].AddRange(cgItems);
}
}
public void ClearCache(int taskID)
{
lock (this)
{
if (_cahceRefs.ContainsKey(taskID))
{
_cahceRefs.Remove(taskID);
}
}
}
///
/// 获取相同任务时间内的参考信号
///
///
/// 开始时间
/// 结束时间
///
public List GetTaskRef(int taskID, DateTime start, DateTime end)
{
lock (this)
{
if (_cahceRefs.ContainsKey(taskID))
{
var filterRefs = _cahceRefs[taskID].Where(r => r.SigTime >= start && r.SigTime <= end).ToList();
return filterRefs;
}
else
{
return new List();
}
}
}
///
/// 清理过期参考信号
///
///
///
public void ClearExpireRef(int taskID)
{
lock (this)
{
if (_cahceRefs.ContainsKey(taskID))
{
var maxtime = _cahceRefs[taskID].Max(m => m.SigTime);
var mintime = maxtime.AddHours(-1);//清理一个小时的参考信号
_cahceRefs[taskID].RemoveAll(r => r.SigTime <= mintime);
}
}
}
public void ClearAll()
{
lock (this)
{
_cahceRefs.Clear();
}
}
}
}