123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace BEMSDataGateway
- {
- 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 e)
- {
- LFC.Log("[Creating_Setting_File.cs] Create_Setting_Point_File 실패 : " + e + " ***" + Environment.NewLine);
- 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("[Creating_Setting_File.cs] WriteToStream 실패 : " + e + ", ***" + Environment.NewLine);
- }
- }
- }
- public static void WriteToStream(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)
- {
- 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++; // 글자수 카운트
- 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")
- {
- if (Type_Store[1] == "False")
- {
- if (Type_Store[2] == "False")
- {
- 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")
- {
- if (Type_Store[2] == "False")
- {
- 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 e)
- {
- conn.Close();
- LFC.Log("[Creating_Setting_File.cs] Create_Point_File 실패" + e + " ,***" + Environment.NewLine);
- }
- }
- StreamWriter sw = new StreamWriter(new FileStream("TagSetFile.csv", FileMode.Create), Encoding.UTF8);
- WriteToStream_TagSetFile(sw, ds.Tables[0], false, false);
- }
- }
- 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();
- }
- }
- }
|