Complete Class
This commit is contained in:
122
Adaptation/FileHandlers/pcl/SummarySegment.cs
Normal file
122
Adaptation/FileHandlers/pcl/SummarySegment.cs
Normal file
@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Adaptation.FileHandlers.pcl;
|
||||
|
||||
#nullable enable
|
||||
|
||||
internal class SummarySegment
|
||||
// internal record SummarySegment([property: JsonPropertyName("Navg")] string NAvg,
|
||||
// [property: JsonPropertyName("Nsl")] string Nsl,
|
||||
// [property: JsonPropertyName("Vd")] string Vd,
|
||||
// [property: JsonPropertyName("@ Flat Z")] string FlatZ,
|
||||
// [property: JsonPropertyName("Rhoavg")] string RhoAvg,
|
||||
// [property: JsonPropertyName("Rhosl")] string Rhosl,
|
||||
// [property: JsonPropertyName("Phase")] string Phase,
|
||||
// [property: JsonPropertyName("Grade")] string Grade,
|
||||
// [property: JsonPropertyName("@ Rs")] string Rs)
|
||||
{
|
||||
|
||||
public SummarySegment(string nAvg, string nsl, string vd, string flatZ, string rhoAvg, string rhosl, string phase, string grade, string rs)
|
||||
{
|
||||
NAvg = nAvg;
|
||||
Nsl = nsl;
|
||||
Vd = vd;
|
||||
FlatZ = flatZ;
|
||||
RhoAvg = rhoAvg;
|
||||
Rhosl = rhosl;
|
||||
Phase = phase;
|
||||
Grade = grade;
|
||||
Rs = rs;
|
||||
}
|
||||
|
||||
[JsonPropertyName("Navg")] public string NAvg { get; }
|
||||
[JsonPropertyName("Nsl")] public string Nsl { get; }
|
||||
[JsonPropertyName("Vd")] public string Vd { get; }
|
||||
[JsonPropertyName("@ Flat Z")] public string FlatZ { get; }
|
||||
[JsonPropertyName("Rhoavg")] public string RhoAvg { get; }
|
||||
[JsonPropertyName("Rhosl")] public string Rhosl { get; }
|
||||
[JsonPropertyName("Phase")] public string Phase { get; }
|
||||
[JsonPropertyName("Grade")] public string Grade { get; }
|
||||
[JsonPropertyName("@ Rs")] public string Rs { get; }
|
||||
|
||||
public static SummarySegment Get() =>
|
||||
new(string.Empty,
|
||||
string.Empty,
|
||||
string.Empty,
|
||||
string.Empty,
|
||||
string.Empty,
|
||||
string.Empty,
|
||||
string.Empty,
|
||||
string.Empty,
|
||||
string.Empty);
|
||||
|
||||
private static ReadOnlyCollection<JsonProperty> GetJsonProperties()
|
||||
{
|
||||
JsonProperty[] results;
|
||||
string json;
|
||||
SummarySegment summarySegment = Get();
|
||||
json = JsonSerializer.Serialize(summarySegment);
|
||||
JsonElement jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
|
||||
results = jsonElement.EnumerateObject().ToArray();
|
||||
return new(results);
|
||||
}
|
||||
|
||||
public static Summary? Get(string[] lines, string site, string summaryLine)
|
||||
{
|
||||
Summary? result;
|
||||
string json;
|
||||
string[] segments;
|
||||
bool found = false;
|
||||
string[] segmentsB;
|
||||
Dictionary<string, string> keyValuePairs = new();
|
||||
Dictionary<string, string> keyValuePairsB = new();
|
||||
Dictionary<string, string> keyValuePairsC = new();
|
||||
ReadOnlyCollection<JsonProperty> jsonProperties = GetJsonProperties();
|
||||
foreach (string line in lines)
|
||||
{
|
||||
if (line == summaryLine)
|
||||
found = true;
|
||||
if (!found)
|
||||
continue;
|
||||
if (line.Contains(site))
|
||||
break;
|
||||
foreach (JsonProperty jsonProperty in jsonProperties)
|
||||
{
|
||||
segments = line.Split(new string[] { $"{jsonProperty.Name}:", $"{jsonProperty.Name} :" }, StringSplitOptions.None);
|
||||
if (segments.Length < 2 || !line.StartsWith(jsonProperty.Name))
|
||||
continue;
|
||||
segmentsB = segments[1].Trim().Split(' ');
|
||||
if (segmentsB.Length < 3)
|
||||
continue;
|
||||
keyValuePairs.Add(jsonProperty.Name, segmentsB[0]);
|
||||
keyValuePairsB.Add(jsonProperty.Name, segmentsB[1]);
|
||||
keyValuePairsC.Add(jsonProperty.Name, segmentsB[2]);
|
||||
}
|
||||
}
|
||||
if (keyValuePairs.Count != jsonProperties.Count || keyValuePairsB.Count != jsonProperties.Count || keyValuePairsC.Count != jsonProperties.Count)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
json = JsonSerializer.Serialize(keyValuePairs);
|
||||
SummarySegment? mean = JsonSerializer.Deserialize(json, SummarySegmentSourceGenerationContext.Default.SummarySegment);
|
||||
json = JsonSerializer.Serialize(keyValuePairsB);
|
||||
SummarySegment? standardDeviationPercentage = JsonSerializer.Deserialize(json, SummarySegmentSourceGenerationContext.Default.SummarySegment);
|
||||
json = JsonSerializer.Serialize(keyValuePairsC);
|
||||
SummarySegment? radialGradient = JsonSerializer.Deserialize(json, SummarySegmentSourceGenerationContext.Default.SummarySegment);
|
||||
result = new(mean, standardDeviationPercentage, radialGradient);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(SummarySegment))]
|
||||
internal partial class SummarySegmentSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
Reference in New Issue
Block a user