167 lines
8.6 KiB
C#

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<string, string> _Reactors;
public FileRead(ISMTP smtp, Dictionary<string, string> fileParameter, string cellInstanceName, string cellInstanceConnectionName, FileConnectorConfiguration fileConnectorConfiguration, string equipmentTypeName, string parameterizedModelObjectDefinitionType, IList<ModelObjectParameterDefinition> modelObjectParameters, string equipmentDictionaryName, Dictionary<string, List<long>> 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, string>();
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<string, Test[], JsonElement[], List<FileInfo>> extractResults, Exception exception) => Move(extractResults, exception);
void IFileRead.WaitForThread() => WaitForThread(thread: null, threadExceptions: null);
string IFileRead.GetEventDescription()
{
string result = _Description.GetEventDescription();
return result;
}
List<string> IFileRead.GetHeaderNames()
{
List<string> results = _Description.GetHeaderNames();
return results;
}
string[] IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> 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<string, string> IFileRead.GetDisplayNamesJsonElement()
{
Dictionary<string, string> results = _Description.GetDisplayNamesJsonElement(this);
return results;
}
List<IDescription> IFileRead.GetDescriptions(IFileRead fileRead, List<Test> tests, IProcessData processData)
{
List<IDescription> results = _Description.GetDescriptions(fileRead, _Logistics, tests, processData);
return results;
}
Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.GetExtractResult(string reportFullPath, string eventName)
{
Tuple<string, Test[], JsonElement[], List<FileInfo>> 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<string, Test[], JsonElement[], List<FileInfo>>(results.Item1, Array.Empty<Test>(), JsonSerializer.Deserialize<JsonElement[]>("[]"), results.Item4);
if (results.Item3.Length > 0 && _IsEAFHosted)
WritePDSF(this, results.Item3);
UpdateLastTicksDuration(DateTime.Now.Ticks - dateTime.Ticks);
return results;
}
Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.ReExtract()
{
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
List<string> headerNames = _Description.GetHeaderNames();
Dictionary<string, string> 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<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
{
Tuple<string, Test[], JsonElement[], List<FileInfo>> results = new(string.Empty, null, null, new List<FileInfo>());
_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;
}
}