Refactor Description and ProcessData classes to improve JSON handling; add GetDefaultJsonElement method and streamline GetExtractResult logic

This commit is contained in:
2025-11-13 08:03:48 -07:00
parent 2b54f085be
commit f1e02e1048
3 changed files with 33 additions and 217 deletions

View File

@ -263,7 +263,7 @@ public class Description : IDescription, Shared.Properties.IDescription
return result;
}
private Description GetDefault(IFileRead fileRead, Logistics logistics)
private static Description GetDefault(IFileRead fileRead, Logistics logistics)
{
Description result = new()
{
@ -319,6 +319,15 @@ public class Description : IDescription, Shared.Properties.IDescription
return result;
}
internal static JsonElement GetDefaultJsonElement(IFileRead fileRead, Logistics logistics)
{
JsonElement result;
Description description = GetDefault(fileRead, logistics);
string json = JsonSerializer.Serialize(description, DescriptionSourceGenerationContext.Default.Description);
result = JsonSerializer.Deserialize<JsonElement>(json);
return result;
}
internal static string GetDateFormat() => "MM/dd/yyyy hh:mm:ss tt";
}

View File

@ -100,41 +100,37 @@ public class FileRead : Shared.FileRead, IFileRead
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime)
{
Tuple<string, Test[], JsonElement[], List<FileInfo>> results = new(string.Empty, Array.Empty<Test>(), Array.Empty<JsonElement>(), new List<FileInfo>());
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
Test[] tests = Array.Empty<Test>();
List<JsonElement> jsonElements = new();
List<FileInfo> fileInfoCollection = new();
_TickOffset ??= 0; // new FileInfo(reportFullPath).LastWriteTime.Ticks - dateTime.Ticks;
_Logistics = new Logistics(this, _TickOffset.Value, reportFullPath, useSplitForMID: true);
SetFileParameterLotIDToLogisticsMID();
fileInfoCollection.Add(_Logistics.FileInfo);
if (_Logistics.FileInfo.Length < _MinFileLength)
results.Item4.Add(_Logistics.FileInfo);
results = new(string.Empty, tests, jsonElements.ToArray(), fileInfoCollection);
else
{
Run? run = Run.Get(_Logistics, results.Item4);
Run? run = Run.Get(_Logistics, fileInfoCollection);
if (run is null)
throw new Exception(string.Concat("A) No Data - ", dateTime.Ticks));
IProcessData iProcessData = new ProcessData(this, _Logistics, results.Item4, run);
if (iProcessData is not ProcessData processData)
results = new(string.Concat("B) No Data - ", dateTime.Ticks), Array.Empty<Test>(), Array.Empty<JsonElement>(), results.Item4);
results = new(string.Concat("A) No Data - ", dateTime.Ticks), tests, jsonElements.ToArray(), fileInfoCollection);
else
{
string mid;
if (!string.IsNullOrEmpty(processData.Run) && string.IsNullOrEmpty(processData.Reactor) && string.IsNullOrEmpty(processData.RDS) && string.IsNullOrEmpty(processData.PSN))
mid = processData.Run;
else if (!string.IsNullOrEmpty(processData.Employee) && string.IsNullOrEmpty(processData.Reactor) && string.IsNullOrEmpty(processData.RDS) && string.IsNullOrEmpty(processData.PSN))
mid = processData.Employee;
Descriptor descriptor = ProcessData.GetDescriptor(run.Line1.Title);
if (!string.IsNullOrEmpty(descriptor.Run) && string.IsNullOrEmpty(descriptor.Reactor) && string.IsNullOrEmpty(descriptor.RDS) && string.IsNullOrEmpty(descriptor.PSN))
mid = descriptor.Run;
else
{
mid = string.Concat(processData.Reactor, "-", processData.RDS, "-", processData.PSN);
mid = string.Concat(descriptor.Reactor, "-", descriptor.RDS, "-", descriptor.PSN);
mid = Regex.Replace(mid, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
}
SetFileParameterLotID(mid);
_Logistics.Update(mid, processData.Reactor);
string logBody = processData.LogBody;
if (iProcessData.Details.Count > 0)
results = iProcessData.GetResults(this, _Logistics, results.Item4);
else
results = new(string.Concat("C) No Data - ", dateTime.Ticks), Array.Empty<Test>(), Array.Empty<JsonElement>(), results.Item4);
if (!_IsEAFHosted)
results = new(logBody, results.Item2, results.Item3, results.Item4);
_Logistics.Update(mid, descriptor.Reactor);
JsonElement jsonElement = Description.GetDefaultJsonElement(this, _Logistics);
jsonElements.Add(jsonElement);
results = new(_Logistics.Logistics1[0], tests, jsonElements.ToArray(), fileInfoCollection);
}
}
return results;

View File

@ -1,15 +1,10 @@
using Adaptation.Shared;
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 System.Text.RegularExpressions;
namespace Adaptation.FileHandlers.RsM;
@ -17,8 +12,6 @@ namespace Adaptation.FileHandlers.RsM;
public class ProcessData : IProcessData
{
private readonly List<object> _Details;
public string JobID { get; set; }
public string MesEntity { get; set; }
public string AutoOptimizeGain { get; set; }
@ -50,46 +43,13 @@ public class ProcessData : IProcessData
public string UniqueId { get; set; }
public string Zone { get; set; }
private readonly ILog _Log;
List<object> Shared.Properties.IProcessData.Details => _Details;
List<object> Shared.Properties.IProcessData.Details { get; }
internal ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, Run run)
{
JobID = logistics.JobID;
_Details = new List<object>();
MesEntity = logistics.MesEntity;
_Log = LogManager.GetLogger(typeof(ProcessData));
Parse(fileRead, logistics, fileInfoCollection, run);
}
string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors) =>
throw new Exception(string.Concat("See ", nameof(IProcessData)));
string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors) => throw new Exception(string.Concat("See ", nameof(Parse)));
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();
}
FileInfo fileInfo = new($"{logistics.ReportFullPath}.descriptions.json");
List<Description> fileReadDescriptions = (from l in descriptions select (Description)l).ToList();
string json = JsonSerializer.Serialize(fileReadDescriptions, fileReadDescriptions.GetType());
File.WriteAllText(fileInfo.FullName, json);
File.SetLastWriteTime(fileInfo.FullName, logistics.DateTimeFromSequence);
fileInfoCollection.Add(fileInfo);
JsonElement[] jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json) ?? throw new Exception();
results = new Tuple<string, Test[], JsonElement[], List<FileInfo>>(logistics.Logistics1[0], tests.ToArray(), jsonElements, fileInfoCollection);
return results;
}
Tuple<string, Test[], JsonElement[], List<FileInfo>> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection) =>
throw new NotImplementedException();
private static (string, string) GetReactorAndRDS(string defaultReactor, string defaultRDS, string text, string formattedText, string[] segments)
{
@ -219,32 +179,6 @@ public class ProcessData : IProcessData
return result;
}
private void SetTitleData(Logistics logistics, Run run)
{
string timeFormat = "yyyyMMddHHmmss";
Descriptor descriptor = GetDescriptor(run.Line1.Title);
PSN = descriptor.PSN;
RDS = descriptor.RDS;
Run = descriptor.Run;
Zone = descriptor.Zone;
Layer = descriptor.Layer;
Reactor = descriptor.Reactor;
Employee = descriptor.Employee;
UniqueId = string.Concat(logistics.JobID, "_", descriptor.Run, "_", logistics.DateTimeFromSequence.ToString(timeFormat));
}
private void SetFileNameData(string[] segments)
{
if (segments.Length > 1)
FileName = segments[0];
if (segments.Length > 2)
{
Project = segments[1];
RecipeName = segments[2];
Recipe = string.Concat(segments[1], " \\ ", segments[2]);
}
}
internal static DateTime GetDateTime(Logistics logistics, string dateTimeText)
{
DateTime result;
@ -265,131 +199,8 @@ public class ProcessData : IProcessData
}
return result;
}
private void SetDateTimeData(Logistics logistics, string[] segments)
{
DateTime dateTime;
if (segments.Length < 2)
dateTime = logistics.DateTimeFromSequence;
else
{
string dateTimeText = string.Concat(segments[0], ' ', segments[1]);
dateTime = GetDateTime(logistics, dateTimeText);
}
Date = dateTime;
if (segments.Length > 3 && float.TryParse(segments[2], out float temp))
Temp = temp.ToString("0.0");
if (segments.Length > 7 && segments[6] == "Avg=")
Avg = segments[7];
if (segments.Length > 7 && segments[8] == "Dev=")
StandardDeviation = segments[9];
if (!string.IsNullOrEmpty(Avg) && !string.IsNullOrEmpty(StandardDeviation) && float.TryParse(Avg, out float average) && float.TryParse(StandardDeviation, out float standardDeviation))
StandardDeviationPercentage = Math.Round(standardDeviation / average, 4).ToString("0.00%");
}
private void SetOperatorData(string[] segments, bool updateEmployee)
{
if (segments.Length > 1 && updateEmployee)
Employee = segments[0];
if (segments.Length > 2)
EquipId = segments[1];
}
private void SetEngineerData(string[] segments)
{
if (segments.Length > 1)
Engineer = segments[0];
}
private void SetNumProbePointsData(string[] segments)
{
if (segments.Length > 6)
DataReject = segments[6];
}
private static Detail GetRData(string[] segments)
{
Detail result = new();
if (segments.Length > 0 && float.TryParse(segments[0], out float r))
result.R = r.ToString("0.0");
if (segments.Length > 1 && float.TryParse(segments[1], out float t))
result.T = t.ToString("0.0");
if (segments.Length > 2 && float.TryParse(segments[2], out float rs))
result.Rs = rs.ToString("0.0000");
if (segments.Length > 12 && float.TryParse(segments[12], out float merit))
result.Merit = merit.ToString("0.00");
result.Pt = "-1";
result.UniqueId = string.Empty;
return result;
}
#nullable enable
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, Run run)
{
if (fileRead is null)
throw new ArgumentNullException(nameof(fileRead));
Lot = "LotID";
Detail detail;
string[] segments;
string[] separator = new string[] { " " };
string[] lines = File.ReadAllLines(logistics.ReportFullPath);
for (int i = 0; i < lines.Length; i++)
{
segments = lines[i].Split(separator, StringSplitOptions.RemoveEmptyEntries);
if (lines[i].Contains("<Title>") && segments.Length > 0)
SetTitleData(logistics, run);
else if (lines[i].Contains("<FileName, Proj,Rcpe, LotID,WfrID"))
SetFileNameData(segments);
else if (lines[i].Contains("<DateTime,Temp,TCR%,N|P>"))
SetDateTimeData(logistics, segments);
else if (lines[i].Contains("<Operator, Epuipment>"))
SetOperatorData(segments, updateEmployee: string.IsNullOrEmpty(Employee));
else if (lines[i].Contains("<Engineer>"))
SetEngineerData(segments);
else if (lines[i].Contains("<NumProbePoints, SingleOrDualProbeConfig, #ActPrbPts, Rsens,IdrvMx,VinGain, DataRejectSigma, MeritThreshold"))
SetNumProbePointsData(segments);
else if (lines[i].Contains("<R,Th,Data, Rs,RsA,RsB, #Smpl, x,y, Irng,Vrng"))
{
for (int z = i; z < lines.Length; z++)
{
i = z;
if (string.IsNullOrEmpty(lines[z]))
continue;
detail = GetRData(lines[z].Split(separator, StringSplitOptions.RemoveEmptyEntries));
_Details.Add(detail);
}
}
}
for (int i = 0; i < _Details.Count; i++)
{
if (_Details[i] is not Detail item)
continue;
item.HeaderUniqueId = UniqueId;
item.Pt = (i + 1).ToString();
item.UniqueId = string.Concat(item, "_Point-", item.Pt);
}
StringBuilder stringBuilder = new();
string reportFileName = Path.GetFileName(logistics.ReportFullPath);
_ = stringBuilder.AppendLine($"RUN [{Title}]");
_ = stringBuilder.AppendLine($"Recipe {Project} \\ {RecipeName} RESISTIVITY {"####"}");
_ = stringBuilder.AppendLine($"EQUIP# {EquipId} Engineer {Engineer}");
_ = stringBuilder.AppendLine($"LotID {Lot} D.L.RATIO {"#.####"}");
_ = stringBuilder.AppendLine($"OPERATOR {Employee} TEMP {Temp} {Date:HH:mm MM/dd/yy}");
_ = stringBuilder.AppendLine($"AutoOptimizeGain = {"###"} AutoProbeHeightSet = {"##"}");
_ = stringBuilder.AppendLine($"DataReject > {"#.#"}Sigma");
_ = stringBuilder.AppendLine($"0 ..\\{Project}.prj\\{RecipeName}.rcp\\{reportFileName} {Date:HH:mm MM/dd/yy}");
_ = stringBuilder.AppendLine($"pt# R Th Rs[Ohm/sq@T] Merit");
for (int i = 0; i < _Details.Count; i++)
{
if (_Details[i] is not Detail item)
continue;
_ = stringBuilder.AppendLine($"{item.Pt} {item.R} {item.T} {item.Rs} {item.Merit}");
}
_ = stringBuilder.AppendLine($"Avg = {Avg} {StandardDeviationPercentage} SEMI Radial= {"#.##%"}");
LogBody = stringBuilder.ToString();
fileInfoCollection.Add(logistics.FileInfo);
}
#nullable enable
internal static List<Description> GetDescriptions(JsonElement[] jsonElements)
{