Update GetEngineeringSpcReview to work without FS access.

IqsFileSegments and WaferCounterTwoFileSecondsWait
This commit is contained in:
2024-03-19 15:18:15 -07:00
parent 811f45a7df
commit 85fe0a7823
6 changed files with 120 additions and 95 deletions

View File

@ -677,12 +677,11 @@ public class InfinityQSV4Repository : IInfinityQSV4Repository
return result;
}
private static ReadOnlyCollection<Record> GetRecords(AppSettings appSettings, string file)
private static ReadOnlyCollection<Record> GetRecords(AppSettings appSettings, string[] lines)
{
Record? record;
List<Record> results = new();
List<string> checkColumns = new();
string[] lines = File.ReadAllLines(file);
foreach (string line in lines)
{
if (line.StartsWith("<th"))
@ -747,10 +746,23 @@ public class InfinityQSV4Repository : IInfinityQSV4Repository
return results;
}
private void FileFindReplaceAndSave(string iqsFile, FileInfo fileInfo)
private void FileFindReplaceAndSave(FileInfo fileInfo, string value)
{
string lines = File.ReadAllText(iqsFile).Replace(_AppSettings.IqsRed, "red").Replace(_AppSettings.IqsYellow, "Yellow");
_FileShareRepository.FileWrite(fileInfo.FullName, lines);
string lines = value.Replace(_AppSettings.IqsRed, "red").Replace(_AppSettings.IqsYellow, "Yellow");
File.WriteAllText(fileInfo.FullName, lines);
}
private static string GetWwwRootDirectory()
{
string result;
Assembly assembly = Assembly.GetExecutingAssembly();
string? assemblyName = assembly.GetName()?.Name;
if (string.IsNullOrEmpty(assemblyName))
throw new Exception();
result = Path.Combine(AppContext.BaseDirectory, "wwwroot");
if (!Directory.Exists(result))
_ = Directory.CreateDirectory(result);
return result;
}
Dictionary<string, List<string>> IInfinityQSV4Repository.GetEngineeringSpcReview()
@ -765,19 +777,27 @@ public class InfinityQSV4Repository : IInfinityQSV4Repository
}
else
{
FileInfo iqsFileInfo = new(_AppSettings.IqsFile);
Assembly assembly = Assembly.GetExecutingAssembly();
string? assemblyName = assembly.GetName()?.Name;
if (string.IsNullOrEmpty(assemblyName))
throw new Exception();
FileInfo localFileInfo = new(Path.Combine(AppContext.BaseDirectory, "wwwroot", iqsFileInfo.Name));
if (!localFileInfo.Exists && (!iqsFileInfo.Exists || iqsFileInfo.Length == 0))
string wwwRootDirectory = GetWwwRootDirectory();
HttpClient httpClient = _HttpClientFactory.CreateClient();
FileInfo localFileInfo = new(Path.Combine(wwwRootDirectory, _AppSettings.IqsFileSegments.Last()));
Uri uri = _FileShareRepository.Append(new Uri(_AppSettings.EcMesaFileShareCharacterizationSi), _AppSettings.IqsFileSegments[0..^1]);
List<NginxFileSystemSortable> nginxFileSystemSortableCollection = _FileShareRepository.GetNginxFileSystemSortableCollection(httpClient, uri, ".html");
if (!localFileInfo.Exists && nginxFileSystemSortableCollection.Count == 0)
results = new();
else
{
if (!localFileInfo.Exists || localFileInfo.LastWriteTime != iqsFileInfo.LastWriteTime)
FileFindReplaceAndSave(iqsFileInfo.FullName, localFileInfo);
ReadOnlyCollection<Record> records = !iqsFileInfo.Exists || iqsFileInfo.Length == 0 ? GetRecords(_AppSettings, localFileInfo.FullName) : GetRecords(_AppSettings, iqsFileInfo.FullName);
string[] lines;
HttpResponseMessage httpResponseMessage = _FileShareRepository.ReadFile(httpClient, nginxFileSystemSortableCollection[0].Uri);
Task<string> value = httpResponseMessage.Content.ReadAsStringAsync();
value.Wait();
if (localFileInfo.Exists && localFileInfo.LastWriteTime == nginxFileSystemSortableCollection[0].DateTime)
lines = File.ReadAllLines(localFileInfo.FullName);
else
{
FileFindReplaceAndSave(localFileInfo, value.Result);
lines = value.Result.Split("\r\n");
}
ReadOnlyCollection<Record> records = GetRecords(_AppSettings, lines);
List<Dictionary<string, Record>> collection = GetCollection(_AppSettings.IqsColumns, records);
results = GetResults(_AppSettings, collection);
}

View File

@ -4,7 +4,6 @@ using OI.Metrology.Shared.Models;
using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Shared.Repositories;
using System.Globalization;
using System.Text.Json;
namespace OI.Metrology.Server.Repository;
@ -137,94 +136,75 @@ public class WaferCounterRepository : IWaferCounterRepository
private List<NginxFileSystemSortable> GetNginxFileSystemSortableCollection(HttpClient httpClient, Uri waferSizeUri, string waferSizeDirectory)
{
List<NginxFileSystemSortable> results = _FileShareRepository.GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, ".wc");
List<NginxFileSystemSortable> results = new();
DateTime dateTime = DateTime.Now;
long ticks = dateTime.AddSeconds(_AppSettings.WaferCounterTwoFileSecondsWait).Ticks;
for (int i = 0; i < int.MaxValue; i++)
{
results = _FileShareRepository.GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, ".wc");
if (results.Count > 1 || DateTime.Now.Ticks > ticks)
break;
Thread.Sleep(250);
}
for (int i = 1; i < results.Count; i++)
MoveFile(waferSizeDirectory, results[i]);
return results;
}
private static WaferCounter? GetLastQuantityAndSlotMapWithText(string waferSize, string text, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable)
private static WaferCounter GetLastQuantityAndSlotMapWithText(string waferSize, string text, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable)
{
WaferCounter? result;
WaferCounter result;
Task<string> value = httpClient.GetStringAsync(nginxFileSystemSortable.Uri);
value.Wait();
string[] lines = value.Result.Split(Environment.NewLine);
if (lines.Length < 2)
result = null;
else
{
string[] segments = nginxFileSystemSortable.Name.Split('-');
Record record = GetRecord(lines[0], lines[1]);
string equipmentId = segments.Length < 2 ? nginxFileSystemSortable.Name : segments[1].Split('.')[0];
if (string.IsNullOrEmpty(record.SlotMap) || record.SlotMap.Length != 25)
result = null; // Wrong length!
else if (record.Total != record.Check)
result = null; // Invalid!
else
result = new(nginxFileSystemSortable.DateTime, nginxFileSystemSortable.DateTime.ToString("yyyy-MM-dd hh:mm tt"), $"WC{waferSize}{equipmentId}", text, record.Total, record.SlotMap);
}
string[] lines = value.Result.Split("\r\n");
if (lines.Length <= 1)
throw new Exception("Incomplete file length!");
string[] segments = nginxFileSystemSortable.Name.Split('-');
Record record = GetRecord(lines[0], lines[1]);
string equipmentId = segments.Length <= 1 ? nginxFileSystemSortable.Name : segments[1].Split('.')[0];
if (string.IsNullOrEmpty(record.SlotMap) || record.SlotMap.Length != 25)
throw new Exception("Wrong length!");
if (record.Total != record.Check)
throw new Exception("Invalid!");
result = new(nginxFileSystemSortable.DateTime, nginxFileSystemSortable.DateTime.ToString("yyyy-MM-dd hh:mm tt"), $"WC{waferSize}{equipmentId}", text, record.Total, record.SlotMap);
return result;
}
WaferCounter? IWaferCounterRepository.GetLastQuantityAndSlotMap(string area, string waferSize)
WaferCounter IWaferCounterRepository.GetLastQuantityAndSlotMap(string area, string waferSize)
{
WaferCounter? result;
if (!string.IsNullOrEmpty(_MockRoot))
{
string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IWaferCounterRepository.GetLastQuantityAndSlotMap)}.json"));
result = JsonSerializer.Deserialize<WaferCounter>(json);
if (result is null)
throw new NullReferenceException(nameof(result));
}
else
{
Uri waferSizeUri = GetWaferSizeUri(area, waferSize);
HttpClient httpClient = _HttpClientFactory.CreateClient();
string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false);
List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, waferSizeDirectory);
if (nginxFileSystemSortableCollection.Count == 0)
result = null;
else
{
string text = string.Empty;
result = GetLastQuantityAndSlotMapWithText(waferSize, text, httpClient, nginxFileSystemSortableCollection[0]);
}
}
WaferCounter result;
Uri waferSizeUri = GetWaferSizeUri(area, waferSize);
HttpClient httpClient = _HttpClientFactory.CreateClient();
string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false);
List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, waferSizeDirectory);
if (nginxFileSystemSortableCollection.Count < 1)
throw new Exception("No files!");
string text = string.Empty;
result = GetLastQuantityAndSlotMapWithText(waferSize, text, httpClient, nginxFileSystemSortableCollection[0]);
return result;
}
private void Save(string waferSizeDestinationDirectory, string area, string waferSize, string text, NginxFileSystemSortable nginxFileSystemSortable, WaferCounter result) =>
_FileShareRepository.FileWrite(Path.Combine(waferSizeDestinationDirectory, $"{nginxFileSystemSortable.Name}.csv"), $"100,{waferSize},{area},{nginxFileSystemSortable.DateTime},{text},{result.Total:00},{result.SlotMap} ");
WaferCounter? IWaferCounterRepository.GetLastQuantityAndSlotMapWithText(string area, string waferSize, string text)
WaferCounter IWaferCounterRepository.GetLastQuantityAndSlotMapWithText(string area, string waferSize, string text)
{
WaferCounter? result;
if (!string.IsNullOrEmpty(_MockRoot))
WaferCounter result;
Uri waferSizeUri = GetWaferSizeUri(area, waferSize);
HttpClient httpClient = _HttpClientFactory.CreateClient();
string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false);
List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, waferSizeDirectory);
if (nginxFileSystemSortableCollection.Count < 1)
throw new Exception("No files!");
result = GetLastQuantityAndSlotMapWithText(waferSize, text, httpClient, nginxFileSystemSortableCollection[0]);
if (result is not null)
{
string json = File.ReadAllText(Path.Combine(string.Concat(AppContext.BaseDirectory, _MockRoot), $"{_RepositoryName}-{nameof(IWaferCounterRepository.GetLastQuantityAndSlotMapWithText)}.json"));
result = JsonSerializer.Deserialize<WaferCounter>(json);
if (result is null)
throw new NullReferenceException(nameof(result));
}
else
{
Uri waferSizeUri = GetWaferSizeUri(area, waferSize);
HttpClient httpClient = _HttpClientFactory.CreateClient();
string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false);
List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri, waferSizeDirectory);
if (nginxFileSystemSortableCollection.Count == 0)
result = null;
else
{
result = GetLastQuantityAndSlotMapWithText(waferSize, text, httpClient, nginxFileSystemSortableCollection[0]);
if (result is not null)
{
string waferSizeDestinationDirectory = _AppSettings.WaferCounterDestinationDirectory;
// string waferSizeDestinationDirectory = GetWaferSizeUri(area, waferSize, destination: true);
Save(waferSizeDestinationDirectory, area, waferSize, text, nginxFileSystemSortableCollection[0], result);
}
}
string waferSizeDestinationDirectory = _AppSettings.WaferCounterDestinationDirectory;
// string waferSizeDestinationDirectory = GetWaferSizeUri(area, waferSize, destination: true);
Save(waferSizeDestinationDirectory, area, waferSize, text, nginxFileSystemSortableCollection[0], result);
}
if (result is null)
throw new NullReferenceException(nameof(result));
return result;
}