using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Text; namespace DataGateWayProject { class Creating_Setting_File { string Dir = ""; string _strFile = "\\SettingFile"; Form1 F1 = new Form1(); LogFileCreate LFC = new LogFileCreate(); public void Create_Setting_Point_File() { Dir = Environment.CurrentDirectory; System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Dir); System.IO.FileInfo fi = new System.IO.FileInfo(Dir + _strFile); if (di.Exists) { My_DB_DML MDD = new My_DB_DML(); string ConnInfo = MDD.ConnDBInfo(); // DB접속정보를 담고 있음. DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(ConnInfo)) { string query = "SELECT * FROM BemsMonitoringPointConfig"; SqlCommand cmd = new SqlCommand(query, conn); cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 120; SqlDataAdapter da = new SqlDataAdapter(cmd); try { conn.Open(); da.Fill(ds); conn.Close(); } catch (Exception ex) { conn.Close(); } } try { StreamWriter sw = new StreamWriter(new FileStream("SettingFile.csv", FileMode.Create), Encoding.UTF8); WriteToStream(sw, ds.Tables[0], false, false); } catch(Exception e) { LFC.Log("파일 생성 에러 = " + e); } //Console.ReadKey(); //LFC.Log("셋팅 파일(SettingFile.csv)이 존재하지 않습니다. 파일을 자동생성합니다." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); } //else //{ // LFC.Log("관제점 셋팅 파일의 폴더가 존재하지 않습니다."+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //} } public static void WriteToStream(TextWriter stream, DataTable table, bool header, bool quoteall) { int totalcount = table.Rows.Count; int literal_count = 0; int TagCount = 0; //if (header) / //{ // for (int i = 0; i < table.Columns.Count; i++) // { // WriteItem(stream, table.Columns[i].Caption, quoteall, literal_count); // if (i >= 0 && i < 3) // { // stream.Write('.'); // } // else if (i >= 3 && i < table.Columns.Count - 1) // { // stream.Write(','); // } // else // { // stream.Write("\r\n"); // } // } //} foreach (DataRow row in table.Rows) { string[] Type_Store = new string[3]; string tmpSaveMode_Store = ""; literal_count = 0; for (int i = 0; i < table.Columns.Count; i++) { string s = row[i].ToString(); s = s.Trim(); literal_count++; // 글자수 카운트 //WriteItem(stream, row[i], quoteall, literal_count); if (row[i] == null) { return; } if(literal_count == 7) { tmpSaveMode_Store = s; continue; } // ****************************값 Type만드는 부분 if (literal_count == 5 || literal_count == 6 || literal_count == 9) { switch (literal_count) { case 5: Type_Store[0] = s; break; case 6: Type_Store[1] = s; break; case 9: Type_Store[2] = s; break; } if (!(literal_count == 9)) { continue; // 다음으로... } else { if (Type_Store[0] == "False" || Type_Store[0] == "FAlSE" || Type_Store[0] == "0") { if (Type_Store[1] == "False" || Type_Store[1] == "FAlSE" || Type_Store[1] == "0") { if (Type_Store[2] == "False" || Type_Store[2] == "FAlSE" || Type_Store[2] == "0") { s = "3"; } else if (Type_Store[2] == "True") { s = "6"; } } else if (Type_Store[1] == "True") { s = "1"; } } else if (Type_Store[0] == "True") { if (Type_Store[1] == "False" || Type_Store[1] == "FAlSE" || Type_Store[1] == "0") { if (Type_Store[2] == "False" || Type_Store[2] == "FAlSE" || Type_Store[2] == "0") { s = "2"; } else if (Type_Store[2] == "True") { s = "5"; } } else if (Type_Store[1] == "True") { s = "4"; } } } TagCount++; stream.Write(s); stream.Write(','); stream.Write("Tag" + TagCount); stream.Write(','); stream.Write(tmpSaveMode_Store); stream.Flush(); } // ****************************값 Type만드는 부분 else { stream.Write(s); stream.Flush(); } if (i >= 0 && i < 3) { stream.Write('.'); } else if (i >= 3 && i < table.Columns.Count - 1) { stream.Write(','); } else { if(!(totalcount== TagCount))stream.Write("\r\n"); } } } stream.Flush(); stream.Close(); } public void Create_Point_File() { Dir = Environment.CurrentDirectory; System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Dir); System.IO.FileInfo fi = new System.IO.FileInfo(Dir + _strFile); if (di.Exists) { My_DB_DML MDD = new My_DB_DML(); string ConnInfo = MDD.ConnDBInfo(); // DB접속정보를 담고 있음. DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(ConnInfo)) { string query = "SELECT * FROM BemsMonitoringPointConfig"; SqlCommand cmd = new SqlCommand(query, conn); cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 120; SqlDataAdapter da = new SqlDataAdapter(cmd); try { conn.Open(); da.Fill(ds); conn.Close(); } catch (Exception ex) { conn.Close(); } } try { StreamWriter sw = new StreamWriter(new FileStream("TagSetFile.csv", FileMode.Create), Encoding.UTF8); WriteToStream_TagSetFile(sw, ds.Tables[0], false, false); } catch(Exception e) { LFC.Log("파일생성 에러 = " + e); } //Console.ReadKey(); //LFC.Log("셋팅 파일(SettingFile.csv)이 존재하지 않습니다. 파일을 자동생성합니다." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); } //else //{ // LFC.Log("관제점 셋팅 파일의 폴더가 존재하지 않습니다."+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //} } public static void WriteToStream_TagSetFile(TextWriter stream, DataTable table, bool header, bool quoteall) { int totalcount = table.Rows.Count; int literal_count = 0; int TagCount = 0; foreach (DataRow row in table.Rows) { TagCount++; literal_count = 0; for (int i = 0; i < table.Columns.Count; i++) { literal_count++; string s = row[i].ToString(); s=s.Trim(); if (row[i] == null) { return; } if (literal_count == 8) { stream.Write(TagCount-1); stream.Write(','); stream.Write(s); stream.Flush(); continue; } } if (!(totalcount == TagCount)) stream.Write("\r\n"); } stream.Flush(); stream.Close(); } } }