ningshuxia
2022-12-12 e78f5936fee9ab4fff600515bb20a41a28f329c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
{
    /// <summary>
    /// json仓库
    /// </summary>
    public class JsonStore
    { 
 
        private static DateTime _debugDay=DateTime.MinValue;
        private static List<JsonData> _dayRecordList = null;//数据日缓存
 
        //获取缓存
        private static List<JsonData> GetDebugCache(DateTime dt)
        {
            var debugDay = dt.Date;
            if (debugDay != _debugDay)
            {
                _debugDay = debugDay;
                _dayRecordList = new List<JsonData>();
                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<List<DebugRecord>>(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;
        }
 
        /// <summary>
        /// 获取当前记录
        /// </summary>
        public static List<SrcRecord> 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<SrcRecord>();
            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;
        }
 
 
      
 
 
    }
}