defdeda38a39eb9881e26228bbff62200abf8f17.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace BEMSDataGateway
  9. {
  10. class Creating_Setting_File
  11. {
  12. string Dir = "";
  13. string _strFile = "\\SettingFile";
  14. Form1 F1 = new Form1();
  15. LogFileCreate LFC = new LogFileCreate();
  16. public void Create_Setting_Point_File()
  17. {
  18. Dir = Environment.CurrentDirectory;
  19. System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Dir);
  20. System.IO.FileInfo fi = new System.IO.FileInfo(Dir + _strFile);
  21. if (di.Exists)
  22. {
  23. My_DB_DML MDD = new My_DB_DML();
  24. string ConnInfo = MDD.ConnDBInfo(); // DB접속정보를 담고 있음.
  25. DataSet ds = new DataSet();
  26. using (SqlConnection conn = new SqlConnection(ConnInfo))
  27. {
  28. string query = "SELECT * FROM BemsMonitoringPointConfig";
  29. SqlCommand cmd = new SqlCommand(query, conn);
  30. cmd.CommandType = CommandType.Text;
  31. cmd.CommandTimeout = 120;
  32. SqlDataAdapter da = new SqlDataAdapter(cmd);
  33. try
  34. {
  35. conn.Open();
  36. da.Fill(ds);
  37. conn.Close();
  38. }
  39. catch (Exception ex)
  40. {
  41. conn.Close();
  42. }
  43. }
  44. StreamWriter sw = new StreamWriter(new FileStream("SettingFile.csv", FileMode.Create), Encoding.UTF8);
  45. WriteToStream(sw, ds.Tables[0], false, false);
  46. //Console.ReadKey();
  47. //LFC.Log("셋팅 파일(SettingFile.csv)이 존재하지 않습니다. 파일을 자동생성합니다." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  48. }
  49. //else
  50. //{
  51. // LFC.Log("관제점 셋팅 파일의 폴더가 존재하지 않습니다."+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  52. //}
  53. }
  54. public static void WriteToStream(TextWriter stream, DataTable table, bool header, bool quoteall)
  55. {
  56. int totalcount = table.Rows.Count;
  57. int literal_count = 0;
  58. int TagCount = 0;
  59. //if (header) /
  60. //{
  61. // for (int i = 0; i < table.Columns.Count; i++)
  62. // {
  63. // WriteItem(stream, table.Columns[i].Caption, quoteall, literal_count);
  64. // if (i >= 0 && i < 3)
  65. // {
  66. // stream.Write('.');
  67. // }
  68. // else if (i >= 3 && i < table.Columns.Count - 1)
  69. // {
  70. // stream.Write(',');
  71. // }
  72. // else
  73. // {
  74. // stream.Write("\r\n");
  75. // }
  76. // }
  77. //}
  78. foreach (DataRow row in table.Rows)
  79. {
  80. string[] Type_Store = new string[3];
  81. string tmpSaveMode_Store = "";
  82. literal_count = 0;
  83. for (int i = 0; i < table.Columns.Count; i++)
  84. {
  85. string s = row[i].ToString();
  86. s = s.Trim();
  87. literal_count++; // 글자수 카운트
  88. //WriteItem(stream, row[i], quoteall, literal_count);
  89. if (row[i] == null)
  90. {
  91. return;
  92. }
  93. if (literal_count == 7)
  94. {
  95. tmpSaveMode_Store = s;
  96. continue;
  97. }
  98. // ****************************값 Type만드는 부분
  99. if (literal_count == 5 || literal_count == 6 || literal_count == 9)
  100. {
  101. switch (literal_count)
  102. {
  103. case 5: Type_Store[0] = s; break;
  104. case 6: Type_Store[1] = s; break;
  105. case 9: Type_Store[2] = s; break;
  106. }
  107. if (!(literal_count == 9))
  108. {
  109. continue; // 다음으로...
  110. }
  111. else
  112. {
  113. if (Type_Store[0] == "False")
  114. {
  115. if (Type_Store[1] == "False")
  116. {
  117. if (Type_Store[2] == "False")
  118. {
  119. s = "3";
  120. }
  121. else if (Type_Store[2] == "True")
  122. {
  123. s = "6";
  124. }
  125. }
  126. else if (Type_Store[1] == "True")
  127. {
  128. s = "1";
  129. }
  130. }
  131. else if (Type_Store[0] == "True")
  132. {
  133. if (Type_Store[1] == "False")
  134. {
  135. if (Type_Store[2] == "False")
  136. {
  137. s = "2";
  138. }
  139. else if (Type_Store[2] == "True")
  140. {
  141. s = "5";
  142. }
  143. }
  144. else if (Type_Store[1] == "True")
  145. {
  146. s = "4";
  147. }
  148. }
  149. }
  150. TagCount++;
  151. stream.Write(s);
  152. stream.Write(',');
  153. stream.Write("Tag" + TagCount);
  154. stream.Write(',');
  155. stream.Write(tmpSaveMode_Store);
  156. stream.Flush();
  157. }
  158. // ****************************값 Type만드는 부분
  159. else
  160. {
  161. stream.Write(s);
  162. stream.Flush();
  163. }
  164. if (i >= 0 && i < 3)
  165. {
  166. stream.Write('.');
  167. }
  168. else if (i >= 3 && i < table.Columns.Count - 1)
  169. {
  170. stream.Write(',');
  171. }
  172. else
  173. {
  174. if (!(totalcount == TagCount)) stream.Write("\r\n");
  175. }
  176. }
  177. }
  178. stream.Flush();
  179. stream.Close();
  180. }
  181. public void Create_Point_File()
  182. {
  183. Dir = Environment.CurrentDirectory;
  184. System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Dir);
  185. System.IO.FileInfo fi = new System.IO.FileInfo(Dir + _strFile);
  186. if (di.Exists)
  187. {
  188. My_DB_DML MDD = new My_DB_DML();
  189. string ConnInfo = MDD.ConnDBInfo(); // DB접속정보를 담고 있음.
  190. DataSet ds = new DataSet();
  191. using (SqlConnection conn = new SqlConnection(ConnInfo))
  192. {
  193. string query = "SELECT * FROM BemsMonitoringPointConfig";
  194. SqlCommand cmd = new SqlCommand(query, conn);
  195. cmd.CommandType = CommandType.Text;
  196. cmd.CommandTimeout = 120;
  197. SqlDataAdapter da = new SqlDataAdapter(cmd);
  198. try
  199. {
  200. conn.Open();
  201. da.Fill(ds);
  202. conn.Close();
  203. }
  204. catch (Exception ex)
  205. {
  206. conn.Close();
  207. }
  208. }
  209. StreamWriter sw = new StreamWriter(new FileStream("TagSetFile.csv", FileMode.Create), Encoding.UTF8);
  210. WriteToStream_TagSetFile(sw, ds.Tables[0], false, false);
  211. //Console.ReadKey();
  212. //LFC.Log("셋팅 파일(SettingFile.csv)이 존재하지 않습니다. 파일을 자동생성합니다." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  213. }
  214. //else
  215. //{
  216. // LFC.Log("관제점 셋팅 파일의 폴더가 존재하지 않습니다."+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  217. //}
  218. }
  219. public static void WriteToStream_TagSetFile(TextWriter stream, DataTable table, bool header, bool quoteall)
  220. {
  221. int totalcount = table.Rows.Count;
  222. int literal_count = 0;
  223. int TagCount = 0;
  224. foreach (DataRow row in table.Rows)
  225. {
  226. TagCount++;
  227. literal_count = 0;
  228. for (int i = 0; i < table.Columns.Count; i++)
  229. {
  230. literal_count++;
  231. string s = row[i].ToString();
  232. s = s.Trim();
  233. if (row[i] == null)
  234. {
  235. return;
  236. }
  237. if (literal_count == 8)
  238. {
  239. stream.Write(TagCount - 1);
  240. stream.Write(',');
  241. stream.Write(s);
  242. stream.Flush();
  243. continue;
  244. }
  245. }
  246. if (!(totalcount == TagCount))
  247. stream.Write("\r\n");
  248. }
  249. stream.Flush();
  250. stream.Close();
  251. }
  252. }
  253. }