using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.IO; using System.Threading; using System.Windows.Forms; using Step.Model; using TeamDev.SDK.MVVM; using static Step.Config.ServerConfig; using static Step.Utils.Constants; using Step.Model.DTOModels; namespace Step.UI { public partial class ServerControlWindow : MetroFramework.Forms.MetroForm { String HomePageUI; String TestJSPageUI; bool ncStatus = false; bool isUpdatingThreads = false; 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(); HomePageUI = "http://localhost:" + ServerStartupConfig.ServerPort.ToString() + "/index.html"; TestJSPageUI = "http://localhost:" + ServerStartupConfig.ServerPort.ToString() + "/Testjavascript//index.html"; TXTType.Text = NcConfig.NcVendor; InitializeMessageListeners(); } 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"); } private void StartCMSClient(string url) { //Setup the Path Variable String CMSClientPath = ""; //Check if the system is 64/32 bit if (Environment.Is64BitOperatingSystem && File.Exists(BASE_PATH + @"\Client\x64\CMS_Client.exe")) CMSClientPath = BASE_PATH + @"\Client\x64\CMS_Client.exe"; else if (File.Exists(BASE_PATH + @"\Client\x86\CMS_Client.exe")) CMSClientPath = BASE_PATH + @"\Client\x86\CMS_Client.exe"; if (!String.IsNullOrEmpty(CMSClientPath)) Process.Start(CMSClientPath, url); else MessageBox.Show("CMS_Client.Exe not found"); } private void TestJSButton_Click(object sender, EventArgs e) { StartCMSClient(TestJSPageUI); } private void InitializeMessageListeners() { MVVMListeners = new List(); MVVMListeners.Add(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; }); })); MVVMListeners.Add(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 == true) { title = "Nc connection established"; message = "Comunication started"; toolTipIcon = ToolTipIcon.Info; } if (!this.IsDisposed) this.Invoke((MethodInvoker)delegate () { StepNotifyIcon.ShowBalloonTip(1000, title, message, toolTipIcon); TXTstatus.Text = "[" + DateTime.Now.ToString("T") + "] " + title; }); } if (ncStatus) if (!this.IsDisposed) this.Invoke((MethodInvoker)delegate () { StepNotifyIcon.Icon = Properties.Resources.Step_Icon; }); else { if(!this.IsDisposed) this.Invoke((MethodInvoker)delegate () { TXTName.Text = ""; TXTNcSerial.Text = ""; TXTCMSMach.Text = ""; TXTSftVers.Text = ""; TXTNCProc.Text = ""; TXTTime.Text = ""; TXTLang.Text = ""; StepNotifyIcon.Icon = Properties.Resources.Step_Disconnected; }); } //Other type if (!this.IsDisposed) this.Invoke((MethodInvoker)delegate () { CHNcConnected.Checked = ncStatus; }); })); MVVMListeners.Add(MessageServices.Current.Subscribe(SEND_THREADS_STATUS, (a, b) => { //Other type if (!this.IsDisposed) this.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; } }); })); MVVMListeners.Add(MessageServices.Current.Subscribe(SEND_GENERIC_DATA, (a, b) => { DTONcGenericDataModel data = (DTONcGenericDataModel)a; //Other type if (!this.IsDisposed) this.Invoke((MethodInvoker)delegate () { TXTName.Text = data.NcModel; TXTNcSerial.Text = data.SerialNumber; TXTCMSMach.Text = data.CmsMachineIdNumber; TXTSftVers.Text = data.NcSoftwareVersion; TXTNCProc.Text = data.ProcessNumber.ToString(); TXTLang.Text = data.Language; TXTTime.Text = data.DateTime.ToShortDateString() + " " + data.DateTime.ToShortTimeString(); }); })); } } }