Added new logger

Fix thread time counters
This commit is contained in:
Lucio Maranta
2020-01-30 12:28:06 +01:00
parent 2251e63d67
commit 98fe230a78
8 changed files with 207 additions and 244 deletions
+159 -201
View File
@@ -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<string, long> Timers = new ConcurrentDictionary<string, long>();
private static ConcurrentDictionary<string, long> Counter = new ConcurrentDictionary<string, long>();
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<int, byte> updatedStatus, out Dictionary<int, uint> 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<int, byte> updatedStatus, out Dictionary<int, uint> 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<int, bool> 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<int, bool> 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)