9fe15df75e1f67fbf215cfe6d31cbf92daba325e.svn-base 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using Microsoft.Win32;
  10. using System.Diagnostics;
  11. // 2015.8.20
  12. // 시운전하고 , 프로젝트를 관리하는 사람들이 사용하도록 프로그램을 구성한다
  13. // 닷넷프레임워크는 2.0 (이전 버전도 적용해야되므로)
  14. // 1. 현재 동작중인 프로그램 종료
  15. // 2. 이벤트로그 데이타 삭제
  16. // 3. 레지스트리값에 최신 작업내역을 쓰고, 세부 내용은 별도의 텍스트 로그로 남겨놓는다
  17. // 2016.11.24
  18. // 수신기 형식 인증을 위해 아래와 같이 프로그램을 준비한다
  19. // 시운전프로그램 준비 -> 동글인식해야 프로그램가동 + 데이타베이스 교체 -> 로그인 (사용자계정이 별도로 있음) -> 사유기록-> 사용기록파일을 남기도록 함
  20. // 2016.11.29
  21. // 동글키를 인식하기 위한 dll 은 Ry4SCom.dll 이다. CIL 을 이용하는 것같다. 반드시 regsvr32 명령을 이용해 CLR 등록하도록 한다
  22. namespace IControls_FireManager
  23. {
  24. public partial class Main : Form
  25. {
  26. // 타이머 카운터
  27. int MainTimerTick_Cnt = 0;
  28. // 로그인 정보
  29. string User = null;
  30. // 폴더 정보
  31. string Receiver_Project_Path = null; // Receiver1
  32. string Usb_Update_Path = Environment.CurrentDirectory + "\\Update";
  33. // 로그 내용
  34. string txt = null;
  35. public Main(string UserID)
  36. {
  37. InitializeComponent();
  38. User = UserID;
  39. // 참고 : 라디오버튼 그룹핑은 tapstop -> true~false 로 이용하면 됨
  40. // 이벤트로그삭제는 전체 삭제가 기본값
  41. this.radioButton_EventLogDel_All.Checked = true;
  42. try
  43. {
  44. // 레지스트리값 읽기
  45. RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\I_FPER_COMM_DAEMON", false);
  46. string Temp_DATABASE_NAME = rk.GetValue("DATABASE_NAME").ToString();
  47. string Registry_DATABASE_NAME = Temp_DATABASE_NAME;
  48. string Registry_DATABASE_NAME_IP = Temp_DATABASE_NAME.Substring(0, Temp_DATABASE_NAME.IndexOf(":"));
  49. string Registry_DATABASE_NAME_PATH = Temp_DATABASE_NAME.Substring(Temp_DATABASE_NAME.IndexOf(":") + 1);
  50. string Registry_Project_Directory = rk.GetValue("PROJECT_DIR").ToString();
  51. _Db.DB_FullPath = Registry_DATABASE_NAME_PATH;
  52. }
  53. catch
  54. {
  55. ;
  56. }
  57. }
  58. // 이벤트 로그 삭제 여부
  59. private void checkBox_EventLogDel_CheckedChanged(object sender, EventArgs e)
  60. {
  61. this.radioButton_EventLogDel_All.Enabled = this.radioButton_EventLogDel_Day.Enabled = this.checkBox_EventLogDel.Checked;
  62. this.dateTimePicker1.Enabled = this.dateTimePicker2.Enabled = this.radioButton_EventLogDel_Day.Checked;
  63. }
  64. // 시간지정시에만 활성화
  65. private void radioButton_EventLogDel_Day_CheckedChanged(object sender, EventArgs e)
  66. {
  67. this.dateTimePicker1.Enabled = this.dateTimePicker2.Enabled = this.radioButton_EventLogDel_Day.Checked;
  68. }
  69. // 시작 버튼을 누름
  70. private void button_Start_Click(object sender, EventArgs e)
  71. {
  72. // 로그 내용
  73. txt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " [" + User + "] \r\n";
  74. if (this.textBox_Comment.Text.Trim().Length == 0)
  75. {
  76. MessageBox.Show("반드시 사유를 입력 하셔야됩니다");
  77. return;
  78. }
  79. if (this.checkBox_FileUpdate.Checked == false && this.checkBox_EventLogDel.Checked == false)
  80. {
  81. MessageBox.Show("최소한 하나 이상은 선택하셔야됩니다");
  82. return;
  83. }
  84. txt = txt + string.Format(" 사유 : {0}\r\n", this.textBox_Comment.Text);
  85. // 만약 파일업데이트가 체크되어있다면 실행파일 경로에 반드시 DB 파일이나 실행파일이 존재해야한다.
  86. if (this.checkBox_FileUpdate.Checked == true)
  87. {
  88. string[] files = Directory.GetFiles(Usb_Update_Path);
  89. string[] folders = Directory.GetDirectories(Usb_Update_Path);
  90. if (files.Length == 0 && folders.Length == 0)
  91. { MessageBox.Show("업데이트 파일이 존재하지 않습니다"); return; }
  92. }
  93. // 타이머 동작
  94. MainTimerTick_Cnt = 0;
  95. _Timer_WindowBase.Delete("Main_Timer");
  96. _Timer_WindowBase.Create("Main_Timer", 1000, Main_Timer_Tick, true);
  97. }
  98. // 타이머
  99. private void Main_Timer_Tick(object sender, EventArgs e)
  100. {
  101. try
  102. {
  103. // UI 갱신
  104. this.label_Status.Text = string.Format("진행상황 : {0} 초 남았습니다.", 35 - MainTimerTick_Cnt);
  105. // 카운터 증사
  106. MainTimerTick_Cnt++;
  107. // 카운터별 동작
  108. switch (MainTimerTick_Cnt)
  109. {
  110. // 비활성화
  111. case 1:
  112. this.checkBox_EventLogDel.Enabled = false;
  113. this.checkBox_FileUpdate.Enabled = false;
  114. this.checkBox_restart.Enabled = false;
  115. this.textBox_Comment.Enabled = false;
  116. this.button_Start.Enabled = false;
  117. break;
  118. // 런쳐 종료
  119. case 2:
  120. if (_Diagnostics.Process_Excute("FLauncher") == true)
  121. _Diagnostics.Process_Delete("FLauncher", false);
  122. break;
  123. // 수신기 UI 프로그램 종료
  124. case 4:
  125. if (_Diagnostics.Process_Excute("FPER") == true)
  126. _Diagnostics.Process_Delete("FPER", false);
  127. break;
  128. // 데몬 종료
  129. case 7:
  130. if (_Diagnostics.Process_Excute("CommDaemon") == true)
  131. _Diagnostics.Process_Delete("CommDaemon", false);
  132. break;
  133. // FireBirdLog 지우기
  134. case 10:
  135. string FireBirdLogPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  136. FireBirdLogPath += "\\Firebird\\Firebird_2_5\\firebird.log";
  137. if (File.Exists(FireBirdLogPath) == true)
  138. File.Delete(FireBirdLogPath);
  139. break;
  140. // 레지스트리값 읽고 DB 접근자 알아낸뒤, 이벤트 로그 삭제
  141. case 13:
  142. // 이벤트 로그 삭제
  143. if (this.checkBox_EventLogDel.Checked == true)
  144. {
  145. txt = txt + " EventLogDel ";
  146. if (this.radioButton_EventLogDel_All.Checked == true)
  147. _Db.Execute(_Sql.Delete_Table("TB_EVENT_LOG", null));
  148. else
  149. { // 날짜 : 2015 08 20 00000000000
  150. // 2015 08 21 99999999999
  151. string start = this.dateTimePicker1.Value.ToShortDateString().Replace("-", "") + "00000000000";
  152. string end = this.dateTimePicker2.Value.ToShortDateString().Replace("-", "") + "99999999999";
  153. // 이벤트 로그 삭제
  154. _Db.Execute(_Sql.DeleteEventLogDataTime(start, end));
  155. // 커맨드 로그 삭제
  156. _Db.Execute(_Sql.DeleteCommandLogData(start, end));
  157. }
  158. }
  159. break;
  160. // DB 폴더에 FPER.FDB 파일외에는 전부 제거
  161. case 16:
  162. string folder = _Db.DB_FullPath.Replace("FPER.FDB", "");
  163. // DB 폴더에 접근
  164. string[] files = Directory.GetFiles(folder);
  165. foreach (string file in files)
  166. {
  167. if (_Db.DB_FullPath != file)
  168. {
  169. File.Delete(file);
  170. }
  171. }
  172. break;
  173. // 덮어쓰기
  174. case 19:
  175. {
  176. // 프로젝트 파일 경로 (Receiver1 폴더에 해당하겠다)
  177. Receiver_Project_Path = _Db.DB_FullPath.Replace("DB\\FPER.FDB", "");
  178. // 파일이 있는지 확인
  179. string[] files_temp = Directory.GetFiles(Usb_Update_Path);
  180. if (files_temp.Length != 0)
  181. {
  182. Copy_Folder(Usb_Update_Path, Receiver_Project_Path);
  183. }
  184. // 폴더가 있는지 확인
  185. string[] folders_temp = Directory.GetDirectories(Usb_Update_Path);
  186. if (folders_temp.Length != 0)
  187. {
  188. foreach (string folders in folders_temp)
  189. {
  190. string temp = Get_Folder(folders);
  191. if (temp == "DB")
  192. {
  193. string[] files_temp_db = Directory.GetFiles(Usb_Update_Path + "\\" + temp);
  194. if (files_temp_db.Length != 0)
  195. {
  196. Copy_Folder(Usb_Update_Path + "\\" + temp, Receiver_Project_Path + "\\" + temp);
  197. }
  198. }
  199. else if (temp == "mapview")
  200. {
  201. string[] folders_temp_mapview = Directory.GetDirectories(Usb_Update_Path + "\\" + temp);
  202. if (folders_temp_mapview.Length != 0)
  203. {
  204. foreach (string folders_temp_mapviews in folders_temp_mapview)
  205. {
  206. string folder_name = Get_Folder(folders_temp_mapviews);
  207. Copy_Folder(Usb_Update_Path + "\\" + temp + "\\" + folder_name, Receiver_Project_Path + "\\" + temp + "\\" + folder_name);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. break;
  215. // 프로그램 종료
  216. case 35:
  217. try
  218. {
  219. if (this.checkBox_EventLogDel.Checked == true)
  220. {
  221. txt += " EventLogDel ";
  222. if (this.radioButton_EventLogDel_All.Checked == true)
  223. txt += "LogDel:All";
  224. else
  225. txt += "LogDel:Day[" + this.dateTimePicker1.Value.ToShortDateString() + "~" + this.dateTimePicker2.Value.ToShortDateString() + "]";
  226. }
  227. // 레지스트리 값에 최근 사용내역 저장
  228. string history = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " >> Excute ";
  229. RegistryKey Set_Rk = Registry.LocalMachine.CreateSubKey("SOFTWARE").CreateSubKey("I_FPER_COMM_DAEMON");
  230. Set_Rk.SetValue("FireOperator", history);
  231. // 로그를 저장하도록함
  232. Write_Log("\r\n" + txt);
  233. }
  234. catch
  235. {
  236. // 로그를 저장하도록함
  237. Write_Log("\r\n" + "레지스트리접근권한 실패");
  238. }
  239. // 종료후 자동으로 프로그램 재실행
  240. if (this.checkBox_restart.Checked == true)
  241. {
  242. Process myProcess = new Process();
  243. myProcess.StartInfo.WorkingDirectory = Receiver_Project_Path;
  244. myProcess.StartInfo.FileName = "FLauncher.exe";
  245. //myProcess.StartInfo.CreateNoWindow = false;
  246. myProcess.Start();
  247. }
  248. // UI 처리완료알림
  249. this.label_Status.Text = string.Format("진행상황 : 처리완료되었습니다.");
  250. // 타이머 종료
  251. _Timer_WindowBase.Delete("Main_Timer");
  252. // 활성화
  253. this.checkBox_EventLogDel.Enabled = true;
  254. this.checkBox_FileUpdate.Enabled = true;
  255. this.checkBox_restart.Enabled = true;
  256. this.textBox_Comment.Enabled = true;
  257. this.button_Start.Enabled = true;
  258. break;
  259. default:
  260. if (MainTimerTick_Cnt < 0)
  261. {
  262. // 종료후 자동으로 프로그램 재실행
  263. if (this.checkBox_restart.Checked == true)
  264. {
  265. Process myProcess = new Process();
  266. myProcess.StartInfo.WorkingDirectory = Receiver_Project_Path;
  267. myProcess.StartInfo.FileName = "FLauncher.exe";
  268. //myProcess.StartInfo.CreateNoWindow = false;
  269. myProcess.Start();
  270. }
  271. // UI 처리완료알림
  272. this.label_Status.Text = string.Format("진행상황 : 처리완료되었습니다.");
  273. // 타이머 종료
  274. _Timer_WindowBase.Delete("Main_Timer");
  275. // 활성화
  276. this.checkBox_EventLogDel.Enabled = true;
  277. this.checkBox_FileUpdate.Enabled = true;
  278. this.checkBox_restart.Enabled = true;
  279. this.textBox_Comment.Enabled = true;
  280. this.button_Start.Enabled = true;
  281. }
  282. break;
  283. }
  284. }
  285. catch
  286. {
  287. ;
  288. }
  289. }
  290. public static void Write_Log(string txt)
  291. {
  292. try
  293. {
  294. string path = "C:\\fireguard\\Log.txt";
  295. if (!File.Exists(path))
  296. {
  297. // Create a file to write to.
  298. string createText = txt;// Environment.NewLine;
  299. File.WriteAllText(path, createText);
  300. }
  301. else
  302. {
  303. // This text is always added, making the file longer over time
  304. // if it is not deleted.
  305. string appendText = txt;
  306. File.AppendAllText(path, appendText);
  307. }
  308. }
  309. catch
  310. {
  311. ;
  312. }
  313. }
  314. // 프로세스 종료 코드 추가
  315. private void Main_FormClosed(object sender, FormClosedEventArgs e)
  316. {
  317. // 스레드 종료
  318. Application.ExitThread();
  319. for (int i = 0; i < 1000000; i++) { ;}
  320. // 프로세스 종료
  321. Application.Exit();
  322. // 프로세스가 남아있다면 최종 종료
  323. System.Diagnostics.Process[] mProcess = System.Diagnostics.Process.GetProcessesByName(Application.ProductName);
  324. foreach (System.Diagnostics.Process p in mProcess)
  325. p.Kill();
  326. // 최종 정리 (보완코드 : 크로스 스레드 문제 발생 여지가 있으므로 사용주의)
  327. Environment.Exit(0);
  328. }
  329. // 폴더명 가져오기
  330. public string Get_Folder(string path)
  331. {
  332. try
  333. {
  334. string[] foldernames = path.Split('\\');
  335. if (foldernames.Length != 0)
  336. {
  337. return foldernames[foldernames.Length - 1];
  338. }
  339. else
  340. return "";
  341. }
  342. catch
  343. {
  344. return null;
  345. }
  346. }
  347. // 폴더 복사 및 로그 생성
  348. public void Copy_Folder(string sourcePath, string targetPath)
  349. {
  350. // 소스 폴더 -> DB폴더의 파일을 모두 가져옴
  351. string[] files = System.IO.Directory.GetFiles(sourcePath);
  352. // 타겟 폴더 -> DB폴더로 파일을 하나씩 복사
  353. foreach (string file in files)
  354. {
  355. // 타켓 폴더가 없으면 폴더 생성
  356. if (!System.IO.Directory.Exists(targetPath))
  357. System.IO.Directory.CreateDirectory(targetPath);
  358. string fileName = System.IO.Path.GetFileName(file);
  359. string destfile = System.IO.Path.Combine(targetPath, fileName);
  360. System.IO.File.Copy(file, destfile, true);
  361. if (fileName == "FPER.FDB")
  362. {
  363. txt += string.Format("FDB Upate : {0} -> {1}\r\n",
  364. File.GetCreationTime(file),
  365. File.GetCreationTime(destfile)
  366. );
  367. }
  368. else if (fileName == "CommDaemon.exe")
  369. {
  370. txt += string.Format("CommDaemon Upate : {0} -> {1}\r\n",
  371. FileVersionInfo.GetVersionInfo(file).FileVersion,
  372. FileVersionInfo.GetVersionInfo(destfile).FileVersion
  373. );
  374. }
  375. else if (fileName == "FPER.exe")
  376. {
  377. txt += string.Format("FPER Upate : {0} -> {1}\r\n",
  378. FileVersionInfo.GetVersionInfo(file).FileVersion,
  379. FileVersionInfo.GetVersionInfo(destfile).FileVersion
  380. );
  381. }
  382. else if (fileName == "FLauncher.exe")
  383. {
  384. txt += string.Format("FLauncher Upate : {0} -> {1}\r\n",
  385. FileVersionInfo.GetVersionInfo(file).FileVersion,
  386. FileVersionInfo.GetVersionInfo(destfile).FileVersion
  387. );
  388. }
  389. txt += string.Format("File Upate : {0}\r\n", destfile);
  390. }
  391. }
  392. }
  393. }