using InSituLaboratory.Entities.Sensor; using InSituLaboratory.Models.Sendsor; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Configuration; using System.Data; using System.Data.OracleClient; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace InSituLaboratory.Base { public class CSVDownload { /// /// Create target file /// /// folder /// folder name /// file extension /// file path 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 MEMS色谱仪 /// /// 获取类的属性集合(以便生成CSV文件的所有Column标题) /// /// public static PropertyInfo[] GetMEMSSPInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(MEMSSpModels); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch (Exception ex) { } return props; } /// /// Save the List data to CSV file /// /// data source /// file path /// success flag public static bool SaveMEMSSPDataToCSVFile(ObservableCollection BaseStationList, string filePath) { bool successFlag = true; StringBuilder strColumn = new StringBuilder(); StringBuilder strValue = new StringBuilder(); StreamWriter sw = null; PropertyInfo[] props = GetMEMSSPInfoArray(); 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].Id); strValue.Append(","); strValue.Append(BaseStationList[i].CreateTime); strValue.Append(","); strValue.Append(BaseStationList[i].SamplingTime); strValue.Append(","); strValue.Append(BaseStationList[i].Tem); strValue.Append(","); strValue.Append(BaseStationList[i].Hum); strValue.Append(","); strValue.Append(BaseStationList[i].Pressure); strValue.Append(","); strValue.Append(BaseStationList[i].Insulation); strValue.Append(","); strValue.Append(BaseStationList[i].C2); strValue.Append(","); strValue.Append(BaseStationList[i].C3); strValue.Append(","); strValue.Append(BaseStationList[i].C4); sw.WriteLine(strValue); //write the row value } } catch (Exception ex) { successFlag = false; } finally { if (sw != null) { sw.Dispose(); } } return successFlag; } #endregion #region MEMS质谱仪 /// /// 获取类的属性集合(以便生成CSV文件的所有Column标题) /// /// public static PropertyInfo[] GetMEMSZPInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(MEMSZpModels); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch (Exception ex) { } return props; } /// /// Save the List data to CSV file /// /// data source /// file path /// success flag public static bool SaveMEMSZPDataToCSVFile(ObservableCollection BaseStationList, string filePath) { bool successFlag = true; StringBuilder strColumn = new StringBuilder(); StringBuilder strValue = new StringBuilder(); StreamWriter sw = null; PropertyInfo[] props = GetMEMSZPInfoArray(); 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].Id); strValue.Append(","); strValue.Append(BaseStationList[i].CreateTime); strValue.Append(","); strValue.Append(BaseStationList[i].SamplingTime); strValue.Append(","); strValue.Append(BaseStationList[i].Tem); strValue.Append(","); strValue.Append(BaseStationList[i].Hum); strValue.Append(","); strValue.Append(BaseStationList[i].Pressure); strValue.Append(","); strValue.Append(BaseStationList[i].Insulation); strValue.Append(","); strValue.Append(BaseStationList[i].CH4); strValue.Append(","); strValue.Append(BaseStationList[i].H2O); strValue.Append(","); strValue.Append(BaseStationList[i].N2); strValue.Append(","); strValue.Append(BaseStationList[i].O2); strValue.Append(","); strValue.Append(BaseStationList[i].Ar); strValue.Append(","); strValue.Append(BaseStationList[i].CO2); sw.WriteLine(strValue); //write the row value } } catch (Exception ex) { successFlag = false; } finally { if (sw != null) { sw.Dispose(); } } return successFlag; } #endregion #region 色质联用仪 /// /// 获取类的属性集合(以便生成CSV文件的所有Column标题) /// /// public static PropertyInfo[] GetColorMSInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(ColorMSModels); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch (Exception ex) { } return props; } /// /// Save the List data to CSV file /// /// data source /// file path /// success flag public static bool SaveColorMSDataToCSVFile(ObservableCollection BaseStationList, string filePath) { bool successFlag = true; StringBuilder strColumn = new StringBuilder(); StringBuilder strValue = new StringBuilder(); StreamWriter sw = null; PropertyInfo[] props = GetColorMSInfoArray(); 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].Id); strValue.Append(","); strValue.Append(BaseStationList[i].CreateTime); strValue.Append(","); strValue.Append(BaseStationList[i].SamplingTime); strValue.Append(","); strValue.Append(BaseStationList[i].Tem); strValue.Append(","); strValue.Append(BaseStationList[i].Hum); strValue.Append(","); strValue.Append(BaseStationList[i].Pressure); strValue.Append(","); strValue.Append(BaseStationList[i].Insulation); strValue.Append(","); strValue.Append(BaseStationList[i].C2); strValue.Append(","); strValue.Append(BaseStationList[i].C3); strValue.Append(","); strValue.Append(BaseStationList[i].C4); strValue.Append(","); strValue.Append(BaseStationList[i].C5); strValue.Append(","); strValue.Append(BaseStationList[i].C6); strValue.Append(","); strValue.Append(BaseStationList[i].C7); strValue.Append(","); strValue.Append(BaseStationList[i].C8); strValue.Append(","); strValue.Append(BaseStationList[i].C9); sw.WriteLine(strValue); //write the row value } } catch (Exception ex) { successFlag = false; } finally { if (sw != null) { sw.Dispose(); } } return successFlag; } #endregion #region 甲烷同位素分析仪 /// /// 获取类的属性集合(以便生成CSV文件的所有Column标题) /// /// public static PropertyInfo[] GetCH4IsotopeInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(CH4IsotopeModels); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch (Exception ex) { } return props; } /// /// Save the List data to CSV file /// /// data source /// file path /// success flag public static bool SaveCh4IsotopeDataToCSVFile(ObservableCollection BaseStationList, string filePath) { bool successFlag = true; StringBuilder strColumn = new StringBuilder(); StringBuilder strValue = new StringBuilder(); StreamWriter sw = null; PropertyInfo[] props = GetCH4IsotopeInfoArray(); 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].Id); strValue.Append(","); strValue.Append(BaseStationList[i].CreateTime); strValue.Append(","); strValue.Append(BaseStationList[i].SamplingTime); strValue.Append(","); strValue.Append(BaseStationList[i].Tem); strValue.Append(","); strValue.Append(BaseStationList[i].Hum); strValue.Append(","); strValue.Append(BaseStationList[i].Pressure); strValue.Append(","); strValue.Append(BaseStationList[i].Insulation); strValue.Append(","); strValue.Append(BaseStationList[i].C1); strValue.Append(","); strValue.Append(BaseStationList[i].C2); strValue.Append(","); strValue.Append(BaseStationList[i].Abundance); sw.WriteLine(strValue); //write the row value } } catch (Exception ex) { successFlag = false; } finally { if (sw != null) { sw.Dispose(); } } return successFlag; } #endregion #region 二氧化碳同位素分析仪 /// /// 获取类的属性集合(以便生成CSV文件的所有Column标题) /// /// public static PropertyInfo[] GetCO2IsotopeInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(CO2IsotopsModels); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch (Exception ex) { } return props; } /// /// Save the List data to CSV file /// /// data source /// file path /// success flag public static bool SaveCO2IsotopeDataToCSVFile(ObservableCollection BaseStationList, string filePath) { bool successFlag = true; StringBuilder strColumn = new StringBuilder(); StringBuilder strValue = new StringBuilder(); StreamWriter sw = null; PropertyInfo[] props = GetCO2IsotopeInfoArray(); 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].Id); strValue.Append(","); strValue.Append(BaseStationList[i].CreateTime); strValue.Append(","); strValue.Append(BaseStationList[i].SamplingTime); strValue.Append(","); strValue.Append(BaseStationList[i].Tem); strValue.Append(","); strValue.Append(BaseStationList[i].Hum); strValue.Append(","); strValue.Append(BaseStationList[i].Pressure); strValue.Append(","); strValue.Append(BaseStationList[i].Insulation); strValue.Append(","); strValue.Append(BaseStationList[i].LightIntensity); strValue.Append(","); strValue.Append(BaseStationList[i].LaserTemperature); strValue.Append(","); strValue.Append(BaseStationList[i].CO2Concentration); strValue.Append(","); strValue.Append(BaseStationList[i].IsotopicAbundance); sw.WriteLine(strValue); //write the row value } } catch (Exception ex) { successFlag = false; } finally { if (sw != null) { sw.Dispose(); } } return successFlag; } #endregion #region 测序仪 /// /// 获取类的属性集合(以便生成CSV文件的所有Column标题) /// /// public static PropertyInfo[] GetSequencerInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(SequencerModels); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch (Exception ex) { } return props; } /// /// Save the List data to CSV file /// /// data source /// file path /// success flag public static bool SaveSequencerDataToCSVFile(ObservableCollection BaseStationList, string filePath) { bool successFlag = true; StringBuilder strColumn = new StringBuilder(); StringBuilder strValue = new StringBuilder(); StreamWriter sw = null; PropertyInfo[] props = GetSequencerInfoArray(); 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].Id); strValue.Append(","); strValue.Append(BaseStationList[i].CreateTime); strValue.Append(","); strValue.Append(BaseStationList[i].SamplingTime); strValue.Append(","); strValue.Append(BaseStationList[i].Tem); strValue.Append(","); strValue.Append(BaseStationList[i].Hum); strValue.Append(","); strValue.Append(BaseStationList[i].Pressure); strValue.Append(","); strValue.Append(BaseStationList[i].Insulation); strValue.Append(","); strValue.Append(BaseStationList[i].ReagentTemperature); strValue.Append(","); strValue.Append(BaseStationList[i].SampleConcentration); strValue.Append(","); strValue.Append(BaseStationList[i].CurrentWorkflow); sw.WriteLine(strValue); //write the row value } } catch (Exception ex) { successFlag = false; } finally { if (sw != null) { sw.Dispose(); } } return successFlag; } #endregion #region 颗粒物分析仪 /// /// 获取类的属性集合(以便生成CSV文件的所有Column标题) /// /// public static PropertyInfo[] GetParticleInfoArray() { PropertyInfo[] props = null; try { Type type = typeof(ParticleAnalyzerModels); object obj = Activator.CreateInstance(type); props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } catch (Exception ex) { } return props; } /// /// Save the List data to CSV file /// /// data source /// file path /// success flag public static bool SaveParticleDataToCSVFile(ObservableCollection BaseStationList, string filePath) { bool successFlag = true; StringBuilder strColumn = new StringBuilder(); StringBuilder strValue = new StringBuilder(); StreamWriter sw = null; PropertyInfo[] props = GetParticleInfoArray(); 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].Id); strValue.Append(","); strValue.Append(BaseStationList[i].CreateTime); strValue.Append(","); strValue.Append(BaseStationList[i].SamplingTime); strValue.Append(","); strValue.Append(BaseStationList[i].Tem); strValue.Append(","); strValue.Append(BaseStationList[i].Hum); strValue.Append(","); strValue.Append(BaseStationList[i].Pressure); strValue.Append(","); strValue.Append(BaseStationList[i].Insulation); strValue.Append(","); strValue.Append(BaseStationList[i].ParticleNumber); strValue.Append(","); strValue.Append(BaseStationList[i].CoordinateX); strValue.Append(","); strValue.Append(BaseStationList[i].CoordinateY); strValue.Append(","); strValue.Append(BaseStationList[i].Type); strValue.Append(","); strValue.Append(BaseStationList[i].Area); strValue.Append(","); strValue.Append(BaseStationList[i].Long); strValue.Append(","); strValue.Append(BaseStationList[i].Wide); sw.WriteLine(strValue); //write the row value } } catch (Exception ex) { successFlag = false; } finally { if (sw != null) { sw.Dispose(); } } return successFlag; } #endregion } }