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;
|
}*/
|
}
|
}
|