using IStation.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace IStation.DAL
{
///
/// 监测月数据集
///
public partial class StationSignalRecordPacket
{
///
/// 文件信息类
///
private class FileInfo
{
public string FullName { get; set; }
public int Year { get; set; }
public int Month { get; set; }
}
#region FolderInfo
///
/// 查询年月
///
private bool GetYearMonth(string folderPath, out int year, out int month)
{
year = month = 0;
if (!File.Exists(folderPath))
return false;
var name = Path.GetFileNameWithoutExtension(folderPath);
var strList = name.Split(SettingsD.File.FileNameSpacer);
if (strList.Length != 4)
return false;
if (!int.TryParse(strList[0], out year))
{
return false;
}
if (!int.TryParse(strList[1], out month))
{
return false;
}
return true;
}
///
/// 查询文件信息列表
///
private List GetStationSignalRecordPacketFileInfoList(string stationFolder)
{
if (!Directory.Exists(stationFolder))
return default;
var files = Directory.GetFiles(stationFolder);
if (files == null || files.Count() < 1)
return default;
var list = new List(files.Count());
foreach (var file in files)
{
if (!GetYearMonth(file, out int year, out int month))
continue;
var info = new FileInfo
{
FullName = file,
Year = year,
Month = month
};
list.Add(info);
}
if (list.Count > 0)
{
list = list.OrderBy(x => x.Year).ThenBy(x => x.Month).ToList();
}
return list;
}
#endregion
#region CsvConvert
//To
private string ToDsString(Model.StationSignalRecord stationSignalRecord)
{
var line =
$"{stationSignalRecord.Time:yyyy-MM-dd HH:mm:ss}|" +
$"{stationSignalRecord.PumpRunCount}|" +
$"{stationSignalRecord.TotalFlow}|" +
$"{stationSignalRecord.TotalPressure}|" +
$"{stationSignalRecord.DiffFlow}|" +
$"{JsonHelper.Object2Json(stationSignalRecord.ModelRecordDict)}";
for (int i = 0; i < stationSignalRecord.PumpSignalRecords.Count; i++)
{
var pumpSignalRecord = stationSignalRecord.PumpSignalRecords[i];
line += $"|{pumpSignalRecord.Flag}" +
$"|{pumpSignalRecord.Rpm}" +
$"|{pumpSignalRecord.Frequency}" +
$"|{pumpSignalRecord.FlowRate}" +
$"|{pumpSignalRecord.InletPressure}" +
$"|{pumpSignalRecord.OutletPressure}" +
$"|{pumpSignalRecord.Head}" +
$"|{pumpSignalRecord.InstantaneousPower}" +
$"|{pumpSignalRecord.WaterLevel}";
}
return line;
}
//from
public Model.StationSignalRecord FromDsString(string dsString)
{
var strList = dsString.Split('|');
if (strList.Length < 2)
return default;
var model = new Model.StationSignalRecord
{
Time = DateTime.Parse(strList[0]),
PumpRunCount = int.Parse(strList[1]),
TotalFlow = double.Parse(strList[2]),
TotalPressure = double.Parse(strList[3]),
DiffFlow = double.Parse(strList[4]),
ModelRecordDict = JsonHelper.Json2Object>(strList[5]),
PumpSignalRecords = new List()
};
for (int i = 0; i < model.PumpRunCount; i++)
{
var pumpSignalRecord = new Model.PumpSignalRecord();
var startIndex = i * 9 + 6;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.Flag = int.Parse(strList[startIndex]);
startIndex++;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.Rpm = double.Parse(strList[startIndex]);
startIndex++;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.Frequency = double.Parse(strList[startIndex]);
startIndex++;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.FlowRate = double.Parse(strList[startIndex]);
startIndex++;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.InletPressure = double.Parse(strList[startIndex]);
startIndex++;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.OutletPressure = double.Parse(strList[startIndex]);
startIndex++;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.Head = double.Parse(strList[startIndex]);
startIndex++;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.InstantaneousPower = double.Parse(strList[startIndex]);
startIndex++;
if (!string.IsNullOrEmpty(strList[startIndex]))
pumpSignalRecord.WaterLevel = double.Parse(strList[startIndex]);
model.PumpSignalRecords.Add(pumpSignalRecord);
}
return model;
}
#endregion
}
}