App.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Windows;
  3. using System.Text;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using log4net;
  7. using Quartz;
  8. using Quartz.Impl;
  9. using Agent.Services;
  10. using System.Threading;
  11. using System.Configuration;
  12. using Quartz.Impl.Matchers;
  13. namespace Agent
  14. {
  15. /// <summary>
  16. /// App.xaml에 대한 상호 작용 논리
  17. /// </summary>
  18. public partial class App : Application
  19. {
  20. public static readonly IScheduler Scheduler = StdSchedulerFactory.GetDefaultScheduler();
  21. public static readonly string _sysPlantCd = @ConfigurationManager.AppSettings["SysPlantCd"];
  22. public static readonly string _userCd = @ConfigurationManager.AppSettings["UserCd"];
  23. public static readonly string _equipCd = @ConfigurationManager.AppSettings["EquipmentCd"];
  24. public static readonly string _exceptExtension = @ConfigurationManager.AppSettings["ExceptExtension"];
  25. public static readonly string _dbServer = @ConfigurationManager.AppSettings["DbServer"];
  26. public static readonly string _dbName = @ConfigurationManager.AppSettings["DbName"];
  27. public static readonly string _dbUser = Decrypt(@ConfigurationManager.AppSettings["DbUser"], "ZR");
  28. public static readonly string _dbPwd = Decrypt(@ConfigurationManager.AppSettings["DbPwd"], "ZR");
  29. //string dbId = Decrypt(GetIni_Values("CONFIG", "dbId", ""), "ZR");
  30. //string dbPw = Decrypt(GetIni_Values("CONFIG", "dbPw", ""), "ZR");
  31. public static readonly string _myConnection = "Server=" + _dbServer + "; Database=" + _dbName + "; User Id=" + _dbUser + "; Password=" + _dbPwd + "; persist security info=true;packet size = 4096; Connect Timeout = 120";
  32. private static readonly ILog log = LogManager.GetLogger(typeof(App));
  33. private readonly string _programName = @ConfigurationManager.AppSettings["ProgramName"];
  34. private readonly HealthService _healthService = new HealthService();
  35. private readonly TrayService _trayService = new TrayService();
  36. private readonly PolicyService _policyService = new PolicyService();
  37. private readonly UserService _userService = new UserService();
  38. public static string _jobuserCd = @ConfigurationManager.AppSettings["UserCd"];
  39. private MainWindow _mainWindow;
  40. private Mutex _mutex;
  41. private void Application_Startup(object sender, StartupEventArgs e)
  42. {
  43. PreventDuplicatedApplication();
  44. if (!_healthService.IsConnectionSetting())
  45. {
  46. MessageBox.Show("서버 연결 정보가 없습니다.\r\n설정 파일을 확인해 주세요.", "오류", MessageBoxButton.OK, MessageBoxImage.Error);
  47. Shutdown();
  48. return;
  49. }
  50. _healthService.Check();
  51. if (!"OK".Equals(Current.Properties["db_status"]))
  52. {
  53. MessageBox.Show("서버와의 연결이 원활하지 않습니다.\r\n연결 상태를 확인해 주세요.", "오류", MessageBoxButton.OK, MessageBoxImage.Error);
  54. Shutdown();
  55. return;
  56. }
  57. var user = _userService.GetUserWithJoin();
  58. if (null == user)
  59. {
  60. MessageBox.Show("에이전트 정보 생성에 실패하였습니다.", "오류", MessageBoxButton.OK, MessageBoxImage.Error);
  61. Shutdown();
  62. }
  63. _userService.SaveUserProperties(user);
  64. _mainWindow = new MainWindow()
  65. {
  66. Title = _programName
  67. };
  68. _mainWindow.Show();
  69. _trayService.GetNotifyIcon().Visible = true;
  70. _trayService.GetNotifyIcon().DoubleClick += NotifyIcon_DoubleClick;
  71. Scheduler.ListenerManager.AddJobListener(_mainWindow, KeyMatcher<JobKey>.KeyEquals(_healthService.JobKey));
  72. _healthService.ScheduledHealthJob();
  73. _policyService.ScheduledPolicyJob();
  74. Scheduler.Start();
  75. log.Info($"{_programName} 시작 (UserTid={Current.Properties["user_tid"]})");
  76. }
  77. private void Application_Exit(object sender, ExitEventArgs e)
  78. {
  79. Scheduler.Shutdown();
  80. _trayService.DisposeNotifyIcon();
  81. log.Info($"{_programName} 종료 (UserTid={Current.Properties["user_tid"]})");
  82. }
  83. private void LoginWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  84. {
  85. if (null != Current.Properties["user_tid"])
  86. {
  87. var Main = new MainWindow();
  88. Main.Show();
  89. Scheduler.Start();
  90. }
  91. }
  92. private void NotifyIcon_DoubleClick(object sender, System.EventArgs e)
  93. {
  94. foreach (Window window in Current.Windows)
  95. {
  96. if (1 == Current.Windows.Count && _programName.Equals(window.Title))
  97. {
  98. window.Show();
  99. window.WindowState = WindowState.Normal;
  100. return;
  101. }
  102. }
  103. }
  104. private void PreventDuplicatedApplication()
  105. {
  106. try
  107. {
  108. _mutex = new Mutex(false, _programName);
  109. if (!_mutex.WaitOne(0, false))
  110. {
  111. MessageBox.Show($"{_programName}가 이미 실행중입니다.", "오류", MessageBoxButton.OK, MessageBoxImage.Error);
  112. Current.Shutdown();
  113. }
  114. }
  115. catch (Exception)
  116. {
  117. Current.Shutdown();
  118. }
  119. }
  120. public static string Decrypt(string cipherText, string Password)
  121. {
  122. try
  123. {
  124. byte[] cipherBytes = Convert.FromBase64String(cipherText);
  125. var pdb = new PasswordDeriveBytes(Password, new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
  126. 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
  127. });
  128. byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
  129. return Encoding.Unicode.GetString(decryptedData);
  130. }
  131. catch (FormatException e)
  132. {
  133. return "입력된 값이 정상적으로 암호화 되지 않은 값 입니다";
  134. }
  135. }
  136. public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
  137. {
  138. var ms = new MemoryStream();
  139. Rijndael alg = Rijndael.Create();
  140. alg.Key = Key;
  141. alg.IV = IV;
  142. var cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
  143. cs.Write(cipherData, 0, cipherData.Length);
  144. cs.Close();
  145. byte[] decryptedData = ms.ToArray();
  146. return decryptedData;
  147. }
  148. }
  149. }