20240815_FJEQ_upperpc_seabed/FujianEarthquake_seabed_UI/FujianEarthquake/Common/CSVDownload.cs

480 lines
17 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FujianEarthquake.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace FujianEarthquake.Common
{
public class CSVDownload
{
/// <summary>
/// Create target file
/// </summary>
/// <param name="folder">folder</param>
/// <param name="fileName">folder name</param>
/// <param name="fileExtension">file extension</param>
/// <returns>file path</returns>
public static string CreateFile(string folder, string fileName, string fileExtension)
{
FileStream fs = null;
string filePath = folder + fileName + "." + fileExtension;
try
{
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
fs = File.Create(filePath);
}
catch (Exception ex)
{ }
finally
{
if (fs != null)
{
fs.Dispose();
}
}
return filePath;
}
#region LogRecord
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetLogRecordPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(LogRecordModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveLogRecordDataToCSVFile(ObservableCollection<LogRecordModel> LogRecordList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetLogRecordPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < LogRecordList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(LogRecordList[i].Index);
strValue.Append(",");
strValue.Append(LogRecordList[i].RecordTime);
strValue.Append(",");
strValue.Append(LogRecordList[i].Device_Name);
strValue.Append(",");
strValue.Append(LogRecordList[i].Operation_Type);
strValue.Append(",");
strValue.Append(LogRecordList[i].Record);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region AlarmRecord
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetAlarmRecordPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(AlarmRecordModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveAlarmRecordDataToCSVFile(ObservableCollection<AlarmRecordModel> AlarmRecordList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetAlarmRecordPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < AlarmRecordList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(AlarmRecordList[i].Index);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].RecordTime);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ParaName);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ParaNum);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ParaContent);
strValue.Append(",");
strValue.Append(AlarmRecordList[i].ProcessingMethod);
strValue.Append(",");
if (AlarmRecordList[i].UrgencyLevel == (ImageSource)Application.Current.FindResource("CycleRed"))
strValue.Append("一级紧急");
else if (AlarmRecordList[i].UrgencyLevel == (ImageSource)Application.Current.FindResource("CycleOrange"))
strValue.Append("二级紧急");
else if (AlarmRecordList[i].UrgencyLevel == (ImageSource)Application.Current.FindResource("CycleYellow"))
strValue.Append("三级紧急");
strValue.Append(",");
strValue.Append(AlarmRecordList[i].IsHandled);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region JunctionBox
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetJunctionBoxEnvironPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(JunctionBoxEnvironModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveJunctionBoxEnvironDataToCSVFile(ObservableCollection<JunctionBoxEnvironModel> BaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetJunctionBoxEnvironPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < BaseStationList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(BaseStationList[i].Index);
strValue.Append(",");
strValue.Append(BaseStationList[i].RecordTime);
strValue.Append(",");
strValue.Append(BaseStationList[i].Temperature);
strValue.Append(",");
strValue.Append(BaseStationList[i].Humidity);
strValue.Append(",");
strValue.Append(BaseStationList[i].AttitudeX);
strValue.Append(",");
strValue.Append(BaseStationList[i].AttitudeY);
strValue.Append(",");
strValue.Append(BaseStationList[i].AttitudeZ);
strValue.Append(",");
if (BaseStationList[i].Leakage == "漏水")
strValue.Append("漏水");
else if (BaseStationList[i].Leakage == "未漏水")
strValue.Append("未漏水");
else
strValue.Append("无数据");
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region JunctionBoxStatus
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetJunctionBoxStatusPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(JunctionBoxMonitorModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveJunctionBoxStatusDataToCSVFile(ObservableCollection<JunctionBoxMonitorModel> JunctionBoxStatusList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetJunctionBoxStatusPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < JunctionBoxStatusList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(JunctionBoxStatusList[i].Index);
strValue.Append(",");
strValue.Append(JunctionBoxStatusList[i].RecordTime);
strValue.Append(",");
strValue.Append(JunctionBoxStatusList[i].Seis1_Voltage);
strValue.Append(",");
strValue.Append(JunctionBoxStatusList[i].Seis1_Current);
strValue.Append(",");
strValue.Append(JunctionBoxStatusList[i].Seis2_Voltage);
strValue.Append(",");
strValue.Append(JunctionBoxStatusList[i].Seis2_Current);
strValue.Append(",");
strValue.Append(JunctionBoxStatusList[i].Video_Voltage);
strValue.Append(",");
strValue.Append(JunctionBoxStatusList[i].Video_Current);
strValue.Append(",");
strValue.Append(JunctionBoxStatusList[i].Out_Voltage12_Reserved2);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
#region ShoreBaseStation
/// <summary>
/// 获取类的属性集合以便生成CSV文件的所有Column标题
/// </summary>
/// <returns></returns>
public static PropertyInfo[] GetShoreBaseStationPropertyInfoArray()
{
PropertyInfo[] props = null;
try
{
Type type = typeof(ShoreBaseStationStateDataModel);
object obj = Activator.CreateInstance(type);
props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
catch (Exception ex)
{ }
return props;
}
/// <summary>
/// Save the List data to CSV file
/// </summary>
/// <param name="BaseStationList">data source</param>
/// <param name="filePath">file path</param>
/// <returns>success flag</returns>
public static bool SaveShoreBaseStationDataToCSVFile(ObservableCollection<ShoreBaseStationStateDataModel> ShoreBaseStationList, string filePath)
{
bool successFlag = true;
StringBuilder strColumn = new StringBuilder();
StringBuilder strValue = new StringBuilder();
StreamWriter sw = null;
PropertyInfo[] props = GetShoreBaseStationPropertyInfoArray();
try
{
sw = new StreamWriter(filePath);
for (int i = 0; i < props.Length; i++)
{
strColumn.Append(props[i].Name);
strColumn.Append(",");
}
strColumn.Remove(strColumn.Length - 1, 1);
sw.WriteLine(strColumn); //write the column name
for (int i = 0; i < ShoreBaseStationList.Count; i++)
{
strValue.Remove(0, strValue.Length); //clear the temp row value
strValue.Append(ShoreBaseStationList[i].Index);
strValue.Append(",");
strValue.Append(ShoreBaseStationList[i].DataTime);
strValue.Append(",");
strValue.Append(ShoreBaseStationList[i].Out_Vol);
strValue.Append(",");
strValue.Append(ShoreBaseStationList[i].Out_Cur);
strValue.Append(",");
strValue.Append(ShoreBaseStationList[i].VolProStatus);
strValue.Append(",");
strValue.Append(ShoreBaseStationList[i].CurProStatus);
strValue.Append(",");
strValue.Append(ShoreBaseStationList[i].RelayStatus);
sw.WriteLine(strValue); //write the row value
}
}
catch (Exception ex)
{
successFlag = false;
}
finally
{
if (sw != null)
{
sw.Dispose();
}
}
return successFlag;
}
#endregion
}
}