123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- 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<LogManager>.Instance.Initialize();
- Singleton<ConfigManager>.Instance.Initialize();
- Singleton<DatabaseManager>.Instance.Initialize(
- System.Configuration.ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString
- , RtInstance.DbName
- , PathManager.GetCfgDir() + "SqlUpdate.sql");
- Singleton<EventManager>.Instance.Initialize(PathManager.GetCfgDir() + "EventDefine.xml", true, true,
- false, PathManager.GetCfgDir() + "RtEventDefine.xml");
- Singleton<OperationManager>.Instance.Initialize();
- Singleton<DataManager>.Instance.Initialize();
- RecipeFileManager.Instance.Initialize(new RtRecipeFileContext());
- Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
- {
- typeof (AccountService), typeof (InvokeService),
- typeof (QueryDataService), typeof (RecipeService)
- });
- if (!Singleton<SCManager>.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<string, string> ioFiles = new Dictionary<string, string>();
- Dictionary<string, int[]> plcIoBaseAddress = new Dictionary<string, int[]>();
- 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<string> logicDi = new List<string>();
- foreach (var fieldInfo in typeof(LogicDi).GetFields())
- {
- logicDi.Add(fieldInfo.Name);
- }
- Singleton<IOGroupManager>.Instance.Initialize(ioFiles, interlockFile, plcIoBaseAddress, logicDi);
- Singleton<DeviceManager>.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<FaManager>.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<FaManager>.Instance.Terminate();
- JobManager.Instance.Terminate();
- RTEntity.Instance.Terminate();
- Singleton<DeviceManager>.Instance.Terminate();
- Singleton<IOGroupManager>.Instance.Terminate();
- Singleton<SCManager>.Instance.Terminate();
- DataCollectionManager.Instance.Terminate();
- Singleton<WcfServiceManager>.Instance.Terminate();
- Singleton<EventManager>.Instance.Terminate();
- Singleton<LogManager>.Instance.Terminate();
- Singleton<DatabaseManager>.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);
- }
- }
- }
|