using IStation.Model;
|
using IStation.ZyModel;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.Common {
|
|
public class PumpReadFileHelper
|
{
|
public class RunParasData
|
{
|
/// <summary>
|
/// 数据日期
|
/// </summary>
|
public DateTime DateTime { get; set; }
|
/// <summary>
|
/// 泵运行状态
|
/// </summary>
|
public List<RunParasBlock> Pump1 { get; set; }
|
public List<RunParasBlock> Pump2 { get; set; }
|
public List<RunParasBlock> Pump3 { get; set; }
|
public List<RunParasBlock> Pump4 { get; set; }
|
public List<RunParasBlock> Pump5 { get; set; }
|
/// <summary>
|
/// 总取水量
|
/// </summary>
|
public double FlowIn { get; set; }
|
/// <summary>
|
/// 总用电量
|
/// </summary>
|
public double Electric { get; set; }
|
/// <summary>
|
/// 总电费
|
/// </summary>
|
public double Money { get; set; }
|
}
|
public class RunParasBlock
|
{
|
/// <summary>
|
/// 开始时间
|
/// </summary>
|
public DateTime StartTime { get; set; }
|
/// <summary>
|
/// 结束时间
|
/// </summary>
|
public DateTime EndTime { get; set; }
|
/// <summary>
|
/// 取水量
|
/// </summary>
|
public double FlowIn { get; set; }
|
/// <summary>
|
/// 用电量
|
/// </summary>
|
public double Electric { get; set; }
|
/// <summary>
|
/// 电费
|
/// </summary>
|
public double Money { get; set; }
|
}
|
|
|
List<ZyModel.RealScadaData> allFlows = null;
|
List<ZyModel.RealScadaData> allEles = null;
|
|
|
//取水量
|
private double? GetPumpFlowIndexByStartTime(int start_index, int pump_index, DateTime time)
|
{
|
for (int i = 0; i < allFlows[pump_index].MonitorRecords.Count; i++)
|
{
|
var v = allFlows[pump_index].MonitorRecords[i];
|
if (v.Time >= time && v.Value != null)
|
{
|
return v.Value;
|
}
|
}
|
|
return -1;
|
}
|
|
//电能
|
private double? GetPumpEleIndexByStartTime(int start_index, int pump_index, DateTime time)
|
{
|
for (int i = 0; i < allEles[pump_index].MonitorRecords.Count; i++)
|
{
|
var v = allEles[pump_index].MonitorRecords[i];
|
if (v.Time >= time && v.Value != null)
|
{
|
return v.Value;
|
}
|
}
|
|
return -1;
|
}
|
|
|
|
|
|
public void GetHistoryData(List< ZyModel.RealScadaData> allFolws, List<ZyModel.RealScadaData> allEle, List<ZyModel.RealScadaData> allRunstatus)
|
{
|
List<RunParasData> runParasDatasList = new List<RunParasData>();
|
|
DateTime startDay = new DateTime(2022, 11, 1);
|
DateTime endDay = new DateTime(2023, 11, 14);
|
|
allFlows = allFolws; //取水量
|
allEles = allEle; //有功电能
|
var allRunStaus = GetAllRunStatusData();
|
//运行参数
|
var pumprun1 = allRunstatus.Where(x => x.TagName == "1").ToList();
|
var pumprun2 = allRunstatus.Where(x => x.TagName == "2").ToList();
|
var pumprun3 = allRunstatus.Where(x => x.TagName == "3").ToList();
|
var pumprun4 = allRunstatus.Where(x => x.TagName == "4").ToList();
|
var pumprun5 = allRunstatus.Where(x => x.TagName == "5").ToList();
|
List<RunParasBlock> runParasBlockpump1 = new List<RunParasBlock>();
|
List<RunParasBlock> runParasBlockpump2 = new List<RunParasBlock>();
|
List<RunParasBlock> runParasBlockpump3 = new List<RunParasBlock>();
|
List<RunParasBlock> runParasBlockpump4 = new List<RunParasBlock>();
|
List<RunParasBlock> runParasBlockpump5 = new List<RunParasBlock>();
|
RunParasData day_sum = new RunParasData();
|
for (DateTime day = startDay; day <= endDay; day = day.AddDays(1))
|
{
|
day_sum = new RunParasData();
|
var next_day = day.AddDays(1);
|
day_sum.DateTime = day;
|
|
foreach (var time_block in pumprun1)
|
{
|
if (time_block.Item2 >= day && time_block.Item3 <= next_day)
|
{
|
RunParasBlock block = new RunParasBlock();
|
block.EndTime = time_block.Item3;
|
block.StartTime = time_block.Item2;
|
double? start_flow_in = GetPumpFlowIndexByStartTime(0, 0, time_block.Item2);
|
double? end_flow = GetPumpFlowIndexByStartTime(0, 0, time_block.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.FlowIn = (double)(end_flow - start_flow_in); //取水量
|
}
|
double? start_ELe = GetPumpEleIndexByStartTime(0, 0, time_block.Item2);
|
double? end_Ele = GetPumpEleIndexByStartTime(0, 0, time_block.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.Electric = (double)(end_Ele - start_ELe); //用电量
|
}
|
block.Money = GetMoney(time_block.Item2, time_block.Item3, 0);
|
if (time_block.Item1 == 1)
|
{
|
runParasBlockpump1.Add(block);
|
}
|
if (time_block.Item1 == 2)
|
{
|
runParasBlockpump2.Add(block);
|
}
|
if (time_block.Item1 == 3)
|
{
|
runParasBlockpump3.Add(block);
|
}
|
if (time_block.Item1 == 4)
|
{
|
runParasBlockpump4.Add(block);
|
}
|
if (time_block.Item1 == 5)
|
{
|
runParasBlockpump5.Add(block);
|
}
|
}
|
}
|
foreach (var item in pumprun2)
|
{
|
if (item.Item2 >= day && item.Item3 <= next_day)
|
{
|
RunParasBlock block = new RunParasBlock();
|
block.EndTime = item.Item3;
|
block.StartTime = item.Item2;
|
double? start_flow_in = GetPumpFlowIndexByStartTime(1, 1, item.Item2);
|
double? end_flow = GetPumpFlowIndexByStartTime(1, 1, item.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.FlowIn = (double)(end_flow - start_flow_in); //取水量
|
}
|
double? start_ELe = GetPumpEleIndexByStartTime(1, 1, item.Item2);
|
double? end_Ele = GetPumpEleIndexByStartTime(1, 1, item.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.Electric = (double)(end_Ele - start_ELe); //用电量
|
}
|
block.Money = GetMoney(item.Item2, item.Item3, 1);
|
if (item.Item1 == 1)
|
{
|
runParasBlockpump1.Add(block);
|
}
|
if (item.Item1 == 2)
|
{
|
runParasBlockpump2.Add(block);
|
}
|
if (item.Item1 == 3)
|
{
|
runParasBlockpump3.Add(block);
|
}
|
if (item.Item1 == 4)
|
{
|
runParasBlockpump4.Add(block);
|
}
|
if (item.Item1 == 5)
|
{
|
runParasBlockpump5.Add(block);
|
}
|
}
|
}
|
foreach (var item in pumprun3)
|
{
|
if (item.Item2 >= day && item.Item3 <= next_day)
|
{
|
RunParasBlock block = new RunParasBlock();
|
block.EndTime = item.Item3;
|
block.StartTime = item.Item2;
|
double? start_flow_in = GetPumpFlowIndexByStartTime(2, 2, item.Item2);
|
double? end_flow = GetPumpFlowIndexByStartTime(2, 2, item.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.FlowIn = (double)(end_flow - start_flow_in); //取水量
|
}
|
double? start_ELe = GetPumpEleIndexByStartTime(2, 2, item.Item2);
|
double? end_Ele = GetPumpEleIndexByStartTime(2, 2, item.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.Electric = (double)(end_Ele - start_ELe); //用电量
|
}
|
block.Money = GetMoney(item.Item2, item.Item3, 2);
|
if (item.Item1 == 1)
|
{
|
runParasBlockpump1.Add(block);
|
}
|
if (item.Item1 == 2)
|
{
|
runParasBlockpump2.Add(block);
|
}
|
if (item.Item1 == 3)
|
{
|
runParasBlockpump3.Add(block);
|
}
|
if (item.Item1 == 4)
|
{
|
runParasBlockpump4.Add(block);
|
}
|
if (item.Item1 == 5)
|
{
|
runParasBlockpump5.Add(block);
|
}
|
}
|
}
|
foreach (var item in pumprun4)
|
{
|
if (item.Item2 >= day && item.Item3 <= next_day)
|
{
|
RunParasBlock block = new RunParasBlock();
|
block.EndTime = item.Item3;
|
block.StartTime = item.Item2;
|
double? start_flow_in = GetPumpFlowIndexByStartTime(3, 3, item.Item2);
|
double? end_flow = GetPumpFlowIndexByStartTime(3, 3, item.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.FlowIn = (double)(end_flow - start_flow_in); //取水量
|
}
|
double? start_ELe = GetPumpEleIndexByStartTime(3, 3, item.Item2);
|
double? end_Ele = GetPumpEleIndexByStartTime(3, 3, item.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.Electric = (double)(end_Ele - start_ELe); //用电量
|
}
|
block.Money = GetMoney(item.Item2, item.Item3, 3);
|
|
if (item.Item1 == 1)
|
{
|
runParasBlockpump1.Add(block);
|
}
|
if (item.Item1 == 2)
|
{
|
runParasBlockpump2.Add(block);
|
}
|
if (item.Item1 == 3)
|
{
|
runParasBlockpump3.Add(block);
|
}
|
if (item.Item1 == 4)
|
{
|
runParasBlockpump4.Add(block);
|
}
|
if (item.Item1 == 5)
|
{
|
runParasBlockpump5.Add(block);
|
}
|
}
|
}
|
foreach (var item in pumprun5)
|
{
|
if (item.Item2 >= day && item.Item3 <= next_day)
|
{
|
RunParasBlock block = new RunParasBlock();
|
block.EndTime = item.Item3;
|
block.StartTime = item.Item2;
|
double? start_flow_in = GetPumpFlowIndexByStartTime(4, 4, item.Item2);
|
double? end_flow = GetPumpFlowIndexByStartTime(4, 4, item.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.FlowIn = (double)(end_flow - start_flow_in); //取水量
|
}
|
double? start_ELe = GetPumpEleIndexByStartTime(4, 4, item.Item2);
|
double? end_Ele = GetPumpEleIndexByStartTime(4, 4, item.Item3);
|
if (end_flow >= 0 && start_flow_in >= 0 && start_flow_in < end_flow)
|
{
|
block.Electric = (double)(end_Ele - start_ELe); //用电量
|
}
|
block.Money = GetMoney(item.Item2, item.Item3, 4);
|
|
if (item.Item1 == 1)
|
{
|
runParasBlockpump1.Add(block);
|
}
|
if (item.Item1 == 2)
|
{
|
runParasBlockpump2.Add(block);
|
}
|
if (item.Item1 == 3)
|
{
|
runParasBlockpump3.Add(block);
|
}
|
if (item.Item1 == 4)
|
{
|
runParasBlockpump4.Add(block);
|
}
|
if (item.Item1 == 5)
|
{
|
runParasBlockpump5.Add(block);
|
}
|
}
|
}
|
day_sum.Pump1 = runParasBlockpump1;
|
day_sum.Pump2 = runParasBlockpump2;
|
day_sum.Pump3 = runParasBlockpump3;
|
day_sum.Pump4 = runParasBlockpump4;
|
day_sum.Pump5 = runParasBlockpump5;
|
foreach (var item in runParasBlockpump1)
|
{
|
day_sum.Electric += item.Electric;
|
day_sum.FlowIn += item.FlowIn;
|
day_sum.Money += item.Money;
|
}
|
foreach (var item in runParasBlockpump2)
|
{
|
day_sum.Electric += item.Electric;
|
day_sum.FlowIn += item.FlowIn;
|
day_sum.Money += item.Money;
|
}
|
foreach (var item in runParasBlockpump3)
|
{
|
day_sum.Electric += item.Electric;
|
day_sum.FlowIn += item.FlowIn;
|
day_sum.Money += item.Money;
|
}
|
foreach (var item in runParasBlockpump4)
|
{
|
day_sum.Electric += item.Electric;
|
day_sum.FlowIn += item.FlowIn;
|
day_sum.Money += item.Money;
|
}
|
foreach (var item in runParasBlockpump5)
|
{
|
day_sum.Electric += item.Electric;
|
day_sum.FlowIn += item.FlowIn;
|
day_sum.Money += item.Money;
|
}
|
runParasDatasList.Add(day_sum);
|
var json = JsonHelper.Object2Json(runParasDatasList);
|
|
|
runParasBlockpump1 = new List<RunParasBlock>();
|
runParasBlockpump2 = new List<RunParasBlock>();
|
runParasBlockpump3 = new List<RunParasBlock>();
|
runParasBlockpump4 = new List<RunParasBlock>();
|
runParasBlockpump5 = new List<RunParasBlock>();
|
}
|
DateTime date = new DateTime(2022, 11, 1);
|
DateTime currenttime = new DateTime(2022, 11, 1);
|
|
// int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month);
|
// var path = "C:\\Users\\ZKC\\Desktop\\新建文件夹 (2)";
|
int processedDays = 0;
|
for (int k = 0; k < 30; k++)
|
{
|
int daysInMonth = DateTime.DaysInMonth(date.Year, date.Month); // 获取当前月份的天数
|
var path22 = $"C:\\Users\\ZKC\\Desktop\\新建文件夹 (2)\\{date.ToString("yyyy-MM")}";
|
var filepath = Directory.CreateDirectory(path22);
|
int i = 0;
|
|
foreach (var item in runParasDatasList.Skip(processedDays)) // 跳过已处理的天数
|
{
|
string jsonpath = string.Concat(path22, $"\\{currenttime.ToString("dd")}");
|
string json = JsonHelper.Object2Json(item);
|
File.WriteAllText($"{jsonpath}.json", json);
|
currenttime = currenttime.AddDays(1);
|
i++;
|
if (i == daysInMonth)
|
break;
|
}
|
|
processedDays += i; // 更新已处理的天数
|
date = date.AddMonths(1); // 更新日期到下一个月份
|
currenttime = date; // 重置 currenttime 为新月份的第一
|
}
|
|
}
|
|
|
|
/// <summary>
|
/// 获取具体时间段的开泵台数
|
/// </summary>
|
public List<(int, DateTime, DateTime)> getPumpIsOpen(List<RealScadaData> TestIsOpen)
|
{
|
List<RealScadaData> isOpenpump1 = new List<RealScadaData>();
|
List<RealScadaData> isOpenpump2 = new List<RealScadaData>();
|
List<RealScadaData> isOpenpump3 = new List<RealScadaData>();
|
List<RealScadaData> isOpenpump4 = new List<RealScadaData>();
|
List<RealScadaData> isOpenpump5 = new List<RealScadaData>();
|
//var TestIsOpen = GetPumpRunParas();
|
if (TestIsOpen == null)
|
return null;
|
var Pump1 = TestIsOpen.Where(x => x.TagName == "_0402010204012101001").ToList();
|
foreach (var item in Pump1)
|
{
|
isOpenpump1.AddRange(item.MonitorRecords);
|
}
|
var Pump2 = TestIsOpen.Where(x => x.TagName == "_0402010204012201001").ToList();
|
foreach (var item in Pump2)
|
{
|
isOpenpump2.AddRange(item.Values);
|
}
|
var Pump3 = TestIsOpen.Where(x => x.TagName == "_0402010204012301001").ToList();
|
foreach (var item in Pump3)
|
{
|
isOpenpump3.AddRange(item.Values);
|
}
|
var Pump4 = TestIsOpen.Where(x => x.TagName == "_0402010204012401001").ToList();
|
foreach (var item in Pump4)
|
{
|
isOpenpump4.AddRange(item.Values);
|
}
|
var Pump5 = TestIsOpen.Where(x => x.TagName == "_0402010204012501001").ToList();
|
foreach (var item in Pump5)
|
{
|
isOpenpump5.AddRange(item.Values);
|
}
|
// List<string> startTime = new List<string>();
|
// List<string> endtime = new List<string>();
|
var Run1 = GetData(isOpenpump1, 1);
|
var Run2 = GetData(isOpenpump2, 2);
|
var Run3 = GetData(isOpenpump3, 3);
|
var Run4 = GetData(isOpenpump4, 4);
|
var Run5 = GetData(isOpenpump5, 5);
|
List<(int, DateTime, DateTime)> mergedList = MergeList(Run1, Run2, Run3, Run4, Run5);
|
return (mergedList);
|
}
|
|
|
|
|
//泵开机参数的辅助方法
|
private static List<(int, DateTime, DateTime)> GetData(List<RealScadaData> PumpList, int sort)
|
{
|
List<(int, DateTime, DateTime)> openTimeRanges = new List<(int, DateTime, DateTime)>();
|
|
// 每天的数据条数
|
int itemsPerDay = 288;
|
|
// 遍历列表
|
for (int i = 0; i < PumpList.Count - 1; i += itemsPerDay)
|
{
|
DateTime currentDate = PumpList[i].DateTime.Date;
|
DateTime startTime = DateTime.MinValue;
|
bool isOpen = false;
|
|
// 遍历当天的数据
|
for (int j = i; j < i + itemsPerDay && j < PumpList.Count - 1; j++)
|
{
|
if (PumpList[j].Isopen == "1")
|
{
|
if (!isOpen)
|
{
|
startTime = PumpList[j].DateTime;
|
isOpen = true;
|
}
|
}
|
else
|
{
|
if (isOpen)
|
{
|
openTimeRanges.Add((sort, startTime, PumpList[j - 1].DateTime));
|
isOpen = false;
|
}
|
}
|
}
|
|
// 如果当天最后一条数据是打开状态,添加时间段
|
if (isOpen)
|
{
|
openTimeRanges.Add((sort, startTime, PumpList[i + itemsPerDay - 1].DateTime));
|
}
|
}
|
|
return openTimeRanges;
|
}
|
|
|
/// <summary>
|
/// 合并五台泵的运行时间
|
/// </summary>
|
/// <param name="lists"></param>
|
/// <returns></returns>
|
static List<(int, DateTime, DateTime)> MergeList(params List<(int, DateTime, DateTime)>[] lists)
|
{
|
List<(int, DateTime, DateTime)> mergedList = new List<(int, DateTime, DateTime)>();
|
foreach (var list in lists)
|
{
|
mergedList.AddRange(list);
|
}
|
return mergedList;
|
}
|
|
}
|
}
|