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.QS408M; #nullable enable internal class Run { public Run(Header header, ReadOnlyCollection sites, Body body, Footer footer) { Header = header; Sites = sites; Body = body; Footer = footer; } public Header Header { get; } public ReadOnlyCollection Sites { get; } public Body Body { get; } public Footer Footer { get; } internal static string GetBefore(string text, int[] i, string search) { string str; string str1; int num = text.IndexOf(search, i[0]); if (num <= -1) { str = text.Substring(i[0]); i[0] = text.Length; str1 = str.Trim(); } else { str = text.Substring(i[0], num - i[0]); i[0] = num + search.Length; str1 = str.Trim(); } return str1; } internal static string GetToEOL(string text, int[] i) { string result; if (text.IndexOf("\n", i[0]) > -1) result = GetBefore(text, i, "\n"); else result = GetBefore(text, i, Environment.NewLine); return result; } internal static int ScanPast(string text, int[] i, string search) { int result; int num = text.IndexOf(search, i[0]); if (num <= -1) result = text.Length; else result = num + search.Length; return result; } private static void WriteJson(Logistics logistics, List 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 GetLines(Logistics logistics, JsonElement[]? jsonElements) { List 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 results = new(); Row row; for (int i = 0; i < run.Sites.Count; i++) { row = new(run, i); results.Add(row); } string json = JsonSerializer.Serialize(results); JsonElement[]? jsonElements = JsonSerializer.Deserialize(json); ReadOnlyCollection lines = GetLines(logistics, jsonElements); File.WriteAllText($"{logistics.ReportFullPath}.csv", string.Join(Environment.NewLine, lines)); } internal static Run? Get(Logistics logistics, List fileInfoCollection, Header[] lastHeader) { Run? result; int[] i = new int[] { 0 }; string text = File.ReadAllText(logistics.ReportFullPath); Header? header = Header.Get(lastHeader, text, i); if (header is null) result = null; else { ReadOnlyCollection sites = Site.Get(text, i); if (sites.Count == 0) result = null; else { Body? body = Body.Get(text, i); if (body is null) result = null; else { Footer? footer = Footer.Get(text, i); if (footer is null) result = null; else { result = new(header, sites, body, footer); WriteJson(logistics, fileInfoCollection, result); WriteCommaSeparatedValues(logistics, result); } } } } return result; } } // Bio-Rad QS400MEPI Recipe: EP_8IN9PT Thu Apr 30 11:29:10 1970 // operator: J batch: BIORAD#2 // cassette: wafer: 52-589368-4445 // -------------------------------------------------------------------------------- // position thickness position thickness position thickness // 1 45.735 2 46.536 3 46.742 // 4 46.015 5 46.648 6 45.366 // 7 46.263 8 46.512 9 46.373 // wafer mean thickness = 46.2433, std. dev = 0.4564 PASS // ================================================================================ // Radial variation (computation B) PASS: // thickness -2.7474 [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(Run))] internal partial class RunSourceGenerationContext : JsonSerializerContext { }