Mike Phares 374e375c5e json in process-data-standard-format
when mes-entity-matches-process save to MET08THFTIRQS408M

EDA Logic for GV
2025-05-19 10:23:00 -07:00

176 lines
6.8 KiB
C#

using Adaptation.Shared;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Adaptation.FileHandlers.txt;
#nullable enable
internal class Run
{
public Run(Header header, ReadOnlyCollection<Wafer> wafers, Grade grade)
{
Header = header;
Wafers = wafers;
Grade = grade;
}
public Header Header { get; }
public ReadOnlyCollection<Wafer> Wafers { get; }
public Grade Grade { get; }
private static void WriteJson(Logistics logistics, List<FileInfo> _, int r, Run result)
{
FileInfo fileInfo = new($"{logistics.ReportFullPath}-{r}.run.json");
string json = JsonSerializer.Serialize(result, RunSourceGenerationContext.Default.Run);
File.WriteAllText(fileInfo.FullName, json);
File.SetLastWriteTime(fileInfo.FullName, logistics.DateTimeFromSequence);
// Can't add until old parse is removed // fileInfoCollection.Add(fileInfo);
}
private static ReadOnlyCollection<string> GetLines(Logistics logistics, JsonElement[]? jsonElements)
{
List<string> results = new();
int columns = 0;
StringBuilder stringBuilder = new();
results.Add($"\"Count\",{jsonElements?.Length}");
results.Add($"\"{nameof(logistics.Sequence)}\",\"{logistics.Sequence}\"");
results.Add($"\"{nameof(logistics.MesEntity)}\",\"{logistics.MesEntity}\"");
string dateTimeFromSequence = logistics.DateTimeFromSequence.ToString("MM/dd/yyyy hh:mm:ss tt");
for (int i = 0; i < jsonElements?.Length;)
{
_ = stringBuilder.Append('"').Append(nameof(logistics.DateTimeFromSequence)).Append('"').Append(',');
foreach (JsonProperty jsonProperty in jsonElements[0].EnumerateObject())
{
columns += 1;
_ = stringBuilder.Append('"').Append(jsonProperty.Name).Append('"').Append(',');
}
break;
}
if (jsonElements?.Length != 0)
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
results.Add(stringBuilder.ToString());
for (int i = 0; i < jsonElements?.Length; i++)
{
_ = stringBuilder.Clear();
_ = stringBuilder.Append('"').Append(dateTimeFromSequence).Append('"').Append(',');
foreach (JsonProperty jsonProperty in jsonElements[i].EnumerateObject())
{
if (jsonProperty.Value.ValueKind == JsonValueKind.Object)
_ = stringBuilder.Append(',');
else if (jsonProperty.Value.ValueKind != JsonValueKind.String)
_ = stringBuilder.Append(jsonProperty.Value).Append(',');
else
_ = stringBuilder.Append('"').Append(jsonProperty.Value).Append('"').Append(',');
}
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
results.Add(stringBuilder.ToString());
}
return results.AsReadOnly();
}
private static void WriteCommaSeparatedValues(Logistics logistics, int r, Run run)
{
List<Row> results = new();
Row row;
int index = 0;
for (int w = 0; w < run.Wafers.Count; w++)
{
for (int s = 0; s < run.Wafers[w].Sites.Count; s++)
{
row = new(run, index, w, s);
results.Add(row);
index += 1;
}
}
string json = JsonSerializer.Serialize(results);
JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
ReadOnlyCollection<string> lines = GetLines(logistics, jsonElements);
File.WriteAllText($"{logistics.ReportFullPath}-{r}.csv", string.Join(Environment.NewLine, lines));
}
private static ReadOnlyCollection<string> GetRuns(Constant constant, string text)
{
List<string> results = new();
string check;
List<string> collection = new();
List<string> lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
lines.Add($"{constant.Batch}{constant.Started}");
foreach (string line in lines)
{
if (line.StartsWith(constant.Batch) && line.Contains(constant.Started))
{
check = string.Join(Environment.NewLine, collection);
if (check.Contains(constant.Finished))
results.Add(check);
collection.Clear();
}
collection.Add(line);
}
return results.AsReadOnly();
}
private static ReadOnlyCollection<Run> GetRuns(Logistics logistics)
{
List<Run> results = new();
int[] i;
Constant constant = new();
string allText = File.ReadAllText(logistics.ReportFullPath);
string[] segments = allText.Split(new string[] { constant.Finished }, StringSplitOptions.None);
if (segments.Length > 1)
{
Run run;
ReadOnlyCollection<string> runs = GetRuns(constant, allText);
foreach (string text in runs)
{
i = new int[] { 0 };
Header? header = Header.Get(text, constant, i);
if (header is not null)
{
ReadOnlyCollection<string> groups = Wafer.GetGroups(text, constant, i);
if (groups.Count > 0)
{
ReadOnlyCollection<Wafer> wafers = Wafer.Get(constant, groups);
if (wafers.Count > 0)
{
Grade? grade = Grade.Get(constant, groups);
if (grade is null)
continue;
run = new(header, wafers, grade);
results.Add(run);
}
}
}
}
}
return results.AsReadOnly();
}
internal static ReadOnlyCollection<Run> Get(long tickOffset, Logistics logistics, List<FileInfo> fileInfoCollection)
{
ReadOnlyCollection<Run> results = GetRuns(logistics);
DateTime afterCheck = new(File.GetLastWriteTime(logistics.ReportFullPath).Ticks + tickOffset);
if (logistics.DateTimeFromSequence != afterCheck)
results = new(new List<Run>());
for (int i = 0; i < results.Count; i++)
{
WriteJson(logistics, fileInfoCollection, i, results[i]);
WriteCommaSeparatedValues(logistics, i, results[i]);
}
return results;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Run))]
internal partial class RunSourceGenerationContext : JsonSerializerContext
{
}