Form1.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;
  10. using System.IO;
  11. using System.Collections.Concurrent;
  12. using System.Net.Sockets;
  13. using System.Net;
  14. using System.Threading;
  15. using System.Timers;
  16. using System.Diagnostics;
  17. namespace DataGateWayProject
  18. {
  19. public partial class Form1 : Form
  20. {
  21. LogFileCreate LFC = new LogFileCreate();
  22. My_TimerThread MTT = new My_TimerThread();
  23. //System.Threading.Timer timer_NetStat;
  24. //private Object iCosCommObj = new Object();
  25. //Stopwatch sw = new Stopwatch();
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. }
  30. //public void Timer_netStat() //iCOS 연결 상태 확인 쓰레드 동작 함수
  31. //{
  32. // timer_NetStat = new System.Threading.Timer(net_state_Thread,null,1,9000);
  33. //}
  34. //bool changed = false;
  35. //public void net_state_Thread(object status)//(object sender, ElapsedEventArgs e) //iCOS 연결 상태 확인 쓰레드 함수
  36. //{
  37. // GetData_From_iCos GFI = new GetData_From_iCos();
  38. // string connectstate = "";
  39. // //lock (iCosCommObj)
  40. // //{
  41. // try
  42. // {
  43. // connectstate = GFI.GetConnectionStatus();
  44. // if (connectstate == "iCOS 통신 중")
  45. // {
  46. // if (changed == true)
  47. // {
  48. // Wirte_logbox_State(Convert.ToString(DateTime.Now) + " iCOS 통신 연결" + Environment.NewLine);
  49. // LFC.Log("iCOS 통신 연결" + Environment.NewLine);
  50. // }
  51. // changed = false;
  52. // }
  53. // else
  54. // {
  55. // Wirte_logbox_State(Convert.ToString(DateTime.Now) + " iCOS 통신 끊김" + Environment.NewLine);
  56. // LFC.Log("iCOS 통신 끊김" + Environment.NewLine);
  57. // changed = true;
  58. // }
  59. // Wirte_Textbox_State(connectstate);
  60. // }
  61. // catch (Exception E)
  62. // {
  63. // LFC.Log("[Form] net_state_Thread 오류 : " + E + Environment.NewLine);
  64. // LogTxt.AppendText(Convert.ToString(DateTime.Now) + " [Form] net_state_Thread 오류 : " + E + Environment.NewLine);
  65. // }
  66. //}
  67. //private delegate void DelegateTextbox(string theValue);
  68. //private void Wirte_Textbox_State(string theValue) //통신상태 변화 Form text box에 적용 함수
  69. //{
  70. // try
  71. // {
  72. // if (this.Net_Stat.InvokeRequired)
  73. // {
  74. // DelegateTextbox d = new DelegateTextbox(Wirte_Textbox_State);
  75. // this.Invoke(d, new object[] { theValue });
  76. // }
  77. // else
  78. // {
  79. // this.Net_Stat.Text = theValue;
  80. // }
  81. // }
  82. // catch (Exception E)
  83. // {
  84. // LFC.Log("[Form] DelegateTextbox 오류 : " + E + Environment.NewLine);
  85. // }
  86. //}
  87. //private delegate void DelegateLogbox(string theValue);
  88. //private void Wirte_logbox_State(string theValue) //통신상태 변화 Form text box에 적용 함수
  89. //{
  90. // try
  91. // {
  92. // if (this.LogTxt.InvokeRequired)
  93. // {
  94. // DelegateLogbox d = new DelegateLogbox(Wirte_logbox_State);
  95. // this.Invoke(d, new object[] { theValue });
  96. // }
  97. // else
  98. // {
  99. // this.LogTxt.AppendText(theValue);
  100. // LogTxt.Select(LogTxt.Text.Length, 0);
  101. // LogTxt.ScrollToCaret();
  102. // }
  103. // }
  104. // catch (Exception E)
  105. // {
  106. // LFC.Log("[Form] DelegateLogbox 오류 : " + E + Environment.NewLine);
  107. // }
  108. //}
  109. private void Form1_Load(object sender, EventArgs e)
  110. {
  111. ProStartBtn.Enabled = false; //시작 버튼 비활성화
  112. ProStopBtn.Enabled = false; //중단 버튼 비활성화
  113. PointSetBtn.Enabled = false; //관제점 등록 버튼 비활성화
  114. PointUnRegBtn.Enabled = false; //관제점 해제 버튼 비활성화
  115. AllValueCheck_btn.Enabled = false; // 일관 확인 버튼 비활성화
  116. RequestValue_btn.Enabled = false; //특정 값 확인 버튼 비활성화
  117. //Timer_netStat(); //iCOS 네트워크 연결 확인 함수
  118. }
  119. private void ProStartBtn_Click(object sender, EventArgs e) //시작버튼 함수
  120. {
  121. FolderBrowserDialog();
  122. if (toolStripStatusLabel1.Text != "")//경로 설정 확인
  123. {
  124. try
  125. {
  126. EndTxt.Clear(); //모니터링 종료시간 clear
  127. StartTxt.Text = Convert.ToString(DateTime.Now);
  128. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 프로그램 시작" + Environment.NewLine);
  129. LFC.Log("프로그램 시작 " + Environment.NewLine);
  130. ProStartBtn.Enabled = false; // 시작버튼 비활성화.
  131. ProStopBtn.Enabled = true; //정지버튼 활성화
  132. FileSettingBtn.Enabled = false; //프로젝트 설정 버튼 비활성화
  133. FileMatchBtn.Enabled = false; //관제점 설정 버튼 비활성화
  134. FileExpBtn.Enabled = false; //성능분석시 버튼 비활성화
  135. FirstCheck FC = new FirstCheck();
  136. FC.Check_DailyData();
  137. OneMinute_Insert OMI = new OneMinute_Insert();
  138. OMI.Today_PeakMinMax();
  139. OMI.Year_PeakMinMax();
  140. MTT.timer2start();
  141. LogTxt.Select(LogTxt.Text.Length, 0);
  142. LogTxt.ScrollToCaret();
  143. }
  144. catch (Exception eProStart)
  145. {
  146. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 프로그램 오류 : " + eProStart + Environment.NewLine);
  147. LFC.Log("프로그램 오류 : " + eProStart + Environment.NewLine);
  148. LogTxt.Select(LogTxt.Text.Length, 0);
  149. LogTxt.ScrollToCaret();
  150. }
  151. }
  152. else
  153. MessageBox.Show("경로를 설정해주세요");
  154. }
  155. private void ProStopBtn_Click(object sender, EventArgs e) //중단
  156. {
  157. GetData_From_iCos GFi = new GetData_From_iCos();
  158. GFi.StopTimer();
  159. MTT.timer2Stop();
  160. FileSettingBtn.Enabled = true; //프로젝트 설정 버튼 활성화
  161. FileMatchBtn.Enabled = true; //관제점설정 버튼 활성화
  162. FileExpBtn.Enabled = true; //성는분석식 버튼 활성화
  163. ProStartBtn.Enabled = true;
  164. EndTxt.Text = Convert.ToString(DateTime.Now);
  165. LFC.Log("프로그램 중단" + Environment.NewLine);
  166. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 프로그램 중단 " + Environment.NewLine);
  167. ProStopBtn.Enabled = false;
  168. LogTxt.Select(LogTxt.Text.Length, 0);
  169. LogTxt.ScrollToCaret();
  170. }
  171. private void ProSetBtn_Click(object sender, EventArgs e) //접속 설정
  172. {
  173. SettingForm SF = new SettingForm();
  174. SF.ShowDialog();
  175. }
  176. private void ProPathBtn_Click_1(object sender, EventArgs e) //프로젝트 경로
  177. {
  178. System.Diagnostics.Process.Start("explorer.exe", Environment.CurrentDirectory);
  179. }
  180. /// <summary>
  181. /// 셋팅파일 불러오기 버튼
  182. /// </summary>
  183. /// <param name="sender"></param>
  184. /// <param name="e"></param>
  185. private void FileSettingBtn_Click(object sender, EventArgs e)// 프로젝트 설정
  186. {
  187. string FSBLocation = Environment.CurrentDirectory + "\\Login.ini";
  188. try
  189. {
  190. System.Diagnostics.Process.Start(FSBLocation);// Login 파일에 접근
  191. }
  192. catch
  193. {
  194. FileError(FSBLocation);
  195. }
  196. }
  197. /// <summary>
  198. /// 매칭파일 불러오기 버튼
  199. /// </summary>
  200. /// <param name="sender"></param>
  201. /// <param name="e"></param>
  202. private void FileMatchBtn_Click(object sender, EventArgs e) //관제점 설정
  203. {
  204. string FSBLocation = Environment.CurrentDirectory + "\\SettingFile.csv";
  205. try
  206. {
  207. System.Diagnostics.Process.Start(FSBLocation);
  208. }
  209. catch
  210. {
  211. FileError(FSBLocation);
  212. }
  213. }
  214. /// <summary>
  215. /// 수식파일 불러오기 버튼
  216. /// </summary>
  217. /// <param name="sender"></param>
  218. /// <param name="e"></param>
  219. private void FileExpBtn_Click(object sender, EventArgs e) //성능 분석식
  220. {
  221. string FSBLocation = Environment.CurrentDirectory + "\\Expression.csv";
  222. try
  223. {
  224. System.Diagnostics.Process.Start(FSBLocation);
  225. }
  226. catch
  227. {
  228. FileError(FSBLocation);
  229. }
  230. }
  231. public void FileError(string Error)// 오류 같음[Matching.csv => SettingFile.csv]
  232. {
  233. if ((!Error.Contains("Login.ini")) || (!Error.Contains("expression.csv")) || (!Error.Contains("Matching.csv")))
  234. {
  235. MessageBox.Show("파일을 찾을 수 없음");
  236. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 파일을 찾을 수 없음 : " + Error + Environment.NewLine);
  237. LFC.Log("파일을 찾을 수 없음 : " + Error + Environment.NewLine);
  238. LogTxt.Select(LogTxt.Text.Length, 0);
  239. LogTxt.ScrollToCaret();
  240. }
  241. }
  242. public void FolderBrowserDialog() //프로젝트 경로 버튼 클릭 -> 팝업창
  243. {
  244. toolStripStatusLabel1.Text = Environment.CurrentDirectory;
  245. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 프로젝트 경로 설정 완료" + Environment.NewLine);
  246. LogTxt.AppendText(toolStripStatusLabel1 + Environment.NewLine);
  247. LFC.Log("프로젝트 경로 설정 완료");
  248. LFC.Log(toolStripStatusLabel1 + Environment.NewLine);
  249. LogTxt.Select(LogTxt.Text.Length, 0);
  250. LogTxt.ScrollToCaret();
  251. }
  252. private void EraseLog_Click(object sender, EventArgs e) //로그지우기
  253. {
  254. LogTxt.Clear();
  255. LogTxt.Select(LogTxt.Text.Length, 0);
  256. LogTxt.ScrollToCaret();
  257. }
  258. // 프로그램 종료시 이벤트
  259. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  260. {
  261. if (MessageBox.Show("프로그램을 종료하시겠습니까?", "종료", MessageBoxButtons.YesNo) == DialogResult.Yes)
  262. {
  263. MTT.timer2Stop();
  264. // 스레드 종료
  265. Application.ExitThread();
  266. for (int i = 0; i < 1000000; i++) {; }
  267. // 프로세스 종료
  268. Application.Exit();
  269. // 프로세스가 남아있다면 최종 종료 (2개의 프로젝트가 공존하고 Application.ProductName 값이 2개다
  270. System.Diagnostics.Process[] mProcess = System.Diagnostics.Process.GetProcessesByName(Application.ProductName);
  271. foreach (System.Diagnostics.Process p in mProcess)
  272. p.Kill();
  273. // 최종 정리 (보완코드 : 크로스 스레드 문제 발생 여지가 있으므로 사용주의)
  274. Environment.Exit(0);
  275. LFC.Log("사용자에 의해 프로그램이 종료되었습니다.");
  276. }
  277. else
  278. {
  279. LFC.Log("사용자에 의해 프로그램 종료가 취소되었습니다.");
  280. e.Cancel = true;
  281. return;
  282. }
  283. }
  284. private void PointSetBtn_Click(object sender, EventArgs e) //관제점 등록
  285. {
  286. bool ReadTag_Result;
  287. GetData_From_iCos GFi = new GetData_From_iCos();
  288. ReadTag_Result = GFi.ReadTag();
  289. if (ReadTag_Result == true)
  290. {
  291. LFC.Log("관제점 등록 완료 " + Environment.NewLine);
  292. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 관제점 등록 완료" + Environment.NewLine);
  293. ProStartBtn.Enabled = true;
  294. PointUnRegBtn.Enabled = true;
  295. GFi.GetValueTimer();
  296. Thread.Sleep(4500);
  297. RequestValue_btn.Enabled = true;
  298. AllValueCheck_btn.Enabled = true;
  299. }
  300. else
  301. {
  302. LFC.Log("관제점 등록 실패 " + Convert.ToString(DateTime.Now));
  303. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " 관제점 등록 실패" + Environment.NewLine);
  304. }
  305. LogTxt.Select(LogTxt.Text.Length, 0);
  306. LogTxt.ScrollToCaret();
  307. }
  308. private void Conn_iCos_Btn_Click(object sender, EventArgs e) //icos 접속
  309. {
  310. GetData_From_iCos GetData = new GetData_From_iCos();
  311. string ReturnValue = GetData.iCos3_Conn();
  312. if (ReturnValue == "SUCCESS")
  313. {
  314. EndTxt.Clear();
  315. LFC.Log("iCOS 접속 성공" + Environment.NewLine);
  316. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " iCOS 접속 성공" + Environment.NewLine);
  317. PointSetBtn.Enabled = true;
  318. }
  319. else
  320. {
  321. LFC.Log("iCOS 접속 실패" + Environment.NewLine);
  322. LogTxt.AppendText(Convert.ToString(DateTime.Now) + " iCOS 접속 실패" + Environment.NewLine);
  323. }
  324. LogTxt.Select(LogTxt.Text.Length, 0);
  325. LogTxt.ScrollToCaret();
  326. }
  327. private void Btn_SettingFile_Click(object sender, EventArgs e) //설정파일 생성 버튼
  328. {
  329. Creating_Setting_File CSF = new Creating_Setting_File();
  330. CSF.Create_Setting_Point_File();
  331. }
  332. private void Btn_TagSetFile_Click(object sender, EventArgs e) //태그등록파일 생성 버튼
  333. {
  334. Creating_Setting_File CSF = new Creating_Setting_File();
  335. CSF.Create_Point_File();
  336. }
  337. private void RequestValue_btn_Click(object sender, EventArgs e) //확인 버튼
  338. {
  339. if (RequestValue.Text != "")
  340. {
  341. LogTxt.AppendText("*** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***" + Environment.NewLine);
  342. LFC.Log("*** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***");
  343. try
  344. {
  345. string tag = RequestValue.Text; //태그값
  346. string temp = GetData_From_iCos.Key_Value_Dic[RequestValue.Text];
  347. LogTxt.AppendText('"' + tag + '"' + " 의 값 : " + temp + Environment.NewLine);
  348. LogTxt.AppendText("***********************************" + Environment.NewLine);
  349. LFC.Log('"' + tag + '"' + " 의 값 : " + temp);
  350. LFC.Log("***************************************" + Environment.NewLine);
  351. }
  352. catch (Exception er)
  353. {
  354. MessageBox.Show("오류 발생");
  355. LogTxt.AppendText("오류 발생 : " + er);
  356. LFC.Log("오류 발생 : " + er);
  357. }
  358. }
  359. else
  360. {
  361. MessageBox.Show("확인 값이 없습니다.");
  362. return;
  363. }
  364. LogTxt.Select(LogTxt.Text.Length, 0);
  365. LogTxt.ScrollToCaret();
  366. }
  367. /// <summary>
  368. /// 패킷분석 기능 추가
  369. /// </summary>
  370. private void AllValueCheck_btn_Click(object sender, EventArgs e) //값 일괄 확인 버튼
  371. {
  372. string[] sArray = File.ReadAllLines("TagSetFile.csv", Encoding.Default);
  373. GetData_From_iCos GFi = new GetData_From_iCos();
  374. LogTxt.AppendText("*** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ***" + Environment.NewLine);
  375. LFC.Log("****** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " *****");
  376. try
  377. {
  378. for (int i = 0; i < sArray.Length; i++)
  379. {
  380. string[] tmp = sArray[i].Split(','); // tmp[0] = index, tmp[1] = Tag명.
  381. string a = GFi.GetTagValue(tmp[1]);
  382. LogTxt.AppendText('"' + tmp[1] + '"' + " 의 값 : " + GetData_From_iCos.Key_Value_Dic[tmp[1]] + Environment.NewLine);
  383. LFC.Log('"' + tmp[1] + '"' + " 의 값 : " + GetData_From_iCos.Key_Value_Dic[tmp[1]]);
  384. }
  385. LogTxt.AppendText("***********************************" + Environment.NewLine);
  386. LFC.Log("********************************************" + Environment.NewLine);
  387. }
  388. catch (Exception er)
  389. {
  390. LogTxt.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 오류 발생 : " + er + Environment.NewLine);
  391. LFC.Log("오류 발생 : " + er + Environment.NewLine);
  392. MessageBox.Show("오류 발생 : " + er);
  393. }
  394. LogTxt.Select(LogTxt.Text.Length, 0);
  395. LogTxt.ScrollToCaret();
  396. }
  397. }
  398. }