153 lines
5.8 KiB
C#

using Adaptation.Eaf.Management.ConfigurationData.CellAutomation;
using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration;
using Adaptation.Shared;
using Adaptation.Shared.Deposition;
using Adaptation.Shared.Methods;
using log4net;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Tesseract;
namespace Adaptation.FileHandlers.jpeg;
public class ProcessData : IProcessData
{
private readonly List<object> _Details;
private readonly ILog _Log;
public string JobID { get; set; }
public string MesEntity { get; set; }
List<object> Shared.Properties.IProcessData.Details => _Details;
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection)
{
if (logistics is null)
{ }
if (fileInfoCollection is null)
{ }
JobID = logistics.JobID;
fileInfoCollection.Clear();
_Details = new List<object>();
MesEntity = logistics.MesEntity;
_Log = LogManager.GetLogger(typeof(ProcessData));
Parse(fileRead);
}
private static string Get(string value, bool useSplitForMID)
{
string result = value;
if (useSplitForMID)
{
if (result.IndexOf(".") > -1)
result = result.Split('.')[0].Trim();
if (result.IndexOf("_") > -1)
result = result.Split('_')[0].Trim();
if (result.IndexOf("-") > -1)
result = result.Split('-')[0].Trim();
}
result = string.Concat(result.Substring(0, 1).ToUpper(), result.Substring(1).ToLower());
return result;
}
string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors)
{
string result = string.Empty;
string filePrefixAsMID;
foreach (KeyValuePair<string, string> keyValuePair in reactors)
{
foreach (string filePrefix in keyValuePair.Value.Split('|'))
{
filePrefixAsMID = Get(filePrefix, useSplitForMID: true);
if (logistics.MID.StartsWith(filePrefix) || logistics.MID.StartsWith(filePrefixAsMID))
{
result = keyValuePair.Key;
break;
}
}
}
if (string.IsNullOrEmpty(result) && reactors.Count == 1)
result = reactors.ElementAt(0).Key;
return result;
}
Tuple<string, Test[], JsonElement[], List<FileInfo>> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection)
{
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
List<Test> tests = new();
foreach (object item in _Details)
tests.Add(Test.CDE);
List<IDescription> descriptions = fileRead.GetDescriptions(fileRead, tests, this);
if (tests.Count != descriptions.Count)
throw new Exception();
for (int i = 0; i < tests.Count; i++)
{
if (descriptions[i] is not Description description)
throw new Exception();
if (description.Test != (int)tests[i])
throw new Exception();
}
List<Description> fileReadDescriptions = (from l in descriptions select (Description)l).ToList();
string json = JsonSerializer.Serialize(fileReadDescriptions, fileReadDescriptions.GetType());
JsonElement[] jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
results = new Tuple<string, Test[], JsonElement[], List<FileInfo>>(logistics.Logistics1[0], tests.ToArray(), jsonElements, fileInfoCollection);
return results;
}
private void Parse(IFileRead fileRead)
{
List<string> blocks = new();
StringBuilder stringBuilder = new();
using TesseractEngine engine = new(string.Empty, "eng", EngineMode.Default);
using Pix img = Pix.LoadFromFile(fileRead.ReportFullPath);
using Page page = engine.Process(img);
string text = page.GetText();
_Log.Debug(string.Format("Mean confidence: {0}", page.GetMeanConfidence()));
_Log.Debug(string.Format("Text (GetText): \r\n{0}", text));
_Log.Debug("Text (iterator):");
using ResultIterator iter = page.GetIterator();
iter.Begin();
do
{
do
{
do
{
do
{
_ = stringBuilder.Append(iter.GetText(PageIteratorLevel.Word)).Append(' ');
if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word) && stringBuilder.Length > 0)
{
blocks.Add(stringBuilder.ToString());
_ = stringBuilder.Clear();
}
} while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
_ = stringBuilder.AppendLine();
} while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
} while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
} while (iter.Next(PageIteratorLevel.Block));
if (stringBuilder.Length > 0)
blocks.Add(stringBuilder.ToString());
if (!blocks.Any())
_Details.Add(text);
else
{
blocks = (from l in blocks where l.Split(':').Length == 3 select l).ToList();
if (!blocks.Any())
_Details.Add(text);
else
_Details.Add(blocks[0]);
}
}
}