64ca48eb8d
Added UI connection tooltip
141 lines
3.6 KiB
C#
141 lines
3.6 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();
|
|
try
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
while (true)
|
|
{
|
|
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)
|
|
{
|
|
ncHandler.Dispose();
|
|
TryNcConnection();
|
|
}
|
|
}
|
|
|
|
public static void ReadNcGenericInfo()
|
|
{
|
|
NcHandler ncHandler = new NcHandler();
|
|
try
|
|
{
|
|
ncHandler.Connect();
|
|
while (true)
|
|
{
|
|
|
|
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)
|
|
{
|
|
ncHandler.Dispose();
|
|
TryNcConnection();
|
|
}
|
|
}
|
|
|
|
public static void ReadAxes()
|
|
{
|
|
NcHandler ncHandler = new NcHandler();
|
|
try
|
|
{
|
|
ncHandler.Connect();
|
|
|
|
while (true)
|
|
{
|
|
|
|
if (ncHandler.numericalControl.NC_IsConnected())
|
|
{
|
|
// Get the list of the axes
|
|
List<DTOAxesModel> genericData = ncHandler.GetAxesPositions();
|
|
// Send through signalR
|
|
MessageServices.Current.Publish(SEND_AXES, null, genericData);
|
|
}
|
|
Thread.Sleep(200);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ncHandler.Dispose();
|
|
TryNcConnection();
|
|
}
|
|
}
|
|
|
|
private static void WillReconnect()
|
|
{
|
|
// Stop all the NC threads
|
|
ThreadsHandler.Stop();
|
|
NcHandler ncHandler = new NcHandler();
|
|
|
|
|
|
while (!ncHandler.numericalControl.NC_IsConnected())
|
|
{
|
|
// Clear connection
|
|
try
|
|
{
|
|
// Try reconnection
|
|
ncHandler.Connect();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ncHandler.Dispose();
|
|
}
|
|
// Send through signalR
|
|
Thread.Sleep(1000);
|
|
MessageServices.Current.Publish(NC_STATUS, null, ncHandler.numericalControl.NC_IsConnected());
|
|
}
|
|
// 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();
|
|
}
|
|
}
|
|
}
|