diff --git a/Libs/CMS_CORE_Library.dll b/Libs/CMS_CORE_Library.dll index 4d40a772..a9dbe146 100644 Binary files a/Libs/CMS_CORE_Library.dll and b/Libs/CMS_CORE_Library.dll differ diff --git a/Step.Config/ServerConfigController.cs b/Step.Config/ServerConfigController.cs index 4c1a30bb..70aefd4b 100644 --- a/Step.Config/ServerConfigController.cs +++ b/Step.Config/ServerConfigController.cs @@ -41,7 +41,7 @@ namespace Step.Config } catch (XmlException ex) { - ExceptionManager.Manage(ERROR_LEVEL.FATAL, + ExceptionManager.ManageError(ERROR_LEVEL.FATAL, "Error while reading file: " + ex.SourceUri + "\n Error: " + ex.Message, true @@ -52,7 +52,7 @@ namespace Step.Config var message = ex.Message; if (ex.InnerException != null) message += "\n"+ex.InnerException.Message; - ExceptionManager.Manage(ERROR_LEVEL.FATAL, message, true); + ExceptionManager.ManageError(ERROR_LEVEL.FATAL, message, true); } } @@ -163,11 +163,11 @@ namespace Step.Config { if (e.Severity == XmlSeverityType.Warning) { - ExceptionManager.Manage(ERROR_LEVEL.WARNING, e.Message, true); + ExceptionManager.ManageError(ERROR_LEVEL.WARNING, e.Message, true); } else if (e.Severity == XmlSeverityType.Error) { - ExceptionManager.Manage(ERROR_LEVEL.FATAL, + ExceptionManager.ManageError(ERROR_LEVEL.FATAL, // "Error while reading file: " + e.Exception.SourceUri + "Error while reading XML file \"" + actualFileName + "\" \n\n" + e.Message, true diff --git a/Step.Core/ThreadsFunctions.cs b/Step.Core/ThreadsFunctions.cs index 2876e48f..a5516bc0 100644 --- a/Step.Core/ThreadsFunctions.cs +++ b/Step.Core/ThreadsFunctions.cs @@ -9,11 +9,13 @@ using Step.Model.DTOModels.ToolModels; using Step.NC; using Step.Utils; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using TeamDev.SDK.MVVM; @@ -25,23 +27,15 @@ using static Step.Utils.ExceptionManager; public static class ThreadsFunctions { public static bool reconnectionIsRunning = false; - private static long ReadAlarmsTimer = 0, ReadAlarmsTimes = 0; - private static long ReadAxesTimer = 0, ReadAxesTimes = 0; - private static long ReadPowerOnTimer = 0, ReadPowerOnTimes = 0; - private static long ReadProcPPTimer = 0, ReadProcPPTimes = 0; - private static long ReadFunctionTimer = 0, ReadFunctionTimes = 0; - private static long ReadMaintenanceTimer = 0, ReadMaintenanceTimes = 0; - private static long ReadUserSoftKeysTimer = 0, ReadUserSoftKeysTimes = 0; - private static long ReadAxesNamesTimer = 0, ReadAxesNamesTimes = 0; - private static long ReadHeadsTimer = 0, ReadHeadsTimes = 0; - private static long ReadToolDataTimer = 0, ReadToolDataTimes = 0; - private static long WatchdogTimer = 0, WatchdogTimes = 0; + + private static ConcurrentDictionary Timers = new ConcurrentDictionary(); + private static ConcurrentDictionary Counter = new ConcurrentDictionary(); private static Thread ConnThread; - + #region Functions - public static void ManageWatchdog() + public static void ManageWatchdog() { NcAdapter ncAdapter = new NcAdapter(); Stopwatch sw = new Stopwatch(); @@ -62,14 +56,17 @@ public static class ThreadsFunctions //} // Check if client is connected - if (ncAdapter.numericalControl.NC_IsConnected()) + if (ncAdapter.numericalControl.NC_IsConnected()) { // Manage watchdog libraryError = ncAdapter.ManageWatchdog(); if (libraryError == PLC_NOT_RUNNING_ERROR) - errorCounter++; + { + if (errorCounter < MAX_NUM_OF_WATCHDOG_ERROR) + errorCounter++; + } else if (libraryError == NO_ERROR) - errorCounter = 0; + errorCounter = 0; } else RestoreConnection(); @@ -77,14 +74,13 @@ public static class ThreadsFunctions sw.Stop(); //Update thread timer - WatchdogTimer += sw.ElapsedMilliseconds; - WatchdogTimes++; + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); } } - catch (ThreadAbortException) + catch (ThreadAbortException ex) { ncAdapter.Dispose(); } @@ -122,8 +118,7 @@ public static class ThreadsFunctions sw.Stop(); //Update thread timer - ReadAlarmsTimer += sw.ElapsedMilliseconds; - ReadAlarmsTimes++; + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(200, (int)sw.ElapsedMilliseconds)); @@ -164,9 +159,8 @@ public static class ThreadsFunctions RestoreConnection(); sw.Stop(); - // Update thread timer - ReadPowerOnTimer += sw.ElapsedMilliseconds; - ReadPowerOnTimes++; + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(400, (int)sw.ElapsedMilliseconds)); @@ -228,9 +222,8 @@ public static class ThreadsFunctions sw.Stop(); - // Update thread timer - ReadProcPPTimer += sw.ElapsedMilliseconds; - ReadProcPPTimes++; + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(200, (int)sw.ElapsedMilliseconds)); @@ -273,9 +266,8 @@ public static class ThreadsFunctions sw.Stop(); - // Update thread timer - ReadFunctionTimer += sw.ElapsedMilliseconds; - ReadFunctionTimes++; + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(200, (int)sw.ElapsedMilliseconds)); @@ -323,9 +315,8 @@ public static class ThreadsFunctions sw.Stop(); - // Update thread timer - ReadMaintenanceTimer += sw.ElapsedMilliseconds; - ReadMaintenanceTimes++; + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(30000); @@ -335,6 +326,10 @@ public static class ThreadsFunctions { ncAdapter.Dispose(); } + catch (Exception ex) + { + + } } public static void ReadUserSoftKeysData() @@ -368,9 +363,8 @@ public static class ThreadsFunctions sw.Stop(); - // Update thread timer - ReadUserSoftKeysTimer += sw.ElapsedMilliseconds; - ReadUserSoftKeysTimes++; + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(200, (int)sw.ElapsedMilliseconds)); @@ -414,8 +408,7 @@ public static class ThreadsFunctions sw.Stop(); //Update thread timer - ReadHeadsTimer += sw.ElapsedMilliseconds; - ReadHeadsTimes++; + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); @@ -459,8 +452,7 @@ public static class ThreadsFunctions sw.Stop(); //Update thread timer - ReadAxesNamesTimer += sw.ElapsedMilliseconds; - ReadAxesNamesTimes++; + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(800, (int)sw.ElapsedMilliseconds)); @@ -504,8 +496,7 @@ public static class ThreadsFunctions sw.Stop(); //Update thread timer - ReadAxesTimer += sw.ElapsedMilliseconds; - ReadAxesTimes++; + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(100, (int)sw.ElapsedMilliseconds)); @@ -549,8 +540,7 @@ public static class ThreadsFunctions sw.Stop(); //Update thread timer - ReadAxesNamesTimer += sw.ElapsedMilliseconds; - ReadAxesNamesTimes++; + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(1000, (int)sw.ElapsedMilliseconds)); @@ -595,8 +585,7 @@ public static class ThreadsFunctions sw.Stop(); //Update thread timer - ReadAxesNamesTimer += sw.ElapsedMilliseconds; - ReadAxesNamesTimes++; + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(400, (int)sw.ElapsedMilliseconds)); @@ -613,96 +602,100 @@ public static class ThreadsFunctions NcToolTableAdapter ncAdapter = new NcToolTableAdapter(); Stopwatch sw = new Stopwatch(); - try + if(NcConfig.NcVendor != NC_VENDOR.SIEMENS) { - // Try connection - CmsError libraryError = ncAdapter.Connect(); - if (libraryError.errorCode != 0) - ManageLibraryError(libraryError); - - while (true) + try { - sw.Restart(); + // Try connection + CmsError libraryError = ncAdapter.Connect(); + if (libraryError.errorCode != 0) + ManageLibraryError(libraryError); - // Check if client is connected - if (ncAdapter.numericalControl.NC_IsConnected()) + while (true) { - // Get new data from PLC - libraryError = ncAdapter.GetUpdatedToolsData(out Dictionary updatedStatus, out Dictionary updatedLives); - if (libraryError.IsError()) - ManageLibraryError(libraryError); + sw.Restart(); - MessageServices.Current.Publish(UPDATE_TOOLS_DATA, null, - new DTONewToolDataModel + // Check if client is connected + if (ncAdapter.numericalControl.NC_IsConnected()) { - UpdatedStatus = updatedStatus, - UpdatedLives = updatedLives - }); + // Get new data from PLC + libraryError = ncAdapter.GetUpdatedToolsData(out Dictionary updatedStatus, out Dictionary updatedLives); + if (libraryError.IsError()) + ManageLibraryError(libraryError); + + MessageServices.Current.Publish(UPDATE_TOOLS_DATA, null, + new DTONewToolDataModel + { + UpdatedStatus = updatedStatus, + UpdatedLives = updatedLives + }); + } + else + RestoreConnection(); + sw.Stop(); + + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); + + // Wait + Thread.Sleep(CalcSleepTime(1000, (int)sw.ElapsedMilliseconds)); } - else - RestoreConnection(); - sw.Stop(); - - //Update thread timer - ReadToolDataTimer += sw.ElapsedMilliseconds; - ReadToolDataTimes++; - - // Wait - Thread.Sleep(CalcSleepTime(1000, (int)sw.ElapsedMilliseconds)); } - } - catch (ThreadAbortException) - { - ncAdapter.Dispose(); + catch (ThreadAbortException) + { + ncAdapter.Dispose(); + } } } public static void ReadNcMagazineActive() { - NcToolTableAdapter ncAdapter = new NcToolTableAdapter(); - Stopwatch sw = new Stopwatch(); - - try + if (NcConfig.NcVendor != NC_VENDOR.SIEMENS) { - // Try connection - CmsError libraryError = ncAdapter.Connect(); - if (libraryError.errorCode != 0) - ManageLibraryError(libraryError); - - while (true) + NcToolTableAdapter ncAdapter = new NcToolTableAdapter(); + Stopwatch sw = new Stopwatch(); + try { - sw.Restart(); + // Try connection + CmsError libraryError = ncAdapter.Connect(); + if (libraryError.errorCode != 0) + ManageLibraryError(libraryError); - // Check if client is connected - if (ncAdapter.numericalControl.NC_IsConnected()) + while (true) { - // Get new data from PLC - libraryError = ncAdapter.GetNcMagazineStatus(out Dictionary status); - if (libraryError.IsError()) - ManageLibraryError(libraryError); + sw.Restart(); - MessageServices.Current.Publish(NC_MAGAZINE_IS_ACTIVE, null, status); - - if (NcConfig.NcVendor != NC_VENDOR.SIEMENS) + // Check if client is connected + if (ncAdapter.numericalControl.NC_IsConnected()) { + // Get new data from PLC + libraryError = ncAdapter.GetNcMagazineStatus(out Dictionary status); + if (libraryError.IsError()) + ManageLibraryError(libraryError); + + MessageServices.Current.Publish(NC_MAGAZINE_IS_ACTIVE, null, status); + libraryError = ncAdapter.ReadAssistedToolingProcedure(out DTOAssistedToolingEndValueModel data); if (libraryError.IsError()) ManageLibraryError(libraryError); MessageServices.Current.Publish(ASSISTED_TOOLING_DATA, null, data); } - } - else - RestoreConnection(); - sw.Stop(); + else + RestoreConnection(); + sw.Stop(); - // Wait - Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); + + // Wait + Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); + } + } + catch (ThreadAbortException) + { + ncAdapter.Dispose(); } - } - catch (ThreadAbortException) - { - ncAdapter.Dispose(); } } @@ -740,6 +733,9 @@ public static class ThreadsFunctions RestoreConnection(); sw.Stop(); + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); + // Wait Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); } @@ -780,8 +776,11 @@ public static class ThreadsFunctions } else RestoreConnection(); + sw.Stop(); + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); // Wait Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); } @@ -954,6 +953,9 @@ public static class ThreadsFunctions RestoreConnection(); sw.Stop(); + //Update thread timer + UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds); + // Wait Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds)); } @@ -996,17 +998,17 @@ public static class ThreadsFunctions { defAlarmsNamesEn = alarmsNames; if(!RedisController.WriteAlarmsConfigEn(alarmsNames)) - Manage(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); + ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); } else if (lang.IsoId.ToLower() == "it") { defAlarmsNamesIt = alarmsNames; if (!RedisController.WriteAlarmsConfigIt(alarmsNames)) - Manage(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); + ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); } else if (!RedisController.WriteAlarmsConfigCurr(alarmsNames)) - Manage(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); + ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); if (count == 3) break; @@ -1016,10 +1018,10 @@ public static class ThreadsFunctions if (availableLanguages.Count < 3 && defAlarmsNamesEn != null) if (!RedisController.WriteAlarmsConfigCurr(defAlarmsNamesEn)) - Manage(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); + ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); else if (availableLanguages.Count < 3 && defAlarmsNamesIt != null) if (!RedisController.WriteAlarmsConfigCurr(defAlarmsNamesIt)) - Manage(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); + ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE); } catch (ThreadAbortException) @@ -1027,6 +1029,7 @@ public static class ThreadsFunctions ncAdapter.Dispose(); } } + #endregion Functions #region SupportFunctions @@ -1046,7 +1049,12 @@ public static class ThreadsFunctions if (cmsError.errorCode == CMS_ERROR_CODES.SIEMENS_ENVIRONMENT_NOT_FOUND || cmsError.errorCode == CMS_ERROR_CODES.SIEMENS_HMI_NOT_RUNNING || cmsError.errorCode == CMS_ERROR_CODES.OSAI_TT_FOLDER_NOT_FOUND) ManageLibraryError(cmsError); else if (cmsError.errorCode != CMS_ERROR_CODES.OK) + { ncAdapter.Dispose(); + } + + if (cmsError.errorCode == CMS_ERROR_CODES.SIEMENS_TOOL_TABLE_ERROR) + ManageLibraryError(cmsError); // Send status to UI MessageServices.Current.Publish(SEND_NC_STATUS, null, ncAdapter.numericalControl.NC_IsConnected()); @@ -1104,7 +1112,7 @@ public static class ThreadsFunctions switch (cmsError.errorCode) { case CMS_ERROR_CODES.NC_PROD_ERROR: - Manage(ERROR_LEVEL.WARNING, cmsError.localizationKey); + ManageError(ERROR_LEVEL.WARNING, cmsError.localizationKey); break; case CMS_ERROR_CODES.NOT_CONNECTED: @@ -1113,94 +1121,53 @@ public static class ThreadsFunctions case CMS_ERROR_CODES.SIEMENS_ENVIRONMENT_NOT_FOUND: case CMS_ERROR_CODES.OSAI_TT_FOLDER_NOT_FOUND: - Manage(ERROR_LEVEL.FATAL, cmsError.localizationKey); + case CMS_ERROR_CODES.OPTION_NOT_CONSISTENT: + case CMS_ERROR_CODES.SIEMENS_TOOL_TABLE_ERROR: + ManageError(ERROR_LEVEL.FATAL, cmsError.localizationKey); break; case CMS_ERROR_CODES.SIEMENS_HMI_NOT_RUNNING: - Manage(ERROR_LEVEL.FATAL, "SIEMENS HMI NOT RUNNING"); - break; - - case CMS_ERROR_CODES.INTERNAL_ERROR: - case CMS_ERROR_CODES.OPTION_NOT_CONSISTENT: - Manage(ERROR_LEVEL.FATAL, cmsError.localizationKey); + ManageError(ERROR_LEVEL.FATAL, "SIEMENS HMI NOT RUNNING"); break; case CMS_ERROR_CODES.SELECTED_PROCESS: - Manage(ERROR_LEVEL.WARNING, cmsError.localizationKey); + ManageError(ERROR_LEVEL.WARNING, cmsError.localizationKey); + break; + + case CMS_ERROR_CODES.INTERNAL_ERROR: + ManageException(ERROR_LEVEL.FATAL, cmsError.exception); break; } } + + internal static void UpdateStat(string functionName, long timer) + { + if (!Timers.ContainsKey(functionName)) + Timers.TryAdd(functionName, timer); + else + Timers[functionName] += timer; + + + if (!Counter.ContainsKey(functionName)) + Counter.TryAdd(functionName, 1); + else + Counter[functionName]++; + } internal static void StatThread() { while (true) { - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadAlarms") && ReadAlarmsTimes != 0) + foreach(var value in Counter) { - ThreadsHandler.RunningThreadStatus["ReadAlarms"] = (ReadAlarmsTimer / ReadAlarmsTimes) + " mS"; - ReadAlarmsTimer = 0; - ReadAlarmsTimes = 0; + if(ThreadsHandler.RunningThreadStatus.ContainsKey(value.Key) && Counter[value.Key] != 0) + { + ThreadsHandler.RunningThreadStatus[value.Key] = (Timers[value.Key] / Counter[value.Key]) + " mS"; + Timers[value.Key] = 0; + Counter[value.Key] = 0; + } } - - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadAxesPositionsData") && ReadAxesTimes != 0) - { - ThreadsHandler.RunningThreadStatus["ReadAxesPositionsData"] = (ReadAxesTimer / ReadAxesTimes) + " mS"; - ReadAxesTimer = 0; - ReadAxesTimes = 0; - } - - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadPowerOnData") && ReadPowerOnTimes != 0) - { - ThreadsHandler.RunningThreadStatus["ReadPowerOnData"] = (ReadPowerOnTimer / ReadPowerOnTimes) + " mS"; - ReadPowerOnTimer = 0; - ReadPowerOnTimes = 0; - } - - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadProcessesPPStatus") && ReadProcPPTimes != 0) - { - ThreadsHandler.RunningThreadStatus["ReadProcessesPPStatus"] = (ReadProcPPTimer / ReadProcPPTimes) + " mS"; - ReadProcPPTimer = 0; - ReadProcPPTimes = 0; - } - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadEnabledFunctionality") && ReadFunctionTimes != 0) - { - ThreadsHandler.RunningThreadStatus["ReadEnabledFunctionality"] = (ReadFunctionTimer / ReadFunctionTimes) + " mS"; - ReadFunctionTimer = 0; - ReadFunctionTimes = 0; - } - - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadExpiredMaintenances") && ReadMaintenanceTimes != 0) - { - ThreadsHandler.RunningThreadStatus["ReadExpiredMaintenances"] = (ReadMaintenanceTimer / ReadMaintenanceTimes) + " mS"; - ReadMaintenanceTimer = 0; - ReadMaintenanceTimes = 0; - } - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadUserSoftKeysData") && ReadUserSoftKeysTimes != 0) - { - ThreadsHandler.RunningThreadStatus["ReadUserSoftKeysData"] = (ReadUserSoftKeysTimer / ReadUserSoftKeysTimes) + " mS"; - ReadUserSoftKeysTimer = 0; - ReadUserSoftKeysTimes = 0; - } - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadHeadsData") && ReadHeadsTimes != 0) - { - ThreadsHandler.RunningThreadStatus["ReadHeadsData"] = (ReadHeadsTimer / ReadHeadsTimes) + " mS"; - ReadHeadsTimer = 0; - ReadHeadsTimes = 0; - } - if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadAxesNamesData") && ReadAxesNamesTimer != 0) - { - ThreadsHandler.RunningThreadStatus["ReadAxesNamesData"] = (ReadAxesNamesTimer / ReadAxesNamesTimes) + " mS"; - ReadAxesNamesTimer = 0; - ReadAxesNamesTimes = 0; - } - - if (ThreadsHandler.RunningThreadStatus.ContainsKey("UpdateToolsData") && ReadAxesNamesTimer != 0) - { - ThreadsHandler.RunningThreadStatus["UpdateToolsData"] = (ReadToolDataTimer / ReadToolDataTimes) + " mS"; - ReadToolDataTimer = 0; - ReadToolDataTimes = 0; - } - + MessageServices.Current.Publish(SEND_THREADS_STATUS, null, ThreadsHandler.RunningThreadStatus); Thread.Sleep(2000); @@ -1209,20 +1176,11 @@ public static class ThreadsFunctions private static void StatReset() { - ReadAlarmsTimer = 0; - ReadAlarmsTimes = 0; - ReadAxesTimer = 0; - ReadAxesTimes = 0; - ReadProcPPTimer = 0; - ReadProcPPTimes = 0; - ReadFunctionTimer = 0; - ReadFunctionTimes = 0; - ReadMaintenanceTimer = 0; - ReadMaintenanceTimes = 0; - ReadHeadsTimer = 0; - ReadHeadsTimes = 0; - ReadAxesNamesTimer = 0; - ReadAxesNamesTimes = 0; + foreach (var value in Counter) + { + Timers[value.Key] = 0; + Counter[value.Key] = 0; + } } private static int CalcSleepTime(int maxSleep, int execTime) diff --git a/Step.Core/ThreadsSiemensHmi.cs b/Step.Core/ThreadsSiemensHmi.cs index c04db148..c3a75204 100644 --- a/Step.Core/ThreadsSiemensHmi.cs +++ b/Step.Core/ThreadsSiemensHmi.cs @@ -39,7 +39,7 @@ namespace Step.Core // Check if exists the Nc Path if (!File.Exists(ProcessPath)) - ExceptionManager.Manage(Constants.ERROR_LEVEL.FATAL, "Siemens HMI not found at: " + ProcessPath); + ExceptionManager.ManageError(Constants.ERROR_LEVEL.FATAL, "Siemens HMI not found at: " + ProcessPath); // If exists the Process if (processes.Length > 0) diff --git a/Step.Database/DatabaseContext.cs b/Step.Database/DatabaseContext.cs index 523e1ad6..0956c426 100644 --- a/Step.Database/DatabaseContext.cs +++ b/Step.Database/DatabaseContext.cs @@ -72,7 +72,7 @@ namespace Step.Database } catch { - Manage(ERROR_LEVEL.FATAL, "Database not started"); + ManageError(ERROR_LEVEL.FATAL, "Database not started"); return false; } @@ -104,7 +104,7 @@ namespace Step.Database } catch (Exception ex) { - Manage(ERROR_LEVEL.FATAL, ex); + ManageException(ERROR_LEVEL.FATAL, ex); return false; } diff --git a/Step.Utils/ExceptionManager.cs b/Step.Utils/ExceptionManager.cs index 5cc07841..78695d7d 100644 --- a/Step.Utils/ExceptionManager.cs +++ b/Step.Utils/ExceptionManager.cs @@ -1,4 +1,7 @@ using System; +using System.Diagnostics; +using System.Linq; +using System.Reflection; using System.Windows.Forms; using Step.Model; using TeamDev.SDK.MVVM; @@ -9,43 +12,24 @@ namespace Step.Utils { public static class ExceptionManager { - public static void Manage(Exception ex) + public static void ManageException(ERROR_LEVEL errorLevel, Exception exception) { - if (ex is System.IO.FileNotFoundException) + string message = exception.Message; + if(exception.InnerException != null) { - // File not found - LogAndNotifyUsers(CreateMessageModel("Configuration file not found!", ex.Message, ERROR_LEVEL.FATAL)); - // Close the application - Environment.Exit(1); + // Add inner exception message (if exists) + message += " | " + exception.InnerException.Message; } - else if(ex is MySql.Data.MySqlClient.MySqlException) - { - // MySql Error - LogAndNotifyUsers(CreateMessageModel("Database Error!", ex.Message, ERROR_LEVEL.FATAL)); - } - else - { - if (ex.InnerException != null) - { - Manage(ex.InnerException); // TODO test - } - else - { - // Default case - LogAndNotifyUsers(CreateMessageModel("Generic Error!", ex.Message, ERROR_LEVEL.FATAL)); - - } - } - } - public static void Manage(ERROR_LEVEL errorLevel, Exception exception) - { - Manage(errorLevel, exception.Message); + // Add method name + message += " | " + GetExceptionMethodName(exception); + + ManageError(errorLevel, message); } private static bool MessageBoxShow = false; - public static void Manage(ERROR_LEVEL errorLevel, string message, bool beforeFormReady = false) + public static void ManageError(ERROR_LEVEL errorLevel, string message, bool beforeFormReady = false) { switch (errorLevel) { @@ -84,7 +68,6 @@ namespace Step.Utils }break; default: { - LogFatal(message); if (!MessageBoxShow) { @@ -132,8 +115,11 @@ namespace Step.Utils } } - private static ErrorMessageModel CreateMessageModel(string title, string message, ERROR_LEVEL level) + private static ErrorMessageModel CreateMessageModel(string title, string message, ERROR_LEVEL level, string methodName = "") { + if (methodName != "") + message = message + " | " + methodName; + return new ErrorMessageModel() { Title = title, @@ -141,5 +127,21 @@ namespace Step.Utils ErrorLevel = level }; } + + public static string GetExceptionMethodName(Exception ex) + { + var s = new StackTrace(ex, true); + var thisasm = Assembly.GetExecutingAssembly(); + + Console.WriteLine(s.GetFrames().Count()); + foreach (var a in s.GetFrames()) + { + var d = a.GetMethod(); + Console.WriteLine(d.Name); + } + + return s.GetFrames().FirstOrDefault()?.GetMethod().Name; + } + } } diff --git a/Step/App_Start/Startup.cs b/Step/App_Start/Startup.cs index 5807544c..185ae9f5 100644 --- a/Step/App_Start/Startup.cs +++ b/Step/App_Start/Startup.cs @@ -52,7 +52,7 @@ namespace Step.App_Start // Check if web site directory exists if (!Directory.Exists(WEBSITE_DIRECTORY)) - ExceptionManager.Manage(ERROR_LEVEL.FATAL, "Main window directory not found!"); + ExceptionManager.ManageError(ERROR_LEVEL.FATAL, "Main window directory not found!"); var options = new FileServerOptions() { @@ -65,7 +65,7 @@ namespace Step.App_Start app.UseFileServer(options); if (!ServerPortIsAvailable(ServerStartupConfig.ServerPort)) { - ExceptionManager.Manage(ERROR_LEVEL.FATAL, "Server port is already in use by another process"); + ExceptionManager.ManageError(ERROR_LEVEL.FATAL, "Server port is already in use by another process"); } } diff --git a/Step/program.cs b/Step/program.cs index 755b046a..7180560e 100644 --- a/Step/program.cs +++ b/Step/program.cs @@ -17,6 +17,7 @@ using TeamDev.SDK.MVVM; using static Step.Config.ServerConfig; using static Step.Model.Constants; using static Step.Utils.StepLogger; +using static Step.Utils.ExceptionManager; namespace Step { @@ -74,7 +75,7 @@ namespace Step // Start server services if (!ValidateAddress(ServerStartupConfig.ServerAddress)) - ExceptionManager.Manage(ERROR_LEVEL.FATAL, "IP Address not valid (must be one configured in Windows-OS)!"); + ExceptionManager.ManageError(ERROR_LEVEL.FATAL, "IP Address not valid (must be one configured in Windows-OS)!"); StartOptions opt = new StartOptions(); opt.Urls.Add("http://localhost:" + ServerStartupConfig.ServerPort.ToString()); @@ -136,9 +137,11 @@ namespace Step } } - LogException((Exception)args.ExceptionObject, ERROR_LEVEL.FATAL); + var exc = (Exception)args.ExceptionObject; + + ManageException(ERROR_LEVEL.FATAL, exc); } - + private static void CreateDirectories() { if (!Directory.Exists(MAINTENANCE_ATTACHMENT_PATH))