148 lines
5.5 KiB
C#

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.pcl;
#nullable enable
internal class Run
{
public Run(Header header, Summary summary, ReadOnlyCollection<Point> points)
{
Header = header;
Summary = summary;
Points = points;
}
public Header Header { get; }
public Summary Summary { get; }
public ReadOnlyCollection<Point> Points { get; }
private static ReadOnlyCollection<string> FilterLines(ReadOnlyCollection<string> collection)
{
List<string> results = new();
foreach (string line in collection)
{
if (string.IsNullOrEmpty(line) || line is "*" or "@")
continue;
if (line.Length < 3 || line[0] is not '*' and not '@' || line[1] != ' ')
results.Add(line);
else
results.Add(line.Substring(2));
}
return new(results);
}
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(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.Points.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));
}
public static Run? Get(Logistics logistics, List<FileInfo> fileInfoCollection, ReadOnlyCollection<string> collection)
{
Run? result;
Constant constant = new();
ReadOnlyCollection<string> lines = FilterLines(collection);
if (collection.Count <= constant.Take)
result = null;
else
{
Header? header = Header.Get(constant, lines);
if (header is null)
result = null;
else
{
Summary? summary = SummarySegment.Get(constant, lines);
if (summary is null)
result = null;
else
{
ReadOnlyCollection<Point> points = Point.GetCollection(constant, lines) ?? throw new NullReferenceException(nameof(summary));
if (points.Count == 0)
result = null;
else
{
result = new(header, summary, points);
WriteJson(logistics, fileInfoCollection, result);
WriteCommaSeparatedValues(logistics, result);
}
}
}
}
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Run))]
internal partial class RunSourceGenerationContext : JsonSerializerContext
{
}