930b7bf5146c68350af6e8d9a68116268fcb7c85.svn-base 11 KB

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