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 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 pumpISopenlist = new List(); 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 { new Model.IsOpen { DateTime = Convert.ToDateTime(arr[0]), Isopen = arr[2] } } }; pumpISopenlist.Add(pumpISopen); } fs.Close(); return pumpISopenlist; } /// /// 获取具体时间段的开泵台数 /// 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 startTime = new List(); List endtime = new List(); 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 PumpList) { List<(DateTime, DateTime)> values = new List<(DateTime, DateTime)>(); values.Clear(); // List Endvalues = new List(); 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; } /// /// 模糊合并时间段 /// /// /// 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; } /// /// 合并五台泵的运行时间 /// /// /// 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 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 pumpISopenlist = new List(); 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 { new Model.Water { DateTime = Convert.ToDateTime(arr[0]), SingleWater =Convert.ToDouble( arr[2]) } } }; pumpISopenlist.Add(pumpISopen); } fs.Close(); return pumpISopenlist; } /// /// 获取有功电能 /// /// public List 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 pumpISopenlist = new List(); 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 { new Model.Ele { DateTime = Convert.ToDateTime(arr[0]), Value =Convert.ToDouble( arr[2]) } } }; pumpISopenlist.Add(pumpISopen); } fs.Close(); return pumpISopenlist; } } }