127 lines
4.4 KiB
C#
127 lines
4.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using TeamDev.SDK.MVVM;
|
|
using static Thermo.Active.Model.Constants;
|
|
|
|
namespace Thermo.Active.Core
|
|
{
|
|
public static class ThreadsHandler
|
|
{
|
|
private static List<Action> ThreadsFunctionsList = new List<Action>
|
|
{
|
|
ThreadsFunctions.ManageWatchdog,
|
|
ThreadsFunctions.ManageStatusCommand,
|
|
ThreadsFunctions.ManageConfRequest,
|
|
ThreadsFunctions.ManageProdUpdate,
|
|
ThreadsFunctions.ReadAlarms,
|
|
ThreadsFunctions.ReadPowerOnData,
|
|
ThreadsFunctions.StatThread,
|
|
//ThreadsFunctions.ReadProcessesPPStatus,
|
|
ThreadsFunctions.ReadEnabledFunctionality,
|
|
ThreadsFunctions.ReadExpiredMaintenances,
|
|
//ThreadsFunctions.ReadAxesPositionsData,
|
|
ThreadsFunctions.ReadUserSoftKeysData,
|
|
//ThreadsFunctions.ReadHeadsData,
|
|
//ThreadsFunctions.ReadAxesNamesData,
|
|
//ThreadsFunctions.ReadActiveProgramData,
|
|
//ThreadsFunctions.ReadPartProgramQueueData,
|
|
ThreadsFunctions.ReadScadaData,
|
|
ThreadsFunctions.ReadM154Data, // levare?
|
|
// ThreadsFunctions.ReadNcSoftKeysData,
|
|
ThreadsFunctions.ReadGaugeData,
|
|
ThreadsFunctions.ReadProdInfoData,
|
|
ThreadsFunctions.ReadProdCycleData,
|
|
ThreadsFunctions.ReadRecipeData,
|
|
ThreadsFunctions.ReadWarmersData,
|
|
ThreadsFunctions.ReadModulesData
|
|
};
|
|
private static Action ThreadSetupCmsConnect = ThreadsFunctions.SetupCmsConnect;
|
|
|
|
private volatile static List<Thread> RunningThreadsList = new List<Thread>();
|
|
public static Thread StartClient;
|
|
|
|
internal volatile static Dictionary<string, string> RunningThreadStatus = new Dictionary<string, string>();
|
|
|
|
public static void Start()
|
|
{
|
|
ThreadsFunctions.RestoreConnection();
|
|
|
|
//Setup CMS Connect
|
|
//if(Config.ServerConfig.CmsConnectConfig.Enabled)
|
|
if (Config.ServerConfig.ServerStartupConfig.CmsConnectReady)
|
|
{
|
|
Thread t = new Thread(() => ThreadSetupCmsConnect());
|
|
t.Start();
|
|
Thread.Sleep(50);
|
|
}
|
|
}
|
|
|
|
public static void StartWorkers()
|
|
{
|
|
lock (RunningThreadStatus)
|
|
RunningThreadStatus.Clear();
|
|
|
|
// For each function run in the list a thread
|
|
ThreadsFunctionsList.ForEach(threadFunction =>
|
|
{
|
|
// Run new Thread in the list
|
|
Thread t = new Thread(() =>
|
|
threadFunction()
|
|
);
|
|
t.Start();
|
|
// aggiungo pausa x evitare sovrapposizioni chiamate
|
|
Thread.Sleep(50);
|
|
// Add thread to running threads list
|
|
lock (RunningThreadsList)
|
|
RunningThreadsList.Add(t);
|
|
|
|
RunningThreadStatus.Add(threadFunction.Method.Name, "---");
|
|
});
|
|
|
|
MessageServices.Current.Publish(SEND_THREADS_STATUS, null, RunningThreadStatus);
|
|
}
|
|
|
|
public static void Stop()
|
|
{
|
|
// Stop each thread
|
|
lock (RunningThreadsList)
|
|
RunningThreadsList.ForEach(thread =>
|
|
{
|
|
thread.Abort();
|
|
});
|
|
|
|
// Remove threads from running status
|
|
lock (RunningThreadsList)
|
|
RunningThreadsList.Clear();
|
|
|
|
lock (RunningThreadStatus)
|
|
{
|
|
RunningThreadStatus.Clear();
|
|
RunningThreadStatus.Add("TryNcConnection", "---");
|
|
}
|
|
|
|
MessageServices.Current.Publish(SEND_THREADS_STATUS, null, RunningThreadStatus);
|
|
}
|
|
|
|
public static void Close()
|
|
{
|
|
//Abort Nc Read Threads
|
|
RunningThreadsList.ForEach(thread =>
|
|
{
|
|
thread.Abort();
|
|
});
|
|
|
|
if (ThreadsHandler.StartClient != null)
|
|
ThreadsHandler.StartClient.Abort();
|
|
|
|
//Abort Connect Thread
|
|
ThreadsFunctions.AbortNcConnection();
|
|
}
|
|
|
|
public static void StartClientFromUI()
|
|
{
|
|
ThreadsFunctions.StartCMSClient();
|
|
}
|
|
}
|
|
} |