Files
cms_thermo_active/Step.Tasks/ThreadsFunctions.cs
T
Lucio Maranta fa485d902b * Added machine model, and new database relations
* Added first centralized database config and  added machine self-registration into db
* Fix database migration with new database
* Refactor
2018-01-17 10:24:18 +01:00

255 lines
7.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using Step.Model.DTOModels;
using TeamDev.SDK.MVVM;
using Step.NC;
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 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
CmsError libraryError = ncHandler.GetNcAlarms(out DTOAlarmsModel alarms);
if (libraryError.errorCode != 0)
ManageLibraryError(libraryError);
// 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);
}
}
catch (ThreadAbortException ex)
{
ncHandler.Dispose();
}
}
public static void Read_Nc_Generic_Info()
{
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);
}
sw.Stop();
//Send to the UI the time
ReadNcGenericInfoTimer += sw.ElapsedMilliseconds;
ReadNcGenericInfoTimes++;
Thread.Sleep(1000);
}
}
catch (ThreadAbortException ex)
{
ncHandler.Dispose();
}
}
public static void Read_Axes()
{
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);
// 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);
}
}
catch (ThreadAbortException ex)
{
ncHandler.Dispose();
}
}
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(MVVM_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
Thread t = new Thread(() =>
WillReconnect()
);
t.Start();
}
}
public static void ManageLibraryError(CmsError cmsError)
{
switch (cmsError.errorCode)
{
case CMS_ERROR_CODES.NC_PROD_ERROR:
TryNcConnection();
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:
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;
}
}