TSV like PDSF ready to test
This commit is contained in:
151
Adaptation/FileHandlers/Stratus/Run.cs
Normal file
151
Adaptation/FileHandlers/Stratus/Run.cs
Normal file
@ -0,0 +1,151 @@
|
||||
using Adaptation.Shared;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Adaptation.FileHandlers.Stratus;
|
||||
|
||||
#nullable enable
|
||||
|
||||
internal class Run
|
||||
{
|
||||
|
||||
public Run(Header header, ReadOnlyCollection<Wafer> wafers, Footer footer)
|
||||
{
|
||||
Header = header;
|
||||
Wafers = wafers;
|
||||
Footer = footer;
|
||||
}
|
||||
|
||||
public Header Header { get; }
|
||||
public ReadOnlyCollection<Wafer> Wafers { get; }
|
||||
public Footer Footer { get; }
|
||||
|
||||
private static void WriteJson(Logistics logistics, List<FileInfo> fileInfoCollection, Run? result)
|
||||
{
|
||||
FileInfo fileInfo = new($"{logistics.ReportFullPath}.json");
|
||||
string json = JsonSerializer.Serialize(result, RunSourceGenerationContext.Default.Run);
|
||||
File.WriteAllText(fileInfo.FullName, json);
|
||||
File.SetLastWriteTime(fileInfo.FullName, logistics.DateTimeFromSequence);
|
||||
fileInfoCollection.Add(fileInfo);
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<string> GetLines(JsonElement[]? jsonElements)
|
||||
{
|
||||
List<string> results = new();
|
||||
int columns = 0;
|
||||
StringBuilder stringBuilder = new();
|
||||
for (int i = 0; i < jsonElements?.Length;)
|
||||
{
|
||||
foreach (JsonProperty jsonProperty in jsonElements[0].EnumerateObject())
|
||||
{
|
||||
columns += 1;
|
||||
_ = stringBuilder.Append('"').Append(jsonProperty.Name).Append('"').Append('\t');
|
||||
}
|
||||
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();
|
||||
foreach (JsonProperty jsonProperty in jsonElements[i].EnumerateObject())
|
||||
{
|
||||
if (jsonProperty.Value.ValueKind == JsonValueKind.Object)
|
||||
_ = stringBuilder.Append('\t');
|
||||
else if (jsonProperty.Value.ValueKind != JsonValueKind.String)
|
||||
_ = stringBuilder.Append(jsonProperty.Value).Append('\t');
|
||||
else
|
||||
_ = stringBuilder.Append('"').Append(jsonProperty.Value).Append('"').Append('\t');
|
||||
}
|
||||
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
|
||||
results.Add(stringBuilder.ToString());
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<string> GetLines(JsonElement jsonElement) =>
|
||||
GetLines(new JsonElement[] { jsonElement });
|
||||
|
||||
private static void WriteTabSeparatedValues(Logistics logistics, List<FileInfo> fileInfoCollection, Run run)
|
||||
{
|
||||
List<Row> results = new();
|
||||
Row row;
|
||||
FileInfo fileInfo = new($"{logistics.ReportFullPath}.tsv");
|
||||
for (int i = 0; i < run.Wafers.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < run.Wafers[i].Sites.Count; j++)
|
||||
{
|
||||
row = new(run, i, j);
|
||||
results.Add(row);
|
||||
}
|
||||
}
|
||||
string json = JsonSerializer.Serialize(results);
|
||||
JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
|
||||
ReadOnlyCollection<string> lines = GetLines(jsonElements);
|
||||
File.WriteAllText(fileInfo.FullName, string.Join(Environment.NewLine, lines));
|
||||
File.SetLastWriteTime(fileInfo.FullName, logistics.DateTimeFromSequence);
|
||||
fileInfoCollection.Add(fileInfo);
|
||||
}
|
||||
|
||||
private static void WriteException(Logistics logistics, Exception ex)
|
||||
{
|
||||
FileInfo fileInfo = new($"{logistics.ReportFullPath}.{nameof(Exception)}.txt");
|
||||
File.WriteAllText(fileInfo.FullName, $"{ex.Message}{Environment.NewLine}{ex.StackTrace}");
|
||||
}
|
||||
|
||||
internal static Run? Get(Logistics logistics, List<FileInfo> fileInfoCollection)
|
||||
{
|
||||
Run? result;
|
||||
Constant constant = new();
|
||||
int[] i = new int[] { 0 };
|
||||
string text = File.ReadAllText(logistics.ReportFullPath);
|
||||
Header? header = Header.Get(text, constant, i);
|
||||
if (header is null)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
ReadOnlyCollection<string> groups = Wafer.GetGroups(text, constant, i);
|
||||
if (groups.Count == 0)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
ReadOnlyCollection<Wafer> wafers = Wafer.Get(constant, groups);
|
||||
if (wafers.Count == 0)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
Footer? footer = Footer.Get(constant, groups);
|
||||
if (footer is null)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
result = new(header, wafers, footer);
|
||||
WriteJson(logistics, fileInfoCollection, result);
|
||||
try
|
||||
{
|
||||
WriteTabSeparatedValues(logistics, fileInfoCollection, result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteException(logistics, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(Run))]
|
||||
internal partial class RunSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
Reference in New Issue
Block a user