280 lines
13 KiB
C#
280 lines
13 KiB
C#
using Step.Database.Controllers;
|
|
using Step.NC;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using TeamDev.SDK.MVVM;
|
|
using CMS_CORE_Library.Models;
|
|
using static CMS_CORE_Library.Models.DataStructures;
|
|
using static Step.Config.ServerConfig;
|
|
using static Step.Model.Constants;
|
|
using static Step.Utils.ExceptionManager;
|
|
using static Step.Core.ThreadsSupportFunctions;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using Step.Model.DTOModels;
|
|
using Step.Utils;
|
|
using System.Globalization;
|
|
using Step.CmsConnectManager;
|
|
|
|
namespace Step.Core.ThreadsFunctions
|
|
{
|
|
public class CmsConnectFunctions
|
|
{
|
|
public static void ReadM154Data()
|
|
{
|
|
NcAdapter ncAdapter = new NcAdapter();
|
|
Stopwatch sw = new Stopwatch();
|
|
|
|
try
|
|
{
|
|
// Try connection
|
|
CmsError libraryError = ncAdapter.Connect();
|
|
if (libraryError.errorCode != 0)
|
|
ManageLibraryError(libraryError);
|
|
|
|
while (true)
|
|
{
|
|
sw.Restart();
|
|
|
|
// Check if client is connected
|
|
if (ncAdapter.numericalControl.NC_IsConnected())
|
|
{
|
|
libraryError = ncAdapter.GetM154Data(out List<M154DataModel> data, out bool MTCStatus);
|
|
if (libraryError.IsError())
|
|
ManageLibraryError(libraryError);
|
|
|
|
string ApplicationPath = ServerStartupConfig.MTCFolderPath + "\\" + ServerStartupConfig.MTCApplicationName + ".exe";
|
|
if (ServerStartupConfig.MTCFolderPath != "")
|
|
{
|
|
// Check if process exists
|
|
Process[] processes = Process.GetProcessesByName(ServerStartupConfig.MTCApplicationName);
|
|
if (!MTCStatus)
|
|
{
|
|
// Kill if is running
|
|
if (processes.Count() > 0)
|
|
processes[0].Kill();
|
|
}
|
|
else
|
|
{
|
|
// if is already running ignore
|
|
if (processes.Count() == 0)
|
|
if (File.Exists(ApplicationPath))
|
|
{
|
|
ProcessStartInfo PSI = new ProcessStartInfo(ApplicationPath);
|
|
var directory = new FileInfo(ApplicationPath).Directory;
|
|
if (directory != null)
|
|
{
|
|
PSI.WorkingDirectory = directory.FullName;
|
|
Process.Start(PSI);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (M154DataModel m154 in data)
|
|
{
|
|
//if (CmsConnectConfig.Enabled)
|
|
if (ServerStartupConfig.CmsConnectReady)
|
|
{
|
|
|
|
if (m154.Parameters.Count() >= 2 && m154.Parameters[0] == "SOUR")
|
|
{
|
|
// Convert tag string in Pascal Case and add to result string
|
|
switch (m154.Parameters[1].ToLower())
|
|
{
|
|
case "currprog":
|
|
if (m154.Parameters.Count() < 3)
|
|
return;
|
|
RedisController.WriteProductionName(m154.Process, m154.Parameters[2]);
|
|
break;
|
|
|
|
case "repstarget":
|
|
if (m154.Parameters.Count() < 3)
|
|
return;
|
|
RedisController.WriteProductionRepsTarget(m154.Process, m154.Parameters[2]);
|
|
break;
|
|
|
|
case "repsdone":
|
|
if (m154.Parameters.Count() < 3)
|
|
return;
|
|
RedisController.WriteProductionRepsDone(m154.Process, m154.Parameters[2]);
|
|
break;
|
|
|
|
default:
|
|
string notif = m154.Parameters[1];
|
|
if (m154.Parameters.Count() > 2)
|
|
notif += m154.Parameters[2];
|
|
RedisController.WriteProductionNotification(m154.Process, notif);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Write ack
|
|
libraryError = ncAdapter.WriteM154Ack((int)m154.Process);
|
|
if (libraryError.IsError())
|
|
ManageLibraryError(libraryError);
|
|
}
|
|
else //MTC
|
|
{
|
|
string stringVal = "Path_0" + m154.Process + "_";
|
|
string val = "";
|
|
|
|
// Check if command is MTC
|
|
if (m154.Parameters[0] == "MTC")
|
|
{
|
|
// Convert tag string in Pascal Case and add to result string
|
|
switch (m154.Parameters[1].ToLower())
|
|
{
|
|
case "currprog":
|
|
stringVal += "CurrProg";
|
|
break;
|
|
|
|
case "partid":
|
|
stringVal += "PartId";
|
|
break;
|
|
|
|
default:
|
|
stringVal += m154.Parameters[1];
|
|
break;
|
|
}
|
|
// Set value
|
|
if (m154.Parameters.Count() > 2)
|
|
val = m154.Parameters[2];
|
|
|
|
// Create MTC Directory if not exist
|
|
if (!Directory.Exists(ServerStartupConfig.MTCFolderPath))
|
|
Directory.CreateDirectory(ServerStartupConfig.MTCFolderPath);
|
|
|
|
string outputFileName = ServerStartupConfig.MTCFolderPath + "\\DATA\\CmsGeneralStatus.mtc";
|
|
// Create file if not exits
|
|
if (!File.Exists(outputFileName))
|
|
using (var f = File.Create(outputFileName)) { };
|
|
// Read file
|
|
List<string> fileLines = File.ReadAllLines(outputFileName).ToList();
|
|
string[] parametersInLine;
|
|
bool found = false;
|
|
for (int i = 0; i < fileLines.Count() && !found; i++)
|
|
{
|
|
// Get tag & value from row
|
|
parametersInLine = fileLines[i].Split('|');
|
|
if (parametersInLine[0] == stringVal)
|
|
{
|
|
found = true;
|
|
fileLines[i] = stringVal + "|" + val;
|
|
}
|
|
}
|
|
// If tag doesn't exists, append new line
|
|
if (!found)
|
|
fileLines.Add(stringVal + "|" + val);
|
|
|
|
// Write file
|
|
File.WriteAllLines(outputFileName, fileLines.ToArray());
|
|
|
|
// Write ack
|
|
libraryError = ncAdapter.WriteM154Ack((int)m154.Process);
|
|
if (libraryError.IsError())
|
|
ManageLibraryError(libraryError);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
RestoreConnection();
|
|
sw.Stop();
|
|
|
|
//Update thread timer
|
|
UpdateStat(MethodBase.GetCurrentMethod().Name, sw.ElapsedMilliseconds);
|
|
|
|
// Wait
|
|
Thread.Sleep(CalcSleepTime(500, (int)sw.ElapsedMilliseconds));
|
|
}
|
|
}
|
|
catch (ThreadAbortException)
|
|
{
|
|
ncAdapter.Dispose();
|
|
}
|
|
}
|
|
|
|
|
|
public static void SetupCmsConnect()
|
|
{
|
|
NcAdapter ncAdapter = new NcAdapter();
|
|
try
|
|
{
|
|
List<DTOLanguageModel> availableLanguages = LanguageController.GetLanguageListFromDirectory();
|
|
if (availableLanguages == null)
|
|
return;
|
|
|
|
ICollection<CultureInfo> cultureInfos = new List<CultureInfo>();
|
|
|
|
// Get nc available language
|
|
CmsError cmsError = ncAdapter.numericalControl.NC_GetAvailableLanguages(ref cultureInfos);
|
|
if (cmsError.IsError())
|
|
ManageLibraryError(cmsError);
|
|
|
|
// Filter available language with
|
|
availableLanguages = availableLanguages.Where(x => cultureInfos.Any(y => y.TwoLetterISOLanguageName == x.IsoId)).ToList();
|
|
|
|
// Fill redis DB
|
|
int count = 1;
|
|
Dictionary<string, string> defAlarmsNamesEn = null;
|
|
Dictionary<string, string> defAlarmsNamesIt = null;
|
|
foreach (DTOLanguageModel lang in availableLanguages)
|
|
{
|
|
Dictionary<string, string> alarmsNames = GetPlcAlarmsTranslations(lang.IsoId);
|
|
|
|
if (lang.IsoId.ToLower() == "en")
|
|
{
|
|
defAlarmsNamesEn = alarmsNames;
|
|
if (!RedisController.WriteAlarmsConfigEn(alarmsNames))
|
|
ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE);
|
|
}
|
|
else if (lang.IsoId.ToLower() == "it")
|
|
{
|
|
defAlarmsNamesIt = alarmsNames;
|
|
if (!RedisController.WriteAlarmsConfigIt(alarmsNames))
|
|
ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE);
|
|
}
|
|
else
|
|
if (!RedisController.WriteAlarmsConfigCurr(alarmsNames))
|
|
ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE);
|
|
|
|
if (count == 3)
|
|
break;
|
|
else
|
|
count++;
|
|
}
|
|
|
|
if (availableLanguages.Count < 3 && defAlarmsNamesEn != null)
|
|
if (!RedisController.WriteAlarmsConfigCurr(defAlarmsNamesEn))
|
|
ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE);
|
|
else if (availableLanguages.Count < 3 && defAlarmsNamesIt != null)
|
|
if (!RedisController.WriteAlarmsConfigCurr(defAlarmsNamesIt))
|
|
ManageError(ERROR_LEVEL.FATAL, CMS_CONNECT_SETUP_ALARM_MESSAGE);
|
|
|
|
}
|
|
catch (ThreadAbortException)
|
|
{
|
|
ncAdapter.Dispose();
|
|
}
|
|
}
|
|
|
|
|
|
public async static void SetupCmsConnectConnection()
|
|
{
|
|
MConnectSDKWrapper _sdk = new MConnectSDKWrapper();
|
|
|
|
//var connection = await _sdk.InitSDK();
|
|
//if (connection)
|
|
//{
|
|
// var activationIsNeeded = await _sdk.CheckActivationData();
|
|
// //await MessageServices.Current.Publish(CONNECT_GENERAL_DATA, null, activationIsNeeded);
|
|
//}
|
|
}
|
|
}
|
|
}
|