437 lines
20 KiB
C#

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.QS408M;
public partial class ProcessData : IProcessData
{
private readonly List<object> _Details;
public string JobID { get; set; }
public string MesEntity { get; set; }
public string Batch { get; set; }
public string Cassette { get; set; }
public DateTime Date { get; set; }
public string Employee { get; set; }
public string Layer { get; set; }
public string MeanThickness { get; set; }
public string PSN { get; set; }
public string PassFail { get; set; }
public string RDS { get; set; }
public string RVThickness { get; set; }
public string Reactor { get; set; }
public string Recipe { get; set; }
public string StdDev { get; set; }
public string Title { get; set; }
public string UniqueId { get; set; }
public string Wafer { get; set; }
public string Zone { get; set; }
//
public string SlotNumber { get; set; }
public string ThicknessFourteen3mmEdgeMean { get; set; }
public string ThicknessFourteen3mmEdgePercent { get; set; }
public string ThicknessFourteen5mmEdgeMean { get; set; }
public string ThicknessFourteen5mmEdgePercent { get; set; }
public string ThicknessFourteenCenterMean { get; set; }
public string ThicknessFourteenCriticalPointsAverage { get; set; }
public string ThicknessFourteenCriticalPointsStdDev { get; set; }
public string ThicknessFourteenMeanFrom { get; set; }
public string ThicknessFourteenPoints { get; set; }
List<object> Shared.Properties.IProcessData.Details => _Details;
private readonly ILog _Log;
public ProcessData()
{
}
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string originalDataBioRad, ProcessData lastProcessData, long tickOffset)
{
JobID = logistics.JobID;
fileInfoCollection.Clear();
_Details = new List<object>();
MesEntity = logistics.MesEntity;
_Log = LogManager.GetLogger(typeof(ProcessData));
TXT txt = Parse(fileRead, logistics, fileInfoCollection, originalDataBioRad);
if (txt is not null)
SetValues(fileRead, logistics, fileInfoCollection, originalDataBioRad, lastProcessData, tickOffset, txt);
}
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.BioRadQS408M);
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;
}
internal static DateTime GetDateTime(Logistics logistics, long tickOffset, string dateTimeText)
{
DateTime result;
string inputDateFormat = "ddd mmm dd HH:mm:ss yyyy";
if (dateTimeText.Length != inputDateFormat.Length)
result = logistics.DateTimeFromSequence;
else
{
if (!DateTime.TryParseExact(dateTimeText, inputDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTimeParsed))
result = logistics.DateTimeFromSequence;
else
{
if (dateTimeParsed < logistics.DateTimeFromSequence.AddDays(1) && dateTimeParsed > logistics.DateTimeFromSequence.AddDays(-1))
result = new(dateTimeParsed.Ticks + tickOffset);
else
result = logistics.DateTimeFromSequence;
}
}
return result;
}
private static (string, string) GetReactorAndRDS(string defaultReactor, string defaultRDS, string text, string formattedText, string[] segments)
{
string rds;
string reactor;
if (string.IsNullOrEmpty(text) || segments.Length == 0 || string.IsNullOrEmpty(formattedText))
reactor = defaultReactor;
else
reactor = segments[0];
if (segments.Length <= 1 || !int.TryParse(segments[1], out int rdsValue) || rdsValue < 99)
rds = defaultRDS;
else
rds = segments[1];
if (reactor.Length > 3)
{
rds = reactor;
reactor = defaultReactor;
}
return new(reactor, rds);
}
private static (string, string) GetLayerAndPSN(string defaultLayer, string defaultPSN, string[] segments)
{
string psn;
string layer;
if (segments.Length <= 2)
{
psn = defaultPSN;
layer = defaultLayer;
}
else
{
string[] segmentsB = segments[2].Split('.');
psn = segmentsB[0];
if (segmentsB.Length <= 1)
layer = defaultLayer;
else
{
layer = segmentsB[1];
if (layer.Length > 1 && layer[0] == '0')
layer = layer.Substring(1);
}
}
return (layer, psn);
}
private static string GetZone(string[] segments)
{
string result;
if (segments.Length <= 3)
result = string.Empty;
else
{
result = segments[3];
if (result.Length > 1 && result[0] == '0')
result = result.Substring(1);
}
return result;
}
public static Descriptor GetDescriptor(string text)
{
Descriptor result;
string psn;
string rds;
string zone;
string layer;
string wafer;
string reactor;
string employee;
string defaultPSN = string.Empty;
string defaultRDS = string.Empty;
string defaultZone = string.Empty;
string defaultLayer = string.Empty;
string defaultReactor = string.Empty;
string defaultEmployee = string.Empty;
if (Regex.IsMatch(text, @"^[a-zA-z][0-9]{2,4}$"))
{
wafer = text.ToUpper();
psn = defaultPSN;
rds = defaultRDS;
zone = defaultZone;
layer = defaultLayer;
reactor = defaultReactor;
employee = defaultEmployee;
}
else if (string.IsNullOrEmpty(text) || (text.Length is 2 or 3 && Regex.IsMatch(text, "^[a-zA-z]{2,3}")))
{
wafer = text;
employee = text;
psn = defaultPSN;
rds = defaultRDS;
zone = defaultZone;
layer = defaultLayer;
reactor = defaultReactor;
}
else if (Regex.IsMatch(text, @"^[0-9]{2}[.][0-9]{1}[.]?[0-9]{0,1}"))
{
string[] segments = text.Split('.');
wafer = text;
psn = defaultPSN;
rds = defaultRDS;
layer = segments[1];
reactor = segments[0];
employee = defaultEmployee;
if (segments.Length <= 2)
zone = defaultZone;
else
zone = segments[2];
}
else
{
// Remove illegal characters \/:*?"<>| found in the Batch
wafer = Regex.Replace(text, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
if (wafer.Length > 2 && wafer[0] == '1' && (wafer[1] == 'T' || wafer[1] == 't'))
wafer = wafer.Substring(2);
string[] segments = wafer.Split('-');
// bool hasRDS = Regex.IsMatch(wafer, "[-]?([QP][0-9]{4,}|[0-9]{5,})[-]?");
(reactor, rds) = GetReactorAndRDS(defaultReactor, defaultRDS, text, wafer, segments);
(layer, psn) = GetLayerAndPSN(defaultLayer, defaultPSN, segments);
zone = GetZone(segments);
if (segments.Length <= 4)
employee = defaultEmployee;
else
employee = segments[4];
}
result = new(employee, layer, psn, rds, reactor, wafer, zone);
return result;
}
#pragma warning disable IDE0060
private void SetValues(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string originalDataBioRad, ProcessData lastProcessData, long tickOffset, TXT txt)
#pragma warning restore IDE0060
{
string psn;
string rds;
string zone;
string layer;
string wafer;
Detail detail;
string reactor;
int counter = 1;
int slotNumber = 0;
List<Detail> details = new();
StringBuilder titleFixed = new();
StringBuilder waferFixed = new();
string recipe = txt.Header.Recipe;
string cassette = txt.Header.Cassette;
string employee = txt.Header.Operator;
DateTime dateTime = GetDateTime(logistics, tickOffset, txt.Header.DateTime);
// Remove illegal characters \/:*?"<>| found in the Batch
string batch = Regex.Replace(txt.Header.Batch, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
bool isWaferSlot = !string.IsNullOrEmpty(txt.Header.Wafer) && txt.Header.Wafer.Length is 1 or 2 && int.TryParse(txt.Header.Wafer, out slotNumber);
Descriptor descriptor = isWaferSlot ? GetDescriptor(txt.Header.Batch) : GetDescriptor(txt.Header.Wafer);
psn = descriptor.PSN;
rds = descriptor.RDS;
zone = descriptor.Zone;
wafer = descriptor.Wafer;
layer = descriptor.Layer;
reactor = descriptor.Reactor;
if (string.IsNullOrEmpty(employee))
employee = descriptor.Employee;
foreach (char c in txt.Header.Title)
{
if (char.IsLetterOrDigit(c) || c == '-' || c == '.')
_ = titleFixed.Append(c);
}
foreach (char c in wafer)
{
if (char.IsLetterOrDigit(c) || c == '-' || c == '.')
_ = waferFixed.Append(c);
}
if (string.IsNullOrEmpty(lastProcessData.Wafer))
{
lastProcessData.Batch = JobID;
lastProcessData.Cassette = JobID;
lastProcessData.Employee = JobID;
lastProcessData.Recipe = JobID;
lastProcessData.Title = JobID;
}
lastProcessData.Wafer = wafer;
lastProcessData.Reactor = reactor;
lastProcessData.RDS = rds;
string check = "--------";
if (string.IsNullOrEmpty(batch) || batch.Contains(check))
batch = lastProcessData.Batch;
else
lastProcessData.Batch = batch;
if (string.IsNullOrEmpty(cassette) || cassette.Contains(check))
cassette = lastProcessData.Cassette;
else
lastProcessData.Cassette = cassette;
if (string.IsNullOrEmpty(employee) || employee.Contains(check))
employee = lastProcessData.Employee;
else
lastProcessData.Employee = employee;
if (string.IsNullOrEmpty(recipe) || recipe.Contains(check))
recipe = lastProcessData.Recipe;
else
lastProcessData.Recipe = recipe;
if (string.IsNullOrEmpty(txt.Header.Title) || txt.Header.Title.Contains(check))
titleFixed = new(lastProcessData.Title);
else
lastProcessData.Title = titleFixed.ToString();
string uniqueId = string.Concat(titleFixed, '_', waferFixed, '_', logistics.DateTimeFromSequence.ToString("yyyyMMddHHmmssffff"), '_', logistics.TotalSecondsSinceLastWriteTimeFromSequence);
PSN = psn;
RDS = rds;
Date = dateTime;
Zone = zone;
Batch = batch;
Layer = layer;
Recipe = recipe;
Reactor = reactor;
Cassette = cassette;
Employee = employee;
JobID = logistics.JobID;
UniqueId = uniqueId;
Title = titleFixed.ToString();
Wafer = waferFixed.ToString();
SlotNumber = slotNumber.ToString("00");
foreach (Site site in txt.Body.Sites)
{
detail = new()
{
Position = site.Position,
HeaderUniqueId = uniqueId,
Thickness = site.Thickness,
UniqueId = string.Concat(uniqueId, "_Point-", counter)
};
details.Add(detail);
counter++;
}
PopulateCalculated(details);
_Details.AddRange(details);
}
#pragma warning disable IDE0060
private static TXT Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string originalDataBioRad)
#pragma warning restore IDE0060
{
TXT result;
string[] files = Directory.GetFiles(Path.GetDirectoryName(logistics.ReportFullPath), string.Concat(originalDataBioRad, logistics.Sequence, "*"), SearchOption.TopDirectoryOnly);
foreach (string file in files)
fileInfoCollection.Add(new FileInfo(file));
string receivedData = File.ReadAllText(logistics.ReportFullPath);
// occasionally there are multiple blocks of details, get the last one as earlier ones may be aborted runs.
int index = receivedData.LastIndexOf("Bio-Rad");
if (index > -1)
receivedData = receivedData.Substring(index);
if (string.IsNullOrEmpty(receivedData))
result = null;
else
{
result = new TXT(receivedData);
string directory = Path.GetDirectoryName(logistics.ReportFullPath);
string fileName = Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(logistics.ReportFullPath)}.json");
string json = JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(fileName, json);
fileInfoCollection.Add(new(fileName));
}
fileInfoCollection.Add(logistics.FileInfo);
return result;
}
#nullable enable
internal static List<Description> GetDescriptions(JsonElement[] jsonElements)
{
List<Description> results = new();
Description? description;
JsonSerializerOptions jsonSerializerOptions = new() { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString };
foreach (JsonElement jsonElement in jsonElements)
{
if (jsonElement.ValueKind != JsonValueKind.Object)
throw new Exception();
description = JsonSerializer.Deserialize<Description>(jsonElement.ToString(), jsonSerializerOptions);
if (description is null)
continue;
results.Add(description);
}
return results;
}
private void PopulateCalculated(List<Detail> details)
{
List<double> thicknessPoints = new();
foreach (Detail bioRadDetail in details)
{
if (!double.TryParse(bioRadDetail.Thickness, out double thickness))
thicknessPoints.Add(0);
else
thicknessPoints.Add(thickness);
}
if (thicknessPoints.Count != 14)
{
ThicknessFourteenPoints = string.Empty;
ThicknessFourteenMeanFrom = string.Empty;
ThicknessFourteenCenterMean = string.Empty;
ThicknessFourteen3mmEdgeMean = string.Empty;
ThicknessFourteen5mmEdgeMean = string.Empty;
ThicknessFourteen3mmEdgePercent = string.Empty;
ThicknessFourteen5mmEdgePercent = string.Empty;
ThicknessFourteenCriticalPointsStdDev = string.Empty;
ThicknessFourteenCriticalPointsAverage = string.Empty;
}
else
{
ThicknessFourteenPoints = string.Join(",", thicknessPoints);
ThicknessFourteenCenterMean = thicknessPoints[4].ToString("0.0000000");
ThicknessFourteen5mmEdgeMean = ((thicknessPoints[0] + thicknessPoints[9]) / 2.0).ToString("0.0000000");
ThicknessFourteenMeanFrom = ((thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[7]) / 3.0).ToString("0.0000000");
ThicknessFourteen3mmEdgeMean = ((thicknessPoints[10] + thicknessPoints[11] + thicknessPoints[12] + thicknessPoints[13]) / 4).ToString("0.0000000");
ThicknessFourteen5mmEdgePercent = ((((thicknessPoints[0] + thicknessPoints[9]) / 2.0) - ((thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[7]) / 3.0)) / ((thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[7]) / 3.0) * 100.0).ToString("0.0000000");
ThicknessFourteenCriticalPointsAverage = ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0).ToString("0.0000000");
ThicknessFourteen3mmEdgePercent = ((((thicknessPoints[10] + thicknessPoints[11] + thicknessPoints[12] + thicknessPoints[13]) / 4.0) - ((thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[7]) / 3.0)) / ((thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[7]) / 3.0) * 100.0).ToString("0.0000000");
ThicknessFourteenCriticalPointsStdDev = Math.Sqrt((Math.Pow(thicknessPoints[0] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[1] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[2] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[3] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[4] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[5] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[6] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[7] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[8] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2) + Math.Pow(thicknessPoints[9] - ((thicknessPoints[0] + thicknessPoints[1] + thicknessPoints[2] + thicknessPoints[3] + thicknessPoints[4] + thicknessPoints[5] + thicknessPoints[6] + thicknessPoints[7] + thicknessPoints[8] + thicknessPoints[9]) / 10.0), 2)) / 9.0).ToString("0.0000000");
}
}
}