using Adaptation.Eaf.Management.ConfigurationData.CellAutomation; using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration; using Adaptation.Shared; using Adaptation.Shared.Methods; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text.Json; namespace Adaptation.FileHandlers.jpeg; public class FileRead : Shared.FileRead, IFileRead { protected string _LastText; protected long _LastChange; protected readonly Dictionary _Reactors; public FileRead(ISMTP smtp, Dictionary fileParameter, string cellInstanceName, string cellInstanceConnectionName, FileConnectorConfiguration fileConnectorConfiguration, string equipmentTypeName, string parameterizedModelObjectDefinitionType, IList modelObjectParameters, string equipmentDictionaryName, Dictionary> dummyRuns, bool useCyclicalForDescription, bool isEAFHosted) : base(new Description(), true, smtp, fileParameter, cellInstanceName, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, dummyRuns, useCyclicalForDescription, isEAFHosted) { _MinFileLength = 10; _NullData = string.Empty; _LastText = string.Empty; _Logistics = new Logistics(this); if (_FileParameter is null) throw new Exception(cellInstanceConnectionName); if (_ModelObjectParameterDefinitions is null) throw new Exception(cellInstanceConnectionName); if (_IsDuplicator) throw new Exception(cellInstanceConnectionName); _LastChange = DateTime.Now.AddDays(-1).Ticks; _Reactors = new Dictionary(); string reactor = string.Concat("Reactor.", cellInstanceName); ModelObjectParameterDefinition[] reactors = GetProperties(cellInstanceConnectionName, modelObjectParameters, "Reactor."); reactors = (from l in reactors where l.Name.EndsWith(".FilePrefix") select l).ToArray(); if (!reactors.Any()) throw new Exception(cellInstanceConnectionName); foreach (ModelObjectParameterDefinition modelObjectParameterDefinition in reactors) _Reactors.Add(modelObjectParameterDefinition.Name.Split('.')[1], modelObjectParameterDefinition.Value); string entryAssemblyLocationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string x86 = Path.Combine(entryAssemblyLocationDirectory, "x86"); if (!Directory.Exists(x86)) _ = Directory.CreateDirectory(x86); string tessdata = Path.Combine(entryAssemblyLocationDirectory, "tessdata"); if (!Directory.Exists(tessdata)) _ = Directory.CreateDirectory(tessdata); string pdfttfSource = Path.Combine(entryAssemblyLocationDirectory, "pdf.ttf"); string pdfttfDestination = Path.Combine(tessdata, Path.GetFileName(pdfttfSource)); if (File.Exists(pdfttfSource) && !File.Exists(pdfttfDestination)) File.Copy(pdfttfSource, pdfttfDestination); string tesseract41dllSource = Path.Combine(entryAssemblyLocationDirectory, "tesseract41.dll"); string tesseract41dllDestination = Path.Combine(x86, Path.GetFileName(tesseract41dllSource)); if (File.Exists(tesseract41dllSource) && !File.Exists(tesseract41dllDestination)) File.Copy(tesseract41dllSource, tesseract41dllDestination); string engtraineddataSource = Path.Combine(entryAssemblyLocationDirectory, "eng.traineddata"); string engtraineddataDestination = Path.Combine(tessdata, Path.GetFileName(engtraineddataSource)); if (File.Exists(engtraineddataSource) && !File.Exists(engtraineddataDestination)) File.Copy(engtraineddataSource, engtraineddataDestination); string leptonica1800dllSource = Path.Combine(entryAssemblyLocationDirectory, "leptonica-1.80.0.dll"); string leptonica1800dllDestination = Path.Combine(x86, Path.GetFileName(leptonica1800dllSource)); if (File.Exists(leptonica1800dllSource) && !File.Exists(leptonica1800dllDestination)) File.Copy(leptonica1800dllSource, leptonica1800dllDestination); } void IFileRead.Move(Tuple> extractResults, Exception exception) => Move(extractResults, exception); void IFileRead.WaitForThread() => WaitForThread(thread: null, threadExceptions: null); string IFileRead.GetEventDescription() { string result = _Description.GetEventDescription(); return result; } List IFileRead.GetHeaderNames() { List results = _Description.GetHeaderNames(); return results; } string[] IFileRead.Move(Tuple> extractResults, string to, string from, string resolvedFileLocation, Exception exception) { string[] results = Move(extractResults, to, from, resolvedFileLocation, exception); return results; } JsonProperty[] IFileRead.GetDefault() { JsonProperty[] results = _Description.GetDefault(this, _Logistics); return results; } Dictionary IFileRead.GetDisplayNamesJsonElement() { Dictionary results = _Description.GetDisplayNamesJsonElement(this); return results; } List IFileRead.GetDescriptions(IFileRead fileRead, List tests, IProcessData processData) { List results = _Description.GetDescriptions(fileRead, _Logistics, tests, processData); return results; } Tuple> IFileRead.GetExtractResult(string reportFullPath, string eventName) { Tuple> results; if (string.IsNullOrEmpty(eventName)) throw new Exception(); _ReportFullPath = reportFullPath; DateTime dateTime = DateTime.Now; results = GetExtractResult(reportFullPath, dateTime); if (results.Item3 is null) results = new Tuple>(results.Item1, Array.Empty(), JsonSerializer.Deserialize("[]"), results.Item4); if (results.Item3.Length > 0 && _IsEAFHosted) WritePDSF(this, results.Item3); UpdateLastTicksDuration(DateTime.Now.Ticks - dateTime.Ticks); return results; } Tuple> IFileRead.ReExtract() { Tuple> results; List headerNames = _Description.GetHeaderNames(); Dictionary keyValuePairs = _Description.GetDisplayNamesJsonElement(this); results = ReExtract(this, headerNames, keyValuePairs); return results; } void IFileRead.CheckTests(Test[] tests, bool extra) => throw new Exception(string.Concat("Not ", nameof(_IsDuplicator))); private Tuple> GetExtractResult(string reportFullPath, DateTime dateTime) { Tuple> results = new(string.Empty, null, null, new List()); _Logistics = new Logistics(this, reportFullPath, useSplitForMID: true); SetFileParameterLotIDToLogisticsMID(); if (reportFullPath.Length < _MinFileLength) results.Item4.Add(new FileInfo(reportFullPath)); else { IProcessData iProcessData = new ProcessData(this, _Logistics, results.Item4); if (iProcessData is ProcessData _) { if (!iProcessData.Details.Any()) throw new Exception(string.Concat("A) No Data - ", dateTime.Ticks)); if (iProcessData.Details[0] is not string mid || string.IsNullOrEmpty(mid)) throw new Exception(string.Concat("B) No Data - ", dateTime.Ticks)); _Logistics.MID = mid; SetFileParameterLotID(mid); _Logistics.ProcessJobID = iProcessData.GetCurrentReactor(this, _Logistics, _Reactors); } if (!iProcessData.Details.Any()) throw new Exception(string.Concat("C) No Data - ", dateTime.Ticks)); if (_LastText != _Logistics.MID || _LastChange < DateTime.Now.AddMinutes(-30).Ticks) { _LastText = _Logistics.MesEntity; _LastChange = DateTime.Now.Ticks; results = iProcessData.GetResults(this, _Logistics, results.Item4); } } return results; } }