151 lines
5.7 KiB
C#
151 lines
5.7 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
|
|
namespace File_Folder_Helper.Day.Q42023;
|
|
|
|
internal static class Helper20231130
|
|
{
|
|
|
|
private record Record(string File, string FileName, string Equipment, string TimeStamp);
|
|
|
|
private static ReadOnlyDictionary<string, string> GetSystemStates()
|
|
{
|
|
Dictionary<string, string> results = [];
|
|
results.Add("1", "cold-idle");
|
|
results.Add("2", "running");
|
|
results.Add("3", "run-wafer");
|
|
results.Add("4", "warm-idle");
|
|
results.Add("5", "pause");
|
|
results.Add("6", "suspend");
|
|
results.Add("7", "startup");
|
|
results.Add("8", "shutdown");
|
|
results.Add("9", "abort");
|
|
results.Add("10", "safety-1");
|
|
results.Add("11", "safety-2");
|
|
results.Add("12", "safety-3");
|
|
return new(results);
|
|
}
|
|
|
|
private static ReadOnlyCollection<Record> GetRecords(string sourceDirectory, string timestampFormat)
|
|
{
|
|
List<Record> results = [];
|
|
Record record;
|
|
string fileName;
|
|
string equipment;
|
|
string timestamp;
|
|
string[] segments;
|
|
string[] files = Directory.GetFiles(sourceDirectory, "*.pdsf", SearchOption.TopDirectoryOnly).ToArray();
|
|
foreach (string file in files)
|
|
{
|
|
fileName = Path.GetFileName(file);
|
|
segments = fileName.Split('_');
|
|
if (segments.Length != 2)
|
|
continue;
|
|
equipment = segments[0];
|
|
timestamp = segments[1].Split('.')[0];
|
|
if (timestamp.Length != timestampFormat.Length)
|
|
continue;
|
|
record = new(file, fileName, equipment, timestamp);
|
|
results.Add(record);
|
|
}
|
|
return new(results.OrderBy(l => l.TimeStamp).ToArray());
|
|
}
|
|
|
|
private static int? GetKeyColumnIndex(string[] columns, string keyColumn)
|
|
{
|
|
int? result = null;
|
|
for (int i = 0; i < columns.Length; i++)
|
|
{
|
|
if (columns[i] != keyColumn)
|
|
continue;
|
|
result = i;
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static ReadOnlyCollection<string> GetSystemStateValues(List<string> lines, string[] columns, int keyColumnIndex, ReadOnlyDictionary<string, string> systemStates)
|
|
{
|
|
List<string> results = [];
|
|
string[] values;
|
|
string? systemState;
|
|
string keyColumnValue;
|
|
for (int i = 7; i < lines.Count; i++)
|
|
{
|
|
values = lines[i].Split('\t');
|
|
if (values.Length != columns.Length)
|
|
continue;
|
|
keyColumnValue = values[keyColumnIndex];
|
|
if (string.IsNullOrEmpty(keyColumnValue))
|
|
continue;
|
|
if (!systemStates.TryGetValue(keyColumnValue, out systemState))
|
|
continue;
|
|
if (results.Contains(systemState))
|
|
continue;
|
|
results.Add(systemState);
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
internal static void RenameReactorProcessDataStandardFormatFiles(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
string[] columns;
|
|
DateTime dateTime;
|
|
List<string> lines;
|
|
string systemState;
|
|
int? keyColumnIndex;
|
|
string checkFileName;
|
|
string keyColumn = args[3];
|
|
List<string> headerLines = [];
|
|
string sourceDirectory = args[0];
|
|
string timestampFormat = args[2];
|
|
if (!Directory.Exists(sourceDirectory))
|
|
throw new Exception(sourceDirectory);
|
|
ReadOnlyCollection<string> systemStateValues;
|
|
string missingKeyDirectory = Path.Combine(sourceDirectory, "Missing-Key");
|
|
if (!Directory.Exists(missingKeyDirectory))
|
|
_ = Directory.CreateDirectory(missingKeyDirectory);
|
|
ReadOnlyDictionary<string, string> systemStates = GetSystemStates();
|
|
ReadOnlyCollection<Record> records = GetRecords(sourceDirectory, timestampFormat);
|
|
foreach (Record record in records)
|
|
{
|
|
headerLines.Clear();
|
|
lines = File.ReadAllLines(record.File).ToList();
|
|
if (lines.Count < 8)
|
|
continue;
|
|
for (int i = 0; i < 6; i++)
|
|
{
|
|
headerLines.Add(lines[0]);
|
|
lines.RemoveAt(0);
|
|
}
|
|
if (lines[0].Length < 1 || lines[0][0] != '"' || !lines[0].StartsWith("\"Time\""))
|
|
continue;
|
|
keyColumnIndex = null;
|
|
columns = lines[0].Split('\t');
|
|
if (columns.Length < 3)
|
|
continue;
|
|
keyColumnIndex = GetKeyColumnIndex(columns, keyColumn);
|
|
if (keyColumnIndex is null)
|
|
{
|
|
File.Move(record.File, Path.Combine(sourceDirectory, missingKeyDirectory, record.FileName));
|
|
continue;
|
|
}
|
|
logger.LogInformation("{timestamp} triggered", record.TimeStamp);
|
|
systemStateValues = GetSystemStateValues(lines, columns, keyColumnIndex.Value, systemStates);
|
|
if (systemStateValues.Count == 0)
|
|
{
|
|
File.Move(record.File, Path.Combine(sourceDirectory, missingKeyDirectory, record.FileName));
|
|
continue;
|
|
}
|
|
lines.AddRange(headerLines);
|
|
systemState = string.Join('-', systemStateValues);
|
|
checkFileName = Path.Combine(Path.GetDirectoryName(record.File) ?? throw new Exception(), $"{record.Equipment}-{record.TimeStamp}-{systemState}.pdsf");
|
|
File.WriteAllLines(checkFileName, lines);
|
|
File.Delete(record.File);
|
|
if (DateTime.TryParseExact(record.TimeStamp, timestampFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
|
|
File.SetLastWriteTime(checkFileName, dateTime);
|
|
}
|
|
}
|
|
|
|
} |