Require Complete

IDE0060
This commit is contained in:
Mike Phares 2024-11-20 10:34:18 -07:00
parent 8ae8386b30
commit 66f5acc6fa
9 changed files with 146 additions and 127 deletions

View File

@ -115,10 +115,10 @@ public class FileRead : Shared.FileRead, IFileRead
}
}
#pragma warning disable IDE0060
private void MoveArchive(string reportFullPath, DateTime dateTime)
#pragma warning restore IDE0060
{
if (dateTime == DateTime.MinValue)
throw new ArgumentNullException(nameof(dateTime));
string logisticsSequence = _Logistics.Sequence.ToString();
string weekOfYear = _Calendar.GetWeekOfYear(_Logistics.DateTimeFromSequence, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
string weekDirectory = $"{_Logistics.DateTimeFromSequence:yyyy}_Week_{weekOfYear}{@"\"}{_Logistics.DateTimeFromSequence:yyyy-MM-dd}";

View File

@ -167,10 +167,10 @@ public class FileRead : Shared.FileRead, IFileRead
OpenInsightMetrologyViewer.WSRequest.PostOpenInsightMetrologyViewerAttachments(this, _Logistics, _OpenInsightMetrologyViewerAPI, _LincPDFCFileName, descriptions, matchDirectories[0], subGroupId, headerId, headerIdDirectory);
}
#pragma warning disable IDE0060
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
#pragma warning restore IDE0060
{
if (dateTime == DateTime.MinValue)
throw new ArgumentNullException(nameof(dateTime));
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
Tuple<string, string[], string[]> pdsf = ProcessDataStandardFormat.GetLogisticsColumnsAndBody(reportFullPath);
_Logistics = new Logistics(reportFullPath, pdsf.Item1);

View File

@ -108,10 +108,10 @@ public class FileRead : Shared.FileRead, IFileRead
return results;
}
#pragma warning disable IDE0060
private void DirectoryMove(string reportFullPath, DateTime dateTime, List<pcl.Description> descriptions)
#pragma warning restore IDE0060
{
if (dateTime == DateTime.MinValue)
throw new ArgumentNullException(nameof(dateTime));
FileInfo fileInfo = new(reportFullPath);
string logisticsSequence = _Logistics.Sequence.ToString();
string jobIdDirectory = Path.Combine(_JobIdParentDirectory, _Logistics.JobID);

View File

@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Adaptation.FileHandlers.pcl;
@ -20,24 +23,38 @@ internal class Complete
public Summary Summary { get; }
public ReadOnlyCollection<Point> Points { get; }
public static Complete? Get(Constant constant, ReadOnlyCollection<string> lines)
public static Complete? Get(Shared.Logistics logistics, List<FileInfo> fileInfoCollection, ReadOnlyCollection<string> lines)
{
Complete? result;
Header? header = Header.Get(constant, lines);
Constant constant = new();
ReadOnlyCollection<string> collection = new(lines);
if (collection.Count <= constant.Take)
result = null;
else
{
Header? header = Header.Get(constant, collection);
if (header is null)
result = null;
else
{
Summary? summary = SummarySegment.Get(constant, lines);
Summary? summary = SummarySegment.Get(constant, collection);
if (summary is null)
result = null;
else
{
ReadOnlyCollection<Point> points = Point.GetCollection(constant, lines) ?? throw new NullReferenceException(nameof(summary));
ReadOnlyCollection<Point> points = Point.GetCollection(constant, collection) ?? throw new NullReferenceException(nameof(summary));
if (points.Count == 0)
result = null;
else
{
result = new(header, summary, points);
FileInfo fileInfo = new($"{logistics.ReportFullPath}.json");
string json = JsonSerializer.Serialize(result, CompleteSourceGenerationContext.Default.Complete);
File.WriteAllText(fileInfo.FullName, json);
File.SetLastWriteTime(fileInfo.FullName, logistics.DateTimeFromSequence);
fileInfoCollection.Add(fileInfo);
}
}
}
}
return result;

View File

@ -0,0 +1,89 @@
using Adaptation.Shared;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
namespace Adaptation.FileHandlers.pcl;
internal class Convert
{
/// <summary>
/// Convert the raw data file to parsable file format - in this case from PCL to PDF
/// </summary>
/// <param name="sourceFile">source file to be converted to PDF</param>
/// <returns></returns>
private static string ConvertSourceFileToPdf(Logistics logistics, string ghostPCLFileName)
{
string result = Path.ChangeExtension(logistics.ReportFullPath, ".pdf");
if (!File.Exists(result))
{
//string arguments = string.Concat("-i \"", sourceFile, "\" -o \"", result, "\"");
string arguments = string.Concat("-dSAFER -dBATCH -dNOPAUSE -dFIXEDMEDIA -dFitPage -dAutoRotatePages=/All -dDEVICEWIDTHPOINTS=792 -dDEVICEHEIGHTPOINTS=612 -sOutputFile=\"", result, "\" -sDEVICE=pdfwrite \"", logistics.ReportFullPath, "\"");
//Process process = Process.Start(configData.LincPDFCFileName, arguments);
Process process = Process.Start(ghostPCLFileName, arguments);
_ = process.WaitForExit(30000);
if (!File.Exists(result))
throw new Exception("PDF file wasn't created");
}
return result;
}
private static ReadOnlyCollection<string> GetTextFromPDF(string pdfTextStripperFileName, string sourceFileNamePdf, string altHeaderFileName)
{
string[] result;
ProcessStartInfo processStartInfo = new(pdfTextStripperFileName, $"s \"{sourceFileNamePdf}\"")
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
Process process = Process.Start(processStartInfo);
_ = process.WaitForExit(30000);
if (!File.Exists(altHeaderFileName))
result = Array.Empty<string>();
else
result = File.ReadAllLines(altHeaderFileName);
return new(result);
}
internal static ReadOnlyCollection<string> PDF(Logistics logistics, string ghostPCLFileName, string pdfTextStripperFileName, List<FileInfo> fileInfoCollection)
{
ReadOnlyCollection<string> result;
string sourceFileNamePdf = ConvertSourceFileToPdf(logistics, ghostPCLFileName);
fileInfoCollection.Add(new FileInfo(sourceFileNamePdf));
string altHeaderFileName = Path.ChangeExtension(logistics.ReportFullPath, ".txt");
if (File.Exists(altHeaderFileName))
{
result = new(File.ReadAllLines(altHeaderFileName));
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
}
else
{
try
{
//Pdfbox, IKVM.AWT.WinForms
org.apache.pdfbox.pdmodel.PDDocument pdfDocument = org.apache.pdfbox.pdmodel.PDDocument.load(sourceFileNamePdf);
org.apache.pdfbox.util.PDFTextStripper stripper = new();
string text = stripper.getText(pdfDocument);
pdfDocument.close();
File.AppendAllText(altHeaderFileName, text);
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
result = new(text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None));
}
catch (Exception)
{
if (!File.Exists(pdfTextStripperFileName))
throw;
result = GetTextFromPDF(pdfTextStripperFileName, sourceFileNamePdf, altHeaderFileName);
if (result.Count == 0)
throw;
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
}
}
return result;
}
}

View File

@ -4,6 +4,7 @@ using Adaptation.Shared;
using Adaptation.Shared.Methods;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text.Json;
using System.Text.RegularExpressions;
@ -104,9 +105,11 @@ public class FileRead : Shared.FileRead, IFileRead
return results;
}
#nullable enable
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>());
Tuple<string, Test[], JsonElement[], List<FileInfo>> results = new(string.Empty, Array.Empty<Test>(), Array.Empty<JsonElement>(), new List<FileInfo>());
_TickOffset ??= 0; // new FileInfo(reportFullPath).LastWriteTime.Ticks - dateTime.Ticks;
_Logistics = new Logistics(this, _TickOffset.Value, reportFullPath, useSplitForMID: true);
SetFileParameterLotIDToLogisticsMID();
@ -114,9 +117,13 @@ public class FileRead : Shared.FileRead, IFileRead
results.Item4.Add(_Logistics.FileInfo);
else
{
IProcessData iProcessData = new ProcessData(this, _Logistics, results.Item4, _GhostPCLFileName, _PDFTextStripperFileName);
if (iProcessData is not ProcessData processData)
ReadOnlyCollection<string> lines = Convert.PDF(_Logistics, _GhostPCLFileName, _PDFTextStripperFileName, results.Item4);
Complete? complete = Complete.Get(_Logistics, results.Item4, lines);
if (complete is null)
throw new Exception(string.Concat("A) No Data - ", dateTime.Ticks));
IProcessData iProcessData = new ProcessData(this, _Logistics, results.Item4, lines);
if (iProcessData is not ProcessData processData)
throw new Exception(string.Concat("B) No Data - ", dateTime.Ticks));
string mid;
if (!string.IsNullOrEmpty(processData.Lot) && string.IsNullOrEmpty(processData.Reactor) && string.IsNullOrEmpty(processData.RDS) && string.IsNullOrEmpty(processData.PSN))
mid = processData.Lot;
@ -130,7 +137,7 @@ public class FileRead : Shared.FileRead, IFileRead
SetFileParameterLotID(mid);
_Logistics.Update(mid, processData.Reactor);
if (iProcessData.Details.Count == 0)
throw new Exception(string.Concat("B) No Data - ", dateTime.Ticks));
throw new Exception(string.Concat("C) No Data - ", dateTime.Ticks));
results = iProcessData.GetResults(this, _Logistics, results.Item4);
}
return results;

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
@ -88,7 +87,7 @@ public class ProcessData : IProcessData
List<object> Shared.Properties.IProcessData.Details => _Details;
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string ghostPCLFileName, string pdfTextStripperFileName)
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, ReadOnlyCollection<string> lines)
{
fileInfoCollection.Clear();
_Details = new List<object>();
@ -98,7 +97,7 @@ public class ProcessData : IProcessData
Date = GetDateTime(logistics);
MesEntity = logistics.MesEntity;
_Log = LogManager.GetLogger(typeof(ProcessData));
Parse(fileRead, logistics, fileInfoCollection, ghostPCLFileName, pdfTextStripperFileName);
Parse(fileRead, logistics, fileInfoCollection, lines);
}
private static DateTime GetDateTime(Logistics logistics) =>
@ -129,27 +128,6 @@ public class ProcessData : IProcessData
return results;
}
/// <summary>
/// Convert the raw data file to parsable file format - in this case from PCL to PDF
/// </summary>
/// <param name="sourceFile">source file to be converted to PDF</param>
/// <returns></returns>
private static string ConvertSourceFileToPdf(Logistics logistics, string ghostPCLFileName)
{
string result = Path.ChangeExtension(logistics.ReportFullPath, ".pdf");
if (!File.Exists(result))
{
//string arguments = string.Concat("-i \"", sourceFile, "\" -o \"", result, "\"");
string arguments = string.Concat("-dSAFER -dBATCH -dNOPAUSE -dFIXEDMEDIA -dFitPage -dAutoRotatePages=/All -dDEVICEWIDTHPOINTS=792 -dDEVICEHEIGHTPOINTS=612 -sOutputFile=\"", result, "\" -sDEVICE=pdfwrite \"", logistics.ReportFullPath, "\"");
//Process process = Process.Start(configData.LincPDFCFileName, arguments);
Process process = Process.Start(ghostPCLFileName, arguments);
_ = process.WaitForExit(30000);
if (!File.Exists(result))
throw new Exception("PDF file wasn't created");
}
return result;
}
private void ScanPast(string text)
{
int num = _Data.IndexOf(text, _I);
@ -213,24 +191,6 @@ public class ProcessData : IProcessData
return text.Trim();
}
private static string GetTextFromPDF(string pdfTextStripperFileName, string sourceFileNamePdf, string altHeaderFileName)
{
string result;
ProcessStartInfo processStartInfo = new(pdfTextStripperFileName, $"s \"{sourceFileNamePdf}\"")
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
Process process = Process.Start(processStartInfo);
_ = process.WaitForExit(30000);
if (!File.Exists(altHeaderFileName))
result = string.Empty;
else
result = File.ReadAllText(altHeaderFileName);
return result;
}
private static (string, string) GetReactorAndRDS(string defaultReactor, string defaultRDS, string text, string formattedText, string[] segments)
{
string rds;
@ -533,57 +493,11 @@ public class ProcessData : IProcessData
#nullable enable
private Complete? GetComplete(string altHeaderFileName)
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, ReadOnlyCollection<string> lines)
{
Complete? result;
Constant constant = new();
string[] lines = File.ReadAllLines(altHeaderFileName);
ReadOnlyCollection<string> collection = new(lines);
if (collection.Count > constant.Take)
result = Complete.Get(constant, collection);
else
{
result = null;
_Log.Warn($"File {altHeaderFileName} has less than {constant.Take} collection");
}
return result;
}
#pragma warning disable IDE0060
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string ghostPCLFileName, string pdfTextStripperFileName)
#pragma warning restore IDE0060
{
string headerText;
string sourceFileNamePdf = ConvertSourceFileToPdf(logistics, ghostPCLFileName);
fileInfoCollection.Add(new FileInfo(sourceFileNamePdf));
string altHeaderFileName = Path.ChangeExtension(logistics.ReportFullPath, ".txt");
if (File.Exists(altHeaderFileName))
{
headerText = File.ReadAllText(altHeaderFileName);
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
}
else
{
try
{
//Pdfbox, IKVM.AWT.WinForms
org.apache.pdfbox.pdmodel.PDDocument pdfDocument = org.apache.pdfbox.pdmodel.PDDocument.load(sourceFileNamePdf);
org.apache.pdfbox.util.PDFTextStripper stripper = new();
headerText = stripper.getText(pdfDocument);
pdfDocument.close();
File.AppendAllText(altHeaderFileName, headerText);
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
}
catch (Exception)
{
if (!File.Exists(pdfTextStripperFileName))
throw;
headerText = GetTextFromPDF(pdfTextStripperFileName, sourceFileNamePdf, altHeaderFileName);
if (string.IsNullOrEmpty(headerText))
throw;
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
}
}
if (fileRead is null)
throw new ArgumentNullException(nameof(fileRead));
string headerText = string.Join(Environment.NewLine, lines);
if (headerText.Contains("G A T E V O L T A G E"))
throw new Exception("Ignore: GATEVOLTAGE runs are not parsed.");
if (!string.IsNullOrEmpty(headerText))
@ -692,23 +606,6 @@ public class ProcessData : IProcessData
detail.UniqueId = string.Concat(detail, detail.UniqueId);
}
fileInfoCollection.Add(logistics.FileInfo);
try
{
Complete? complete = GetComplete(altHeaderFileName);
if (complete is null)
_Log.Warn($"Could not get Complete from {altHeaderFileName}");
else
{
FileInfo fileInfo = new($"{altHeaderFileName}.json");
string json = JsonSerializer.Serialize(complete, CompleteSourceGenerationContext.Default.Complete);
File.WriteAllText(fileInfo.FullName, json);
fileInfoCollection.Add(fileInfo);
}
}
catch (Exception ex)
{
_Log.Error($"Error in {nameof(GetComplete)}", ex);
}
}
internal static List<Description> GetDescriptions(JsonElement[] jsonElements)

View File

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
namespace Adaptation.FileHandlers.pcl;
#nullable enable
@ -17,3 +19,9 @@ internal class Summary
public SummarySegment? RadialGradient { get; }
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Summary))]
internal partial class SummarySourceGenerationContext : JsonSerializerContext
{
}

View File

@ -118,6 +118,7 @@
<Compile Include="Adaptation\FileHandlers\OpenInsightMetrologyViewerAttachments\FileRead.cs" />
<Compile Include="Adaptation\FileHandlers\pcl\Complete.cs" />
<Compile Include="Adaptation\FileHandlers\pcl\Constant.cs" />
<Compile Include="Adaptation\FileHandlers\pcl\Convert.cs" />
<Compile Include="Adaptation\FileHandlers\pcl\Description.cs" />
<Compile Include="Adaptation\FileHandlers\pcl\Descriptor.cs" />
<Compile Include="Adaptation\FileHandlers\pcl\Detail.cs" />