51adbcc5ffd80f1671a4983a80a811c1247aa74c.svn-base 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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.Threading;
  13. using System.Timers;
  14. namespace BEMSDataGateway
  15. {
  16. public partial class Form1 : Form
  17. {
  18. //event 부분 추가 . 20161209
  19. //EventWaitHandle ewhLoginSuccess = new EventWaitHandle(false, EventResetMode.ManualReset, "Login.Success");
  20. //EventWaitHandle ewhLoginFail = new EventWaitHandle(false, EventResetMode.ManualReset, "Login.Fail");
  21. //EventWaitHandle ewhDisconnect = new EventWaitHandle(false, EventResetMode.ManualReset, "Connect.Fail");
  22. bool startOk = false;
  23. public bool bConnect;
  24. ////서로 다른 두 스레드(ResponseFunc와 Form)의 충돌을 피하기 위해 델리게이트 사용.
  25. ////ResponseFunc에서 오류 발생시 델리게이트를 통해 Log를 출력함
  26. //delegate void CrossThreadSafetySetErrorCodeTxt(string text);
  27. //private void SetText(string text)
  28. //{
  29. // if (this.LogTxt.InvokeRequired)
  30. // {
  31. // CrossThreadSafetySetErrorCodeTxt d = new CrossThreadSafetySetErrorCodeTxt(SetText);
  32. // this.Invoke(d, text );
  33. // //this.Invoke(d, text);
  34. // }
  35. // else
  36. // {
  37. // this.LogTxt.Text = text;
  38. // }
  39. //}
  40. // public delegate void ErrorFunc(int nErrorCode);
  41. //결과 학인 콜백 함수 이름변경(ErrorFunc -> ResponseFunc) 및 static 삭제 swpark 2016.12.01
  42. public delegate int ErrorFunc(int nErrorCode);//선언위치 변경 swpark 20161209
  43. public ErrorFunc _errfunc;// 선언위치변경 swpark 20161209
  44. public static int ResponseFunc(int nErrorCode)
  45. {
  46. LogFileCreate LFC = new LogFileCreate();
  47. Form1 F1 = new Form1();
  48. // nErrorCode 정보
  49. // 1 : 서버 연결 성공
  50. // 2 : 연결 끊어짐
  51. // 3 : 프로젝트 다운로드 완료
  52. // 4 : 로그인 성공
  53. // 5 : 로그인 실패
  54. // 100~ : 기타 오류
  55. switch (nErrorCode)
  56. {
  57. case 1:
  58. LFC.Log("서버에 연결 성공 = " + nErrorCode);
  59. //this.SetText("서버에 연결 성공 = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  60. break;
  61. case 2:
  62. LFC.Log("서버에 의해서 소켓 연결이 끊어졌습니다 = " + nErrorCode);
  63. //CloseConnection();
  64. //SetText("서버에 의해서 소켓 연결이 끊어졌습니다 = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  65. //CloseConnection();
  66. break;
  67. case 3:
  68. LFC.Log("프로젝트 다운로드를 완료했습니다. = " + nErrorCode);
  69. UserLogin("bems_gw", "1", true);
  70. //this.SetText("프로젝트 다운로드를 완료했습니다. = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  71. break;
  72. case 4:
  73. //System.Threading.Thread.Sleep(2000);
  74. LFC.Log("서버 로그인에 성공했습니다. = " + nErrorCode);
  75. My_TimerThread.check = false;
  76. F1.ReadTag();
  77. //this.SetText("서버 로그인에 성공했습니다.. = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  78. break;
  79. case 5:
  80. LFC.Log("알 수 없는 에러로 연결이 끊겼습니다. = " + nErrorCode);
  81. //this.SetText("알 수 없는 에러로 연결이 끊겼습니다. = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  82. //CloseConnection();
  83. break;
  84. default:
  85. LFC.Log("서버 로그인에 실패했습니다. 연결을 종료합니다. = " + nErrorCode);
  86. //this.SetText("서버 로그인에 실패했습니다. 연결을 종료합니다. = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  87. //CloseConnection();
  88. break;
  89. }
  90. return nErrorCode;
  91. }
  92. //public delegate void MonitoringTag(string szTag, float fValue);
  93. //public delegate void MonitoringPacket([MarshalAs(UnmanagedType.LPArray, SizeConst = 8192)] byte[] bPacket);
  94. // 접속정보 전달용 구조체 (마샬링)
  95. public struct runMode
  96. {
  97. public UInt32 uNetType;
  98. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
  99. public String szRunIp;
  100. public UInt32 uRunPort;
  101. public UInt32 uRunScan;
  102. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
  103. public String szServer2;
  104. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
  105. public String szHaIp;
  106. public UInt32 uHaPort;
  107. public UInt32 uHaScan;
  108. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  109. public String szUser;
  110. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  111. public String szPassword;
  112. }
  113. public struct DBInfo
  114. {
  115. public UInt32 uDbType;//0 : MDB. 1 : MSSQL
  116. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  117. public String szDBPath;//MDB 파일 위치 값이 없으면 실행파일위치에서 하위 Project\DB\iCos4.mdb 를 읽어옴
  118. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  119. public String szSqlIP; //MSSQL IP주소
  120. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  121. public String szSqlDB; //MSSQL DB명
  122. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  123. public String szSqlID; //MSSQL ID
  124. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  125. public String szSqlPW; //MSSQL PASSWORD
  126. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  127. public String szDbAddr; //
  128. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
  129. public String szDbTable; //DB 테이블명
  130. }
  131. // 접속 초기화 함수
  132. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  133. public static extern bool InitSocket(string szUser, string szPassword, bool bLogout);
  134. //접속정보 설정
  135. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  136. public static extern void SetNetwork(ref runMode tRunMode);
  137. // DB정보 설정
  138. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  139. public static extern void SetDBInfo(ref DBInfo tDBInfo);
  140. // 태그값 등록 함수
  141. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  142. public static extern void RegTag(int nTagCnt, string szTag);
  143. // 태그값 등록해제 함수
  144. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  145. public static extern void UnregTag(int nTagCnt, string szTag);
  146. // 디바이스 태그 DB 설정
  147. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  148. public static extern void SetDeviceDB(int nDBType, string szDBPath);
  149. // 디바이스 태그 값 가져오기
  150. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  151. public static extern float GetTagValue(string szTag);
  152. // 디바이스 태그 값 쓰기
  153. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  154. public static extern float SetTagValue(string szTag, float fValue);
  155. // 로그인 전용 함수
  156. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  157. public static extern bool UserLogin(string szUser, string szPassword, bool bLogout);
  158. // 로그아웃 전용 함수
  159. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  160. public static extern bool UserLogout(string szUser);
  161. // 접속 초기화 함수
  162. [DllImport("ICosSocket.dll")]
  163. public static extern void CloseConnection();
  164. // 결과확인 함수 포인터 전달 함수
  165. [DllImport("ICosSocket.dll", EntryPoint = "SetErrorFunc")]
  166. public static extern void SetErrorFunc(ErrorFunc x);
  167. //[DllImport("ICosSocket.dll", EntryPoint = "SetPacketFunc")]
  168. //public static extern void SetPacketFunc(MonitoringPacket x);
  169. //[DllImport("ICosSocket.dll", EntryPoint = "SetMonitoringFunc")]
  170. //public static extern void SetMonitoringFunc(MonitoringTag x);
  171. // 변수추가 szText swpark 2016.12.01
  172. //public static void MonitoringTag(string szTag, float fValue, string szText)
  173. //{
  174. // string str;
  175. // str = szTag + " : " + fValue.ToString();
  176. //}
  177. // iCos4 네트워크 상태
  178. // NetworkStatus 정보
  179. // 0 : 연결안됨
  180. // 1 : 정상상태
  181. [DllImport("ICosSocket.dll", CallingConvention = CallingConvention.Cdecl)]
  182. public static extern int GetNetworkStatus();
  183. public string iCosFourConn()
  184. {
  185. string strRet = "";
  186. string Result_Conn_iCos = "";
  187. Readini Ri = new Readini();
  188. Ri.ReadData(); // ini의 설정값을 불러옴.
  189. LogTxt.Text += "iCos 에 접속중입니다. 기다려주세요...." + Environment.NewLine;
  190. runMode tRunMode = new runMode();
  191. tRunMode.szRunIp = Ri.IP1;
  192. tRunMode.szHaIp = Ri.DGW_IP;
  193. tRunMode.szServer2 = Ri.Ip2;
  194. tRunMode.uNetType = 7; //TYPE_CLIENT_ONLY
  195. tRunMode.uRunPort = 6000;
  196. tRunMode.uRunScan = 3000;
  197. tRunMode.uHaPort = 2;
  198. tRunMode.uHaScan = 1;
  199. tRunMode.szUser = "bems_gw";
  200. tRunMode.szPassword = "1";
  201. try
  202. {
  203. SetNetwork(ref tRunMode);
  204. //ErrorFunc _errfunc = new ErrorFunc(ResponseFunc); //응답처리함수 swpark 2016.12.01
  205. _errfunc = new ErrorFunc(ResponseFunc);
  206. SetErrorFunc(_errfunc); //콜백 함수 넘겨주는 방식 변경 (웹서버용 함수) swpark 2016.12.01
  207. //SetMonitoringPointValueFunc(_monitorvalue);//모니터링값 수신 함수 콜백 연결 웹서버용
  208. bool bRet = InitSocket("bems_gw", "1", true);
  209. strRet = bRet.ToString();
  210. LogTxt.Text += "iCos 접속 함수 동작 시작 : " + Convert.ToString(strRet) + " = " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  211. Result_Conn_iCos = "SUCCESS";
  212. }
  213. catch(Exception e)
  214. {
  215. LogTxt.Text += "iCos 접속완료 여부 : 실패하였습니다. = " + Convert.ToString(DateTime.Now) + e + Environment.NewLine ;
  216. Result_Conn_iCos = "FAIL";
  217. }
  218. return Result_Conn_iCos;
  219. }
  220. public void iCosFourReadDB()
  221. {
  222. SetDeviceDB(0, "C:\\DGW2.0\\Release\\Project\\DB\\iCos4.mdb");
  223. }
  224. string[] ReturnTag = new string[2];
  225. public void ReadTag()
  226. {
  227. timerstart_readTag();
  228. }
  229. System.Timers.Timer timer2 = new System.Timers.Timer();
  230. public void timerstart_readTag()
  231. {
  232. timer2.Interval = 3000;
  233. timer2.Elapsed += new ElapsedEventHandler(timer2_Elapsed);
  234. timer2.Start();
  235. }
  236. bool Check_reconn = false;
  237. public void timer2_Elapsed(object sender, ElapsedEventArgs e) // 1분마다 실행되는 데이터 수집 로직
  238. {
  239. if (Check_reconn == false)
  240. {
  241. Check_reconn = true;
  242. System.Threading.Thread.Sleep(5000);
  243. Parsing Ps = new Parsing();
  244. ReturnTag = Ps.TagParsing();
  245. RegTag(Convert.ToInt32(ReturnTag[1]), ReturnTag[0]);
  246. LogFileCreate LFC = new LogFileCreate();
  247. LFC.Log("포인트 등록완료");
  248. Check_reconn = false;
  249. timer2.Stop();
  250. timer2.Enabled = false;
  251. }
  252. }
  253. public void UnReg()
  254. {
  255. Parsing Ps = new Parsing();
  256. ReturnTag = Ps.TagParsing();
  257. UnregTag(Convert.ToInt32(ReturnTag[1]), ReturnTag[0]);
  258. }
  259. //public void GetValue()
  260. //{
  261. // float fval;
  262. // fval = GetTagValue("ch.ctrl.tag1");
  263. // //string strRet;
  264. // //strRet = fval.ToString();
  265. // //MessageBox.Show(strRet);
  266. //}
  267. My_TimerThread TTs = new My_TimerThread();
  268. static string SelecDir;
  269. public Form1()
  270. {
  271. InitializeComponent();
  272. }
  273. private void Form1_Load(object sender, EventArgs e)
  274. {
  275. ProStopBtn.Enabled = false;
  276. PointSetBtn.Enabled = false;
  277. PointUnRegBtn.Enabled = false;
  278. ProStartBtn.Enabled = false;
  279. ProStopBtn.Enabled = false;
  280. Shown += Form1_Shown;
  281. }
  282. System.Timers.Timer timer_NetStat = new System.Timers.Timer();
  283. LogFileCreate LFC = new LogFileCreate();
  284. public void Timer_netStat()
  285. {
  286. timer_NetStat.Interval = 5000;
  287. timer_NetStat.Elapsed += new ElapsedEventHandler(net_state_Thread);
  288. timer_NetStat.Start();
  289. }
  290. private delegate void myDelegate(string theValue);
  291. private void Wirte_Communicaion_State(string theValue)
  292. {
  293. try
  294. {
  295. LFC.Log(theValue + "= 들어온 통신상태");
  296. if (theValue == "1")
  297. {
  298. Net_Stat.BackColor = Color.LightSkyBlue;
  299. Net_Stat.Text = "통신중";
  300. }
  301. else if (theValue == "0")
  302. {
  303. Net_Stat.BackColor = Color.Red;
  304. Net_Stat.Text = "통신끊김";
  305. }
  306. }
  307. catch(Exception E)
  308. {
  309. LFC.Log(Convert.ToString(E));
  310. LogTxt.Text += Convert.ToString(E)+ Environment.NewLine;
  311. }
  312. }
  313. public void net_state_Thread(object sender, ElapsedEventArgs e)
  314. {
  315. try
  316. {
  317. //Net_Stat.BackColor = Color.LemonChiffon;
  318. int nNet;
  319. nNet = GetNetworkStatus();
  320. string strRet;
  321. strRet = nNet.ToString();
  322. this.Invoke(new myDelegate(Wirte_Communicaion_State), new object[] { strRet });
  323. }
  324. catch (Exception E)
  325. {
  326. LFC.Log(Convert.ToString(E));
  327. LogTxt.Text += Convert.ToString(E) + Environment.NewLine;
  328. }
  329. }
  330. private void Form1_Shown(object sender, EventArgs e)
  331. {
  332. LogFileCreate LFC = new LogFileCreate();
  333. string ReturnValue = iCosFourConn();
  334. iCosFourReadDB();
  335. LFC.Log("접속 함수 동작 시작 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  336. LogTxt.Text += "접속 함수 동작 시작 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
  337. if (ReturnValue == "SUCCESS")
  338. {
  339. LogTxt.Text += "접속 성공 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
  340. PointSetBtn.Enabled = true;
  341. PointUnRegBtn.Enabled = true;
  342. ProStartBtn.Enabled = true;
  343. }
  344. SetDeviceDB(0, "");
  345. //ReadTag();
  346. try
  347. {
  348. //Creating_Setting_File CSF = new Creating_Setting_File();
  349. //CSF.Create_Setting_Point_File();
  350. EndTxt.Clear();
  351. //TTs.timer1start();
  352. ProStartBtn.Enabled = false; // 시작버튼 비활성화.
  353. StartTxt.Text = Convert.ToString(DateTime.Now);
  354. LogTxt.Text += "프로그램 시작 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  355. ProStopBtn.Enabled = true;
  356. FileSettingBtn.Enabled = false;
  357. FileMatchBtn.Enabled = false;
  358. FileExpBtn.Enabled = false;
  359. FirstCheck FC = new FirstCheck();
  360. FC.Check_DailyData();
  361. OneMinute_Insert OMI = new OneMinute_Insert();
  362. OMI.Today_PeakMinMax();
  363. OMI.Year_PeakMinMax();
  364. My_TimerThread MTT = new My_TimerThread();
  365. MTT.timer2start();
  366. //Timer_netStat();
  367. }
  368. catch (Exception eProStart)
  369. {
  370. LogTxt.Text += "프로그램 오류 " + Convert.ToString("DB에 접속되지 않았습니다.") + " " + eProStart + " = " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  371. }
  372. }
  373. //private static DateTime Delay(int MS)
  374. //{
  375. // DateTime ThisMoment = DateTime.Now;
  376. // TimeSpan duration = new TimeSpan(0, 0, 0, 0, MS);
  377. // DateTime AfterWards = ThisMoment.Add(duration);
  378. // while (AfterWards >= ThisMoment)
  379. // {
  380. // System.Windows.Forms.Application.DoEvents();
  381. // ThisMoment = DateTime.Now;
  382. // }
  383. // return DateTime.Now;
  384. //}
  385. private void ProStartBtn_Click(object sender, EventArgs e)
  386. {
  387. if (toolStripStatusLabel1.Text != "")
  388. {
  389. try
  390. {
  391. //Creating_Setting_File CSF = new Creating_Setting_File();
  392. //CSF.Create_Setting_Point_File();
  393. EndTxt.Clear();
  394. //TTs.timer1start();
  395. ProStartBtn.Enabled = false; // 시작버튼 비활성화.
  396. StartTxt.Text = Convert.ToString(DateTime.Now);
  397. LogTxt.Text += "프로그램 시작 " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  398. ProStopBtn.Enabled = true;
  399. FileSettingBtn.Enabled = false;
  400. FileMatchBtn.Enabled = false;
  401. FileExpBtn.Enabled = false;
  402. FirstCheck FC = new FirstCheck();
  403. FC.Check_DailyData();
  404. OneMinute_Insert OMI = new OneMinute_Insert();
  405. OMI.Today_PeakMinMax();
  406. OMI.Year_PeakMinMax();
  407. My_TimerThread MTT = new My_TimerThread();
  408. MTT.timer2start();
  409. }
  410. catch (Exception eProStart)
  411. {
  412. LogTxt.Text += "프로그램 오류 " + Convert.ToString("DB에 접속되지 않았습니다.") + " " + eProStart + " = " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  413. }
  414. }
  415. else
  416. MessageBox.Show("경로를 설정해주세요");
  417. }
  418. private void ProStopBtn_Click(object sender, EventArgs e)
  419. {
  420. CloseConnection();
  421. TTs.timer2Stop();
  422. FileSettingBtn.Enabled = true;
  423. FileMatchBtn.Enabled = true;
  424. FileExpBtn.Enabled = true;
  425. ProStartBtn.Enabled = true;
  426. EndTxt.Text = Convert.ToString(DateTime.Now);
  427. LogTxt.Text += "프로그램 중단 " + " = " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  428. ProStopBtn.Enabled = false;
  429. }
  430. private void ProSetBtn_Click(object sender, EventArgs e)
  431. {
  432. SettingForm SF = new SettingForm();
  433. SF.ShowDialog();
  434. }
  435. private void ProPathBtn_Click_1(object sender, EventArgs e)
  436. {
  437. FolderBrowserDialog();
  438. }
  439. /// <summary>
  440. /// 셋팅파일 불러오기 버튼
  441. /// </summary>
  442. /// <param name="sender"></param>
  443. /// <param name="e"></param>
  444. private void FileSettingBtn_Click(object sender, EventArgs e)
  445. {
  446. string FSBLocation = Environment.CurrentDirectory + "\\Login.ini";
  447. try
  448. {
  449. System.Diagnostics.Process.Start(FSBLocation);
  450. }
  451. catch
  452. {
  453. FileError(FSBLocation);
  454. }
  455. }
  456. /// <summary>
  457. /// 매칭파일 불러오기 버튼
  458. /// </summary>
  459. /// <param name="sender"></param>
  460. /// <param name="e"></param>
  461. private void FileMatchBtn_Click(object sender, EventArgs e)
  462. {
  463. string FSBLocation = Environment.CurrentDirectory + "\\SettingFile.csv";
  464. try
  465. {
  466. System.Diagnostics.Process.Start(FSBLocation);
  467. }
  468. catch
  469. {
  470. FileError(FSBLocation);
  471. }
  472. }
  473. /// <summary>
  474. /// 수식파일 불러오기 버튼
  475. /// </summary>
  476. /// <param name="sender"></param>
  477. /// <param name="e"></param>
  478. private void FileExpBtn_Click(object sender, EventArgs e)
  479. {
  480. string FSBLocation = Environment.CurrentDirectory + "\\Expression.csv";
  481. try
  482. {
  483. System.Diagnostics.Process.Start(FSBLocation);
  484. }
  485. catch
  486. {
  487. FileError(FSBLocation);
  488. }
  489. }
  490. public void FileError(string Error)
  491. {
  492. if ((!Error.Contains("Login.ini")) || (!Error.Contains("expression.csv")) || (!Error.Contains("Matching.csv")))
  493. {
  494. MessageBox.Show("파일을 찾을 수 없습니다.");
  495. }
  496. }
  497. public string FolderBrowserDialog()
  498. {
  499. LogFileCreate LFC = new LogFileCreate();
  500. FolderBrowserDialog folderDig = new FolderBrowserDialog();
  501. folderDig.ShowNewFolderButton = true;
  502. // Show the FolderBrowserDialog.
  503. DialogResult result = folderDig.ShowDialog();
  504. if (result == DialogResult.OK)
  505. {
  506. SelecDir = folderDig.SelectedPath;
  507. Environment.SpecialFolder root = folderDig.RootFolder;
  508. toolStripStatusLabel1.Text = "프로젝트 경로 : " + Environment.CurrentDirectory;
  509. LogTxt.Text += "경로설정완료 " + SelecDir + " " + Convert.ToString(DateTime.Now) + Environment.NewLine;
  510. LFC.Log("경로 설정 완료" + SelecDir);
  511. }
  512. return SelecDir;
  513. }
  514. private void EraseLog_Click(object sender, EventArgs e)
  515. {
  516. LogTxt.Clear();
  517. }
  518. // 프로그램 종료시 이벤트
  519. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  520. {
  521. LogFileCreate LFC = new LogFileCreate();
  522. if (MessageBox.Show("프로그램을 종료하시겠습니까?", "종료", MessageBoxButtons.YesNo) != DialogResult.Yes)
  523. {
  524. LFC.Log("사용자에 의해 프로그램 종료가 취소되었습니다. = " + Convert.ToString(DateTime.Now));
  525. e.Cancel = true;
  526. }
  527. else
  528. {
  529. LFC.Log("사용자에 의해 프로그램이 종료되었습니다. = " + Convert.ToString(DateTime.Now));
  530. }
  531. }
  532. private void PointSetBtn_Click(object sender, EventArgs e)
  533. {
  534. //iCosFourDBSET();
  535. ReadTag();
  536. //UnReg();
  537. }
  538. private void Conn_iCos_Btn_Click(object sender, EventArgs e)
  539. {
  540. //Thread thread_tLS = new Thread(threadLoginSuccess);
  541. //thread_tLS.IsBackground = true;
  542. //thread_tLS.Start();
  543. //Thread thread_tLF = new Thread(threadLoginFail);
  544. //thread_tLF.IsBackground = true;
  545. //thread_tLF.Start();
  546. //Thread thread_tCF = new Thread(threadConnectFail);
  547. //thread_tCF.IsBackground = true;
  548. //thread_tCF.Start();
  549. LogFileCreate LFC = new LogFileCreate();
  550. string ReturnValue = iCosFourConn();
  551. iCosFourReadDB();
  552. LFC.Log("접속 함수 동작 시작 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  553. LogTxt.Text += "접속 함수 동작 시작 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
  554. if (ReturnValue == "SUCCESS")
  555. {
  556. LogTxt.Text += "접속 성공 = " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine;
  557. PointSetBtn.Enabled = true;
  558. PointUnRegBtn.Enabled = true;
  559. ProStartBtn.Enabled = true;
  560. }
  561. SetDeviceDB(0, "");
  562. }
  563. //서로 다른 두 스레드(ResponseFunc와 Form)의 충돌을 피하기 위해 델리게이트 사용.
  564. //ResponseFunc에서 오류 발생시 델리게이트를 통해 Log를 출력함
  565. delegate void CrossThreadSafetySetErrorCodeTxt(string text);
  566. private void SetText(string text)
  567. {
  568. if (this.LogTxt.InvokeRequired)
  569. {
  570. CrossThreadSafetySetErrorCodeTxt d = new CrossThreadSafetySetErrorCodeTxt(SetText);
  571. this.Invoke(d, text);
  572. //this.Invoke(d, text);
  573. }
  574. else
  575. {
  576. this.LogTxt.Text = text;
  577. }
  578. }
  579. //void threadLoginSuccess(object state)
  580. //{
  581. // //int count = 0;
  582. // while (ewhLoginSuccess.WaitOne()) // 이벤트 대기
  583. // {
  584. // ewhLoginSuccess.Reset(); // ManualReset으로 설정했으므로 Reset 명시
  585. // this.SetText("로그인 성공 = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  586. // }
  587. //}
  588. //void threadLoginFail(object state)
  589. //{
  590. // //int count = 0;
  591. // while (ewhLoginFail.WaitOne()) // 이벤트 대기
  592. // {
  593. // ewhLoginFail.Reset(); // ManualReset으로 설정했으므로 Reset 명시
  594. // this.SetText("로그인 실패 = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  595. // }
  596. //}
  597. //void threadConnectFail(object state)
  598. //{
  599. // //int count = 0;
  600. // while (ewhDisconnect.WaitOne()) // 이벤트 대기
  601. // {
  602. // ewhDisconnect.Reset(); // ManualReset으로 설정했으므로 Reset 명시
  603. // this.SetText("연결 종료 = " + Convert.ToString(DateTime.Now) + Environment.NewLine);
  604. // }
  605. //}
  606. private void PointUnRegBtn_Click(object sender, EventArgs e)
  607. {
  608. UnReg();
  609. }
  610. private void Btn_TagSetFile_Click(object sender, EventArgs e)
  611. {
  612. Creating_Setting_File CSF = new Creating_Setting_File();
  613. CSF.Create_Point_File();
  614. }
  615. private void Btn_SettingFile_Click(object sender, EventArgs e)
  616. {
  617. Creating_Setting_File CSF = new Creating_Setting_File();
  618. CSF.Create_Setting_Point_File();
  619. }
  620. private void AllValueCheck_btn_Click(object sender, EventArgs e)
  621. {
  622. string[] sArray = File.ReadAllLines("TagSetFile.csv", Encoding.Default);
  623. LogTxt.Text += "*********** 현재 시간 : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "**********" + Environment.NewLine;
  624. try
  625. {
  626. for (int i = 0; i < sArray.Length; i++) //
  627. {
  628. string[] tmp = sArray[i].Split(','); // tmp[0] = index, tmp[1] = Tag명.
  629. LogTxt.Text += tmp[1] + " 의 값 : " + GetTagValue(tmp[1]) + Environment.NewLine;
  630. }
  631. LogTxt.Text += "**************************************************************" + Environment.NewLine;
  632. //LogTxt.Text += "일괄 조회 종료";
  633. }
  634. catch (Exception er)
  635. {
  636. MessageBox.Show("오류 발생 = " + er);
  637. }
  638. }
  639. }
  640. }