Mike Phares 0f95c45638 Extra Thickness, um line
Nuget Bump
del
ProcessData : IProcessData
IDescription.GetDescriptions with body
2023-06-19 10:32:15 -07:00

637 lines
23 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.Stratus;
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 FilePath { get; set; }
public string Layer { get; set; }
public string MeanThickness { get; set; }
public string Employee { get; set; }
public string PSN { get; set; }
public string RDS { get; set; }
public string Reactor { get; set; }
public string StdDev { get; set; }
public string Title { get; set; }
public string UniqueId { get; set; }
public string Zone { get; set; }
List<object> Shared.Properties.IProcessData.Details => _Details;
private int _I;
private string _Data;
private readonly ILog _Log;
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string originalDataBioRad, string dataText)
{
JobID = logistics.JobID;
fileInfoCollection.Clear();
_Details = new List<object>();
MesEntity = logistics.MesEntity;
_Log = LogManager.GetLogger(typeof(ProcessData));
Parse(fileRead, logistics, fileInfoCollection, originalDataBioRad, dataText);
}
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.BioRadStratus);
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 string GetBefore(string text)
{
string str;
string str1;
int num = _Data.IndexOf(text, _I);
if (num <= -1)
{
str = _Data.Substring(_I);
_I = _Data.Length;
str1 = str.Trim();
}
else
{
str = _Data.Substring(_I, num - _I);
_I = num + text.Length;
str1 = str.Trim();
}
return str1;
}
private string GetBefore(string text, bool trim)
{
string str;
string before;
if (!trim)
{
int num = _Data.IndexOf(text, _I);
if (num <= -1)
{
str = _Data.Substring(_I);
_I = _Data.Length;
before = str;
}
else
{
str = _Data.Substring(_I, num - _I);
_I = num + text.Length;
before = str;
}
}
else
{
before = GetBefore(text);
}
return before;
}
private string GetToEOL()
{
string result;
if (_Data.IndexOf("\n", _I) > -1)
result = GetBefore("\n");
else
result = GetBefore(Environment.NewLine);
return result;
}
private string GetToEOL(bool trim)
{
string str;
if (_Data.IndexOf("\n", _I) > -1)
str = !trim ? GetBefore("\n", false) : GetToEOL();
else
str = !trim ? GetBefore(Environment.NewLine, false) : GetToEOL();
return str;
}
private string GetToken()
{
while (true)
{
if (_I >= _Data.Length || !IsNullOrWhiteSpace(_Data.Substring(_I, 1)))
{
break;
}
_I++;
}
int num = _I;
while (true)
{
if (num >= _Data.Length || IsNullOrWhiteSpace(_Data.Substring(num, 1)))
{
break;
}
num++;
}
string str = _Data.Substring(_I, num - _I);
_I = num;
return str.Trim();
}
private string GetToText(string text)
{
string str = _Data.Substring(_I, _Data.IndexOf(text, _I) - _I).Trim();
return str;
}
private bool IsBlankLine()
{
int num = _Data.IndexOf("\n", _I);
return IsNullOrWhiteSpace(num > -1 ? _Data.Substring(_I, num - _I) : _Data.Substring(_I));
}
private static bool IsNullOrWhiteSpace(string text)
{
bool flag;
int num = 0;
while (true)
{
if (num >= text.Length)
{
flag = true;
break;
}
else if (char.IsWhiteSpace(text[num]))
{
num++;
}
else
{
flag = false;
break;
}
}
return flag;
}
private string PeekNextLine()
{
int num = _I;
string toEOL = GetToEOL();
_I = num;
return toEOL;
}
private void ScanPast(string text)
{
int num = _Data.IndexOf(text, _I);
if (num <= -1)
{
_I = _Data.Length;
}
else
{
_I = num + text.Length;
}
}
internal static DateTime GetDateTime(Logistics logistics, string dateTimeText)
{
DateTime result;
string inputDateFormat = "MM/dd/yy HH:mm";
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 = dateTimeParsed;
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 reactor;
string cassette;
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}$"))
{
cassette = 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}")))
{
cassette = text;
psn = defaultPSN;
rds = defaultRDS;
zone = defaultZone;
employee = cassette;
layer = defaultLayer;
reactor = defaultReactor;
}
else if (Regex.IsMatch(text, @"^[0-9]{2}[.][0-9]{1}[.]?[0-9]{0,1}"))
{
string[] segments = text.Split('.');
cassette = text;
psn = defaultPSN;
rds = defaultRDS;
layer = segments[1];
reactor = segments[0];
employee = defaultEmployee;
if (segments.Length <= 2)
zone = defaultZone;
else
zone = segments[2];
}
else
{
string[] segments;
// Remove illegal characters \/:*?"<>| found in the Cassette.
cassette = Regex.Replace(text, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
if (cassette.Length > 2 && cassette[0] == '1' && (cassette[1] == 'T' || cassette[1] == 't'))
cassette = cassette.Substring(2);
if (cassette.Contains('-'))
segments = cassette.Split(new char[] { '-' });
else if (!cassette.Contains('\u005F'))
segments = cassette.Split(new char[] { ' ' });
else if (cassette.Contains('.'))
segments = cassette.Split(new char[] { '.' });
else
segments = cassette.Split(new char[] { '\u005F' });
// bool hasRDS = Regex.IsMatch(cassette, "[-]?([QP][0-9]{4,}|[0-9]{5,})[-]?");
(reactor, rds) = GetReactorAndRDS(defaultReactor, defaultRDS, text, cassette, segments);
(layer, psn) = GetLayerAndPSN(defaultLayer, defaultPSN, segments);
zone = GetZone(segments);
if (segments.Length <= 3 || segments[3].Length <= 1)
employee = defaultEmployee;
else
employee = segments[3];
}
result = new(cassette, employee, layer, psn, rds, reactor, zone);
return result;
}
private void Set(Logistics logistics)
{
string psn;
string rds;
string text;
string zone;
string batch;
string layer;
string title;
DateTime date;
string reactor;
string cassette;
string employee;
const string batchKey = "Batch";
const string startedKey = "started";
const string cassetteKey = "Cassette";
const string startedAtKey = "started at";
if (!_Data.Contains(batchKey) || !_Data.Contains(startedKey))
batch = string.Empty;
else
{
for (int z = 0; z < int.MaxValue; z++)
{
ScanPast(batchKey);
if (!_Data.Substring(_I).Contains(batchKey))
break;
}
batch = GetToText(startedKey);
ScanPast(startedAtKey);
}
ScanPast(cassetteKey);
if (!_Data.Substring(_I).Contains(startedKey))
text = string.Empty;
else
text = GetToText(startedKey);
ScanPast(startedAtKey);
string dateTimeText = GetToEOL();
if (dateTimeText.EndsWith("."))
dateTimeText = dateTimeText.Remove(dateTimeText.Length - 1, 1);
date = GetDateTime(logistics, dateTimeText);
Descriptor descriptor = GetDescriptor(text);
cassette = descriptor.Cassette;
psn = descriptor.PSN;
rds = descriptor.RDS;
zone = descriptor.Zone;
layer = descriptor.Layer;
reactor = descriptor.Reactor;
employee = descriptor.Employee;
title = !string.IsNullOrEmpty(batch) ? batch : cassette;
PSN = psn;
RDS = rds;
Date = date;
Zone = zone;
Batch = batch;
Layer = layer;
Title = title;
Reactor = reactor;
Cassette = cassette;
Employee = employee;
UniqueId = string.Concat("StratusBioRad_", reactor, "_", rds, "_", psn, "_", logistics.DateTimeFromSequence.ToString("yyyyMMddHHmmssffff"));
}
#pragma warning disable IDE0060
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string originalDataBioRad, string receivedData)
#pragma warning restore IDE0060
{
_I = 0;
_Data = string.Empty;
List<Detail> details = new();
if (string.IsNullOrEmpty(receivedData))
receivedData = File.ReadAllText(logistics.ReportFullPath);
_Log.Debug($"****ParseData - Source file contents:");
_Log.Debug(receivedData);
List<string> moveFiles = new();
string directoryName = Path.GetDirectoryName(logistics.ReportFullPath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(logistics.ReportFullPath);
moveFiles.AddRange(Directory.GetFiles(directoryName, string.Concat(originalDataBioRad, "*", logistics.Sequence, "*"), SearchOption.TopDirectoryOnly));
moveFiles.AddRange(Directory.GetFiles(directoryName, string.Concat(originalDataBioRad, "*", fileNameWithoutExtension.Split('_').Last(), "*"), SearchOption.TopDirectoryOnly));
foreach (string moveFile in moveFiles.Distinct())
fileInfoCollection.Add(new FileInfo(moveFile));
if (!string.IsNullOrEmpty(receivedData))
{
int i;
int num;
int num2;
Point point;
int num1 = 0;
Detail detail;
string recipe;
_I = 0;
_Data = receivedData;
Set(logistics);
string cassette = "Cassette";
if (PeekNextLine().Contains("Wafer"))
{
_Log.Debug("****ProcessData Contains Wafer");
while (!PeekNextLine().Contains(cassette))
{
num2 = num1;
num1 = num2 + 1;
if (num2 > 25)
break;
else
{
_Log.Debug("****ProcessData new stratusBioRadWaferDetail");
detail = new Detail();
ScanPast("Wafer");
detail.Wafer = GetToEOL();
if (detail.Wafer.EndsWith("."))
{
_Log.Debug("****ProcessData Removing Wafer");
detail.Wafer = detail.Wafer.Remove(detail.Wafer.Length - 1, 1);
}
ScanPast("Slot");
detail.Slot = GetToEOL();
ScanPast("Recipe");
recipe = GetToEOL();
if (recipe.EndsWith("."))
{
_Log.Debug("****ProcessData Removing Recipe");
recipe = recipe.Remove(recipe.Length - 1, 1);
}
detail.Recipe = recipe;
_ = GetToEOL();
if (PeekNextLine().Contains("Thickness"))
{
ScanPast("1 - ");
num = Convert.ToInt32(GetToken());
_Log.Debug(string.Concat("****ProcessData Thickness =", num));
detail.Points = new();
for (i = 0; i < num; i++)
{
point = new() { Thickness = GetToken() };
if (point.Thickness == "Thickness,")
{
ScanPast("um");
continue;
}
detail.Points.Add(point);
point.Position = Convert.ToString(detail.Points.Count);
}
}
_ = GetToEOL();
if (PeekNextLine().Contains("Thickness"))
{
ScanPast("11 - ");
num = Convert.ToInt32(GetToken());
for (i = detail.Points.Count; i < num; i++)
{
point = new() { Thickness = GetToken() };
detail.Points.Add(point);
point.Position = Convert.ToString(detail.Points.Count);
}
}
ScanPast("Slot");
_ = GetToken();
detail.PassFail = GetToken();
if (detail.PassFail.EndsWith("."))
{
_Log.Debug("****ProcessData Removing PassFail");
detail.PassFail = detail.PassFail.Remove(detail.PassFail.Length - 1, 1);
}
ScanPast("Mean");
detail.Mean = GetToken();
if (detail.Mean.EndsWith(","))
{
_Log.Debug("****ProcessData Removing Mean");
detail.Mean = detail.Mean.Remove(detail.Mean.Length - 1, 1);
}
ScanPast("STDD");
detail.StdDev = GetToEOL();
if (detail.StdDev.EndsWith("."))
{
_Log.Debug("****ProcessData Removing stdDev");
detail.StdDev = detail.StdDev.Remove(detail.StdDev.Length - 1, 1);
}
detail.UniqueId = string.Concat("_Wafer-", detail.Wafer, "_Slot-", detail.Slot, "_Point-", detail.Position);
details.Add(detail);
if (PeekNextLine().Contains(cassette))
_ = GetToEOL();
if (PeekNextLine().Contains(cassette))
_ = GetToEOL();
if (PeekNextLine().Contains("Process failed"))
_ = GetToEOL();
}
}
ScanPast("Mean");
MeanThickness = GetToken();
if (MeanThickness.EndsWith(","))
{
_Log.Debug("****ProcessData Removing MeanThickness");
MeanThickness = MeanThickness.Remove(MeanThickness.Length - 1, 1);
}
ScanPast("STDD");
StdDev = GetToken();
if (StdDev.EndsWith(","))
{
_Log.Debug("****ProcessData Removing thi.StdDev");
StdDev = StdDev.Remove(StdDev.Length - 1, 1);
}
}
if (receivedData.Contains("------------- Process failed -------------"))
details.Add(new());
}
StringBuilder stringBuilder = new();
foreach (Detail detail in details)
{
detail.HeaderUniqueId = UniqueId;
detail.UniqueId = string.Concat(UniqueId, detail.UniqueId);
detail.Points ??= new List<Point>();
foreach (Point bioRadDetail in detail.Points)
{
bioRadDetail.HeaderUniqueId = detail.HeaderUniqueId;
bioRadDetail.UniqueId = detail.UniqueId;
}
_ = stringBuilder.Clear();
foreach (Point point in detail.Points)
_ = stringBuilder.Append(point.Thickness).Append(',');
if (stringBuilder.Length > 0)
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
detail.Thickness = stringBuilder.ToString();
_ = stringBuilder.Clear();
foreach (Point point in detail.Points)
_ = stringBuilder.Append(point.Position).Append(',');
if (stringBuilder.Length > 0)
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
detail.Position = stringBuilder.ToString();
}
fileInfoCollection.Add(logistics.FileInfo);
_Details.AddRange(details);
}
#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;
}
}