using Adaptation.Helpers; using Infineon.Yoda; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Threading; namespace Adaptation.Si { internal partial class Transport { private static object _IfxTransport; private static ConfigData _ConfigData; internal static void Initialize(ConfigData configData) { _IfxTransport = null; _ConfigData = configData; } internal static List Setup(bool useSleep, bool setIfxTransport) { List results = new List(); if (useSleep) { for (int i = 1; i < 4; i++) Thread.Sleep(500); } if (setIfxTransport) { results.Add(string.Concat("IfxTransport Subject: ", _ConfigData.IfxSubject)); IfxDoc ifxDoc = new IfxDoc(); ifxDoc.Add(IfxConst.SUBJECT_PREFIX, _ConfigData.IfxSubjectPrefix); ifxDoc.Add(IfxConst.IFX_CHANNEL, _ConfigData.IfxChannel); ifxDoc.Add(IfxConst.IFX_CONFIGURATION_LOCATION, _ConfigData.IfxConfigurationLocation); ifxDoc.Add(IfxConst.IFX_CONFIGURATION_LOCATION_LOCAL_COPY, _ConfigData.IfxConfigurationLocationLocalCopy); 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 = _ConfigData.IfxSubject.Split('|'); foreach (string subject in subjects) ifxTransport.Subscribe(string.Concat(_ConfigData.IfxSubjectPrefix, ".", 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(); IfxDoc itemDoc; IfxDoc jobDoc = new IfxDoc(); IfxDoc lotDoc = new IfxDoc(); IfxDoc recipeDoc = new IfxDoc(); List itemDocs = new List(); 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 CultureInfo("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(_ConfigData.FileConnectorConfiguration.SourceFileLocation)) { string directory = string.Concat(_ConfigData.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 Job(_ConfigData, mid); if (job.IsAreaSi) { IfxDoc sendReply = GetJobsReply(job); ifxEnvelope.Transport.SendReply(ifxEnvelope, sendReply); if (!string.IsNullOrEmpty(_ConfigData.FileConnectorConfiguration.TargetFileLocation)) { string directory = string.Concat(_ConfigData.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 null) && !string.IsNullOrEmpty(pdsfFileLogistics)) MoveSourceFiles(sourceFiles, pdsfFileLogistics, calendar); } catch (Exception exception) { try { Eaf.Core.Smtp.ISmtp smtp = Eaf.Core.Backbone.Instance.GetBackboneComponentsOfType().SingleOrDefault(); Eaf.Core.Smtp.EmailMessage emailMessage = new Eaf.Core.Smtp.EmailMessage(string.Concat("Exception:", _ConfigData.EquipmentElementName, ":MainTransport_ReliableMessage"), string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace), Eaf.Core.Smtp.MailPriority.High); smtp.Send(emailMessage); } catch (Exception) { } string directory = _ConfigData.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) { } } } } } }