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; } //&&start>=mergedEnd.AddMinutes(-15) /// /// 模糊合并时间段 /// /// /// public List<(DateTime, DateTime, int, double, double, double)> MergeTimeSlots(List<(DateTime, DateTime, double, double, double)> Date) { List<(DateTime, DateTime, int, double, double, double)> mergedSlots = new List<(DateTime, DateTime, int, double, double, double)>(); // 初始化合并后的列表 Date.Sort((x, y) => x.Item1.CompareTo(y.Item1)); // 按照起始时间排序 if (Date.Count==0) return null; DateTime mergedStart = Date[0].Item1; DateTime mergedEnd = Date[0].Item2; int mergeCount = 1; double mergedDoubleELePrice = Date[0].Item3; // 电费 double mergedDoubleWater = Date[0].Item4; // 取水量 double mergedDoubleEle = Date[0].Item5; // 用电量 // 从第二个时间段开始迭代 for (int i = 1; i < Date.Count; i++) { DateTime start = Date[i].Item1; DateTime end = Date[i].Item2; double currentDouble1 = Date[i].Item3; double currentDouble2 = Date[i].Item4; double currentDouble3 = Date[i].Item5; // 如果当前时间段与合并时间段有重叠,则合并 if (start <= mergedStart.AddMinutes(30) && start >= mergedStart.AddMinutes(-30) && end <= mergedEnd.AddMinutes(30) && end >= mergedEnd.AddMinutes(-30)) { mergedStart = DateTime.Compare(mergedStart, start) < 0 ? mergedStart : start; mergedEnd = DateTime.Compare(mergedEnd, end) < 0 ? end : mergedEnd; mergeCount++; mergedDoubleELePrice += currentDouble1; mergedDoubleWater += currentDouble2; mergedDoubleEle += currentDouble3; } else { // 将已合并的时间段添加到列表中 mergedSlots.Add((mergedStart, mergedEnd, mergeCount, mergedDoubleELePrice, mergedDoubleWater, mergedDoubleEle)); // 更新合并时间段和 double 字段为当前时间段的值 mergedStart = start; mergedEnd = end; mergeCount = 1; mergedDoubleELePrice = currentDouble1; mergedDoubleWater = currentDouble2; mergedDoubleEle = currentDouble3; } } // 添加最后一个时间段 mergedSlots.Add((mergedStart, mergedEnd, mergeCount, mergedDoubleELePrice, mergedDoubleWater, mergedDoubleEle)); 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; }*/ } }