From 5f8c79e087de8200c33098d91cf7264dd8644535 Mon Sep 17 00:00:00 2001 From: "phares@iscn5cg20977xq" Date: Mon, 8 Sep 2025 10:45:56 -0700 Subject: [PATCH] Refactor OpenInsight file handling to utilize JsonElement for data processing and enhance serialization with JsonSourceGeneration. Updated methods across multiple classes to improve data handling and reduce dependencies on ProcessDataStandardFormat. --- .../FileHandlers/OpenInsight/FileRead.cs | 11 +-- .../FileHandlers/OpenInsight/FromIQS.cs | 60 +-------------- .../OpenInsightMetrologyViewer/FileRead.cs | 11 +-- .../OpenInsightMetrologyViewer/WSRequest.cs | 15 ++-- .../FileRead.cs | 11 +-- Adaptation/FileHandlers/Processed/FileRead.cs | 17 ++-- Adaptation/FileHandlers/RsM/Description.cs | 77 +++++++++++-------- Adaptation/FileHandlers/RsM/Detail.cs | 26 +++++-- Adaptation/FileHandlers/RsM/ProcessData.cs | 3 +- Adaptation/Shared/Duplicator/Description.cs | 13 ++++ Adaptation/Shared/FileRead.cs | 5 +- Adaptation/Shared/Metrology/WS.cs | 35 +++++++++ .../Shared/ProcessDataStandardFormat.cs | 33 +++++++- 13 files changed, 186 insertions(+), 131 deletions(-) diff --git a/Adaptation/FileHandlers/OpenInsight/FileRead.cs b/Adaptation/FileHandlers/OpenInsight/FileRead.cs index df37292..c552ed0 100644 --- a/Adaptation/FileHandlers/OpenInsight/FileRead.cs +++ b/Adaptation/FileHandlers/OpenInsight/FileRead.cs @@ -110,7 +110,7 @@ public class FileRead : Shared.FileRead, IFileRead return results; } - private void SaveOpenInsightFile(string reportFullPath, DateTime dateTime, ProcessDataStandardFormat processDataStandardFormat, List descriptions, Test[] tests) + private void SaveOpenInsightFile(string reportFullPath, DateTime dateTime, List descriptions, Test[] tests) { string duplicateFile; bool isDummyRun = false; @@ -143,7 +143,7 @@ public class FileRead : Shared.FileRead, IFileRead else duplicateFile = Path.Combine(duplicateDirectory, $"{$"Viewer {subgroupId}".TrimEnd()} {fileName.Replace("Viewer", string.Empty)}"); string weekOfYear = _Calendar.GetWeekOfYear(_Logistics.DateTimeFromSequence, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00"); - FromIQS.Save(_OpenInsightApiECDirectory, _Logistics, reportFullPath, processDataStandardFormat, descriptions.First(), subgroupId, weekOfYear); + FromIQS.Save(_OpenInsightApiECDirectory, _Logistics, reportFullPath, descriptions.First(), subgroupId, weekOfYear); } if (_IsEAFHosted && _FileConnectorConfiguration.FileScanningIntervalInSeconds > 0) { @@ -155,14 +155,15 @@ public class FileRead : Shared.FileRead, IFileRead private Tuple> GetExtractResult(string reportFullPath, DateTime dateTime) { Tuple> results; - ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath); + string[] lines = File.ReadAllLines(reportFullPath); + ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath, lines); _Logistics = new Logistics(reportFullPath, processDataStandardFormat); SetFileParameterLotIDToLogisticsMID(); - JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(processDataStandardFormat); + JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(reportFullPath, lines, processDataStandardFormat); List descriptions = RsM.ProcessData.GetDescriptions(jsonElements); Test[] tests = (from l in descriptions select (Test)l.Test).ToArray(); if (_IsEAFHosted && _FileConnectorConfiguration.FileScanningIntervalInSeconds > 0) - SaveOpenInsightFile(reportFullPath, dateTime, processDataStandardFormat, descriptions, tests); + SaveOpenInsightFile(reportFullPath, dateTime, descriptions, tests); results = new Tuple>(string.Join(Environment.NewLine, processDataStandardFormat.Logistics), tests, jsonElements, new List()); return results; } diff --git a/Adaptation/FileHandlers/OpenInsight/FromIQS.cs b/Adaptation/FileHandlers/OpenInsight/FromIQS.cs index ef5fd24..b0934ca 100644 --- a/Adaptation/FileHandlers/OpenInsight/FromIQS.cs +++ b/Adaptation/FileHandlers/OpenInsight/FromIQS.cs @@ -325,74 +325,18 @@ public class FromIQS return new(result, count, commandText); } - private static string GetJson(Logistics logistics, ProcessDataStandardFormat processDataStandardFormat, RsM.Description description) + internal static void Save(string openInsightApiECDirectory, Logistics logistics, string reportFullPath, RsM.Description description, long? subGroupId, string weekOfYear) { - string result; - StringBuilder stringBuilder = new(); - var @object = new - { - description.MesEntity, - description.Employee, - description.Layer, - description.PSN, - description.RDS, - description.Reactor, - description.Recipe, - description.Zone, - logistics.DateTimeFromSequence.Ticks - }; - string[] pair; - string safeValue; - string[] segments; - string serializerValue; - foreach (string line in processDataStandardFormat.Logistics) - { - segments = line.Split('\t'); - if (segments.Length < 2) - continue; - segments = segments[1].Split(';'); - _ = stringBuilder.Append('{'); - foreach (string segment in segments) - { - pair = segment.Split('='); - if (pair.Length != 2 || pair[0].Length < 3) - continue; - serializerValue = JsonSerializer.Serialize(pair[1]); - safeValue = serializerValue.Substring(1, serializerValue.Length - 2); - _ = stringBuilder.Append('"').Append(pair[0].Substring(2)).Append('"').Append(':').Append('"').Append(safeValue).Append('"').Append(','); - } - if (stringBuilder.Length > 0) - _ = stringBuilder.Remove(stringBuilder.Length - 1, 1); - _ = stringBuilder.Append('}').Append(','); - } - if (stringBuilder.Length > 0) - _ = stringBuilder.Remove(stringBuilder.Length - 1, 1); - _ = stringBuilder.Append(']').Append('}'); - _ = stringBuilder.Insert(0, ",\"Logistics\":["); - string json = JsonSerializer.Serialize(@object); - _ = stringBuilder.Insert(0, json.Substring(0, json.Length - 1)); - JsonElement? jsonElement = JsonSerializer.Deserialize(stringBuilder.ToString()); - result = jsonElement is null ? "{}" : JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions { WriteIndented = true }); - return result; - } - - internal static void Save(string openInsightApiECDirectory, Logistics logistics, string reportFullPath, ProcessDataStandardFormat processDataStandardFormat, RsM.Description description, long? subGroupId, string weekOfYear) - { - string checkFile; string fileName = Path.GetFileName(reportFullPath); - string json = GetJson(logistics, processDataStandardFormat, description); string? ecPathRoot = Path.GetPathRoot(openInsightApiECDirectory); bool ecExists = ecPathRoot is not null && Directory.Exists(ecPathRoot); string weekYear = $"{logistics.DateTimeFromSequence:yyyy}_Week_{weekOfYear}"; string ecDirectory = Path.Combine(openInsightApiECDirectory, weekYear, $"-{description.PSN}", $"-{description.Reactor}", $"-{description.RDS}", $"-{subGroupId}"); if (ecExists && !Directory.Exists(ecDirectory)) _ = Directory.CreateDirectory(ecDirectory); - checkFile = Path.Combine(ecDirectory, fileName); + string checkFile = Path.Combine(ecDirectory, fileName); if (ecExists && !File.Exists(checkFile)) File.Copy(reportFullPath, checkFile); - checkFile = Path.Combine(ecDirectory, $"{logistics.DateTimeFromSequence.Ticks}.json"); - if (ecExists && !File.Exists(checkFile)) - File.WriteAllText(checkFile, json); } private static string GetCommandText(string[] iqsCopyValues) diff --git a/Adaptation/FileHandlers/OpenInsightMetrologyViewer/FileRead.cs b/Adaptation/FileHandlers/OpenInsightMetrologyViewer/FileRead.cs index bb18147..1bb664e 100644 --- a/Adaptation/FileHandlers/OpenInsightMetrologyViewer/FileRead.cs +++ b/Adaptation/FileHandlers/OpenInsightMetrologyViewer/FileRead.cs @@ -110,10 +110,10 @@ public class FileRead : Shared.FileRead, IFileRead return results; } - private void SendData(string reportFullPath, DateTime dateTime, List descriptions) + private void SendData(string reportFullPath, DateTime dateTime, JsonElement[] jsonElements, List descriptions) { string checkDirectory; - WSRequest wsRequest = new(this, _Logistics, descriptions); + WSRequest wsRequest = new(this, _Logistics, jsonElements, descriptions); int weekOfYear = _Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); string directory = Path.Combine(_OpenInsightMetrologyViewerFileShare, dateTime.Year.ToString(), $"WW{weekOfYear:00}"); checkDirectory = Path.Combine(directory, _Logistics.Sequence.ToString()); @@ -139,14 +139,15 @@ public class FileRead : Shared.FileRead, IFileRead private Tuple> GetExtractResult(string reportFullPath, DateTime dateTime) { Tuple> results; - ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath); + string[] lines = File.ReadAllLines(reportFullPath); + ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath, lines); _Logistics = new Logistics(reportFullPath, processDataStandardFormat); SetFileParameterLotIDToLogisticsMID(); - JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(processDataStandardFormat); + JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(reportFullPath, lines, processDataStandardFormat); List descriptions = RsM.ProcessData.GetDescriptions(jsonElements); Test[] tests = (from l in descriptions select (Test)l.Test).ToArray(); if (_IsEAFHosted && _FileConnectorConfiguration.FileScanningIntervalInSeconds > 0) - SendData(reportFullPath, dateTime, descriptions); + SendData(reportFullPath, dateTime, jsonElements, descriptions); results = new Tuple>(string.Join(Environment.NewLine, processDataStandardFormat.Logistics), tests, jsonElements, new List()); return results; } diff --git a/Adaptation/FileHandlers/OpenInsightMetrologyViewer/WSRequest.cs b/Adaptation/FileHandlers/OpenInsightMetrologyViewer/WSRequest.cs index a54c7d7..6396377 100644 --- a/Adaptation/FileHandlers/OpenInsightMetrologyViewer/WSRequest.cs +++ b/Adaptation/FileHandlers/OpenInsightMetrologyViewer/WSRequest.cs @@ -4,6 +4,7 @@ using Adaptation.Shared.Properties; using System; using System.Collections.Generic; using System.IO; +using System.Text.Json; namespace Adaptation.FileHandlers.OpenInsightMetrologyViewer; @@ -45,7 +46,7 @@ public class WSRequest [Obsolete("For json")] public WSRequest() { } #pragma warning disable IDE0060 - internal WSRequest(IFileRead fileRead, Logistics logistics, List descriptions, string processDataStandardFormat = null) + internal WSRequest(IFileRead fileRead, Logistics logistics, JsonElement[] jsonElements, List descriptions, string processDataStandardFormat = null) #pragma warning restore IDE0060 { Id = -1; @@ -61,12 +62,12 @@ public class WSRequest DLRatio = x.DLRatio; DataReject = x.DataReject; Date = x.Date; - Op = x.Employee; + Op = x.Employee; // different name Engineer = x.Engineer; - EquipId = logistics.MesEntity; + EquipId = logistics.MesEntity; // different name FileName = x.FileName; Layer = x.Layer; - LotId = x.Lot; + LotId = x.Lot; // different name PSN = x.PSN; RDS = x.RDS; Reactor = x.Reactor; @@ -74,7 +75,7 @@ public class WSRequest ResistivitySpec = x.ResistivitySpec; Run = x.Run; SemiRadial = x.SemiRadial; - StDev = x.StdDev; + StDev = x.StdDev; // different name Temp = x.Temp; UniqueId = x.UniqueId; Zone = x.Zone; @@ -105,14 +106,14 @@ public class WSRequest FilePath = onlyWSRequest; } - internal static long GetHeaderId(IFileRead fileRead, Logistics logistics, string openInsightMetrologyViewerAPI, string openInsightMetrologyViewerFileShare, int weekOfYear, WS.Results results, List descriptions) + internal static long GetHeaderId(IFileRead fileRead, Logistics logistics, string openInsightMetrologyViewerAPI, string openInsightMetrologyViewerFileShare, int weekOfYear, WS.Results results, JsonElement[] jsonElements, List descriptions) { long result; if (results is not null && results.HeaderId is not null) result = results.HeaderId.Value; else { - WSRequest wsRequest = new(fileRead, logistics, descriptions); + WSRequest wsRequest = new(fileRead, logistics, jsonElements, descriptions); string directory = Path.Combine(openInsightMetrologyViewerFileShare, logistics.DateTimeFromSequence.Year.ToString(), $"WW{weekOfYear:00}"); (_, WS.Results wsResults) = WS.SendData(openInsightMetrologyViewerAPI, logistics.Sequence, directory, wsRequest); if (wsResults.Success is null || !wsResults.Success.Value) diff --git a/Adaptation/FileHandlers/OpenInsightMetrologyViewerAttachments/FileRead.cs b/Adaptation/FileHandlers/OpenInsightMetrologyViewerAttachments/FileRead.cs index 71562d1..699a2cd 100644 --- a/Adaptation/FileHandlers/OpenInsightMetrologyViewerAttachments/FileRead.cs +++ b/Adaptation/FileHandlers/OpenInsightMetrologyViewerAttachments/FileRead.cs @@ -135,7 +135,7 @@ public class FileRead : Shared.FileRead, IFileRead return result; } - private void PostOpenInsightMetrologyViewerAttachments(List descriptions) + private void PostOpenInsightMetrologyViewerAttachments(JsonElement[] jsonElements, List descriptions) { Shared.Metrology.WS.Results? results; string jobIdDirectory = Path.Combine(Path.GetDirectoryName(_FileConnectorConfiguration.AlternateTargetFolder) ?? throw new Exception(), _Logistics.JobID); @@ -151,7 +151,7 @@ public class FileRead : Shared.FileRead, IFileRead results = wsResults[0]; } int weekOfYear = _Calendar.GetWeekOfYear(_Logistics.DateTimeFromSequence, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); - long headerId = !_IsEAFHosted ? -1 : OpenInsightMetrologyViewer.WSRequest.GetHeaderId(this, _Logistics, _OpenInsightMetrologyViewerAPI, _OpenInsightMetrologyViewerFileShare, weekOfYear, results, descriptions); + long headerId = !_IsEAFHosted ? -1 : OpenInsightMetrologyViewer.WSRequest.GetHeaderId(this, _Logistics, _OpenInsightMetrologyViewerAPI, _OpenInsightMetrologyViewerFileShare, weekOfYear, results, jsonElements, descriptions); string? headerIdDirectory = GetHeaderIdDirectory(headerId); if (string.IsNullOrEmpty(headerIdDirectory)) throw new Exception($"Didn't find header id directory <{headerId}>"); @@ -163,14 +163,15 @@ public class FileRead : Shared.FileRead, IFileRead if (dateTime == DateTime.MinValue) throw new ArgumentNullException(nameof(dateTime)); Tuple> results; - ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath); + string[] lines = File.ReadAllLines(reportFullPath); + ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath, lines); _Logistics = new Logistics(reportFullPath, processDataStandardFormat); SetFileParameterLotIDToLogisticsMID(); - JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(processDataStandardFormat); + JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(reportFullPath, lines, processDataStandardFormat); List descriptions = RsM.ProcessData.GetDescriptions(jsonElements); Test[] tests = (from l in descriptions select (Test)l.Test).ToArray(); if (_IsEAFHosted && _FileConnectorConfiguration.FileScanningIntervalInSeconds > 0) - PostOpenInsightMetrologyViewerAttachments(descriptions); + PostOpenInsightMetrologyViewerAttachments(jsonElements, descriptions); results = new Tuple>(string.Join(Environment.NewLine, processDataStandardFormat.Logistics), tests, jsonElements, new List()); return results; } diff --git a/Adaptation/FileHandlers/Processed/FileRead.cs b/Adaptation/FileHandlers/Processed/FileRead.cs index 168e6a7..5b68fcc 100644 --- a/Adaptation/FileHandlers/Processed/FileRead.cs +++ b/Adaptation/FileHandlers/Processed/FileRead.cs @@ -108,7 +108,7 @@ public class FileRead : Shared.FileRead, IFileRead return results; } - private void DirectoryMove(string reportFullPath, DateTime dateTime, List descriptions) + private void DirectoryMove(string reportFullPath, DateTime dateTime, JsonElement[] jsonElements, List descriptions) { if (dateTime == DateTime.MinValue) throw new ArgumentNullException(nameof(dateTime)); @@ -122,7 +122,7 @@ public class FileRead : Shared.FileRead, IFileRead throw new Exception("Didn't find directory by logistics sequence"); if (fileInfo.Exists && fileInfo.LastWriteTime < fileInfo.CreationTime) File.SetLastWriteTime(reportFullPath, fileInfo.CreationTime); - OpenInsightMetrologyViewer.WSRequest wsRequest = new(this, _Logistics, descriptions); + OpenInsightMetrologyViewer.WSRequest wsRequest = new(this, _Logistics, jsonElements, descriptions); JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true }; string json = JsonSerializer.Serialize(wsRequest, wsRequest.GetType(), jsonSerializerOptions); string directoryName = $"{Path.GetFileName(matchDirectories[0]).Split(new string[] { logisticsSequence }, StringSplitOptions.None)[0]}{_Logistics.DateTimeFromSequence:yyyy-MM-dd_hh;mm_tt_}{DateTime.Now.Ticks - _Logistics.Sequence}"; @@ -166,23 +166,24 @@ public class FileRead : Shared.FileRead, IFileRead private Tuple> GetExtractResult(string reportFullPath, DateTime dateTime) { Tuple> results; - ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath); + string[] lines = File.ReadAllLines(reportFullPath); + ProcessDataStandardFormat processDataStandardFormat = ProcessDataStandardFormat.GetProcessDataStandardFormat(reportFullPath, lines); _Logistics = new Logistics(reportFullPath, processDataStandardFormat); SetFileParameterLotIDToLogisticsMID(); - JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(processDataStandardFormat); + JsonElement[] jsonElements = ProcessDataStandardFormat.GetArray(reportFullPath, lines, processDataStandardFormat); List descriptions = RsM.ProcessData.GetDescriptions(jsonElements); Test[] tests = (from l in descriptions select (Test)l.Test).ToArray(); results = new Tuple>(string.Join(Environment.NewLine, processDataStandardFormat.Logistics), tests, jsonElements, new List()); if (_IsEAFHosted && _FileConnectorConfiguration.FileScanningIntervalInSeconds > 0) - DirectoryMove(reportFullPath, dateTime, descriptions); + DirectoryMove(reportFullPath, dateTime, jsonElements, descriptions); else if (!_IsEAFHosted) { - OpenInsightMetrologyViewer.WSRequest wsRequest = new(this, _Logistics, descriptions); + OpenInsightMetrologyViewer.WSRequest wsRequest = new(this, _Logistics, jsonElements, descriptions); JsonSerializerOptions jsonSerializerOptions = new() { WriteIndented = true }; - string json = JsonSerializer.Serialize(wsRequest, wsRequest.GetType(), jsonSerializerOptions); + string check = JsonSerializer.Serialize(wsRequest, wsRequest.GetType(), jsonSerializerOptions); string jsonFileName = Path.ChangeExtension(reportFullPath, ".json"); string historicalText = File.ReadAllText(jsonFileName); - if (json != historicalText) + if (check != historicalText) throw new Exception("File doesn't match historical!"); } return results; diff --git a/Adaptation/FileHandlers/RsM/Description.cs b/Adaptation/FileHandlers/RsM/Description.cs index ddcf5c3..e789b55 100644 --- a/Adaptation/FileHandlers/RsM/Description.cs +++ b/Adaptation/FileHandlers/RsM/Description.cs @@ -4,59 +4,60 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; +using System.Text.Json.Serialization; namespace Adaptation.FileHandlers.RsM; public class Description : IDescription, Shared.Properties.IDescription { - public int Test { get; set; } - public int Count { get; set; } - public int Index { get; set; } + [JsonPropertyName("EventId")] public int Test { get; set; } + [JsonPropertyName("Count")] public int Count { get; set; } + [JsonPropertyName("Index")] public int Index { get; set; } // public string EventName { get; set; } public string NullData { get; set; } public string JobID { get; set; } public string Sequence { get; set; } - public string MesEntity { get; set; } + [JsonPropertyName("MesEntity")] public string MesEntity { get; set; } public string ReportFullPath { get; set; } public string ProcessJobID { get; set; } public string MID { get; set; } // - public string Date { get; set; } - public string Employee { get; set; } - public string Lot { get; set; } - public string PSN { get; set; } - public string Reactor { get; set; } - public string Recipe { get; set; } + [JsonPropertyName("Date")] public string Date { get; set; } + [JsonPropertyName("Operator")] public string Employee { get; set; } + [JsonPropertyName("Lot")] public string Lot { get; set; } + [JsonPropertyName("PSN")] public string PSN { get; set; } + [JsonPropertyName("Reactor")] public string Reactor { get; set; } + [JsonPropertyName("RecipeName")] public string Recipe { get; set; } // - public string AutoOptimizeGain { get; set; } - public string AutoProbeHeightSet { get; set; } - public string Avg { get; set; } - public string DataReject { get; set; } - public string DLRatio { get; set; } - public string Merit { get; set; } - public string Pt { get; set; } - public string R { get; set; } - public string ResistivitySpec { get; set; } - public string Rs { get; set; } - public string SemiRadial { get; set; } - public string StdDev { get; set; } - public string T { get; set; } - public string Temp { get; set; } + [JsonPropertyName("AutoOptimizeGain")] public string AutoOptimizeGain { get; set; } + [JsonPropertyName("AutoProbeHeightSet")] public string AutoProbeHeightSet { get; set; } + [JsonPropertyName("Avg")] public string Avg { get; set; } + [JsonPropertyName("DataRejectSigma")] public string DataReject { get; set; } + [JsonPropertyName("DLRatio")] public string DLRatio { get; set; } + [JsonPropertyName("MeritGOF")] public string Merit { get; set; } + [JsonPropertyName("InferredPoint")] public string Pt { get; set; } + [JsonPropertyName("R")] public string R { get; set; } + [JsonPropertyName("ResistivitySpec")] public string ResistivitySpec { get; set; } + [JsonPropertyName("Rs")] public string Rs { get; set; } + [JsonPropertyName("SemiRadial")] public string SemiRadial { get; set; } + [JsonPropertyName("StandardDeviationPercentage")] public string StdDev { get; set; } + [JsonPropertyName("Th")] public string T { get; set; } + [JsonPropertyName("Temp")] public string Temp { get; set; } // - public string Engineer { get; set; } - public string EquipId { get; set; } - public string FileName { get; set; } + [JsonPropertyName("Engineer")] public string Engineer { get; set; } + [JsonPropertyName("Equipment")] public string EquipId { get; set; } + [JsonPropertyName("TheFileName")] public string FileName { get; set; } public string HeaderUniqueId { get; set; } public string Id { get; set; } - public string Layer { get; set; } - public string RDS { get; set; } - public string Run { get; set; } + [JsonPropertyName("Layer")] public string Layer { get; set; } + [JsonPropertyName("RDS")] public string RDS { get; set; } + [JsonPropertyName("Title")] public string Run { get; set; } public string UniqueId { get; set; } - public string Zone { get; set; } + [JsonPropertyName("Zone")] public string Zone { get; set; } // - public string SheetRhoVariation { get; set; } + [JsonPropertyName("SheetRhoVariation")] public string SheetRhoVariation { get; set; } string IDescription.GetEventDescription() => "File Has been read and parsed"; @@ -314,4 +315,16 @@ public class Description : IDescription, Shared.Properties.IDescription internal static string GetDateFormat() => "MM/dd/yyyy hh:mm:ss tt"; +} + +[JsonSourceGenerationOptions(WriteIndented = true, NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)] +[JsonSerializable(typeof(Description))] +internal partial class DescriptionSourceGenerationContext : JsonSerializerContext +{ +} + +[JsonSourceGenerationOptions(WriteIndented = true, NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)] +[JsonSerializable(typeof(Description[]))] +internal partial class DescriptionArraySourceGenerationContext : JsonSerializerContext +{ } \ No newline at end of file diff --git a/Adaptation/FileHandlers/RsM/Detail.cs b/Adaptation/FileHandlers/RsM/Detail.cs index 3eed99d..7ff5790 100644 --- a/Adaptation/FileHandlers/RsM/Detail.cs +++ b/Adaptation/FileHandlers/RsM/Detail.cs @@ -1,16 +1,30 @@ -namespace Adaptation.FileHandlers.RsM; +using System.Text.Json.Serialization; + +namespace Adaptation.FileHandlers.RsM; public class Detail { public string HeaderUniqueId { get; set; } - public string Merit { get; set; } - public string Pt { get; set; } - public string R { get; set; } - public string Rs { get; set; } - public string T { get; set; } + [JsonPropertyName("MeritGOF")] public string Merit { get; set; } + [JsonPropertyName("InferredPoint")] public string Pt { get; set; } + [JsonPropertyName("R")] public string R { get; set; } + [JsonPropertyName("Rs")] public string Rs { get; set; } + [JsonPropertyName("Th")] public string T { get; set; } public string UniqueId { get; set; } public override string ToString() => string.Concat(Merit, ";", Pt, ";", R, ";", Rs, ";", T); +} + +[JsonSourceGenerationOptions(WriteIndented = true, NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)] +[JsonSerializable(typeof(Detail))] +internal partial class DetailSourceGenerationContext : JsonSerializerContext +{ +} + +[JsonSourceGenerationOptions(WriteIndented = true, NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)] +[JsonSerializable(typeof(Detail[]))] +internal partial class DetailArraySourceGenerationContext : JsonSerializerContext +{ } \ No newline at end of file diff --git a/Adaptation/FileHandlers/RsM/ProcessData.cs b/Adaptation/FileHandlers/RsM/ProcessData.cs index 1eb5c03..41a416a 100644 --- a/Adaptation/FileHandlers/RsM/ProcessData.cs +++ b/Adaptation/FileHandlers/RsM/ProcessData.cs @@ -395,12 +395,11 @@ public class ProcessData : IProcessData { List 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(jsonElement.ToString(), jsonSerializerOptions); + description = JsonSerializer.Deserialize(jsonElement.ToString(), DescriptionSourceGenerationContext.Default.Description); if (description is null) continue; results.Add(description); diff --git a/Adaptation/Shared/Duplicator/Description.cs b/Adaptation/Shared/Duplicator/Description.cs index 964612e..d9bb3b8 100644 --- a/Adaptation/Shared/Duplicator/Description.cs +++ b/Adaptation/Shared/Duplicator/Description.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; +using System.Text.Json.Serialization; namespace Adaptation.Shared.Duplicator; @@ -178,4 +179,16 @@ public class Description : IDescription, Properties.IDescription internal static string GetDateFormat() => "MM/dd/yyyy hh:mm:ss tt"; +} + +[JsonSourceGenerationOptions(WriteIndented = true, NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)] +[JsonSerializable(typeof(Description))] +internal partial class SharedDescriptionSourceGenerationContext : JsonSerializerContext +{ +} + +[JsonSourceGenerationOptions(WriteIndented = true, NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)] +[JsonSerializable(typeof(Description[]))] +internal partial class SharedDescriptionArraySourceGenerationContext : JsonSerializerContext +{ } \ No newline at end of file diff --git a/Adaptation/Shared/FileRead.cs b/Adaptation/Shared/FileRead.cs index 48aacab..773eeae 100644 --- a/Adaptation/Shared/FileRead.cs +++ b/Adaptation/Shared/FileRead.cs @@ -447,12 +447,13 @@ public class FileRead : Properties.IFileRead { List results = new(); Duplicator.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(jsonElement.ToString(), jsonSerializerOptions); + description = JsonSerializer.Deserialize(jsonElement.ToString(), Duplicator.SharedDescriptionSourceGenerationContext.Default.Description); + if (description is null) + continue; results.Add(description); } return results; diff --git a/Adaptation/Shared/Metrology/WS.cs b/Adaptation/Shared/Metrology/WS.cs index b7666db..8f9f5c4 100644 --- a/Adaptation/Shared/Metrology/WS.cs +++ b/Adaptation/Shared/Metrology/WS.cs @@ -48,6 +48,41 @@ public partial class WS return new(resultsJson, wsResults); } + public static (string, Results) SendData(string url, long sequence, string directory, string json, int timeoutSeconds = 120) + { + Results? wsResults = null; + string resultsJson = string.Empty; + try + { + if (string.IsNullOrEmpty(url) || !url.Contains(":") || !url.Contains(".")) + throw new Exception("Invalid URL"); + using (HttpClient httpClient = new()) + { + httpClient.Timeout = new TimeSpan(0, 0, 0, timeoutSeconds, 0); + HttpRequestMessage httpRequestMessage = new() + { + RequestUri = new Uri(url), + Method = HttpMethod.Post, + Content = new StringContent(json, Encoding.UTF8, "application/json") + }; + HttpResponseMessage httpResponseMessage = httpClient.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead).Result; + resultsJson = httpResponseMessage.Content.ReadAsStringAsync().Result; + wsResults = JsonSerializer.Deserialize(resultsJson, ResultsSourceGenerationContext.Default.Results); + if (wsResults is null) + throw new NullReferenceException(nameof(wsResults)); + string checkDirectory = Path.Combine(directory, $"-{wsResults.HeaderId}"); + if (!Directory.Exists(checkDirectory)) + _ = Directory.CreateDirectory(checkDirectory); + File.WriteAllText(Path.Combine(checkDirectory, $"{sequence}.json"), json); + } + if (wsResults.Success is null || !wsResults.Success.Value) + wsResults.Errors?.Add(wsResults.ToString()); + } + catch (Exception e) + { wsResults ??= Results.Get(resultsJson, e); } + return new(resultsJson, wsResults); + } + public static void AttachFile(string url, Attachment attachment, int timeoutSeconds = 60) { using HttpClient httpClient = new(); diff --git a/Adaptation/Shared/ProcessDataStandardFormat.cs b/Adaptation/Shared/ProcessDataStandardFormat.cs index 1fb9e2a..9d48a31 100644 --- a/Adaptation/Shared/ProcessDataStandardFormat.cs +++ b/Adaptation/Shared/ProcessDataStandardFormat.cs @@ -654,6 +654,17 @@ internal class ProcessDataStandardFormat return results; } + internal static JsonElement[] GetArray(string reportFullPath, string[] lines, ProcessDataStandardFormat processDataStandardFormat) + { + JsonElement[] results; + string? json = GetRecordsJson(reportFullPath, lines); + if (string.IsNullOrEmpty(json)) + results = GetArray(processDataStandardFormat); + else + results = JsonSerializer.Deserialize(json, JsonElementCollectionSourceGenerationContext.Default.JsonElementArray) ?? throw new Exception(); + return results; + } + internal static string GetPDSFText(IFileRead fileRead, Logistics logistics, JsonElement[] jsonElements, string logisticsText) { string result; @@ -903,7 +914,7 @@ internal class ProcessDataStandardFormat } foreach (KeyValuePair> keyValuePair in results) { - if (body.Count < 3) + if (body.Count < 2) break; if (keyValuePair.Value.Count != body.Count) continue; @@ -956,6 +967,26 @@ internal class ProcessDataStandardFormat return result; } + private static string? GetRecordsJson(string reportFullPath, string[] lines) + { + string? result; + bool foundRecords = false; + List results = new(); + lines ??= File.ReadAllLines(reportFullPath); + foreach (string line in lines) + { + if (line.StartsWith("\"Records\"")) + foundRecords = true; + if (!foundRecords) + continue; + if (line == "],") + break; + results.Add(line); + } + result = results.Count == 0 ? null : $"{string.Join(Environment.NewLine, results.Skip(1))}{Environment.NewLine}]"; + return result; + } + } [JsonSourceGenerationOptions(WriteIndented = true)]