From 66f5acc6fa58659b661ed8d3c33751c4e98db19f Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Wed, 20 Nov 2024 10:34:18 -0700 Subject: [PATCH] Require Complete IDE0060 --- Adaptation/FileHandlers/Archive/FileRead.cs | 4 +- .../FileRead.cs | 4 +- Adaptation/FileHandlers/Processed/FileRead.cs | 4 +- Adaptation/FileHandlers/pcl/Complete.cs | 33 +++-- Adaptation/FileHandlers/pcl/Convert.cs | 89 ++++++++++++++ Adaptation/FileHandlers/pcl/FileRead.cs | 15 ++- Adaptation/FileHandlers/pcl/ProcessData.cs | 115 +----------------- Adaptation/FileHandlers/pcl/Summary.cs | 8 ++ MET08RESIHGCV.csproj | 1 + 9 files changed, 146 insertions(+), 127 deletions(-) create mode 100644 Adaptation/FileHandlers/pcl/Convert.cs diff --git a/Adaptation/FileHandlers/Archive/FileRead.cs b/Adaptation/FileHandlers/Archive/FileRead.cs index 0e79541..6dfdff4 100644 --- a/Adaptation/FileHandlers/Archive/FileRead.cs +++ b/Adaptation/FileHandlers/Archive/FileRead.cs @@ -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}"; diff --git a/Adaptation/FileHandlers/OpenInsightMetrologyViewerAttachments/FileRead.cs b/Adaptation/FileHandlers/OpenInsightMetrologyViewerAttachments/FileRead.cs index 4e95969..2ddbe59 100644 --- a/Adaptation/FileHandlers/OpenInsightMetrologyViewerAttachments/FileRead.cs +++ b/Adaptation/FileHandlers/OpenInsightMetrologyViewerAttachments/FileRead.cs @@ -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> GetExtractResult(string reportFullPath, DateTime dateTime) -#pragma warning restore IDE0060 { + if (dateTime == DateTime.MinValue) + throw new ArgumentNullException(nameof(dateTime)); Tuple> results; Tuple pdsf = ProcessDataStandardFormat.GetLogisticsColumnsAndBody(reportFullPath); _Logistics = new Logistics(reportFullPath, pdsf.Item1); diff --git a/Adaptation/FileHandlers/Processed/FileRead.cs b/Adaptation/FileHandlers/Processed/FileRead.cs index 928e84d..8fbbc3f 100644 --- a/Adaptation/FileHandlers/Processed/FileRead.cs +++ b/Adaptation/FileHandlers/Processed/FileRead.cs @@ -108,10 +108,10 @@ public class FileRead : Shared.FileRead, IFileRead return results; } -#pragma warning disable IDE0060 private void DirectoryMove(string reportFullPath, DateTime dateTime, List 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); diff --git a/Adaptation/FileHandlers/pcl/Complete.cs b/Adaptation/FileHandlers/pcl/Complete.cs index 7ff9cbe..ce3a8db 100644 --- a/Adaptation/FileHandlers/pcl/Complete.cs +++ b/Adaptation/FileHandlers/pcl/Complete.cs @@ -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 Points { get; } - public static Complete? Get(Constant constant, ReadOnlyCollection lines) + public static Complete? Get(Shared.Logistics logistics, List fileInfoCollection, ReadOnlyCollection lines) { Complete? result; - Header? header = Header.Get(constant, lines); - if (header is null) + Constant constant = new(); + ReadOnlyCollection collection = new(lines); + if (collection.Count <= constant.Take) result = null; else { - Summary? summary = SummarySegment.Get(constant, lines); - if (summary is null) + Header? header = Header.Get(constant, collection); + if (header is null) result = null; else { - ReadOnlyCollection points = Point.GetCollection(constant, lines) ?? throw new NullReferenceException(nameof(summary)); - if (points.Count == 0) + Summary? summary = SummarySegment.Get(constant, collection); + if (summary is null) result = null; else - result = new(header, summary, points); + { + ReadOnlyCollection 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; diff --git a/Adaptation/FileHandlers/pcl/Convert.cs b/Adaptation/FileHandlers/pcl/Convert.cs new file mode 100644 index 0000000..022a0eb --- /dev/null +++ b/Adaptation/FileHandlers/pcl/Convert.cs @@ -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 +{ + + /// + /// Convert the raw data file to parsable file format - in this case from PCL to PDF + /// + /// source file to be converted to PDF + /// + 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 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(); + else + result = File.ReadAllLines(altHeaderFileName); + return new(result); + } + + internal static ReadOnlyCollection PDF(Logistics logistics, string ghostPCLFileName, string pdfTextStripperFileName, List fileInfoCollection) + { + ReadOnlyCollection 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; + } + +} \ No newline at end of file diff --git a/Adaptation/FileHandlers/pcl/FileRead.cs b/Adaptation/FileHandlers/pcl/FileRead.cs index 5dd2f3e..578da8c 100644 --- a/Adaptation/FileHandlers/pcl/FileRead.cs +++ b/Adaptation/FileHandlers/pcl/FileRead.cs @@ -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> GetExtractResult(string reportFullPath, DateTime dateTime) { - Tuple> results = new(string.Empty, null, null, new List()); + Tuple> results = new(string.Empty, Array.Empty(), Array.Empty(), new List()); _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 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; diff --git a/Adaptation/FileHandlers/pcl/ProcessData.cs b/Adaptation/FileHandlers/pcl/ProcessData.cs index a346718..6f50037 100644 --- a/Adaptation/FileHandlers/pcl/ProcessData.cs +++ b/Adaptation/FileHandlers/pcl/ProcessData.cs @@ -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 Shared.Properties.IProcessData.Details => _Details; - public ProcessData(IFileRead fileRead, Logistics logistics, List fileInfoCollection, string ghostPCLFileName, string pdfTextStripperFileName) + public ProcessData(IFileRead fileRead, Logistics logistics, List fileInfoCollection, ReadOnlyCollection lines) { fileInfoCollection.Clear(); _Details = new List(); @@ -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; } - /// - /// Convert the raw data file to parsable file format - in this case from PCL to PDF - /// - /// source file to be converted to PDF - /// - 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 fileInfoCollection, ReadOnlyCollection lines) { - Complete? result; - Constant constant = new(); - string[] lines = File.ReadAllLines(altHeaderFileName); - ReadOnlyCollection 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 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 GetDescriptions(JsonElement[] jsonElements) diff --git a/Adaptation/FileHandlers/pcl/Summary.cs b/Adaptation/FileHandlers/pcl/Summary.cs index ba80aa1..3b1e597 100644 --- a/Adaptation/FileHandlers/pcl/Summary.cs +++ b/Adaptation/FileHandlers/pcl/Summary.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace Adaptation.FileHandlers.pcl; #nullable enable @@ -16,4 +18,10 @@ internal class Summary public SummarySegment? StandardDeviationPercentage { get; } public SummarySegment? RadialGradient { get; } +} + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(Summary))] +internal partial class SummarySourceGenerationContext : JsonSerializerContext +{ } \ No newline at end of file diff --git a/MET08RESIHGCV.csproj b/MET08RESIHGCV.csproj index c5d6499..6602de1 100644 --- a/MET08RESIHGCV.csproj +++ b/MET08RESIHGCV.csproj @@ -118,6 +118,7 @@ +