using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Threading; using System.Windows.Forms; using Aitex.Common.Util; using Aitex.Core.Account; using Aitex.Core.Backend; using Aitex.Core.RT.ConfigCenter; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.DataCollection; using Aitex.Core.RT.DBCore; using Aitex.Core.RT.Event; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.Job; using Aitex.Core.RT.Key; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.RecipeCenter; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using Aitex.Core.Utilities; using Aitex.Core.WCF; using Aitex.RT.FactoryAutomation; using Aitex.RT.Module; using Aitex.RT.Properties; using Aitex.Triton160.RT; using Aitex.Triton160.RT.Device; using Aitex.Triton160.RT.Module; using Aitex.Triton160.RT.WCF; namespace Aitex.RT { class RtApplication : ApplicationContext { private NotifyIcon _notifyIcon = new NotifyIcon(); static Mutex _mutex; public RtApplication() { CheckAnotherInstanceRunning("Triton160.RT"); if (!Init()) { Environment.Exit(0); } InitIconDisplay(); } ~RtApplication() { Terminate(); } public bool Init() { try { Resources.Culture = new CultureInfo(CultureSupported.English); Singleton.Instance.Initialize(); Singleton.Instance.Initialize(); Singleton.Instance.Initialize( System.Configuration.ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString , RtInstance.DbName , PathManager.GetCfgDir() + "SqlUpdate.sql"); Singleton.Instance.Initialize(PathManager.GetCfgDir() + "EventDefine.xml", true, true, false, PathManager.GetCfgDir() + "RtEventDefine.xml"); Singleton.Instance.Initialize(); Singleton.Instance.Initialize(); RecipeFileManager.Instance.Initialize(new RtRecipeFileContext()); Singleton.Instance.Initialize(new Type[] { typeof (AccountService), typeof (InvokeService), typeof (QueryDataService), typeof (RecipeService) }); if (!Singleton.Instance.Initialize()) throw new Exception("SC System Initialize false."); UpdateCultureResource(((int)SC.GetItemValue(SCName.System_Language)) == 2 ? CultureSupported.Chinese : CultureSupported.English); LeakCheckResultManager.Instance.Initialize(); string interlockFile = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\", RtInstance.InterlockFileName); Dictionary ioFiles = new Dictionary(); Dictionary plcIoBaseAddress = new Dictionary(); if (!string.IsNullOrEmpty(RtInstance.LocalIoDefineFileName)) { ioFiles["local"] = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\", RtInstance.LocalIoDefineFileName); plcIoBaseAddress["local"] = new int[] { RtInstance.LocalPlcIoBaseDi, RtInstance.LocalPlcIoBaseDo, RtInstance.LocalPlcIoBaseAi, RtInstance.LocalPlcIoBaseAo }; } if (!string.IsNullOrEmpty(RtInstance.RemoteIoDefineFileName)) { ioFiles["remote"] = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\", RtInstance.RemoteIoDefineFileName); plcIoBaseAddress["remote"] = new int[] { RtInstance.RemotePlcIoBaseDi, RtInstance.RemotePlcIoBaseDo, RtInstance.RemotePlcIoBaseAi, RtInstance.RemotePlcIoBaseAo }; } List logicDi = new List(); foreach (var fieldInfo in typeof(LogicDi).GetFields()) { logicDi.Add(fieldInfo.Name); } Singleton.Instance.Initialize(ioFiles, interlockFile, plcIoBaseAddress, logicDi); Singleton.Instance.Initialize(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\", RtInstance.DeviceModelFileName), RtInstance.SystemName); RTEntity.Instance.Initialize(); DataCollectionManager.Instance.Initialize(new RtDataCollectionCallback()); JobManager.Instance.Initialize(); RtInstance.SetDefaultConfig(); KeyManager.Instance.Initialize(DeviceModel.PlcDateTime); Singleton.Instance.Initialize(); } catch (Exception ex) { MessageBox.Show("System Initialize Failed, please check the log file for detail information. \r\n" + ex.Message); return false; } return true; } public void Terminate() { try { Singleton.Instance.Terminate(); JobManager.Instance.Terminate(); RTEntity.Instance.Terminate(); Singleton.Instance.Terminate(); Singleton.Instance.Terminate(); Singleton.Instance.Terminate(); DataCollectionManager.Instance.Terminate(); Singleton.Instance.Terminate(); Singleton.Instance.Terminate(); Singleton.Instance.Terminate(); Singleton.Instance.Terminate(); } catch (Exception ex) { LOG.Write(ex); } } void CheckAnotherInstanceRunning(string appName) { _mutex = new Mutex(true, appName, out bool createNew); if (!createNew) { if (!_mutex.WaitOne(1000)) { string msg = $"{appName} is already running,can not start again"; System.Windows.Forms.MessageBox.Show(msg, $"{appName} Launch Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); Environment.Exit(0); } } } private void InitIconDisplay() { MenuItem configMenuItem = new MenuItem(string.Format("Login {0} RT Server(&L)", RtInstance.SystemName), new EventHandler(ConfirmLogin)); MenuItem exitMenuItem = new MenuItem(string.Format("Exit {0} RT Server(&Q)", RtInstance.SystemName), new EventHandler(ConfirmExit)); _notifyIcon.Icon = Aitex.RT.Properties.Resources.logo; _notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { configMenuItem, exitMenuItem }); _notifyIcon.Visible = true; _notifyIcon.Text = string.Format("{0} RT Server", RtInstance.SystemName); } private void ConfirmLogin(object sender, EventArgs e) { LOG.Write("user login server"); UserLoginView.Display(); } private void ConfirmExit(object sender, EventArgs e) { if (MessageBox.Show(string.Format("are you sure you want to exit {0} RT server?", RtInstance.SystemName), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _notifyIcon.Dispose(); UserLoginView.StopUpdate(); Environment.Exit(0); } } public static void UpdateCultureResource(string culture) { Resources.Culture = new CultureInfo(culture); CultureSupported.UpdateCoreCultureResource(culture); } } }