duheng
2024-03-27 dc97e187c607119bbd2945b9a277db8da15f8dc0
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using IStation.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
namespace IStation.WinFrmUI.Monitor
{
    public class HistoryDataAPiHelper
    {
        public List<Model.PumpIsopen> GetPumpRunParas()
        {
            string path = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(),
               "二取机泵参数", "RunTimeTest.csv");
            if (!File.Exists(path))
                return null;
            int totalLines = File.ReadLines(path, Encoding.GetEncoding("gb2312")).Count();//总行数
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
            string tempText;
            List<Model.PumpIsopen> pumpISopenlist = new List<Model.PumpIsopen>();
            for (int i = 0; i < totalLines; i++)
            {
                tempText = sr.ReadLine();
                string[] arr = tempText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var pumpISopen = new Model.PumpIsopen
                {
                    Tag = arr[1],
                    Values = new List<Model.IsOpen>
                    {
                        new Model.IsOpen
                        {
                            DateTime = Convert.ToDateTime(arr[0]),
                            Isopen = arr[2]
                        }
                     }
                };
                pumpISopenlist.Add(pumpISopen);
            }
            fs.Close();
            return pumpISopenlist;
        }
 
      
 
 
        /// <summary>
        /// 获取具体时间段的开泵台数
        /// </summary>
        public List<(DateTime, DateTime, int)> getPumpIsOpen()
        {
            var TestIsOpen = GetPumpRunParas();
            if (TestIsOpen == null)
                return null;
             var Pump1 = TestIsOpen.Where(x => x.Tag == "_0402010204012101001").ToList();
            var Pump2 = TestIsOpen.Where(x => x.Tag == "_0402010204012201001").ToList();
            var Pump3 = TestIsOpen.Where(x => x.Tag == "_0402010204012301001").ToList();
            var Pump4 = TestIsOpen.Where(x => x.Tag == "_0402010204012401001").ToList();
            var Pump5 = TestIsOpen.Where(x => x.Tag == "_0402010204012501001").ToList();
            List<string> startTime = new List<string>();
            List<string> endtime = new List<string>();
            var one = GetData(Pump1);
            var two = GetData(Pump2);
            var three = GetData(Pump3);
            var four = GetData(Pump4);
            var five = GetData(Pump5);
            List<(DateTime, DateTime)> mergedList = MergeList(one, two, three, four, five);
            return MergeTimeSlots(mergedList);
         }
 
 
 
        //获取详细开关机时间
        private List<(DateTime, DateTime)> GetData(List<PumpIsopen> PumpList)
        {
            List<(DateTime, DateTime)> values = new List<(DateTime, DateTime)>();
            values.Clear();
            //    List<DateTime> Endvalues = new List<DateTime>();
            DateTime startTime;
            DateTime EndTime;
            for (int time = 0; time < PumpList.Count; time++)
            {
                foreach (var value in PumpList[time].Values)
                {
                    if (value.Isopen == "1")
                    {
                        startTime = value.DateTime;
                        EndTime = value.DateTime;
                        for (int k = time + 1; k < PumpList.Count; k++)   //k为从开机后比较的变量下标
                        {
                            if (PumpList[k].Values[0].Isopen == "1")
                            {
                                EndTime = PumpList[k].Values[0].DateTime;
                                time = k;
                            }
                            else break;
                        }
                        values.Add((startTime, EndTime));
                    }
                }
            }
            return values;
        }
 
 
 
 
        /// <summary>
        /// 模糊合并时间段
        /// </summary>
        /// <param name="Date"></param>
        /// <returns></returns>
        static List<(DateTime, DateTime, int)> MergeTimeSlots(List<(DateTime, DateTime)> Date)
        {
            List<(DateTime, DateTime, int)> mergedSlots = new List<(DateTime, DateTime, int)>();
            Date.Sort((x, y) => x.Item1.CompareTo(y.Item1)); // 按照起始时间排序
 
            DateTime mergedStart = Date[0].Item1;
            DateTime mergedEnd = Date[0].Item2;
            int mergeCount = 1;
 
            // 从第二个时间段开始迭代
            for (int i = 1; i < Date.Count; i++)
            {
                DateTime start = Date[i].Item1;
                DateTime end = Date[i].Item2;
 
                // 如果当前时间段的起始时间在模糊时间范围内,则合并
                if (start <= mergedEnd.AddMinutes(15)) // 假设模糊时间范围为15分钟
                {
                    mergedEnd = DateTime.Compare(mergedEnd, end) < 0 ? end : mergedEnd;
                    mergeCount++;
                }
                else
                {
                    mergedSlots.Add((mergedStart, mergedEnd, mergeCount));
                    mergedStart = start;
                    mergedEnd = end;
                    mergeCount = 1;
                }
            }
 
            // 添加最后一个时间段
            mergedSlots.Add((mergedStart, mergedEnd, mergeCount));
 
            return mergedSlots;
        }
 
 
        /// <summary>
        /// 合并五台泵的运行时间
        /// </summary>
        /// <param name="lists"></param>
        /// <returns></returns>
        static List<(DateTime, DateTime)> MergeList(params List<(DateTime, DateTime)>[] lists)
        {
            List<(DateTime, DateTime)> mergedList = new List<(DateTime, DateTime)>();
              foreach (var list in lists)
            {
                mergedList.AddRange(list);
            }
             return mergedList;
         }
 
/*
 
        public List <Model.PumpWater> GetWaterData()
        {
            string path = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(),
              "二取机泵参数", "WaterTest.csv");
            if (!File.Exists(path))
                return  null;
            int totalLines = File.ReadLines(path, Encoding.GetEncoding("gb2312")).Count();//总行数
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
            string tempText;
            List<Model.PumpWater> pumpISopenlist = new List<Model.PumpWater>();
            for (int i = 0; i < totalLines; i++)
            {
                tempText = sr.ReadLine();
                string[] arr = tempText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var pumpISopen = new Model.PumpWater
                {
                    Tag = arr[1],
                    Values = new List<Model.Water>
                    {
                        new Model.Water
                        {
                            DateTime = Convert.ToDateTime(arr[0]),
                            SingleWater =Convert.ToDouble( arr[2])
                        }
                     }
                };
                pumpISopenlist.Add(pumpISopen);
            }
            fs.Close();
            return pumpISopenlist;
        }
*/
      
        /// <summary>
        /// 获取有功电能
        /// </summary>
        /// <returns></returns>
/*       public List<Model.electricity> GetEleData()
        {
            string path = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(),
              "二取机泵参数", "AmountEle.csv");
            if (!File.Exists(path))
                return null;
            int totalLines = File.ReadLines(path, Encoding.GetEncoding("gb2312")).Count();//总行数
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.GetEncoding("gb2312"));
            string tempText;
            List<Model.electricity> pumpISopenlist = new List<Model.electricity>();
            for (int i = 0; i < totalLines; i++)
            {
                tempText = sr.ReadLine();
                string[] arr = tempText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var pumpISopen = new Model.electricity
                {
                    Tag = arr[1],
                    TotalEle = new List<Model.Ele>
                    {
                       new Model.Ele
                        {
                            DateTime = Convert.ToDateTime(arr[0]),
                            Value =Convert.ToDouble( arr[2])
                        }
                    }
                };
                pumpISopenlist.Add(pumpISopen);
            }
            fs.Close();
            return pumpISopenlist;
        }*/
    }
}