lixiaojun
2022-08-22 cd280a9457eda95433896e3e9e3aa5164bdd64a0
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Model
{
    public partial class MonitorFluctRecord
    {
        /// <summary>
        /// 验证
        /// </summary>
        public bool Verify()
        {
            if (this.TimeArray == null || this.TimeArray.Count < 1)
                return default;
            if (this.MaxValues == null || this.MaxValues.Count < 1)
                return default;
            if (this.MinValues == null || this.MinValues.Count < 1)
                return default;
            if ((this.TimeArray.Count == this.MaxValues.Count) && (this.MaxValues.Count == this.MinValues.Count))
                return true;
            return default;
        }
 
        /// <summary>
        /// 获取最小值
        /// </summary>
        public double? GetMinValue(DateTime time)
        {
            if (!Verify())
                return default;
            var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
            var index = this.TimeArray.IndexOf(seconds);
            if (index < 0)
                return default;
            return this.MinValues[index];
        }
 
        /// <summary>
        /// 获取大约最小值
        /// </summary>
        public double? GetRoundMinValue(DateTime time)
        {
            if (!Verify())
                return default;
            var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
            var index = this.TimeArray.IndexOf(seconds);
            if (index < 0)
            {
                var smallerIndex = this.TimeArray.FindLastIndex(x => x < seconds);
                var biggerIndex = this.TimeArray.FindIndex(x => x > seconds);
                if (smallerIndex > -1)
                {
                    //大一点和小一点的都存在
                    if (biggerIndex > -1)
                    {
                        var smaller = this.MinValues[smallerIndex];
                        var bigger = this.MinValues[biggerIndex];
                        var value = smaller + (bigger - smaller) / (this.TimeArray[biggerIndex] - this.TimeArray[smallerIndex]) * (seconds - this.TimeArray[smallerIndex]);
                        return value;
                    }
                    else//小一点的存在 大一点的不存在
                    {
                        var smaller = this.MinValues[smallerIndex];
                        return smaller;
                    }
                }
                else
                {
                    //小一点的不存在 大一点的存在
                    if (biggerIndex > -1)
                    {
                        var bigger = this.MinValues[biggerIndex];
                        return bigger;
                    }
                    else//小一点的和大一点的都不存在
                    {
                        return default;
                    }
                }
 
            }
            return this.MinValues[index];
        }
 
        /// <summary>
        /// 获取最大值
        /// </summary>
        public double? GetMaxValue(DateTime time)
        {
            if (!Verify())
                return default;
            var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
            var index = this.TimeArray.IndexOf(seconds);
            if (index < 0)
                return default;
            return this.MaxValues[index];
        }
 
        /// <summary>
        /// 获取大约最大值
        /// </summary>
        public double? GetRoundMaxValue(DateTime time)
        {
            if (!Verify())
                return default;
            var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
            var index = this.TimeArray.IndexOf(seconds);
            if (index < 0)
            {
                var smallerIndex = this.TimeArray.FindLastIndex(x => x < seconds);
                var biggerIndex = this.TimeArray.FindIndex(x => x > seconds);
                if (smallerIndex > -1)
                {
                    //大一点和小一点的都存在
                    if (biggerIndex > -1)
                    {
                        var smaller = this.MaxValues[smallerIndex];
                        var bigger = this.MaxValues[biggerIndex];
                        var value = smaller + (bigger - smaller) / (this.TimeArray[biggerIndex] - this.TimeArray[smallerIndex]) * (seconds - this.TimeArray[smallerIndex]);
                        return value;
                    }
                    else//小一点的存在 大一点的不存在
                    {
                        var smaller = this.MaxValues[smallerIndex];
                        return smaller;
                    }
                }
                else
                {
                    //小一点的不存在 大一点的存在
                    if (biggerIndex > -1)
                    {
                        var bigger = this.MaxValues[biggerIndex];
                        return bigger;
                    }
                    else//小一点的和大一点的都不存在
                    {
                        return default;
                    }
                }
            }
            return this.MaxValues[index];
        }
 
        /// <summary>
        /// 设置值
        /// </summary>
        public void SetValue(DateTime time, double value)
        {
            if (this.TimeArray == null)
                this.TimeArray = new List<int>();
            if (this.MaxValues == null)
                this.MaxValues = new List<double>();
            if (this.MinValues == null)
                this.MinValues = new List<double>();
            var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
            var index = this.TimeArray.IndexOf(seconds);
            if (index < 0)
            {
                var biggerIndex = this.TimeArray.FindIndex(x => x > seconds);
                if (biggerIndex < 0)
                {
                    this.TimeArray.Add(seconds);
                    this.MaxValues.Add(value);
                    this.MinValues.Add(value);
                }
                else
                {
                    this.TimeArray.Insert(biggerIndex, seconds);
                    this.MaxValues.Insert(biggerIndex, value);
                    this.MinValues.Insert(biggerIndex, value);
                }
            }
            else
            {
                if (this.MaxValues[index] < value)
                    this.MaxValues[index] = value;
                if (this.MinValues[index] > value)
                    this.MinValues[index] = value;
            }
        }
 
    }
}