229 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			229 lines
		
	
	
		
			11 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.Net.Http;
 | 
						|
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 _MetrologyFileShare;
 | 
						|
    private static string _BarcodeHostFileShare;
 | 
						|
    private static string _LSL2SQLConnectionString;
 | 
						|
    private static string _TibcoParameterSubjectPrefix;
 | 
						|
    private static HttpClient _HttpClient;
 | 
						|
    private static FileConnectorConfiguration _FileConnectorConfiguration;
 | 
						|
 | 
						|
    internal static void Initialize(ISMTP smtp, string cellInstanceName, FileConnectorConfiguration fileConnectorConfiguration, string lsl2SQLConnectionString, string metrologyFileShare, string barcodeHostFileShare, HttpClient httpClient)
 | 
						|
    {
 | 
						|
        _SMTP = smtp;
 | 
						|
        _IfxTransport = null;
 | 
						|
        _HttpClient = httpClient;
 | 
						|
        _CellInstanceName = cellInstanceName;
 | 
						|
        _MetrologyFileShare = metrologyFileShare;
 | 
						|
        _TibcoParameterSubjectPrefix = string.Empty;
 | 
						|
        _BarcodeHostFileShare = barcodeHostFileShare;
 | 
						|
        _LSL2SQLConnectionString = lsl2SQLConnectionString;
 | 
						|
        _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)
 | 
						|
        {
 | 
						|
            _TibcoParameterSubjectPrefix = tibcoParameterSubjectPrefix;
 | 
						|
            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(nameof(Job.AutomationMode), job.AutomationMode);
 | 
						|
        jobDoc.Add("CreationTimestamp", job.DateTime);
 | 
						|
        jobDoc.Add(nameof(Job.CreationUser), job.CreationUser);
 | 
						|
        jobDoc.Add("CurrentState", true);
 | 
						|
        jobDoc.Add(nameof(Job.Equipment), job.Equipment);
 | 
						|
        jobDoc.Add(nameof(Job.JobName), job.JobName);
 | 
						|
        jobDoc.Add("LastUpdateTimestamp", job.DateTime);
 | 
						|
        jobDoc.Add(nameof(Job.LastUpdateUser), job.LastUpdateUser);
 | 
						|
        jobDoc.Add(nameof(Job.ProcessType), job.ProcessType);
 | 
						|
        jobDoc.Add(nameof(Job.StateModel), job.StateModel);
 | 
						|
        jobDoc.Add(nameof(Job.Status), job.Status);
 | 
						|
        lotDoc.Add(nameof(Job.BasicType), job.BasicType);
 | 
						|
        lotDoc.Add("IsActive", true);
 | 
						|
        lotDoc.Add(nameof(Job.LotName), job.LotName);
 | 
						|
        lotDoc.Add(nameof(Job.LotState), job.LotState);
 | 
						|
        lotDoc.Add(nameof(Job.PackageName), job.PackageName);
 | 
						|
        lotDoc.Add(nameof(Job.ProcessSpecName), job.ProcessSpecName);
 | 
						|
        lotDoc.Add(nameof(Job.ProductName), job.ProductName);
 | 
						|
        lotDoc.Add(nameof(Job.Qty), job.Qty);
 | 
						|
        lotDoc.Add(nameof(Job.Qty2), job.Qty2);
 | 
						|
        recipeDoc.Add(nameof(Job.RecipeName), job.RecipeName);
 | 
						|
        lotDoc.Add(nameof(Job.SpecName), job.SpecName);
 | 
						|
        foreach (Item item in job.Items)
 | 
						|
        {
 | 
						|
            itemDoc = new IfxDoc();
 | 
						|
            itemDoc.Add(nameof(Item.Name), item.Name);
 | 
						|
            itemDoc.Add(nameof(Item.Type), item.Type);
 | 
						|
            itemDoc.Add(nameof(Item.Number), item.Number);
 | 
						|
            itemDoc.Add(nameof(Item.Qty), item.Qty);
 | 
						|
            itemDoc.Add(nameof(Item.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.Contains(_TibcoParameterSubjectPrefix))
 | 
						|
                throw new Exception("Invalid Subject");
 | 
						|
            mid = GetJobsMID(envelopeDocument);
 | 
						|
            Job job = new(_LSL2SQLConnectionString, _MetrologyFileShare, _BarcodeHostFileShare, _HttpClient, 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) { }
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
} |