Files
cms_thermo_active/Step.Tasks/ThreadsFunctions.cs
T
Lucio Maranta 5bd278fa69 * Added NC comunication
* Added NC handler class
* Fixed threads
* Added threads: AXES, ALARMS, GENERIC INFO + API
* Refactoring
2017-12-20 17:17:02 +01:00

124 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using CMS_CORE;
using Step.Model.DTOModels;
using TeamDev.SDK.MVVM;
using static Step.Utils.Constants;
using Step.NC;
using System.Diagnostics;
using Step.Core;
public static class ThreadsFunctions
{
public static bool reconnectionIsRunning = false;
public static void ReadAlarmsAsync()
{
NcHandler ncHandler = new NcHandler();
while (true)
{
try
{
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get Alarms from NC
DTOAlarmsModel alarms = ncHandler.GetNcAlarms();
// Send through signalR
MessageServices.Current.Publish(SEND_ALARMS, null, alarms);
}
Thread.Sleep(200);
}
catch (Exception ex)
{
Send(ncHandler);
}
}
}
public static void ReadNcGenericInfo()
{
NcHandler ncHandler = new NcHandler();
while (true)
{
try
{
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get Generic data from NC
DTONcGenericDataModel genericData = ncHandler.GetNcGenericData();
// Send through signalR
MessageServices.Current.Publish(SEND_GENERIC_DATA, null, genericData);
}
Thread.Sleep(1000);
}
catch (Exception ex)
{
Send(ncHandler);
}
}
}
public static void ReadAxes()
{
NcHandler ncHandler = new NcHandler();
while (true)
{
try
{
if (ncHandler.numericalControl.NC_IsConnected())
{
// Get the list of the axes
List<DTOAxesModel> genericData = ncHandler.GetAxesPositions();
// Send through signalR
MessageServices.Current.Publish(SEND_ALARMS, null, genericData);
}
Thread.Sleep(200);
}
catch (Exception ex)
{
Send(ncHandler);
}
}
}
public static void WillReconnect(NcHandler ncHandler)
{
// Stop all the NC threads
ThreadsHandler.Stop();
while (!ncHandler.numericalControl.NC_IsConnected())
{
Console.WriteLine("Riconnessione");
// Clear connection
ncHandler.Dispose();
// Try reconnection
ncHandler = new NcHandler();
Thread.Sleep(1000);
}
// Restart NC threads
ThreadsHandler.Start();
reconnectionIsRunning = false;
}
private static void Send(NcHandler ncHandler)
{
if(reconnectionIsRunning == false)
{ // Set reconnection threas as running
reconnectionIsRunning = true;
// Start reconnection thread
Thread t = new Thread(() =>
WillReconnect(ncHandler)
);
t.Start();
}
}
}