using IStation.Untity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.DataProvider
{
///
/// json仓库
///
public class JsonStore
{
private static DateTime _debugDay=DateTime.MinValue;
private static List _dayRecordList = null;//数据日缓存
//获取缓存
private static List GetDebugCache(DateTime dt)
{
var debugDay = dt.Date;
if (debugDay != _debugDay)
{
_debugDay = debugDay;
_dayRecordList = new List();
var fullFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigHelper.TestDataFolder, _debugDay.Day.ToString() + ".txt");
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
var allLines = File.ReadAllLines(fullFileName,Encoding.GetEncoding("GB2312"));
foreach (var line in allLines)
{
if (string.IsNullOrEmpty(line))
continue;
var timeIndex = line.IndexOf(",");
var timeStr = line.Substring(0, timeIndex);
var time = DateTime.Parse(timeStr);
var jsonIndex = line.IndexOf("|");
var jsonStr = line.Substring(jsonIndex + 1);
jsonStr = jsonStr.Substring(0, jsonStr.Length - 1);
var jsonList = JsonConvert.DeserializeObject>(jsonStr);
_dayRecordList.Add(new JsonData(new DateTime(_debugDay.Year,_debugDay.Month,_debugDay.Day,time.Hour,time.Minute,time.Second), jsonList));
}
_dayRecordList = _dayRecordList.OrderBy(x => x.DebugTime).ToList();
}
return _dayRecordList;
}
///
/// 获取当前记录
///
public static List GetCurrentRecord(DateTime dt)
{
var debugList = GetDebugCache(dt);
if (debugList == null || debugList.Count < 1)
return default;
var debugLast = debugList.FindLast(x => x.DebugTime < dt);
if (debugLast == null)
return default;
var srcList = new List();
foreach (var debugRecord in debugLast.DebugRecords)
{
if (!string.IsNullOrEmpty(debugRecord.SignId))
{
var srcRecord = new SrcRecord();
srcRecord.TagName = debugRecord.SignId;
srcRecord.DataTime = debugRecord.SrcTime;
srcRecord.DataValue = debugRecord.SrcValue;
srcList.Add(srcRecord);
}
}
return srcList;
}
}
}