From a58c6ecd8281922aac48ae7f73894a87dc6ad9e9 Mon Sep 17 00:00:00 2001 From: "CMS3762\\carminatini" Date: Fri, 12 Jan 2018 12:43:32 +0100 Subject: [PATCH] Added NC-Read Threads timing info --- Step.Tasks/ThreadsFunctions.cs | 90 +++++++++++++++++++++++-- Step.Tasks/ThreadsHandler.cs | 53 +++++++++++---- Step.UI/ServerControlWindow.Designer.cs | 83 ++++++++++++++++++----- Step.UI/ServerControlWindow.cs | 28 ++++++-- Step.Utils/Constants.cs | 13 ++-- Step.Utils/ExceptionManager.cs | 2 +- Step/program.cs | 2 +- 7 files changed, 220 insertions(+), 51 deletions(-) diff --git a/Step.Tasks/ThreadsFunctions.cs b/Step.Tasks/ThreadsFunctions.cs index a70761a1..b10f5053 100644 --- a/Step.Tasks/ThreadsFunctions.cs +++ b/Step.Tasks/ThreadsFunctions.cs @@ -8,20 +8,28 @@ using Step.Core; using static Step.Utils.Constants; using static Step.Utils.ExceptionManager; using static CMS_CORE.Nc; +using System.Diagnostics; public static class ThreadsFunctions { public static bool reconnectionIsRunning = false; + private static long ReadAlarmsTimer = 0, ReadAlarmsTimes = 0; + private static long ReadNcGenericInfoTimer = 0, ReadNcGenericInfoTimes = 0; + private static long ReadAxesTimer = 0, ReadAxesTimes = 0; + - public static void ReadAlarmsAsync() + public static void Read_Alarms() { NcHandler ncHandler = new NcHandler(); + Stopwatch sw = new Stopwatch(); + try { ncHandler.Connect(); while (true) { + sw.Restart(); if (ncHandler.numericalControl.NC_IsConnected()) { // Get Alarms from NC @@ -31,6 +39,12 @@ public static class ThreadsFunctions // Send through signalR MessageServices.Current.Publish(SEND_ALARMS, null, alarms); } + sw.Stop(); + + //Send to the UI the time + ReadAlarmsTimer += sw.ElapsedMilliseconds; + ReadAlarmsTimes++; + Thread.Sleep(200); } } @@ -40,9 +54,11 @@ public static class ThreadsFunctions } } - public static void ReadNcGenericInfo() + public static void Read_Nc_Generic_Info() { NcHandler ncHandler = new NcHandler(); + Stopwatch sw = new Stopwatch(); + try { CmsError libraryError = ncHandler.Connect(); @@ -51,7 +67,7 @@ public static class ThreadsFunctions while (true) { - + sw.Restart(); if (ncHandler.numericalControl.NC_IsConnected()) { // Get Generic data from NC @@ -62,6 +78,12 @@ public static class ThreadsFunctions // Send through signalR MessageServices.Current.Publish(SEND_GENERIC_DATA, null, genericData); } + sw.Stop(); + + //Send to the UI the time + ReadNcGenericInfoTimer += sw.ElapsedMilliseconds; + ReadNcGenericInfoTimes++; + Thread.Sleep(1000); } } @@ -71,9 +93,12 @@ public static class ThreadsFunctions } } - public static void ReadAxes() + + public static void Read_Axes() { NcHandler ncHandler = new NcHandler(); + Stopwatch sw = new Stopwatch(); + try { CmsError libraryError = ncHandler.Connect(); @@ -82,7 +107,7 @@ public static class ThreadsFunctions while (true) { - + sw.Restart(); if (ncHandler.numericalControl.NC_IsConnected()) { // Get the list of the axes @@ -92,6 +117,12 @@ public static class ThreadsFunctions // Send through signalR MessageServices.Current.Publish(SEND_AXES, null, genericData); } + sw.Stop(); + + //Send to the UI the time + ReadAxesTimer += sw.ElapsedMilliseconds; + ReadAxesTimes++; + Thread.Sleep(200); } } @@ -105,6 +136,7 @@ public static class ThreadsFunctions { // Stop all the NC threads ThreadsHandler.Stop(); + StatReset(); NcHandler ncHandler = new NcHandler(); // Run loop until NC is connected @@ -120,9 +152,9 @@ public static class ThreadsFunctions ncHandler.Dispose(); Thread.Sleep(1000); - + // Send status to UI - MessageServices.Current.Publish(NC_STATUS, null, ncHandler.numericalControl.NC_IsConnected()); + MessageServices.Current.Publish(MVVM_NC_STATUS, null, ncHandler.numericalControl.NC_IsConnected()); } // Start/Restart NC threads ThreadsHandler.StartWorkers(); @@ -173,6 +205,50 @@ public static class ThreadsFunctions case CMS_ERROR_CODES.SIEMENS_HMI_NOT_RUNNING: Manage(ERROR_LEVEL.FATAL, cmsError.message); break; + } + } + + + internal static void StatThread() + { + while(true) + { + if (ThreadsHandler.RunningThreadStatus.ContainsKey("Read_Alarms") && ReadAlarmsTimes != 0) + { + ThreadsHandler.RunningThreadStatus["Read_Alarms"] = (ReadAlarmsTimer/ReadAlarmsTimes) + " mS"; + ReadAlarmsTimer = 0; + ReadAlarmsTimes = 0; + } + + if (ThreadsHandler.RunningThreadStatus.ContainsKey("Read_Nc_Generic_Info") && ReadNcGenericInfoTimes != 0) + { + ThreadsHandler.RunningThreadStatus["Read_Nc_Generic_Info"] = (ReadNcGenericInfoTimer / ReadNcGenericInfoTimes) + " mS"; + ReadNcGenericInfoTimer = 0; + ReadNcGenericInfoTimes = 0; + } + + if (ThreadsHandler.RunningThreadStatus.ContainsKey("Read_Axes") && ReadAxesTimes != 0) + { + ThreadsHandler.RunningThreadStatus["Read_Axes"] = (ReadAxesTimer / ReadAxesTimes) + " mS"; + ReadAxesTimer = 0; + ReadAxesTimes = 0; + } + + MessageServices.Current.Publish(MVVM_NC_THREADS, null, ThreadsHandler.RunningThreadStatus); + + Thread.Sleep(2000); } } + + + private static void StatReset() + { + ReadAlarmsTimer = 0; + ReadAlarmsTimes = 0; + ReadNcGenericInfoTimer = 0; + ReadNcGenericInfoTimes = 0; + ReadAxesTimer = 0; + ReadAxesTimes = 0; + } + } diff --git a/Step.Tasks/ThreadsHandler.cs b/Step.Tasks/ThreadsHandler.cs index b8c08929..985e21a5 100644 --- a/Step.Tasks/ThreadsHandler.cs +++ b/Step.Tasks/ThreadsHandler.cs @@ -12,6 +12,8 @@ using CMS_CORE.Siemens; using Step.NC; using static Step.Config.StartupConfig; using static Step.Utils.Constants; +using TeamDev.SDK.MVVM; +using System.Diagnostics; namespace Step.Core { @@ -19,12 +21,13 @@ namespace Step.Core { private static List ThreadFunctionsList = new List { - ThreadsFunctions.ReadAlarmsAsync, - ThreadsFunctions.ReadNcGenericInfo, - ThreadsFunctions.ReadAxes + ThreadsFunctions.Read_Alarms, + ThreadsFunctions.Read_Nc_Generic_Info, + ThreadsFunctions.Read_Axes }; private static List RunningThreadsList = new List(); - + internal static Dictionary RunningThreadStatus = new Dictionary(); + private static Thread StatThread; public static void Start() { @@ -33,18 +36,29 @@ namespace Step.Core public static void StartWorkers() { - // For each function run in the list a thread - ThreadFunctionsList.ForEach(threadFunction => + lock (RunningThreadsList) { - // Run new Thread in the list - Thread t = new Thread(() => - threadFunction() - ); + RunningThreadStatus.Clear(); + + // For each function run in the list a thread + ThreadFunctionsList.ForEach(threadFunction => + { + // Run new Thread in the list + Thread t = new Thread(() => + threadFunction() + ); + t.Start(); + // Add thread to running threads list + RunningThreadsList.Add(t); + + RunningThreadStatus.Add(threadFunction.Method.Name, "---"); + }); + + MessageServices.Current.Publish(MVVM_NC_THREADS, null, RunningThreadStatus); + StatThread = new Thread(() => ThreadsFunctions.StatThread()); + StatThread.Start(); + } - t.Start(); - // Add thread to running threads list - RunningThreadsList.Add(t); - }); } public static void Stop() @@ -54,8 +68,19 @@ namespace Step.Core { thread.Abort(); }); + if(StatThread != null) + StatThread.Abort(); // No running status RunningThreadsList.Clear(); + + + lock (RunningThreadsList) + { + RunningThreadStatus.Clear(); + RunningThreadStatus.Add("TryNcConnection", "---"); + MessageServices.Current.Publish(MVVM_NC_THREADS, null, RunningThreadStatus); + } + } } } diff --git a/Step.UI/ServerControlWindow.Designer.cs b/Step.UI/ServerControlWindow.Designer.cs index 3abcf41c..214ee805 100644 --- a/Step.UI/ServerControlWindow.Designer.cs +++ b/Step.UI/ServerControlWindow.Designer.cs @@ -1,4 +1,6 @@ -namespace Step.UI +using System.Windows.Forms; + +namespace Step.UI { partial class ServerControlWindow { @@ -53,12 +55,16 @@ this.metroLabel3 = new MetroFramework.Controls.MetroLabel(); this.metroLabel2 = new MetroFramework.Controls.MetroLabel(); this.metroLabel1 = new MetroFramework.Controls.MetroLabel(); + this.ThreadsInfo = new MetroFramework.Controls.MetroTabPage(); + this.LISTThreadStatus = new MetroFramework.Controls.MetroListView(); + this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.stopServerButton = new MetroFramework.Controls.MetroButton(); this.TestJSButton = new MetroFramework.Controls.MetroButton(); - this.ThreadsInfo = new MetroFramework.Controls.MetroTabPage(); this.NotifyIconMenu.SuspendLayout(); this.metroTabControl1.SuspendLayout(); this.NCInfo.SuspendLayout(); + this.ThreadsInfo.SuspendLayout(); this.SuspendLayout(); // // StepNotifyIcon @@ -92,7 +98,7 @@ this.openUiButton.Size = new System.Drawing.Size(104, 42); this.openUiButton.TabIndex = 1; this.openUiButton.TabStop = false; - this.openUiButton.Text = "Open CMS Server"; + this.openUiButton.Text = "Open CMS Client"; this.openUiButton.UseSelectable = true; this.openUiButton.Click += new System.EventHandler(this.OpenUiButton_Click); // @@ -102,7 +108,7 @@ this.metroTabControl1.Controls.Add(this.ThreadsInfo); this.metroTabControl1.Location = new System.Drawing.Point(12, 50); this.metroTabControl1.Name = "metroTabControl1"; - this.metroTabControl1.SelectedIndex = 0; + this.metroTabControl1.SelectedIndex = 1; this.metroTabControl1.Size = new System.Drawing.Size(328, 329); this.metroTabControl1.TabIndex = 7; this.metroTabControl1.UseSelectable = true; @@ -477,6 +483,57 @@ this.metroLabel1.Text = "Vendor:"; this.metroLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // + // ThreadsInfo + // + this.ThreadsInfo.Controls.Add(this.LISTThreadStatus); + this.ThreadsInfo.HorizontalScrollbarBarColor = true; + this.ThreadsInfo.HorizontalScrollbarHighlightOnWheel = false; + this.ThreadsInfo.HorizontalScrollbarSize = 10; + this.ThreadsInfo.Location = new System.Drawing.Point(4, 38); + this.ThreadsInfo.Name = "ThreadsInfo"; + this.ThreadsInfo.Size = new System.Drawing.Size(320, 287); + this.ThreadsInfo.TabIndex = 1; + this.ThreadsInfo.Text = "Active Threads Info"; + this.ThreadsInfo.VerticalScrollbarBarColor = true; + this.ThreadsInfo.VerticalScrollbarHighlightOnWheel = false; + this.ThreadsInfo.VerticalScrollbarSize = 10; + // + // LISTThreadStatus + // + this.LISTThreadStatus.Alignment = System.Windows.Forms.ListViewAlignment.Default; + this.LISTThreadStatus.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.LISTThreadStatus.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { + this.columnHeader1, + this.columnHeader2}); + this.LISTThreadStatus.Font = new System.Drawing.Font("Segoe UI", 12F); + this.LISTThreadStatus.FullRowSelect = true; + this.LISTThreadStatus.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; + this.LISTThreadStatus.Location = new System.Drawing.Point(0, 3); + this.LISTThreadStatus.MultiSelect = false; + this.LISTThreadStatus.Name = "LISTThreadStatus"; + this.LISTThreadStatus.OwnerDraw = true; + this.LISTThreadStatus.Scrollable = false; + this.LISTThreadStatus.ShowGroups = false; + this.LISTThreadStatus.Size = new System.Drawing.Size(324, 288); + this.LISTThreadStatus.TabIndex = 2; + this.LISTThreadStatus.UseCompatibleStateImageBehavior = false; + this.LISTThreadStatus.UseSelectable = true; + this.LISTThreadStatus.UseStyleColors = true; + this.LISTThreadStatus.View = System.Windows.Forms.View.Details; + // + // columnHeader1 + // + this.columnHeader1.Text = "Name"; + this.columnHeader1.Width = 233; + // + // columnHeader2 + // + this.columnHeader2.Text = "Exec. Time"; + this.columnHeader2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + this.columnHeader2.Width = 84; + // // stopServerButton // this.stopServerButton.Location = new System.Drawing.Point(234, 385); @@ -499,20 +556,6 @@ this.TestJSButton.UseSelectable = true; this.TestJSButton.Click += new System.EventHandler(this.TestJSButton_Click); // - // ThreadsInfo - // - this.ThreadsInfo.HorizontalScrollbarBarColor = true; - this.ThreadsInfo.HorizontalScrollbarHighlightOnWheel = false; - this.ThreadsInfo.HorizontalScrollbarSize = 10; - this.ThreadsInfo.Location = new System.Drawing.Point(4, 38); - this.ThreadsInfo.Name = "ThreadsInfo"; - this.ThreadsInfo.Size = new System.Drawing.Size(320, 287); - this.ThreadsInfo.TabIndex = 1; - this.ThreadsInfo.Text = "Threads Info"; - this.ThreadsInfo.VerticalScrollbarBarColor = true; - this.ThreadsInfo.VerticalScrollbarHighlightOnWheel = false; - this.ThreadsInfo.VerticalScrollbarSize = 10; - // // ServerControlWindow // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -537,6 +580,7 @@ this.metroTabControl1.ResumeLayout(false); this.NCInfo.ResumeLayout(false); this.NCInfo.PerformLayout(); + this.ThreadsInfo.ResumeLayout(false); this.ResumeLayout(false); } @@ -568,5 +612,8 @@ private MetroFramework.Controls.MetroButton stopServerButton; private MetroFramework.Controls.MetroButton TestJSButton; private MetroFramework.Controls.MetroTabPage ThreadsInfo; + private MetroFramework.Controls.MetroListView LISTThreadStatus; + private System.Windows.Forms.ColumnHeader columnHeader1; + private System.Windows.Forms.ColumnHeader columnHeader2; } } \ No newline at end of file diff --git a/Step.UI/ServerControlWindow.cs b/Step.UI/ServerControlWindow.cs index d644d1ee..3f9b2a7b 100644 --- a/Step.UI/ServerControlWindow.cs +++ b/Step.UI/ServerControlWindow.cs @@ -74,13 +74,13 @@ namespace Step.UI private void OpenUiButton_Click(object sender, EventArgs e) { //Open CMS Client - StartCMSClient(HomePageUI); + StartCMSClient(null); } private void StopServerButton_Click(object sender, EventArgs e) { // Send message to listeners and close server - MessageServices.Current.Publish(STOP_SERVER); + MessageServices.Current.Publish(MVVM_STOP_SERVER); } private void NotifyIcon_Click(object sender, EventArgs e) @@ -123,7 +123,7 @@ namespace Step.UI private void InitializeMessageListeners() { - MessageServices.Current.Subscribe(SEND_MESSAGE, (a, b) => + MessageServices.Current.Subscribe(MVVM_SEND_MESSAGE, (a, b) => { // Cast object to ErrorMessageModel ErrorMessageModel message = (ErrorMessageModel)a; @@ -133,7 +133,7 @@ namespace Step.UI StepNotifyIcon.ShowBalloonTip(1000, message.Title, message.Message, (ToolTipIcon)message.ErrorLevel); }); - MessageServices.Current.Subscribe(NC_STATUS, (a, b) => + MessageServices.Current.Subscribe(MVVM_NC_STATUS, (a, b) => { bool newNcStatus = (bool)a; @@ -186,6 +186,26 @@ namespace Step.UI }); + MessageServices.Current.Subscribe(MVVM_NC_THREADS, (a, b) => + { + //Other type + this.Invoke((MethodInvoker)delegate () { + + //Begin the update + LISTThreadStatus.BeginUpdate(); + + //clear the List + LISTThreadStatus.Items.Clear(); + + //Add all items + foreach (KeyValuePair Thr in (Dictionary)a) + LISTThreadStatus.Items.Add(new ListViewItem(new string[] { Thr.Key, Thr.Value })); + + //End the update + LISTThreadStatus.EndUpdate(); + }); + }); + MessageServices.Current.Subscribe(SEND_GENERIC_DATA, (a, b) => { DTONcGenericDataModel data = (DTONcGenericDataModel)a; diff --git a/Step.Utils/Constants.cs b/Step.Utils/Constants.cs index da0e1847..904ddb66 100644 --- a/Step.Utils/Constants.cs +++ b/Step.Utils/Constants.cs @@ -59,13 +59,14 @@ namespace Step.Utils public static string LANGUAGE_SCHEMA_PATH = BASE_PATH + "\\LanguageValidator.xsd"; // MVVM Messages names - public const string STOP_SERVER = "STOP_SERVER"; - public const string SEND_MESSAGE = "SEND_MESSAGE"; - public const string NC_STATUS = "NC_STATUS"; + public const string MVVM_STOP_SERVER = "STOP_SERVER"; + public const string MVVM_SEND_MESSAGE = "SEND_MESSAGE"; + public const string MVVM_NC_STATUS = "NC_STATUS"; + public const string MVVM_NC_THREADS = "THREAD_STATUS"; // MVVM Messages tasks names - public const string SEND_ALARMS = "SEND_ALARMS"; - public const string SEND_GENERIC_DATA = "SEND_GENERIC_DATA"; - public const string SEND_AXES = "SEND_AXES"; + public const string SEND_ALARMS = "SEND_ALARMS"; + public const string SEND_GENERIC_DATA = "SEND_GENERIC_DATA"; + public const string SEND_AXES = "SEND_AXES"; } } diff --git a/Step.Utils/ExceptionManager.cs b/Step.Utils/ExceptionManager.cs index fb3c9f13..c0efa7cb 100644 --- a/Step.Utils/ExceptionManager.cs +++ b/Step.Utils/ExceptionManager.cs @@ -102,7 +102,7 @@ namespace Step.Utils else { // Notify user - MessageServices.Current.Publish(SEND_MESSAGE, null, error); + MessageServices.Current.Publish(MVVM_SEND_MESSAGE, null, error); } } diff --git a/Step/program.cs b/Step/program.cs index 29f022ab..b69fe881 100644 --- a/Step/program.cs +++ b/Step/program.cs @@ -31,7 +31,7 @@ namespace Step string configuredUri = "http://localhost:" + ServerConfig.ServerPort.ToString(); // Register listener to "close application" messages - MessageServices.Current.Subscribe(STOP_SERVER, (a, b) => + MessageServices.Current.Subscribe(MVVM_STOP_SERVER, (a, b) => { StopRequest.Set(); });