Files
cms_thermo_active/Step.Tasks/ThreadsFunctions.cs
T
Lucio Maranta 99c2e8dc01 Config reorganization
General refactor
2018-02-09 14:59:24 +01:00

443 lines
14 KiB
C#

using Step.Core;
using Step.Model.DTOModels;
using Step.NC;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using TeamDev.SDK.MVVM;
using static CMS_CORE_Library.DataStructures;
using static Step.Utils.Constants;
using static Step.Utils.ExceptionManager;
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;
private static long ReadPowerOnTimer = 0, ReadPowerOnTimes = 0;
private static long ReadProcPPTimer = 0, ReadProcPPTimes = 0;
private static long ReadFunctionTimer = 0, ReadFunctionTimes = 0;
private static Thread ConnThread;
#region Nc Threads
public static void ReadAlarms()
{
NcHandler ncHandler = new NcHandler();
Stopwatch sw = new Stopwatch();
try
{
CmsError libraryError = ncHandler.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
// Check if client is connected
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get Alarms from NC
libraryError = ncHandler.GetNcAlarms(out DTOAlarmsModel alarms);
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
else
// Send through signalR
MessageServices.Current.Publish(SEND_ALARMS, null, alarms);
}
else
TryNcConnection();
sw.Stop();
//Send to the UI the time
ReadAlarmsTimer += sw.ElapsedMilliseconds;
ReadAlarmsTimes++;
// Wait
Thread.Sleep(200);
}
}
catch (ThreadAbortException)
{
ncHandler.Dispose();
}
}
public static void ReadNcGenericInfo()
{
NcHandler ncHandler = new NcHandler();
Stopwatch sw = new Stopwatch();
try
{
CmsError libraryError = ncHandler.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get Generic data from NC
libraryError = ncHandler.GetNcGenericData(out DTONcGenericDataModel genericData);
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
else
// Send through signalR
MessageServices.Current.Publish(SEND_GENERIC_DATA, null, genericData);
}
else
TryNcConnection();
sw.Stop();
//Send to the UI the time
ReadNcGenericInfoTimer += sw.ElapsedMilliseconds;
ReadNcGenericInfoTimes++;
Thread.Sleep(1000);
}
}
catch (ThreadAbortException)
{
ncHandler.Dispose();
}
}
public static void ReadAxes()
{
NcHandler ncHandler = new NcHandler();
Stopwatch sw = new Stopwatch();
try
{
CmsError libraryError = ncHandler.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get the list of the axes
libraryError = ncHandler.GetAxesPositions(out List<DTOAxesModel> genericData);
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
else
// Send through signalR
MessageServices.Current.Publish(SEND_AXES, null, genericData);
}
else
TryNcConnection();
sw.Stop();
//Send to the UI the time
ReadAxesTimer += sw.ElapsedMilliseconds;
ReadAxesTimes++;
Thread.Sleep(200);
}
}
catch (ThreadAbortException)
{
ncHandler.Dispose();
}
}
public static void ReadPowerOnData()
{
NcHandler ncHandler = new NcHandler();
Stopwatch sw = new Stopwatch();
try
{
CmsError libraryError = ncHandler.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
// Check if client is connected
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get Data from NC
libraryError = ncHandler.GetPowerOnData(out DTOPowerOnDataModel powerOnData);
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
else
// Send through signalR
MessageServices.Current.Publish(SEND_POWER_ON_DATA, null, powerOnData);
}
else
TryNcConnection();
sw.Stop();
//Send time to the UI
ReadPowerOnTimer += sw.ElapsedMilliseconds;
ReadPowerOnTimes++;
// Wait
Thread.Sleep(400);
}
}
catch (ThreadAbortException)
{
ncHandler.Dispose();
}
}
// Read processes part program status
public static void ReadProcessesPPStatus()
{
NcHandler ncHandler = new NcHandler();
Stopwatch sw = new Stopwatch();
try
{
// Try connection
CmsError libraryError = ncHandler.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get Data from NC
libraryError = ncHandler.GetProcessesData(out DTOProcessDataModel processesPPData);
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
else
// Send through signalR
MessageServices.Current.Publish(SEND_PROCESSES_DATA, null, processesPPData);
}
else
TryNcConnection();
sw.Stop();
//Send to the UI the time
ReadProcPPTimer += sw.ElapsedMilliseconds;
ReadProcPPTimes++;
// Wait
Thread.Sleep(200);
}
}
catch (ThreadAbortException)
{
ncHandler.Dispose();
}
}
public static void ReadEnabledFunctionality()
{
NcHandler ncHandler = new NcHandler();
Stopwatch sw = new Stopwatch();
try
{
// Try connection
CmsError libraryError = ncHandler.Connect();
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
while (true)
{
sw.Restart();
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get Data from NC
libraryError = ncHandler.GetFunctionsMappedWithNC(out List<DTORuntimeFunctionalityModel> functionsAccessList);
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
else
// Send through signalR
MessageServices.Current.Publish(SEND_FUNCTIONALITY_DATA, null, functionsAccessList);
}
else
TryNcConnection();
sw.Stop();
//Send to the UI the time
ReadFunctionTimer += sw.ElapsedMilliseconds;
ReadFunctionTimes++;
// Wait
Thread.Sleep(200);
}
}
catch (ThreadAbortException)
{
ncHandler.Dispose();
}
}
#endregion Nc Threads
#region SupportFunctions
private static void WillReconnect()
{
// Stop all the NC threads
ThreadsHandler.Stop();
StatReset();
NcHandler ncHandler = new NcHandler();
// Run loop until NC is connected
while (!ncHandler.numericalControl.NC_IsConnected())
{
// Try reconnection
CmsError cmsError = ncHandler.Connect();
if (cmsError.errorCode == CMS_ERROR_CODES.SIEMENS_ENVIRONMENT_NOT_FOUND || cmsError.errorCode == CMS_ERROR_CODES.SIEMENS_HMI_NOT_RUNNING)
ManageLibraryError(cmsError);
else if (cmsError.errorCode != CMS_ERROR_CODES.OK)
ncHandler.Dispose();
Thread.Sleep(1000);
// Send status to UI
MessageServices.Current.Publish(SEND_NC_STATUS, null, ncHandler.numericalControl.NC_IsConnected());
}
// Start/Restart NC threads
ThreadsHandler.StartWorkers();
reconnectionIsRunning = false;
}
public static void TryNcConnection()
{
if (reconnectionIsRunning == false)
{ // Set thread as running state
reconnectionIsRunning = true;
// Start reconnection thread
ConnThread = new Thread(() =>
WillReconnect()
);
ConnThread.Start();
}
}
public static void AbortNcConnection()
{
if (ConnThread != null && ConnThread.IsAlive)
ConnThread.Abort();
}
public static void ManageLibraryError(CmsError cmsError)
{
switch (cmsError.errorCode)
{
case CMS_ERROR_CODES.NC_PROD_ERROR:
Manage(ERROR_LEVEL.WARNING, cmsError.message);
break;
case CMS_ERROR_CODES.NOT_CONNECTED:
TryNcConnection(); // If not connected try reconnection
break;
case CMS_ERROR_CODES.PROC_NOT_FOUND:
break;
case CMS_ERROR_CODES.FUNCTION_NOT_ALLOWED:
break;
case CMS_ERROR_CODES.BIT_NOT_IN_RANGE:
break;
case CMS_ERROR_CODES.BYTE_NOT_IN_RANGE:
break;
case CMS_ERROR_CODES.INTERNAL_ERROR:
break;
case CMS_ERROR_CODES.INCORRECT_PARAMETERS:
break;
case CMS_ERROR_CODES.NC_LANGUAGE_ERROR:
break;
case CMS_ERROR_CODES.SIEMENS_ENVIRONMENT_NOT_FOUND:
Manage(ERROR_LEVEL.FATAL, cmsError.message);
break;
case CMS_ERROR_CODES.SIEMENS_HMI_NOT_RUNNING:
Manage(ERROR_LEVEL.WARNING, cmsError.message);
TryNcConnection(); // If not connected try reconnection
break;
}
}
internal static void StatThread()
{
while (true)
{
if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadAlarms") && ReadAlarmsTimes != 0)
{
ThreadsHandler.RunningThreadStatus["ReadAlarms"] = (ReadAlarmsTimer / ReadAlarmsTimes) + " mS";
ReadAlarmsTimer = 0;
ReadAlarmsTimes = 0;
}
if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadNcGenericInfo") && ReadNcGenericInfoTimes != 0)
{
ThreadsHandler.RunningThreadStatus["ReadNcGenericInfo"] = (ReadNcGenericInfoTimer / ReadNcGenericInfoTimes) + " mS";
ReadNcGenericInfoTimer = 0;
ReadNcGenericInfoTimes = 0;
}
if (ThreadsHandler.RunningThreadStatus.ContainsKey("ReadAxes") && ReadAxesTimes != 0)
{
ThreadsHandler.RunningThreadStatus["ReadAxes"] = (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("ReadRuntimeFunctionAccess") && ReadFunctionTimes != 0)
{
ThreadsHandler.RunningThreadStatus["ReadRuntimeFunctionAccess"] = (ReadFunctionTimer / ReadFunctionTimes) + " mS";
ReadFunctionTimer = 0;
ReadFunctionTimes = 0;
}
MessageServices.Current.Publish(SEND_THREADS_STATUS, null, ThreadsHandler.RunningThreadStatus);
Thread.Sleep(2000);
}
}
private static void StatReset()
{
ReadAlarmsTimer = 0;
ReadAlarmsTimes = 0;
ReadNcGenericInfoTimer = 0;
ReadNcGenericInfoTimes = 0;
ReadAxesTimer = 0;
ReadAxesTimes = 0;
}
#endregion SupportFunctions
}