using Step.Model; using Step.Model.DTOModels; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Threading; using System.Windows.Forms; using TeamDev.SDK.MVVM; using static Step.Config.ServerConfig; using static Step.Model.Constants; namespace Step.UI { public partial class ServerControlWindow : MetroFramework.Forms.MetroForm { private bool ncStatus = false; private bool isUpdatingThreads = false; private List MVVMListeners; public ServerControlWindow() { InitializeComponent(); //Begin the update LISTThreadStatus.BeginUpdate(); //clear the List LISTThreadStatus.Items.Clear(); //Add all items LISTThreadStatus.Items.Add(new ListViewItem(new string[] { "TryNcConnection", "---" })); //End the update LISTThreadStatus.EndUpdate(); TXTType.Text = NcConfig.NcVendor; InitializeMessageListeners(); MessageServices.Current.Subscribe(SHOW_MSG_UI, (a, b) => { Invoke((MethodInvoker)delegate () { Focus(); MessageBox.Show(a.ToString()); }); }); } private static ServerControlWindow ctrlwindow = null; public static void Start() { // Open WinForm Thread th = new Thread(() => { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ctrlwindow = new ServerControlWindow(); Application.Run(ctrlwindow); }); th.SetApartmentState(ApartmentState.STA); th.Start(); } public static void Stop() { // Close WinForm if (ctrlwindow != null) { ctrlwindow.Invoke((ThreadStart)delegate () { ctrlwindow._closing = true; ctrlwindow.Close(); ctrlwindow = null; }); } } private bool _closing = false; // Avoid closing the window protected override void OnClosing(CancelEventArgs e) { if (!_closing) { e.Cancel = true; Hide(); } } private void OpenUiButton_Click(object sender, EventArgs e) { //Open CMS Client StartCMSClient(null); } private void StopServerButton_Click(object sender, EventArgs e) { // Send message to listeners and close server MessageServices.Current.Publish(SEND_STOP_SERVER); } private void NotifyIcon_Click(object sender, EventArgs e) { this.Show(); this.TopMost = true; this.TopMost = false; } private void StopServerItem_Click(object sender, EventArgs e) { //Unsubscrube MVVM threads foreach (RegistrationInfo Info in MVVMListeners) MessageServices.Current.UnSubscribe(Info); MessageServices.Current.Publish("StopServer"); } public static void StartCMSClient(string url) { //Setup the Path Variable String CMSClientPath = ""; if (ClientIsRunning()) return; //Check if the system is 64/32 bit if (Environment.Is64BitOperatingSystem && File.Exists(BASE_PATH + @"\Client\x64\" + CLIENT_EXE_NAME)) CMSClientPath = BASE_PATH + @"\Client\x64\" + CLIENT_EXE_NAME; else if (File.Exists(BASE_PATH + @"\Client\x86\" + CLIENT_EXE_NAME)) CMSClientPath = BASE_PATH + @"\Client\x86\" + CLIENT_EXE_NAME; if (!String.IsNullOrEmpty(CMSClientPath)) Process.Start(CMSClientPath, url); else MessageBox.Show("CMS-Active Client not found"); } private void InitializeMessageListeners() { MVVMListeners = new List { MessageServices.Current.Subscribe(SEND_MESSAGE, (a, b) => { // Cast object to ErrorMessageModel ErrorMessageModel message = (ErrorMessageModel)a; // If error has a fatal level, icon must be error message.ErrorLevel = message.ErrorLevel > (int)ERROR_LEVEL.ERROR ? (int)ERROR_LEVEL.ERROR : message.ErrorLevel; if (!this.IsDisposed) this.Invoke((MethodInvoker)delegate () { // Open BalloonTip with data StepNotifyIcon.ShowBalloonTip(1000, message.Title, message.Message, (ToolTipIcon)message.ErrorLevel); //ShowMessage on UI TXTstatus.Text = "[" + DateTime.Now.ToString("T") + "] " + message.Message; }); }), // NC status handler MessageServices.Current.Subscribe(SEND_NC_STATUS, (a, b) => { bool newNcStatus = (bool)a; if (ncStatus != newNcStatus) { ncStatus = newNcStatus; string title = "NC not connected"; string message = "Check NC connection"; ToolTipIcon toolTipIcon = ToolTipIcon.Error; if (ncStatus) { title = "Nc connection established"; message = "Comunication started"; toolTipIcon = ToolTipIcon.Info; } if (!IsDisposed) Invoke((MethodInvoker)delegate () { // Show balloon with new status StepNotifyIcon.ShowBalloonTip(1000, title, message, toolTipIcon); TXTstatus.Text = "[" + DateTime.Now.ToString("T") + "] " + title; }); } if (ncStatus) if (!IsDisposed) Invoke((MethodInvoker)delegate () { StepNotifyIcon.Icon = Properties.Resources.CMS_Icon; }); //Other type if (!IsDisposed) Invoke((MethodInvoker)delegate () { CHNcConnected.Checked = ncStatus; }); }), // Threads status handler MessageServices.Current.Subscribe(SEND_THREADS_STATUS, (a, b) => { //Other type if (!IsDisposed) Invoke((MethodInvoker)delegate () { if (!isUpdatingThreads) { isUpdatingThreads = true; Dictionary Threads = new Dictionary((Dictionary)a); //Begin the update LISTThreadStatus.BeginUpdate(); //clear the List LISTThreadStatus.Items.Clear(); //Add all items foreach (KeyValuePair Thr in Threads) LISTThreadStatus.Items.Add(new ListViewItem(new string[] { Thr.Key, Thr.Value })); //End the update LISTThreadStatus.EndUpdate(); isUpdatingThreads = false; } }); }) }; } private static bool ClientIsRunning() { Process[] p = Process.GetProcessesByName(CLIENT_EXE_NAME_NOEXT); if (p.Length>0) return true; return false; } } }