580 lines
19 KiB
C#
580 lines
19 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.txt;
|
|
|
|
public class ProcessData : IProcessData
|
|
{
|
|
|
|
private int _I;
|
|
private string _Data;
|
|
|
|
private readonly ILog _Log;
|
|
private readonly List<object> _Details;
|
|
|
|
public string JobID { get; set; }
|
|
public string MesEntity { get; set; }
|
|
public string AutoOptimizeGain { get; set; }
|
|
public string AutoProbeHeightSet { get; set; }
|
|
public string Avg { get; set; }
|
|
public string DLRatio { get; set; }
|
|
public string DataReject { get; set; }
|
|
public DateTime Date { get; set; }
|
|
public string Employee { get; set; }
|
|
public string Engineer { get; set; }
|
|
public string EquipId { get; set; }
|
|
public string FileName { get; set; }
|
|
public string Layer { get; set; }
|
|
public string Lot { get; set; }
|
|
public string PSN { get; set; }
|
|
public string RDS { get; set; }
|
|
public string Reactor { get; set; }
|
|
public string Recipe { get; set; }
|
|
public string ResistivitySpec { get; set; }
|
|
public string Run { get; set; }
|
|
public string SemiRadial { get; set; }
|
|
public string StdDev { get; set; }
|
|
public string Temp { get; set; }
|
|
public string UniqueId { get; set; }
|
|
public string Zone { get; set; }
|
|
|
|
List<object> Shared.Properties.IProcessData.Details => _Details;
|
|
|
|
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection)
|
|
{
|
|
fileInfoCollection.Clear();
|
|
_Details = new List<object>();
|
|
_I = 0;
|
|
_Data = string.Empty;
|
|
JobID = logistics.JobID;
|
|
MesEntity = logistics.MesEntity;
|
|
_Log = LogManager.GetLogger(typeof(ProcessData));
|
|
Parse(fileRead, logistics, fileInfoCollection);
|
|
}
|
|
|
|
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();
|
|
}
|
|
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() => GetBefore("\n");
|
|
|
|
private string GetToEOL(bool trim)
|
|
{
|
|
string str;
|
|
str = !trim ? GetBefore("\n", 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 = "HH:mm MM/dd/yy";
|
|
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, bool hasRDS)
|
|
{
|
|
string rds;
|
|
string reactor;
|
|
if (string.IsNullOrEmpty(text) || segments.Length == 0 || string.IsNullOrEmpty(formattedText) || (segments.Length > 1 && !hasRDS))
|
|
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, bool hasRDS)
|
|
{
|
|
string psn;
|
|
string layer;
|
|
if (segments.Length <= 2 || (segments.Length > 1 && !hasRDS))
|
|
{
|
|
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 run;
|
|
string zone;
|
|
string layer;
|
|
string title;
|
|
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 (string.IsNullOrEmpty(text) || (text.Length is 2 or 3 && Regex.IsMatch(text, "^[a-zA-z]{2,3}")))
|
|
{
|
|
run = text;
|
|
title = 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('.');
|
|
run = text;
|
|
title = 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 run.
|
|
title = Regex.Replace(text, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
|
|
if (title.Length > 2 && title[0] == '1' && (title[1] == 'T' || title[1] == 't'))
|
|
title = title.Substring(2);
|
|
run = title;
|
|
string[] segments = title.Split('-');
|
|
bool hasRDS = Regex.IsMatch(title, "[-]?[0-9]{5,}[-]?");
|
|
(reactor, rds) = GetReactorAndRDS(defaultReactor, defaultRDS, text, title, segments, hasRDS);
|
|
(layer, psn) = GetLayerAndPSN(defaultLayer, defaultPSN, segments, hasRDS);
|
|
zone = GetZone(segments);
|
|
employee = defaultEmployee;
|
|
}
|
|
result = new(employee, layer, psn, rds, reactor, run, title, zone);
|
|
return result;
|
|
}
|
|
|
|
private void Set(Logistics logistics, string receivedData)
|
|
{
|
|
string lot;
|
|
string psn;
|
|
string rds;
|
|
string run;
|
|
string temp;
|
|
string zone;
|
|
string layer;
|
|
string title;
|
|
DateTime date;
|
|
string recipe;
|
|
string dlRatio;
|
|
string equipId;
|
|
string reactor;
|
|
string employee;
|
|
string engineer;
|
|
string resistivitySpec;
|
|
ScanPast("RUN:");
|
|
title = GetToEOL();
|
|
ScanPast("Recipe:");
|
|
recipe = GetBefore("RESISTIVITY SPEC:");
|
|
if (string.IsNullOrEmpty(recipe))
|
|
{
|
|
_I = 0;
|
|
_Data = receivedData;
|
|
ScanPast("RUN:");
|
|
title = GetToEOL();
|
|
ScanPast("DEVICE:");
|
|
recipe = GetBefore("RESISTIVITY SPEC:");
|
|
}
|
|
title = title.Trim();
|
|
if (title[0] != '[' && title[title.Length - 1] != ']')
|
|
throw new Exception("Lot summary data is invalid or missing.");
|
|
title = title.Substring(1, title.Length - 2);
|
|
Descriptor descriptor = GetDescriptor(title);
|
|
psn = descriptor.PSN;
|
|
rds = descriptor.RDS;
|
|
run = descriptor.Run;
|
|
zone = descriptor.Zone;
|
|
layer = descriptor.Layer;
|
|
title = descriptor.Title;
|
|
reactor = descriptor.Reactor;
|
|
employee = descriptor.Employee;
|
|
resistivitySpec = GetToEOL();
|
|
ScanPast("EQUIP#:");
|
|
equipId = GetBefore("Engineer:");
|
|
// Remove illegal characters \/:*?"<>| found in the equipId.
|
|
equipId = Regex.Replace(equipId, @"[\\,\/,\:,\*,\?,\"",\<,\>,\|]", "_").Split('\r')[0].Split('\n')[0];
|
|
engineer = GetToEOL();
|
|
ScanPast("LotID:");
|
|
lot = GetBefore("D.L.RATIO:");
|
|
dlRatio = GetToEOL();
|
|
ScanPast("OPERATOR:");
|
|
if (!string.IsNullOrEmpty(employee))
|
|
_ = GetBefore("TEMP:");
|
|
else
|
|
employee = GetBefore("TEMP:");
|
|
temp = GetToken();
|
|
string dateTimeText = GetToEOL();
|
|
date = GetDateTime(logistics, dateTimeText);
|
|
//create filename / unique id
|
|
string timeFormat = "yyyyMMddHHmmss";
|
|
_Log.Debug($"****ParseData - Title:{title}; EquipId:{equipId};");
|
|
if (string.IsNullOrEmpty(title))
|
|
throw new Exception("Batch (title) information does not exist");
|
|
Lot = lot;
|
|
PSN = psn;
|
|
RDS = rds;
|
|
Run = run;
|
|
Date = date;
|
|
Temp = temp;
|
|
Zone = zone;
|
|
Layer = layer;
|
|
Recipe = recipe;
|
|
DLRatio = dlRatio;
|
|
Reactor = reactor;
|
|
Employee = employee;
|
|
Engineer = engineer;
|
|
ResistivitySpec = resistivitySpec;
|
|
UniqueId = string.Concat(equipId, "_", title, "_", logistics.DateTimeFromSequence.ToString(timeFormat));
|
|
}
|
|
|
|
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection)
|
|
{
|
|
if (fileRead is null)
|
|
{ }
|
|
// Convert the source file to UTF8Encoding format and then back to string for processing. This convertion
|
|
// shall eliminate the special HEX characters such as 0x18 "CANCEL" and 0x20 "SPACE" captured via nPort.
|
|
string rawText = File.ReadAllText(logistics.ReportFullPath);
|
|
UTF8Encoding utf8Encoding = new();
|
|
byte[] bytes = utf8Encoding.GetBytes(rawText);
|
|
string convertedText = utf8Encoding.GetString(bytes);
|
|
// Replaces all control characters with a space, except for the TAB (0x09), LF (0x0A), CR (0x0D), and
|
|
// normal ASCII characters, which are valid characters for SharePoint.
|
|
string receivedData = Regex.Replace(convertedText, @"[^\u0009\u000A\u000D\u0020-\u007E]", " ");
|
|
string log = receivedData;
|
|
for (short i = 0; i < short.MaxValue; i++)
|
|
{
|
|
if (!log.Contains(" "))
|
|
break;
|
|
log = log.Replace(" ", " ");
|
|
}
|
|
log = log.Replace(" ", "\t").Replace(": ", "\t").Replace(":\t", "\t");
|
|
IEnumerable<string> lines = from l in log.Split('\r') select l.Trim();
|
|
string logFile = Path.ChangeExtension(logistics.ReportFullPath, ".log");
|
|
File.WriteAllLines(logFile, lines);
|
|
fileInfoCollection.Add(new FileInfo(logFile));
|
|
//parse file
|
|
string h = string.Empty;
|
|
receivedData = receivedData.Replace("\r", "\n").Trim();
|
|
_I = 0;
|
|
_Data = string.Empty;
|
|
if (string.IsNullOrEmpty(receivedData))
|
|
throw new Exception("No data!");
|
|
Detail detail;
|
|
_I = 0;
|
|
_Data = receivedData;
|
|
Set(logistics, receivedData);
|
|
ScanPast("AutoOptimizeGain =");
|
|
AutoOptimizeGain = GetBefore("AutoProbeHeightSet =");
|
|
AutoProbeHeightSet = GetToEOL();
|
|
ScanPast("DataReject");
|
|
DataReject = GetToEOL();
|
|
_ = GetToEOL();
|
|
FileName = GetToEOL();
|
|
_ = GetToEOL();
|
|
_ = GetToEOL();
|
|
bool check = false;
|
|
while (!IsBlankLine())
|
|
{
|
|
detail = new Detail() { Pt = GetToken() };
|
|
if (detail.Pt.Contains("Avg"))
|
|
break;
|
|
else if (!detail.Pt.Contains(':'))
|
|
{
|
|
detail.R = GetToken();
|
|
detail.T = GetToken();
|
|
detail.Rs = GetToken();
|
|
detail.Merit = GetToken();
|
|
detail.UniqueId = string.Concat("_Point-", _Details.Count + 1);
|
|
_ = GetToEOL();
|
|
_Details.Add(detail);
|
|
}
|
|
else
|
|
{
|
|
check = true;
|
|
break;
|
|
}
|
|
}
|
|
_I = 0;
|
|
_Data = receivedData;
|
|
if (!check)
|
|
{
|
|
ScanPast("Avg =");
|
|
Avg = GetToken();
|
|
StdDev = GetToken();
|
|
ScanPast("SEMI Radial=");
|
|
SemiRadial = GetToEOL();
|
|
}
|
|
else
|
|
{
|
|
ScanPast("RsAv ");
|
|
Avg = GetBefore("+/-");
|
|
StdDev = GetToken();
|
|
_Log.Debug($"****ProcessData - RsAv StDev={StdDev}");
|
|
ScanPast("(Mx+Mn)");
|
|
SemiRadial = GetToken();
|
|
_Log.Debug($"****ProcessData - RsAv SemiRadial={SemiRadial}");
|
|
_ = GetToEOL();
|
|
int num = 0;
|
|
_ = GetBefore(": ");
|
|
for (string i = GetToken(); !string.IsNullOrEmpty(i); i = GetToken())
|
|
{
|
|
if (!i.Contains(':'))
|
|
{
|
|
detail = new Detail();
|
|
int num1 = num + 1;
|
|
num = num1;
|
|
_Log.Debug($"****ProcessData - RsAv Point={num1}");
|
|
detail.Pt = num1.ToString();
|
|
detail.Rs = i;
|
|
detail.Merit = GetToken().Replace("|", "");
|
|
detail.UniqueId = string.Concat("_Point-", _Details.Count + 1);
|
|
_Details.Add(detail);
|
|
}
|
|
}
|
|
}
|
|
foreach (Detail item in _Details.Cast<Detail>())
|
|
{
|
|
item.HeaderUniqueId = UniqueId;
|
|
item.UniqueId = string.Concat(item, item.UniqueId);
|
|
}
|
|
fileInfoCollection.Add(logistics.FileInfo);
|
|
}
|
|
|
|
#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;
|
|
}
|
|
|
|
} |