Add Transmission Control Protocol file handling and update PCL serialization
- Introduced FileRead and Record classes for handling file reading in the Transmission Control Protocol. - Enhanced Description, Detail, and other related classes with JSON serialization attributes for improved data handling. - Implemented methods for reading and processing files, including network stream management. - Updated unit tests to cover new functionality and ensure robust testing. - Added new PDSF file handling classes and integrated them into the project structure. - Refactored existing code to utilize source generation for JSON serialization, improving performance and maintainability.
This commit is contained in:
146
Adaptation/FileHandlers/pdsf/Run.cs
Normal file
146
Adaptation/FileHandlers/pdsf/Run.cs
Normal file
@ -0,0 +1,146 @@
|
||||
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.pdsf;
|
||||
|
||||
#nullable enable
|
||||
|
||||
internal class Run
|
||||
{
|
||||
|
||||
public Header Header { get; }
|
||||
public ReadOnlyCollection<Wafer> Wafers { get; }
|
||||
|
||||
public Run(Header header, ReadOnlyCollection<Wafer> wafers)
|
||||
{
|
||||
Header = header;
|
||||
Wafers = wafers;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<Wafer> GetLastWaferForEachSlot(ReadOnlyDictionary<string, string> pages, Constant constant, string headerFileName, Header header)
|
||||
{
|
||||
List<Wafer> results = new();
|
||||
string id;
|
||||
Wafer wafer;
|
||||
ReadOnlyCollection<Wafer>? wafers;
|
||||
ReadOnlyDictionary<string, ReadOnlyCollection<Wafer>> keyValuePairs = Wafer.Get(pages, constant, headerFileName);
|
||||
ReadOnlyCollection<string> waferIds = GetWaferIds(header);
|
||||
for (int i = 0; i < waferIds.Count; i++)
|
||||
{
|
||||
id = waferIds[i];
|
||||
if (!keyValuePairs.TryGetValue(id, out wafers) || wafers.Count == 0)
|
||||
wafer = Wafer.Get(id);
|
||||
else
|
||||
wafer = (from l in wafers where l.Recipe == header.Recipe select l).Last();
|
||||
if (wafer is null)
|
||||
break;
|
||||
results.Add(wafer);
|
||||
}
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
private static void WriteJson(Logistics logistics, List<FileInfo> fileInfoCollection, Run result)
|
||||
{
|
||||
FileInfo fileInfo = new($"{logistics.ReportFullPath}.run.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(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, Run run)
|
||||
{
|
||||
List<Row> results = new();
|
||||
Row row;
|
||||
for (int i = 0; i < run.Wafers.Count; i++)
|
||||
{
|
||||
row = new(run, i);
|
||||
results.Add(row);
|
||||
}
|
||||
string json = JsonSerializer.Serialize(results);
|
||||
JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
|
||||
ReadOnlyCollection<string> lines = GetLines(logistics, jsonElements);
|
||||
File.WriteAllText($"{logistics.ReportFullPath}.csv", string.Join(Environment.NewLine, lines));
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<string> GetWaferIds(Header header)
|
||||
{
|
||||
List<string> results = new();
|
||||
foreach (WaferSummary waferSummary in header.WaferSummary)
|
||||
results.Add(waferSummary.Id);
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
internal static Run? Get(Logistics logistics, List<FileInfo> fileInfoCollection, ReadOnlyDictionary<string, string> pages)
|
||||
{
|
||||
Run? result;
|
||||
Constant constant = new();
|
||||
string headerFileName = pages.ElementAt(pages.Count - 1).Key;
|
||||
Header? header = Header.Get(pages, constant, headerFileName);
|
||||
if (header is null)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
ReadOnlyCollection<Wafer> wafers = GetLastWaferForEachSlot(pages, constant, headerFileName, header);
|
||||
result = new(header, wafers);
|
||||
WriteJson(logistics, fileInfoCollection, result);
|
||||
WriteCommaSeparatedValues(logistics, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(Run))]
|
||||
internal partial class RunSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user