lixiaojun
2022-11-23 b5c20e5143555a1bdd450a1e660216b90c93b6f1
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
84
85
86
87
88
89
90
91
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<List<DebugRecord>> _dayRecordList = null;//数据日缓存
 
        //获取缓存
        private static List<DebugRecord> GetDebugCache(DateTime dt)
        {
            var debugDay = dt.Date;
            if (debugDay != _debugDay)
            {
                _debugDay = debugDay;
                _dayRecordList = new List<List<DebugRecord>>();
                var testDataFolder = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigHelper.TestDataFolder, _debugDay.Day.ToString());
                if (!Directory.Exists(testDataFolder))
                    return default;
                var directInfo = new DirectoryInfo(testDataFolder);
                var files = directInfo.GetFiles();
                if (files == null || files.Count() < 1)
                    return default;
                foreach (var filePath in files)
                {
                    var str = File.ReadAllText(filePath.FullName);
                    if (string.IsNullOrEmpty(str))
                        return default;
                    var data = JsonHelper.Json2Object<List<DebugRecord>>(str);
                    if (data == null || data.Count < 1)
                        continue;
                    _dayRecordList.Add(data);
                }
            }
 
            var list = new List<DebugRecord>();
            foreach (var records in _dayRecordList)
            {
                var record = records.FindLast(x =>
                {
                    var dt1 = Convert.ToDateTime(x.DataTime);
                    var dt2 = new DateTime(dt.Year, dt.Month, dt.Day, dt1.Hour, dt1.Minute, dt1.Second);
                    return dt2 < dt;
                });
                list.Add(record);
            }
            return list.Where(x => x != null).ToList();
        }
 
        /// <summary>
        /// 获取当前记录
        /// </summary>
        public static List<SrcRecord> GetCurrentRecord(DateTime dt)
        {
            var debugList = GetDebugCache(dt);
            if (debugList == null || debugList.Count < 1)
                return default;
 
 
            var srcList = new List<SrcRecord>();
            foreach (var debugRecord in debugList)
            {
                if (!string.IsNullOrEmpty(debugRecord.ObjectId))
                {
                    var srcRecord = new SrcRecord();
                    srcRecord.TagName = debugRecord.ObjectId;
                    srcRecord.DataTime = debugRecord.DataTime;
                    srcRecord.DataValue = debugRecord.DataValue;
                    srcList.Add(srcRecord);
                }
            }
            return srcList;
        }
 
 
      
 
 
    }
}