228 lines
10 KiB
C#
228 lines
10 KiB
C#
using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration;
|
|
using Adaptation.Shared.Methods;
|
|
using Infineon.Yoda;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
|
|
namespace Adaptation.FileHandlers.TIBCO.Transport;
|
|
|
|
internal partial class Main
|
|
{
|
|
|
|
private static ISMTP _SMTP;
|
|
private static object _IfxTransport;
|
|
private static string _CellInstanceName;
|
|
private static string _LSL2SQLConnectionString;
|
|
private static string _OIContextDataSearchPath;
|
|
private static string _OIContextDataPendingPath;
|
|
private static string _OIContextDataResultsPath;
|
|
private static FileConnectorConfiguration _FileConnectorConfiguration;
|
|
|
|
internal static void Initialize(ISMTP smtp, string cellInstanceName, FileConnectorConfiguration fileConnectorConfiguration, string oiContextDataPendingPath, string oiContextDataResultsPath, string oiContextDataSearchPath, string lsl2SQLConnectionString)
|
|
{
|
|
_SMTP = smtp;
|
|
_IfxTransport = null;
|
|
_CellInstanceName = cellInstanceName;
|
|
_LSL2SQLConnectionString = lsl2SQLConnectionString;
|
|
_OIContextDataSearchPath = oiContextDataSearchPath;
|
|
_OIContextDataPendingPath = oiContextDataPendingPath;
|
|
_OIContextDataResultsPath = oiContextDataResultsPath;
|
|
_FileConnectorConfiguration = fileConnectorConfiguration;
|
|
}
|
|
|
|
internal static List<string> Setup(bool useSleep, bool setIfxTransport, string tibcoParameterChannel, string tibcoParameterSubjectPrefix, string tibcoParameterConfigurationLocation, string tibcoParameterConfigurationLocationCopy, string tibcoParameterSubject)
|
|
{
|
|
List<string> results = new();
|
|
if (useSleep)
|
|
{
|
|
for (int i = 1; i < 4; i++)
|
|
Thread.Sleep(500);
|
|
}
|
|
if (setIfxTransport)
|
|
{
|
|
results.Add(string.Concat("IfxTransport Subject: ", tibcoParameterSubject));
|
|
IfxDoc ifxDoc = new();
|
|
ifxDoc.Add(IfxConst.SUBJECT_PREFIX, tibcoParameterSubjectPrefix);
|
|
ifxDoc.Add(IfxConst.IFX_CHANNEL, tibcoParameterChannel);
|
|
ifxDoc.Add(IfxConst.IFX_CONFIGURATION_LOCATION, tibcoParameterConfigurationLocation);
|
|
ifxDoc.Add(IfxConst.IFX_CONFIGURATION_LOCATION_LOCAL_COPY, tibcoParameterConfigurationLocationCopy);
|
|
results.Add(string.Concat("IfxTransport Config: ", ifxDoc));
|
|
_IfxTransport = new IfxTransport();
|
|
IfxTransport ifxTransport = (IfxTransport)_IfxTransport;
|
|
ifxTransport.Create(ifxDoc);
|
|
if (useSleep)
|
|
{
|
|
for (int i = 1; i < 10; i++)
|
|
Thread.Sleep(500);
|
|
}
|
|
results.Add(string.Concat("IfxTransport Current Daemon: ", ifxTransport.CurrentDaemon));
|
|
results.Add(string.Concat("IfxTransport Current Network: ", ifxTransport.CurrentNetwork));
|
|
results.Add(string.Concat("IfxTransport Current Service: ", ifxTransport.CurrentService));
|
|
results.Add(string.Concat("IfxTransport Current PoolName: ", ifxTransport.CurrentPoolName));
|
|
}
|
|
for (int i = 1; i < 3; i++)
|
|
Thread.Sleep(500);
|
|
if (_IfxTransport is null)
|
|
throw new Exception();
|
|
else
|
|
{
|
|
IfxTransport ifxTransport = (IfxTransport)_IfxTransport;
|
|
string[] subjects = tibcoParameterSubject.Split('|');
|
|
foreach (string subject in subjects)
|
|
ifxTransport.Subscribe(string.Concat(tibcoParameterSubjectPrefix, ".", subject));
|
|
ifxTransport.ReliableMessage += MainTransport_ReliableMessage;
|
|
for (int i = 1; i < 3; i++)
|
|
Thread.Sleep(500);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static void MoveSourceFiles(string[] sourceFiles, string pdsfFileLogistics, Calendar calendar)
|
|
{
|
|
DateTime dateTime;
|
|
string weekOfYear;
|
|
string checkDirectory;
|
|
foreach (string pdsfFile in sourceFiles)
|
|
{
|
|
if (pdsfFile == pdsfFileLogistics)
|
|
continue;
|
|
dateTime = new FileInfo(pdsfFile).LastWriteTime;
|
|
weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
checkDirectory = string.Concat(Path.GetDirectoryName(pdsfFile), @"\_ Logistics Archive\", dateTime.ToString("yyyy"), "_Week_", weekOfYear);
|
|
if (!Directory.Exists(checkDirectory))
|
|
_ = Directory.CreateDirectory(checkDirectory);
|
|
try
|
|
{ File.Move(pdsfFile, string.Concat(checkDirectory, @"\", Path.GetFileName(pdsfFile))); }
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
|
|
private static string GetJobsMID(IfxDoc envelopeDocument)
|
|
{
|
|
string mid;
|
|
if (envelopeDocument is null || !envelopeDocument.FieldExists("LotName"))
|
|
mid = string.Empty;
|
|
else
|
|
mid = envelopeDocument.GetFieldByName("LotName").ToString();
|
|
return mid;
|
|
}
|
|
|
|
private static IfxDoc GetJobsReply(Job job)
|
|
{
|
|
IfxDoc result = new();
|
|
IfxDoc itemDoc;
|
|
IfxDoc jobDoc = new();
|
|
IfxDoc lotDoc = new();
|
|
IfxDoc recipeDoc = new();
|
|
List<IfxDoc> itemDocs = new();
|
|
jobDoc.Add("AutomationMode", job.AutomationMode);
|
|
jobDoc.Add("CreationTimestamp", job.DateTime);
|
|
jobDoc.Add("CreationUser", "-");
|
|
jobDoc.Add("CurrentState", true);
|
|
jobDoc.Add("Equipment", job.Equipment);
|
|
jobDoc.Add("JobName", job.JobName);
|
|
jobDoc.Add("LastUpdateTimestamp", job.DateTime);
|
|
jobDoc.Add("LastUpdateUser", "-");
|
|
jobDoc.Add("ProcessType", job.ProcessType);
|
|
jobDoc.Add("StateModel", job.StateModel);
|
|
jobDoc.Add("Status", "-");
|
|
lotDoc.Add("BasicType", job.BasicType);
|
|
lotDoc.Add("IsActive", true);
|
|
lotDoc.Add("LotName", job.LotName);
|
|
lotDoc.Add("LotState", "-");
|
|
lotDoc.Add("PackageName", job.PackageName);
|
|
lotDoc.Add("ProcessSpecName", job.ProcessSpecName);
|
|
lotDoc.Add("ProductName", job.ProductName);
|
|
lotDoc.Add("Qty", job.Qty);
|
|
lotDoc.Add("Qty2", "-");
|
|
recipeDoc.Add("RecipeName", job.RecipeName);
|
|
lotDoc.Add("SpecName", "-");
|
|
foreach (Item item in job.Items)
|
|
{
|
|
itemDoc = new IfxDoc();
|
|
itemDoc.Add("Name", item.Name);
|
|
itemDoc.Add("Type", item.Type);
|
|
itemDoc.Add("Number", item.Number);
|
|
itemDoc.Add("Qty", item.Qty);
|
|
itemDoc.Add("CarrierName", item.CarrierName);
|
|
itemDocs.Add(itemDoc);
|
|
}
|
|
jobDoc.Add("Recipe", recipeDoc);
|
|
lotDoc.Add("Items", itemDocs.ToArray());
|
|
jobDoc.Add("Lots", new IfxDoc[] { lotDoc });
|
|
result.Add("FAJobs", new IfxDoc[] { jobDoc });
|
|
result.Add("IFX_ECD", "0");
|
|
result.Add("IFX_ETX", 0);
|
|
return result;
|
|
}
|
|
|
|
private static void MainTransport_ReliableMessage(string subject, string replySubject, IfxEnvelope ifxEnvelope)
|
|
{
|
|
try
|
|
{
|
|
string mid = string.Empty;
|
|
string[] sourceFiles = null;
|
|
DateTime dateTime = DateTime.Now;
|
|
string pdsfFileLogistics = string.Empty;
|
|
IfxDoc envelopeDocument = ifxEnvelope.ExtractDocument();
|
|
CultureInfo cultureInfo = new("en-US");
|
|
Calendar calendar = cultureInfo.Calendar;
|
|
string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
string weekOfYearSegment = string.Concat(@"\", dateTime.ToString("yyyy"), "_Week_", weekOfYear, @"\", dateTime.ToString("yyyy-MM-dd"));
|
|
if (!string.IsNullOrEmpty(_FileConnectorConfiguration.SourceFileLocation))
|
|
{
|
|
string directory = string.Concat(_FileConnectorConfiguration.SourceFileLocation, weekOfYearSegment);
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
string fileName = string.Concat(directory, @"\", subject.Replace(".", "~"), " - ", DateTime.Now.Ticks, ".xml");
|
|
try
|
|
{ envelopeDocument.SaveAsXml(fileName); }
|
|
catch (Exception) { }
|
|
}
|
|
if (!subject.EndsWith("GETJOBS"))
|
|
throw new Exception();
|
|
mid = GetJobsMID(envelopeDocument);
|
|
Job job = new(_OIContextDataPendingPath, _OIContextDataResultsPath, _OIContextDataSearchPath, _LSL2SQLConnectionString, mid);
|
|
if (job.IsAreaSi)
|
|
{
|
|
IfxDoc sendReply = GetJobsReply(job);
|
|
ifxEnvelope.Transport.SendReply(ifxEnvelope, sendReply);
|
|
if (!string.IsNullOrEmpty(_FileConnectorConfiguration.TargetFileLocation))
|
|
{
|
|
string directory = string.Concat(_FileConnectorConfiguration.TargetFileLocation, weekOfYearSegment);
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
string fileName = string.Concat(directory, @"\", subject.Replace(".", "~"), " - ", DateTime.Now.Ticks, ".xml");
|
|
try
|
|
{ sendReply.SaveAsXml(fileName); }
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
if (sourceFiles is not null && !string.IsNullOrEmpty(pdsfFileLogistics))
|
|
MoveSourceFiles(sourceFiles, pdsfFileLogistics, calendar);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
subject = string.Concat("Exception:", _CellInstanceName, ":MainTransport_ReliableMessage");
|
|
string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace);
|
|
try
|
|
{ _SMTP.SendHighPriorityEmailMessage(subject, body); }
|
|
catch (Exception) { }
|
|
string directory = _FileConnectorConfiguration.ErrorTargetFileLocation;
|
|
if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory))
|
|
{
|
|
string fileName = string.Concat(directory, @"\", subject.Replace(".", "~"), " - ", DateTime.Now.Ticks, ".txt");
|
|
try
|
|
{ File.WriteAllLines(fileName, new string[] { exception.Message, string.Empty, string.Empty, exception.StackTrace }); }
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
}
|
|
|
|
} |