RtApplication.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using Aitex.Common.Util;
  8. using Aitex.Core.Account;
  9. using Aitex.Core.Backend;
  10. using Aitex.Core.RT.ConfigCenter;
  11. using Aitex.Core.RT.DataCenter;
  12. using Aitex.Core.RT.DataCollection;
  13. using Aitex.Core.RT.DBCore;
  14. using Aitex.Core.RT.Event;
  15. using Aitex.Core.RT.IOCore;
  16. using Aitex.Core.RT.Job;
  17. using Aitex.Core.RT.Key;
  18. using Aitex.Core.RT.Log;
  19. using Aitex.Core.RT.OperationCenter;
  20. using Aitex.Core.RT.RecipeCenter;
  21. using Aitex.Core.RT.SCCore;
  22. using Aitex.Core.Util;
  23. using Aitex.Core.Utilities;
  24. using Aitex.Core.WCF;
  25. using Aitex.RT.FactoryAutomation;
  26. using Aitex.RT.Module;
  27. using Aitex.RT.Properties;
  28. using Aitex.Triton160.RT;
  29. using Aitex.Triton160.RT.Device;
  30. using Aitex.Triton160.RT.Module;
  31. using Aitex.Triton160.RT.WCF;
  32. namespace Aitex.RT
  33. {
  34. class RtApplication : ApplicationContext
  35. {
  36. private NotifyIcon _notifyIcon = new NotifyIcon();
  37. static Mutex _mutex;
  38. public RtApplication()
  39. {
  40. CheckAnotherInstanceRunning("Triton160.RT");
  41. if (!Init())
  42. {
  43. Environment.Exit(0);
  44. }
  45. InitIconDisplay();
  46. }
  47. ~RtApplication()
  48. {
  49. Terminate();
  50. }
  51. public bool Init()
  52. {
  53. try
  54. {
  55. Resources.Culture = new CultureInfo(CultureSupported.English);
  56. Singleton<LogManager>.Instance.Initialize();
  57. Singleton<ConfigManager>.Instance.Initialize();
  58. Singleton<DatabaseManager>.Instance.Initialize(
  59. System.Configuration.ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString
  60. , RtInstance.DbName
  61. , PathManager.GetCfgDir() + "SqlUpdate.sql");
  62. Singleton<EventManager>.Instance.Initialize(PathManager.GetCfgDir() + "EventDefine.xml", true, true,
  63. false, PathManager.GetCfgDir() + "RtEventDefine.xml");
  64. Singleton<OperationManager>.Instance.Initialize();
  65. Singleton<DataManager>.Instance.Initialize();
  66. RecipeFileManager.Instance.Initialize(new RtRecipeFileContext());
  67. Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
  68. {
  69. typeof (AccountService), typeof (InvokeService),
  70. typeof (QueryDataService), typeof (RecipeService)
  71. });
  72. if (!Singleton<SCManager>.Instance.Initialize())
  73. throw new Exception("SC System Initialize false.");
  74. UpdateCultureResource(((int)SC.GetItemValue(SCName.System_Language)) == 2 ? CultureSupported.Chinese : CultureSupported.English);
  75. LeakCheckResultManager.Instance.Initialize();
  76. string interlockFile = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\",
  77. RtInstance.InterlockFileName);
  78. Dictionary<string, string> ioFiles = new Dictionary<string, string>();
  79. Dictionary<string, int[]> plcIoBaseAddress = new Dictionary<string, int[]>();
  80. if (!string.IsNullOrEmpty(RtInstance.LocalIoDefineFileName))
  81. {
  82. ioFiles["local"] = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\", RtInstance.LocalIoDefineFileName);
  83. plcIoBaseAddress["local"] = new int[]
  84. {
  85. RtInstance.LocalPlcIoBaseDi, RtInstance.LocalPlcIoBaseDo,
  86. RtInstance.LocalPlcIoBaseAi, RtInstance.LocalPlcIoBaseAo
  87. };
  88. }
  89. if (!string.IsNullOrEmpty(RtInstance.RemoteIoDefineFileName))
  90. {
  91. ioFiles["remote"] = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\", RtInstance.RemoteIoDefineFileName);
  92. plcIoBaseAddress["remote"] = new int[]
  93. {
  94. RtInstance.RemotePlcIoBaseDi, RtInstance.RemotePlcIoBaseDo,
  95. RtInstance.RemotePlcIoBaseAi, RtInstance.RemotePlcIoBaseAo
  96. };
  97. }
  98. List<string> logicDi = new List<string>();
  99. foreach (var fieldInfo in typeof(LogicDi).GetFields())
  100. {
  101. logicDi.Add(fieldInfo.Name);
  102. }
  103. Singleton<IOGroupManager>.Instance.Initialize(ioFiles, interlockFile, plcIoBaseAddress, logicDi);
  104. Singleton<DeviceManager>.Instance.Initialize(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\",
  105. RtInstance.DeviceModelFileName), RtInstance.SystemName);
  106. RTEntity.Instance.Initialize();
  107. DataCollectionManager.Instance.Initialize(new RtDataCollectionCallback());
  108. JobManager.Instance.Initialize();
  109. RtInstance.SetDefaultConfig();
  110. KeyManager.Instance.Initialize(DeviceModel.PlcDateTime);
  111. Singleton<FaManager>.Instance.Initialize();
  112. }
  113. catch (Exception ex)
  114. {
  115. MessageBox.Show("System Initialize Failed, please check the log file for detail information. \r\n" + ex.Message);
  116. return false;
  117. }
  118. return true;
  119. }
  120. public void Terminate()
  121. {
  122. try
  123. {
  124. Singleton<FaManager>.Instance.Terminate();
  125. JobManager.Instance.Terminate();
  126. RTEntity.Instance.Terminate();
  127. Singleton<DeviceManager>.Instance.Terminate();
  128. Singleton<IOGroupManager>.Instance.Terminate();
  129. Singleton<SCManager>.Instance.Terminate();
  130. DataCollectionManager.Instance.Terminate();
  131. Singleton<WcfServiceManager>.Instance.Terminate();
  132. Singleton<EventManager>.Instance.Terminate();
  133. Singleton<LogManager>.Instance.Terminate();
  134. Singleton<DatabaseManager>.Instance.Terminate();
  135. }
  136. catch (Exception ex)
  137. {
  138. LOG.Write(ex);
  139. }
  140. }
  141. void CheckAnotherInstanceRunning(string appName)
  142. {
  143. _mutex = new Mutex(true, appName, out bool createNew);
  144. if (!createNew)
  145. {
  146. if (!_mutex.WaitOne(1000))
  147. {
  148. string msg = $"{appName} is already running,can not start again";
  149. System.Windows.Forms.MessageBox.Show(msg, $"{appName} Launch Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
  150. Environment.Exit(0);
  151. }
  152. }
  153. }
  154. private void InitIconDisplay()
  155. {
  156. MenuItem configMenuItem = new MenuItem(string.Format("Login {0} RT Server(&L)", RtInstance.SystemName), new EventHandler(ConfirmLogin));
  157. MenuItem exitMenuItem = new MenuItem(string.Format("Exit {0} RT Server(&Q)", RtInstance.SystemName), new EventHandler(ConfirmExit));
  158. _notifyIcon.Icon = Aitex.RT.Properties.Resources.logo;
  159. _notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { configMenuItem, exitMenuItem });
  160. _notifyIcon.Visible = true;
  161. _notifyIcon.Text = string.Format("{0} RT Server", RtInstance.SystemName);
  162. }
  163. private void ConfirmLogin(object sender, EventArgs e)
  164. {
  165. LOG.Write("user login server");
  166. UserLoginView.Display();
  167. }
  168. private void ConfirmExit(object sender, EventArgs e)
  169. {
  170. if (MessageBox.Show(string.Format("are you sure you want to exit {0} RT server?", RtInstance.SystemName), "",
  171. MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  172. {
  173. _notifyIcon.Dispose();
  174. UserLoginView.StopUpdate();
  175. Environment.Exit(0);
  176. }
  177. }
  178. public static void UpdateCultureResource(string culture)
  179. {
  180. Resources.Culture = new CultureInfo(culture);
  181. CultureSupported.UpdateCoreCultureResource(culture);
  182. }
  183. }
  184. }