4c51e7683343c804cf0e0b672ad0f8c43820063b.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. namespace DataGateWayProject
  15. {
  16. public partial class Form1 : Form
  17. {
  18. bool startOk = false;
  19. LogFileCreate LFC = new LogFileCreate();
  20. My_TimerThread TTs = new My_TimerThread();
  21. static string SelecDir;
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26. private void Form1_Load(object sender, EventArgs e)
  27. {
  28. ProStartBtn.Enabled = false; //시작 버튼 비활성화
  29. ProStopBtn.Enabled = false; //중단 버튼 비활성화
  30. PointSetBtn.Enabled = false; //관제점 등록 버튼 비활성화
  31. PointUnRegBtn.Enabled = false; //관제점 해제 버튼 비활성화
  32. }
  33. private void ProStartBtn_Click(object sender, EventArgs e) //시작버튼 함수
  34. {
  35. if (toolStripStatusLabel1.Text != "")//경로 설정 확인
  36. {
  37. try
  38. {
  39. //PingConfirm();
  40. //Creating_Setting_File CSF = new Creating_Setting_File();
  41. //CSF.Create_Setting_Point_File();
  42. EndTxt.Clear(); //모니터링 종료시간 clear
  43. //TTs.timer1start();
  44. ProStartBtn.Enabled = false; // 시작버튼 비활성화.
  45. StartTxt.Text = Convert.ToString(DateTime.Now);//모니터링 시작
  46. LogTxt.Text += "프로그램 시작 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  47. ProStopBtn.Enabled = true; //정지버튼 활성화
  48. FileSettingBtn.Enabled = false; //프로젝트 설정 버튼 비활성화
  49. FileMatchBtn.Enabled = false; //관제점 설정 버튼 비활성화
  50. FileExpBtn.Enabled = false; //성능분석시 버튼 비활성화
  51. FirstCheck FC = new FirstCheck();
  52. FC.Check_DailyData();
  53. OneMinute_Insert OMI = new OneMinute_Insert();
  54. OMI.Today_PeakMinMax();
  55. OMI.Year_PeakMinMax();
  56. My_TimerThread MTT = new My_TimerThread();
  57. MTT.timer2start();
  58. }
  59. catch (Exception eProStart)
  60. {
  61. LogTxt.Text += "프로그램 오류 " + Convert.ToString("DB에 접속되지 않았습니다.") + " " + eProStart + Convert.ToString(DateTime.Now) + Environment.NewLine;
  62. }
  63. }
  64. else
  65. MessageBox.Show("경로를 설정해주세요");
  66. }
  67. //System.Timers.Timer timer2 = new System.Timers.Timer();
  68. //public void PingConfirm()
  69. //{
  70. // System.Timers.Timer timer2 = new System.Timers.Timer();
  71. // timer2.Interval = 10000;
  72. // timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed_ping);
  73. // timer2.Enabled = true;
  74. //}
  75. //void timer_Elapsed_ping(object sender, System.Timers.ElapsedEventArgs e)
  76. //{
  77. // Readini Ri = new Readini();
  78. // Ri.ReadData(); // ini의 설정값을 불러옴.
  79. // IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(Ri.IP1), 1609);
  80. // Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp);
  81. // server.Connect(ipep);
  82. // LFC.Log("연결여부" + ipep + Convert.ToString(DateTime.Now));
  83. //}
  84. private void ProStopBtn_Click(object sender, EventArgs e) //중단
  85. {
  86. //Close();
  87. TTs.timer2Stop();
  88. FileSettingBtn.Enabled = true; //프로젝트 설정 버튼 활성화
  89. FileMatchBtn.Enabled = true; //관제점설정 버튼 활성화
  90. FileExpBtn.Enabled = true; //성는분석식 버튼 활성화
  91. ProStartBtn.Enabled = true;
  92. EndTxt.Text = Convert.ToString(DateTime.Now);
  93. LogTxt.Text += "프로그램 중단 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  94. ProStopBtn.Enabled = false;
  95. }
  96. private void ProSetBtn_Click(object sender, EventArgs e) //접속 설정
  97. {
  98. SettingForm SF = new SettingForm();
  99. SF.ShowDialog();
  100. }
  101. private void ProPathBtn_Click_1(object sender, EventArgs e) //프로젝트 경로
  102. {
  103. FolderBrowserDialog();
  104. }
  105. /// <summary>
  106. /// 셋팅파일 불러오기 버튼
  107. /// </summary>
  108. /// <param name="sender"></param>
  109. /// <param name="e"></param>
  110. private void FileSettingBtn_Click(object sender, EventArgs e)// 프로젝트 설정
  111. {
  112. string FSBLocation = SelecDir + "\\Login.ini";
  113. try
  114. {
  115. System.Diagnostics.Process.Start(FSBLocation);// Login 파일에 접근
  116. }
  117. catch
  118. {
  119. FileError(FSBLocation);
  120. }
  121. }
  122. /// <summary>
  123. /// 매칭파일 불러오기 버튼
  124. /// </summary>
  125. /// <param name="sender"></param>
  126. /// <param name="e"></param>
  127. private void FileMatchBtn_Click(object sender, EventArgs e) //관제점 설정
  128. {
  129. string FSBLocation = SelecDir + "\\SettingFile.csv";
  130. try
  131. {
  132. System.Diagnostics.Process.Start(FSBLocation);
  133. }
  134. catch
  135. {
  136. FileError(FSBLocation);
  137. }
  138. }
  139. /// <summary>
  140. /// 수식파일 불러오기 버튼
  141. /// </summary>
  142. /// <param name="sender"></param>
  143. /// <param name="e"></param>
  144. private void FileExpBtn_Click(object sender, EventArgs e) //성능 분석식
  145. {
  146. string FSBLocation = SelecDir + "\\Expression.csv";
  147. try
  148. {
  149. System.Diagnostics.Process.Start(FSBLocation);
  150. }
  151. catch
  152. {
  153. FileError(FSBLocation);
  154. }
  155. }
  156. public void FileError(string Error)// 오류 같음[Matching.csv => SettingFile.csv]
  157. {
  158. if ((!Error.Contains("Login.ini")) || (!Error.Contains("expression.csv")) || (!Error.Contains("Matching.csv")))
  159. {
  160. MessageBox.Show("파일을 찾을 수 없습니다.");
  161. }
  162. }
  163. public string FolderBrowserDialog() //프로젝트 경로 버튼 클릭 -> 팝업창
  164. {
  165. LogFileCreate LFC = new LogFileCreate();
  166. FolderBrowserDialog folderDig = new FolderBrowserDialog();
  167. folderDig.ShowNewFolderButton = true;
  168. // Show the FolderBrowserDialog.
  169. DialogResult result = folderDig.ShowDialog();
  170. if (result == DialogResult.OK)
  171. {
  172. SelecDir = folderDig.SelectedPath;
  173. Environment.SpecialFolder root = folderDig.RootFolder;
  174. toolStripStatusLabel1.Text = "프로젝트 경로 : " + SelecDir;
  175. LogTxt.Text += "경로설정완료 " + SelecDir + " " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  176. LFC.Log("경로 설정 완료" + SelecDir);
  177. }
  178. return SelecDir;
  179. }
  180. private void EraseLog_Click(object sender, EventArgs e) //로그지우기
  181. {
  182. LogTxt.Clear();
  183. }
  184. // 프로그램 종료시 이벤트
  185. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  186. {
  187. if (MessageBox.Show("프로그램을 종료하시겠습니까?", "종료", MessageBoxButtons.YesNo) != DialogResult.Yes)
  188. {
  189. e.Cancel = true;
  190. }
  191. }
  192. private void PointSetBtn_Click(object sender, EventArgs e) //관제점 등록
  193. {
  194. LFC.Log("관제점 등록 시작" + Convert.ToString(DateTime.Now));
  195. LogTxt.Text += "관제점 등록 시작 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  196. //iCosFourDBSET();
  197. GetData_From_iCos GFi = new GetData_From_iCos();
  198. GFi.ReadTag();
  199. LFC.Log("관제점 등록 완료" + Convert.ToString(DateTime.Now));
  200. LogTxt.Text += "관제점 등록 완료 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  201. GFi.GetValueTimer();
  202. }
  203. private void Conn_iCos_Btn_Click(object sender, EventArgs e) //icos 접속
  204. {
  205. GetData_From_iCos GetData = new GetData_From_iCos();
  206. LogFileCreate LFC = new LogFileCreate();
  207. LFC.Log("접속시작" + Convert.ToString(DateTime.Now));
  208. LogTxt.Text += "접속시작 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  209. string ReturnValue = GetData.iCos3_Conn();
  210. if (ReturnValue == "SUCCESS")
  211. {
  212. LFC.Log("접속완료" + Convert.ToString(DateTime.Now));
  213. LogTxt.Text += "접속완료 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  214. PointSetBtn.Enabled = true;
  215. PointUnRegBtn.Enabled = true;
  216. ProStartBtn.Enabled = true;
  217. }
  218. else
  219. {
  220. MessageBox.Show("접속에 실패하였습니다.");
  221. }
  222. }
  223. /// <summary>
  224. /// 생존확인
  225. /// </summary>
  226. /// <returns></returns>
  227. //public byte[] Alive_Check()
  228. //{
  229. // byte[] Send_Alive = new byte[1024];
  230. // Send_Alive[0] = 0x03;
  231. // Send_Alive[1023] = 0x03;
  232. // return Send_Alive;
  233. //}
  234. //static UdpClient client; //20170116.. 318번째 줄 대신
  235. //static IPEndPoint ep; //20170116
  236. //public string iCos3_Conn()
  237. //{
  238. // Readini Ri = new Readini();
  239. // Ri.ReadData(); // ini의 설정값을 불러옴.
  240. // bool Re_Conn_Check = false;
  241. // //var client = new UdpClient();
  242. // var receivedData = new byte[1024];
  243. // string Result_Conn_iCos = "";
  244. // client.Client.SendTimeout = 5000; // send Timeout 5초 설정
  245. // client.Client.ReceiveTimeout = 5000; // receive timeout 5초 설정
  246. // //IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Ri.IP1), Convert.ToInt32(Ri.Port1)); // endpoint where server is listening
  247. // ep = new IPEndPoint(IPAddress.Parse(Ri.IP1), Convert.ToInt32(Ri.Port1)); // endpoint where server is listening
  248. // LogFileCreate LFC = new LogFileCreate();
  249. // byte[] Send_Alive = Alive_Check(); // 생존 프로토콜 생성
  250. // bool Conn_Flag = false;
  251. // bool Error_Conn = false;
  252. // string tmplogstring = "";
  253. // try
  254. // {
  255. // client.Connect(ep); // 접속
  256. // client.Send(Send_Alive, 1024);
  257. // receivedData = client.Receive(ref ep);
  258. // foreach (byte tmplog in receivedData)
  259. // {
  260. // tmplogstring += tmplog;
  261. // }
  262. // LFC.Log(tmplogstring);
  263. // foreach (byte tmp in receivedData)
  264. // {
  265. // if (tmp == 0x06)
  266. // {
  267. // LogTxt.Text += "통신 오류가 발생하였습니다. 다시 시도해주세요." + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
  268. // Conn_Flag = true;
  269. // client.Close();
  270. // Error_Conn = true;
  271. // }
  272. // break;
  273. // }
  274. // }
  275. // catch (Exception e1)
  276. // {
  277. // LFC.Log("접속실패 " + e1 + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  278. // Result_Conn_iCos = "FAIL";
  279. // Re_Conn_Check = true;
  280. // }
  281. // if (Re_Conn_Check == true)
  282. // {
  283. // for (int Re_Conn = 0; Re_Conn < 2; Re_Conn++)
  284. // {
  285. // try
  286. // {
  287. // LFC.Log("재접속 시도 중 ...접속 시도 횟수 = " + Re_Conn + " = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  288. // LogTxt.Text += "재접속 시도 중 ...접속 시도 횟수 = " + Re_Conn + " = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
  289. // client.Connect(ep); // 접속
  290. // client.Send(Send_Alive, 1024);
  291. // receivedData = client.Receive(ref ep);
  292. // if (receivedData != null)
  293. // {
  294. // break;
  295. // }
  296. // }
  297. // catch
  298. // {
  299. // LFC.Log("재접속 실패 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  300. // LogTxt.Text += "재접속 실패 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
  301. // Result_Conn_iCos = "FAIL";
  302. // }
  303. // }
  304. // }
  305. // int len = receivedData.Length; // 수신받은데이터 길이 확인
  306. // byte sum = CheckSum(receivedData); // 수신받은 데이터 체크썸 확인
  307. // byte rec = receivedData[1023];
  308. // string packetstring = "";
  309. // foreach (byte log in receivedData)
  310. // {
  311. // packetstring += log + " ";
  312. // }
  313. // LogTxt.Text += "수신받은 데이터의 체크섬 = " + rec + Environment.NewLine;
  314. // LogTxt.Text += "합산 = " + sum + Environment.NewLine;
  315. // LogTxt.Text += "길이 = " + len + Environment.NewLine;
  316. // if (receivedData[1023] != sum || receivedData[0] == 0 || len != 1024 || Error_Conn == true) // CheckSum 과 길이 체크.
  317. // {
  318. // LFC.Log("체크섬 = " + Convert.ToString(sum));
  319. // LFC.Log("길이 = " + Convert.ToString(len));
  320. // LFC.Log("접속 실패 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  321. // Result_Conn_iCos = "FAIL";
  322. // }
  323. // else
  324. // {
  325. // LFC.Log("접속 성공 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  326. // Result_Conn_iCos = "SUCCESS";
  327. // }
  328. // return Result_Conn_iCos;
  329. //}
  330. public byte CheckSum(byte[] CheckData) //미사용
  331. {
  332. byte Sum = 00;
  333. for (int bytenum = 0; bytenum < 1023; bytenum++)
  334. {
  335. Sum += CheckData[bytenum];
  336. }
  337. Sum = (byte)(Sum & 0xFF);
  338. return Sum;
  339. }
  340. private void Control_Req_Btn_Click(object sender, EventArgs e) //없음
  341. {
  342. //SetTagValue("ch.ctrl.tag1", 111);
  343. }
  344. private void PointUnRegBtn_Click(object sender, EventArgs e) //관제점해제 함수
  345. {
  346. //UnReg();
  347. }
  348. private void LogBtn_Click(object sender, EventArgs e) //없음
  349. {
  350. }
  351. private void Btn_SettingFile_Click(object sender, EventArgs e) //설정파일 생성 버튼
  352. {
  353. Creating_Setting_File CSF = new Creating_Setting_File();
  354. CSF.Create_Setting_Point_File();
  355. }
  356. private void Btn_TagSetFile_Click(object sender, EventArgs e) //태그등록파일 생성 버튼
  357. {
  358. Creating_Setting_File CSF = new Creating_Setting_File();
  359. CSF.Create_Point_File();
  360. }
  361. private void button1_Click(object sender, EventArgs e) //값확인 버튼
  362. {
  363. try
  364. {
  365. LogTxt.Text += GetData_From_iCos.Key_Value_Dic[testtxt.Text] + Environment.NewLine;
  366. }
  367. catch (Exception e1)
  368. {
  369. LogTxt.Text += "키값을 입력해주세요" + e1 + Environment.NewLine;
  370. }
  371. }
  372. /// <summary>
  373. /// 패킷분석 기능 추가
  374. /// </summary>
  375. private void Btn_Packet_Analysis_Click(object sender, EventArgs e) //값 일괄 확인 버튼
  376. {
  377. string[] sArray = File.ReadAllLines("TagSetFile.csv", Encoding.Default);
  378. LogTxt.Text += "*********** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "**********"+ Environment.NewLine;
  379. try
  380. {
  381. for (int i = 0; i < sArray.Length; i++) //
  382. {
  383. string[] tmp = sArray[i].Split(','); // tmp[0] = index, tmp[1] = Tag명.
  384. LogTxt.Text += tmp[1] + " 의 값 : " + GetData_From_iCos.Key_Value_Dic[tmp[1]] + Environment.NewLine;
  385. }
  386. LogTxt.Text += "**************************************************************" + Environment.NewLine;
  387. }
  388. catch(Exception er)
  389. {
  390. MessageBox.Show("오류 발생 = " + er);
  391. }
  392. }
  393. private void testtxt_TextChanged(object sender, EventArgs e)
  394. {
  395. }
  396. }
  397. }