_Data.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Net.NetworkInformation;
  7. using System.Collections;
  8. using System.Runtime.InteropServices;
  9. using System.Drawing;
  10. using Microsoft.Win32;
  11. using System.Net.Sockets;
  12. using System.Threading;
  13. using System.Net; // cyim 2016.12.26 : UI 이탈방지 코드 추가 (후킹)
  14. namespace FPER
  15. {
  16. // cyim 2015.6.24 소스 정리
  17. // 공용으로 사용하는 클래스와 변수를 _Data.cs 에서 사용한다
  18. // 기존에 Static 으로 구성된 것은 어쩔수없지만, 기존 개발자가 만든 파일들을 모두 정리하는 것은 불가능하다
  19. // 공용 변수
  20. public static class _Data
  21. {
  22. // cyim 2016.12.26 : UI 이탈방지 코드 추가 (후킹)
  23. public static string Hook_Enable = null;
  24. // cyim 2016.12.20 : 현장명 사용자 지정
  25. public static string Site_Name = null;
  26. // cyim 2015.7.29 수신반 모드
  27. // 수신반 모드 여부 (0:평상시모드, 1:수신반모드)
  28. // 수신반 모드에서는 레지스트리 값을 이용하지 않고 데이터베이스에서 읽은 값을 토대로 셋팅된다
  29. public static bool FireDeskMode = false;
  30. // 수신기 아이디 해쉬테이블 (key : id, data : class type - FireDeskReceiver)
  31. public static Hashtable Hash_Receiver = new Hashtable();
  32. // 수신기 아이디 배열
  33. public static string[] Hash_Receiver_keys = null;
  34. // cyim 2015.7.29 수신반 루틴을 위해 외부 파라미터로 동작하도록 수정 : 레지스트리값을 여기서 다 읽고 처리함 (1회만 읽으면 된다)
  35. public static int Registry_ReceiverID = 0;
  36. public static string Registry_DATABASE_NAME = null; // IP + PATH
  37. public static string Registry_DATABASE_NAME_IP = null; // IP
  38. public static string Registry_DATABASE_NAME_PATH = null; // PATH
  39. public static bool Registry_MonitorDaemon_Enable = false; // 모니터데몬 상태 (1이면 네트워크 수신기)
  40. public static string Registry_MonitorDaemon_IP = null; // 모니터데몬 아이피
  41. public static string Registry_Project_Directory = null; // 프로젝트 폴더 경로 : 예) D:\FireTest
  42. // cyim 2015.8.3 수신반 연동기능 추가 : 수신반 운영모드 팝업창
  43. public static bool FireDesk_Operation_Enable = false;
  44. // cyim 2015.9.1 통합조회창기능 추가
  45. public static bool FireDesk_FormLogView_Enable = false;
  46. // cyim 2015.9.11 : 과거내역 통합조회창에서 사용할 DB 에서 미리 읽은값을 사용하도록 수정함
  47. public static DataTable dtBoardList = null;
  48. public static DataTable dtRepeaterList2 = null;
  49. public static DataTable dtAnalogDetecterLevel = null;
  50. public static DataTable dtDeviceDBList2 = null;
  51. // cyim 2016.12.16 : 모바일앱 연동
  52. public static string MOBILE_ENABLE = null;
  53. // cyim 2016.12.20 : 푸시메세지 연동 기능 추가
  54. public static string GCM_ENABLE = null;
  55. public static string GCM_API_KEY = null;
  56. public static string GCM_TEST_MODE = null;
  57. public static string GCM_INFO_FIRE = null; //; 화재
  58. public static string GCM_INFO_FACILITY = null; //; 설비
  59. public static string GCM_INFO_COMERROR = null; //; 통신에러
  60. public static string GCM_INFO_BLOCK = null; //; 단선
  61. public static string GCM_INFO_CUT = null; //; 차단
  62. public static string GCM_INFO_CHECK = null; //; 점검
  63. // 디버깅 창 열림 확인
  64. public static bool Enable_Form_Debug = false;
  65. // 로그 표시
  66. public static bool Enable_Log = false;
  67. }
  68. //
  69. // 추가 클래스는 아래에 정리된다
  70. //
  71. // cyim 2015.8.4 수신반을 위한 static 클래스 정리
  72. ////
  73. //// cyim 2014.5.7 : 수신기설정정보 변경 (하단 주석처리하고 변수는 전역으로 처리한다)
  74. ////
  75. ////public static class _frmReceiverConfig_Data
  76. ////{
  77. //// // cyim 2014.5.7 : 수신기설정정보 변경
  78. //// //public static Boolean receiverConfig_Change_Flag = false;
  79. ////}
  80. //
  81. // cyim 2014.5.8 : 프로세스 관리 클래스 추가
  82. //
  83. public static class _Diagnostics
  84. {
  85. public static void Process_Kill(string ProcessName)
  86. {
  87. try
  88. {
  89. if (ProcessName != null)
  90. {
  91. System.Diagnostics.Process[] mProcess = System.Diagnostics.Process.GetProcessesByName(ProcessName);
  92. foreach (System.Diagnostics.Process p in mProcess)
  93. p.Kill();
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. Util.UErrorMessage(ex, 0, 0);
  99. }
  100. }
  101. //
  102. // cyim 2017.01.23 : Explorer 프로세스 제어 루틴 추가
  103. //
  104. public static bool Process_Status(string ProcessName)
  105. {
  106. try
  107. {
  108. if (ProcessName != null)
  109. {
  110. System.Diagnostics.Process[] mProcess = System.Diagnostics.Process.GetProcessesByName(ProcessName);
  111. foreach (System.Diagnostics.Process p in mProcess)
  112. return true;
  113. }
  114. return false;
  115. }
  116. catch (Exception ex)
  117. {
  118. Util.UErrorMessage(ex, 0, 0);
  119. return false;
  120. }
  121. }
  122. public static void taskkill_explorer()
  123. {
  124. string Explorer_Path = null;
  125. Explorer_Path = "C:\\Windows\\explorer.exe";
  126. if (_Diagnostics.Process_Status("explorer") == true)
  127. {
  128. if (System.IO.File.Exists(Explorer_Path) == true)
  129. {
  130. System.Diagnostics.Process proc = new System.Diagnostics.Process();
  131. proc.EnableRaisingEvents = false;
  132. proc.StartInfo.FileName = "taskkill.exe";
  133. proc.StartInfo.Arguments = "/f /im explorer.exe";
  134. proc.Start();
  135. proc.Close();
  136. }
  137. }
  138. }
  139. public static void start_explorer()
  140. {
  141. string Explorer_Path = null; Explorer_Path = "C:\\Windows\\explorer.exe";
  142. if (_Diagnostics.Process_Status("explorer") == false)
  143. {
  144. if (System.IO.File.Exists(Explorer_Path) == true)
  145. {
  146. System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
  147. myProcess.StartInfo.FileName = Explorer_Path;
  148. myProcess.StartInfo.CreateNoWindow = false;
  149. myProcess.Start();
  150. myProcess.Close();
  151. }
  152. }
  153. }
  154. }
  155. //
  156. // cyim 2015.7.22 : 통신데몬 연결 핑테스트
  157. //
  158. public class _Ethernet
  159. {
  160. public PingOptions options = new PingOptions(); // cyim 2017.01.02 : Memory leak
  161. public bool Ping_SyncCheck(string DestIP)
  162. {
  163. try
  164. {
  165. if (DestIP == null || DestIP.Trim().Length == 0) return false;
  166. using (Ping pingSender = new Ping()) // cyim 2017.01.02 : Memory leak
  167. {
  168. options.DontFragment = true;
  169. string data = "0";
  170. byte[] buffer = Encoding.ASCII.GetBytes(data);
  171. PingReply reply = pingSender.Send(DestIP, 400, buffer, options); // cyim 2016.12.29 : 화면 멈춤 문제 수정
  172. if (reply.Status == IPStatus.Success)
  173. {
  174. return true;
  175. }
  176. else
  177. {
  178. return false;
  179. }
  180. }
  181. }
  182. catch
  183. {
  184. return false;
  185. }
  186. }
  187. // 비동기 코드 테스트
  188. public void Ping_Async(string DestIP)
  189. {
  190. AutoResetEvent waiter = new AutoResetEvent(false);
  191. Ping pingSender = new Ping();
  192. pingSender.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
  193. IPAddress address = IPAddress.Parse(DestIP);
  194. string data = "0";
  195. byte[] buffer = Encoding.ASCII.GetBytes(data);
  196. int timeout = 1000;
  197. PingOptions options = new PingOptions(64, true);
  198. pingSender.SendAsync(address, timeout, buffer, options, waiter);
  199. waiter.WaitOne();
  200. Console.WriteLine("Ping example completed.");
  201. }
  202. public void PingCompletedCallback(object sender, PingCompletedEventArgs e)
  203. {
  204. if (e.Cancelled)
  205. {
  206. Console.WriteLine("Ping canceled.");
  207. // 주 스레드가 진행되게 한다.
  208. // 유저토큰은 주 스레드가 대기하고 있는 AutoResetEvent 객체이다.
  209. ((AutoResetEvent)e.UserState).Set();
  210. }
  211. // 에러가 발생하면 메세지가 표시된다.
  212. if (e.Error != null)
  213. {
  214. Console.WriteLine("Ping failed:");
  215. Console.WriteLine(e.Error.ToString());
  216. // 주 스레드가 진행되게 한다.
  217. ((AutoResetEvent)e.UserState).Set();
  218. }
  219. PingReply reply = e.Reply;
  220. DisplayReply(reply);
  221. // 주 스레드가 진행되게 한다.
  222. ((AutoResetEvent)e.UserState).Set();
  223. }
  224. public void DisplayReply(PingReply reply)
  225. {
  226. if (reply == null)
  227. return;
  228. Console.WriteLine("ping status: {0}", reply.Status);
  229. if (reply.Status == IPStatus.Success)
  230. {
  231. //Console.WriteLine("Address: {0}", reply.Address.ToString());
  232. //Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
  233. //Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
  234. //Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
  235. //Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
  236. }
  237. }
  238. }
  239. //
  240. // 공용 변환 함수
  241. //
  242. public static class _Convert
  243. {
  244. // 코딩 방식
  245. public enum Coding
  246. {
  247. Default, UTF8, ASCII
  248. }
  249. // 인코딩
  250. public static byte[] EncodingData(Coding coding, string Data)
  251. {
  252. byte[] data = null;
  253. switch (coding)
  254. {
  255. case Coding.Default:
  256. data = Encoding.Default.GetBytes(Data);
  257. break;
  258. case Coding.UTF8:
  259. data = Encoding.UTF8.GetBytes(Data);
  260. break;
  261. case Coding.ASCII:
  262. data = Encoding.ASCII.GetBytes(Data);
  263. break;
  264. }
  265. return data;
  266. }
  267. // 디코딩
  268. public static string DecodingData(Coding coding, byte[] Data)
  269. {
  270. string data = null;
  271. switch (coding)
  272. {
  273. case Coding.Default:
  274. data = Encoding.Default.GetString(Data);
  275. break;
  276. case Coding.UTF8:
  277. data = Encoding.UTF8.GetString(Data);
  278. break;
  279. case Coding.ASCII:
  280. data = Encoding.ASCII.GetString(Data);
  281. break;
  282. }
  283. return data;
  284. }
  285. // 어레이리스트를 구분자로 삽입하여 표시
  286. public static string ArrayList_to_String(ArrayList Datas, string spilt)
  287. {
  288. if (Datas == null || Datas.Count == 0) return "";
  289. string Temp = null;
  290. foreach (string Data in Datas)
  291. {
  292. Temp = Temp + Data + spilt;
  293. }
  294. Temp = Temp.TrimEnd(Convert.ToChar(spilt));
  295. return Temp;
  296. }
  297. }
  298. // cyim 2014.7.23 마우스 이벤트 강제로 발생
  299. public static class _MouseEvent
  300. {
  301. private const uint LBUTTONDOWN = 0x00000002; // 왼쪽 마우스 버튼 누름
  302. private const uint LBUTTONUP = 0x00000004; // 왼쪽 마우스 버튼 땜
  303. [DllImport("user32.dll")]
  304. static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, int dwExtraInfo);
  305. [DllImport("user32.dll")]
  306. static extern int SetCursorPos(int x, int y);
  307. // 특정 좌표에 클릭을 발생시킨다
  308. public static void Click(int x, int y)
  309. {
  310. Point pt = new Point(x, y);
  311. SetCursorPos(pt.X, pt.Y);
  312. mouse_event(LBUTTONDOWN, (uint)pt.X, (uint)pt.Y, 0, 0);
  313. mouse_event(LBUTTONUP, (uint)pt.X, (uint)pt.Y, 0, 0);
  314. }
  315. }
  316. // cyim 2016.12.26 : UI 이탈방지 코드 추가 (후킹)
  317. public static class _Hook
  318. {
  319. public static void Enable_TaskMgr()
  320. {
  321. try
  322. {
  323. string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
  324. RegistryKey rk = Registry.CurrentUser;
  325. RegistryKey sk1 = rk.OpenSubKey(subKey);
  326. if (sk1 != null)
  327. rk.DeleteSubKeyTree(subKey);
  328. rk.Close();
  329. }
  330. catch
  331. {
  332. ;
  333. }
  334. }
  335. public static void Disable_TaskMgr()
  336. {
  337. RegistryKey rk;
  338. string keyValueInt = "1";
  339. string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
  340. try
  341. {
  342. rk = Registry.CurrentUser.CreateSubKey(subKey);
  343. rk.SetValue("DisableTaskMgr", keyValueInt);
  344. rk.Close();
  345. }
  346. catch
  347. {
  348. ;
  349. }
  350. }
  351. // cyim 2017.01.24 : Alt+Ctrl+Del 화면 변경
  352. public static void Enable_NoClose()
  353. {
  354. try
  355. {
  356. RegistryKey rk = null;
  357. RegistryKey sk1 = null;
  358. rk = Registry.LocalMachine;
  359. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", true);
  360. if (sk1 != null) sk1.SetValue("NoClose", 0, RegistryValueKind.DWord);
  361. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  362. if (sk1 != null) sk1.SetValue("HideFastUserSwitching", 0, RegistryValueKind.DWord);
  363. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  364. if (sk1 != null) sk1.SetValue("DisableChangePassword", 0, RegistryValueKind.DWord);
  365. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  366. if (sk1 != null) sk1.SetValue("DisableLockWorkstation", 0, RegistryValueKind.DWord);
  367. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  368. if (sk1 != null) sk1.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  369. rk = Registry.CurrentUser;
  370. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", true);
  371. if (sk1 != null) sk1.SetValue("NoClose", 0, RegistryValueKind.DWord);
  372. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  373. if (sk1 != null) sk1.SetValue("HideFastUserSwitching", 0, RegistryValueKind.DWord);
  374. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  375. if (sk1 != null) sk1.SetValue("DisableChangePassword", 0, RegistryValueKind.DWord);
  376. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  377. if (sk1 != null) sk1.SetValue("DisableLockWorkstation", 0, RegistryValueKind.DWord);
  378. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  379. if (sk1 != null) sk1.SetValue("DisableTaskMgr", 0, RegistryValueKind.DWord);
  380. sk1.Close();
  381. rk.Close();
  382. }
  383. catch
  384. {
  385. ;
  386. }
  387. }
  388. public static void Disable_NoClose()
  389. {
  390. try
  391. {
  392. RegistryKey rk = null;
  393. RegistryKey sk1 = null;
  394. rk = Registry.LocalMachine;
  395. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", true);
  396. if (sk1 != null) sk1.SetValue("NoClose", 1, RegistryValueKind.DWord);
  397. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  398. if (sk1 != null) sk1.SetValue("HideFastUserSwitching", 1, RegistryValueKind.DWord);
  399. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  400. if (sk1 != null) sk1.SetValue("DisableChangePassword", 1, RegistryValueKind.DWord);
  401. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  402. if (sk1 != null) sk1.SetValue("DisableLockWorkstation", 1, RegistryValueKind.DWord);
  403. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  404. if (sk1 != null) sk1.SetValue("DisableTaskMgr", 1, RegistryValueKind.DWord);
  405. rk = Registry.CurrentUser;
  406. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer", true);
  407. if (sk1 != null) sk1.SetValue("NoClose", 1, RegistryValueKind.DWord);
  408. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  409. if (sk1 != null) sk1.SetValue("HideFastUserSwitching", 1, RegistryValueKind.DWord);
  410. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  411. if (sk1 != null) sk1.SetValue("DisableChangePassword", 1, RegistryValueKind.DWord);
  412. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  413. if (sk1 != null) sk1.SetValue("DisableLockWorkstation", 1, RegistryValueKind.DWord);
  414. sk1 = rk.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", true);
  415. if (sk1 != null) sk1.SetValue("DisableTaskMgr", 1, RegistryValueKind.DWord);
  416. sk1.Close();
  417. rk.Close();
  418. }
  419. catch
  420. {
  421. ;
  422. }
  423. }
  424. }
  425. //
  426. // 수신반 전용 클래스
  427. // 이 클래스는 범용으로 수신반에서 다른 수신기간의 데이터를 쉽게 공유할수 있게 만든 클래스이다.
  428. // 실제로 사용하는 데이터만 추가하도록 한다
  429. // 자세한 정보는 결국 직접 접속해서 볼수 있는 화면으로 구성한다
  430. public class _FireDesk_Receiver
  431. {
  432. // 수신기 기본정보
  433. public string RECEIVER_ID = "1";
  434. public string MASTER_FLAG = "N";
  435. public string PROJECT_DIRECTORY;
  436. public string DATABASE_NAME; // IP + PATH
  437. public string DATABASE_NAME_IP; // PATH
  438. public string DATABASE_NAME_PATH; // PATH
  439. // 카운터 정보
  440. public int FireDetect = 0; // 화재
  441. public int DeviceInput = 0; // 설비
  442. public int RepeaterError = 0; // 통신
  443. public int DeviceCut = 0; // 단선
  444. public int DeviceNotuse = 0; // 차단
  445. // 클래스 정보
  446. public bool LoadingOk = false;
  447. public MDIParent mdi = null;
  448. }
  449. // cyim 2016.12.29 : 화면 멈춤 문제 수정 : 스레드 처리
  450. public class _ConnectThread
  451. {
  452. public TcpClient socket = null;
  453. public int port = 0;
  454. public TimeSpan timeout = new TimeSpan();
  455. }
  456. // cyim 2017.01.03 : 연동기능 보완
  457. public class _Sort
  458. {
  459. // 일반 서브포트용
  460. public class ID : IComparer
  461. {
  462. int IComparer.Compare(object a, object b)
  463. {
  464. // 첫번째 데이타 검사
  465. int data1 = Int32.Parse(a.ToString());
  466. int data2 = Int32.Parse(b.ToString());
  467. // 비교
  468. if (data1 > data2)
  469. return 1;
  470. else if (data1 < data2)
  471. return -1;
  472. else
  473. return 0;
  474. }
  475. }
  476. }
  477. }