bf5031c308a7094e0b270434f8c6ca7377264466.svn-base 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 e)
  40. {
  41. LFC.Log("[Creating_Setting_File.cs] Create_Setting_Point_File 실패 : " + e + " ***" + Environment.NewLine);
  42. conn.Close();
  43. }
  44. }
  45. try
  46. {
  47. StreamWriter sw = new StreamWriter(new FileStream("SettingFile.csv", FileMode.Create), Encoding.UTF8);
  48. WriteToStream(sw, ds.Tables[0], false, false);
  49. }
  50. catch (Exception e)
  51. {
  52. LFC.Log("[Creating_Setting_File.cs] WriteToStream 실패 : " + e + ", ***" + Environment.NewLine);
  53. }
  54. }
  55. }
  56. public static void WriteToStream(TextWriter stream, DataTable table, bool header, bool quoteall)
  57. {
  58. int totalcount = table.Rows.Count;
  59. int literal_count = 0;
  60. int TagCount = 0;
  61. foreach (DataRow row in table.Rows)
  62. {
  63. string[] Type_Store = new string[3];
  64. string tmpSaveMode_Store = "";
  65. literal_count = 0;
  66. for (int i = 0; i < table.Columns.Count; i++)
  67. {
  68. string s = row[i].ToString();
  69. s = s.Trim();
  70. literal_count++; // 글자수 카운트
  71. if (row[i] == null)
  72. {
  73. return;
  74. }
  75. if (literal_count == 7)
  76. {
  77. tmpSaveMode_Store = s;
  78. continue;
  79. }
  80. // ****************************값 Type만드는 부분
  81. if (literal_count == 5 || literal_count == 6 || literal_count == 9)
  82. {
  83. switch (literal_count)
  84. {
  85. case 5: Type_Store[0] = s; break;
  86. case 6: Type_Store[1] = s; break;
  87. case 9: Type_Store[2] = s; break;
  88. }
  89. if (!(literal_count == 9))
  90. {
  91. continue; // 다음으로...
  92. }
  93. else
  94. {
  95. if (Type_Store[0] == "False")
  96. {
  97. if (Type_Store[1] == "False")
  98. {
  99. if (Type_Store[2] == "False")
  100. {
  101. s = "3";
  102. }
  103. else if (Type_Store[2] == "True")
  104. {
  105. s = "6";
  106. }
  107. }
  108. else if (Type_Store[1] == "True")
  109. {
  110. s = "1";
  111. }
  112. }
  113. else if (Type_Store[0] == "True")
  114. {
  115. if (Type_Store[1] == "False")
  116. {
  117. if (Type_Store[2] == "False")
  118. {
  119. s = "2";
  120. }
  121. else if (Type_Store[2] == "True")
  122. {
  123. s = "5";
  124. }
  125. }
  126. else if (Type_Store[1] == "True")
  127. {
  128. s = "4";
  129. }
  130. }
  131. }
  132. TagCount++;
  133. stream.Write(s);
  134. stream.Write(',');
  135. stream.Write("Tag" + TagCount);
  136. stream.Write(',');
  137. stream.Write(tmpSaveMode_Store);
  138. stream.Flush();
  139. }
  140. // ****************************값 Type만드는 부분
  141. else
  142. {
  143. stream.Write(s);
  144. stream.Flush();
  145. }
  146. if (i >= 0 && i < 3)
  147. {
  148. stream.Write('.');
  149. }
  150. else if (i >= 3 && i < table.Columns.Count - 1)
  151. {
  152. stream.Write(',');
  153. }
  154. else
  155. {
  156. if (!(totalcount == TagCount)) stream.Write("\r\n");
  157. }
  158. }
  159. }
  160. stream.Flush();
  161. stream.Close();
  162. }
  163. public void Create_Point_File()
  164. {
  165. Dir = Environment.CurrentDirectory;
  166. System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Dir);
  167. System.IO.FileInfo fi = new System.IO.FileInfo(Dir + _strFile);
  168. if (di.Exists)
  169. {
  170. My_DB_DML MDD = new My_DB_DML();
  171. string ConnInfo = MDD.ConnDBInfo(); // DB접속정보를 담고 있음.
  172. DataSet ds = new DataSet();
  173. using (SqlConnection conn = new SqlConnection(ConnInfo))
  174. {
  175. string query = "SELECT * FROM BemsMonitoringPointConfig";
  176. SqlCommand cmd = new SqlCommand(query, conn);
  177. cmd.CommandType = CommandType.Text;
  178. cmd.CommandTimeout = 120;
  179. SqlDataAdapter da = new SqlDataAdapter(cmd);
  180. try
  181. {
  182. conn.Open();
  183. da.Fill(ds);
  184. conn.Close();
  185. }
  186. catch (Exception e)
  187. {
  188. conn.Close();
  189. LFC.Log("[Creating_Setting_File.cs] Create_Point_File 실패" + e + " ,***" + Environment.NewLine);
  190. }
  191. }
  192. StreamWriter sw = new StreamWriter(new FileStream("TagSetFile.csv", FileMode.Create), Encoding.UTF8);
  193. WriteToStream_TagSetFile(sw, ds.Tables[0], false, false);
  194. }
  195. }
  196. public static void WriteToStream_TagSetFile(TextWriter stream, DataTable table, bool header, bool quoteall)
  197. {
  198. int totalcount = table.Rows.Count;
  199. int literal_count = 0;
  200. int TagCount = 0;
  201. foreach (DataRow row in table.Rows)
  202. {
  203. TagCount++;
  204. literal_count = 0;
  205. for (int i = 0; i < table.Columns.Count; i++)
  206. {
  207. literal_count++;
  208. string s = row[i].ToString();
  209. s = s.Trim();
  210. if (row[i] == null)
  211. {
  212. return;
  213. }
  214. if (literal_count == 8)
  215. {
  216. stream.Write(TagCount - 1);
  217. stream.Write(',');
  218. stream.Write(s);
  219. stream.Flush();
  220. continue;
  221. }
  222. }
  223. if (!(totalcount == TagCount))
  224. stream.Write("\r\n");
  225. }
  226. stream.Flush();
  227. stream.Close();
  228. }
  229. }
  230. }