Separated Wafer-Counter
JsonElement instead of Request body Attachment Class Bump Ready to test GetLastGroupIdWithValue Changed to v4
This commit is contained in:
40
Wafer-Counter/Repositories/AppSettingsRepository.cs
Normal file
40
Wafer-Counter/Repositories/AppSettingsRepository.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using OI.Metrology.Wafer.Counter.Models;
|
||||
using OI.Metrology.Shared.Models.Stateless;
|
||||
|
||||
namespace OI.Metrology.Wafer.Counter.Repository;
|
||||
|
||||
public class AppSettingsRepository : IAppSettingsRepository<Models.Binder.AppSettings>
|
||||
{
|
||||
|
||||
private readonly AppSettings _AppSettings;
|
||||
|
||||
public AppSettingsRepository(AppSettings appSettings) => _AppSettings = appSettings;
|
||||
|
||||
internal Models.Binder.AppSettings GetAppSettings()
|
||||
{
|
||||
Models.Binder.AppSettings result = new()
|
||||
{
|
||||
BuildNumber = _AppSettings.BuildNumber,
|
||||
Company = _AppSettings.Company,
|
||||
GitCommitSeven = _AppSettings.GitCommitSeven,
|
||||
IsDevelopment = _AppSettings.IsDevelopment,
|
||||
IsStaging = _AppSettings.IsStaging,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
Models.Binder.AppSettings IAppSettingsRepository<Models.Binder.AppSettings>.GetAppSettings() => GetAppSettings();
|
||||
|
||||
internal string GetBuildNumberAndGitCommitSeven()
|
||||
{
|
||||
string result = string.Concat(_AppSettings.BuildNumber, '-', _AppSettings.GitCommitSeven);
|
||||
return result;
|
||||
}
|
||||
|
||||
string IAppSettingsRepository<Models.Binder.AppSettings>.GetBuildNumberAndGitCommitSeven() =>
|
||||
GetBuildNumberAndGitCommitSeven();
|
||||
|
||||
void IAppSettingsRepository<Models.Binder.AppSettings>.VerifyConnectionStrings() =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
}
|
99
Wafer-Counter/Repositories/FileShareRepository.cs
Normal file
99
Wafer-Counter/Repositories/FileShareRepository.cs
Normal file
@ -0,0 +1,99 @@
|
||||
using OI.Metrology.Shared.Models;
|
||||
using OI.Metrology.Shared.Models.Stateless;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace OI.Metrology.Wafer.Counter.Repository;
|
||||
|
||||
public class FileShareRepository : IFileShareRepository
|
||||
{
|
||||
|
||||
Uri IFileShareRepository.Append(Uri uri, params string[] paths) =>
|
||||
new(paths.Aggregate(uri.AbsoluteUri, (current, path) =>
|
||||
string.Format("{0}/{1}", current.TrimEnd('/'), path.TrimStart('/'))));
|
||||
|
||||
private Uri GetEndPoint(HttpClient httpClient, string method)
|
||||
{
|
||||
Uri result;
|
||||
if (httpClient.BaseAddress is null)
|
||||
throw new NullReferenceException(nameof(httpClient.BaseAddress));
|
||||
IFileShareRepository fileShareRepository = this;
|
||||
result = fileShareRepository.Append(httpClient.BaseAddress, "api", "v1", "file-share", method);
|
||||
return result;
|
||||
}
|
||||
|
||||
void IFileShareRepository.CopyFile(string from, string to)
|
||||
{
|
||||
string directory = Path.GetDirectoryName(to) ?? throw new NullReferenceException();
|
||||
if (!Directory.Exists(directory))
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
File.Copy(from, to);
|
||||
}
|
||||
|
||||
void IFileShareRepository.MoveFile(string from, string to)
|
||||
{
|
||||
string directory = Path.GetDirectoryName(to) ?? throw new NullReferenceException();
|
||||
if (!Directory.Exists(directory))
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
if (File.Exists(to))
|
||||
File.Move(to, $"{to}.{DateTime.Now.Ticks}.old");
|
||||
File.Move(from, to);
|
||||
}
|
||||
|
||||
void IFileShareRepository.FileWrite(string path, string contents)
|
||||
{
|
||||
string directory = Path.GetDirectoryName(path) ?? throw new NullReferenceException();
|
||||
if (!Directory.Exists(directory))
|
||||
_ = Directory.CreateDirectory(directory);
|
||||
File.WriteAllText(path, contents);
|
||||
}
|
||||
|
||||
void IFileShareRepository.CopyFile(HttpClient httpClient, string from, string to)
|
||||
{
|
||||
Uri uri = GetEndPoint(httpClient, "copy-file");
|
||||
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync(uri);
|
||||
httpResponseMessage.Wait();
|
||||
if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
throw new Exception(httpResponseMessage.Result.StatusCode.ToString());
|
||||
}
|
||||
|
||||
void IFileShareRepository.MoveFile(HttpClient httpClient, string from, string to)
|
||||
{
|
||||
Uri uri = GetEndPoint(httpClient, "move-file");
|
||||
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync(uri);
|
||||
httpResponseMessage.Wait();
|
||||
if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK)
|
||||
throw new Exception(httpResponseMessage.Result.StatusCode.ToString());
|
||||
}
|
||||
|
||||
HttpResponseMessage IFileShareRepository.ReadFile(HttpClient httpClient, Uri uri)
|
||||
{
|
||||
HttpResponseMessage result;
|
||||
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync(uri);
|
||||
httpResponseMessage.Wait();
|
||||
result = httpResponseMessage.Result;
|
||||
return result;
|
||||
}
|
||||
|
||||
List<NginxFileSystemSortable> IFileShareRepository.GetNginxFileSystemSortableCollection(HttpClient httpClient, Uri uri, string? endsWith)
|
||||
{
|
||||
List<NginxFileSystemSortable> results = new();
|
||||
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync(uri);
|
||||
httpResponseMessage.Wait();
|
||||
if (httpResponseMessage.Result.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
{
|
||||
FileShareRepository fileShareRepository = this;
|
||||
Task<string> json = httpResponseMessage.Result.Content.ReadAsStringAsync();
|
||||
json.Wait();
|
||||
NginxFileSystem[]? nginxFileSystemCollection = JsonSerializer.Deserialize(json.Result, NginxFileSystemCollectionSourceGenerationContext.Default.NginxFileSystemArray);
|
||||
List<NginxFileSystemSortable> nginxFileSystemSortableCollection = NginxFileSystemSortable.Convert(fileShareRepository, uri, nginxFileSystemCollection);
|
||||
foreach (NginxFileSystemSortable nginxFileSystemSortable in nginxFileSystemSortableCollection.OrderByDescending(l => l.DateTime))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(endsWith) && !nginxFileSystemSortable.Name.EndsWith(endsWith))
|
||||
continue;
|
||||
results.Add(nginxFileSystemSortable);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
186
Wafer-Counter/Repositories/WaferCounterRepository.cs
Normal file
186
Wafer-Counter/Repositories/WaferCounterRepository.cs
Normal file
@ -0,0 +1,186 @@
|
||||
using OI.Metrology.Wafer.Counter.Models;
|
||||
using OI.Metrology.Shared.DataModels;
|
||||
using OI.Metrology.Shared.Models;
|
||||
using OI.Metrology.Shared.Models.Stateless;
|
||||
using OI.Metrology.Shared.Repositories;
|
||||
using System.Globalization;
|
||||
|
||||
namespace OI.Metrology.Wafer.Counter.Repository;
|
||||
|
||||
public class WaferCounterRepository : IWaferCounterRepository
|
||||
{
|
||||
|
||||
private record Record(int Check,
|
||||
int Total,
|
||||
string? SlotMap);
|
||||
|
||||
private readonly string _MockRoot;
|
||||
private readonly string _RepositoryName;
|
||||
private readonly AppSettings _AppSettings;
|
||||
private readonly IHttpClientFactory _HttpClientFactory;
|
||||
private readonly IDbConnectionFactory _DBConnectionFactory;
|
||||
private readonly IFileShareRepository _FileShareRepository;
|
||||
|
||||
public WaferCounterRepository(AppSettings appSettings, IDbConnectionFactory dbConnectionFactory, IHttpClientFactory httpClientFactory, IFileShareRepository fileShareRepository)
|
||||
{
|
||||
_AppSettings = appSettings;
|
||||
_MockRoot = appSettings.MockRoot;
|
||||
_HttpClientFactory = httpClientFactory;
|
||||
_DBConnectionFactory = dbConnectionFactory;
|
||||
_FileShareRepository = fileShareRepository;
|
||||
_RepositoryName = nameof(WaferCounterRepository)[..^10];
|
||||
}
|
||||
|
||||
private void MoveFile(string waferSizeDirectory, NginxFileSystemSortable nginxFileSystemSortable)
|
||||
{
|
||||
Calendar calendar = new CultureInfo("en-US").Calendar;
|
||||
string from = Path.Combine(waferSizeDirectory, nginxFileSystemSortable.Name);
|
||||
string weekOfYear = $"{nginxFileSystemSortable.DateTime:yyyy}_Week_{calendar.GetWeekOfYear(nginxFileSystemSortable.DateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
|
||||
string to = Path.Combine(waferSizeDirectory, "Archive", weekOfYear, nginxFileSystemSortable.Name);
|
||||
_FileShareRepository.MoveFile(from, to);
|
||||
}
|
||||
|
||||
private static Record GetRecord(string line1, string line2)
|
||||
{
|
||||
Record result;
|
||||
string? slotMap = string.IsNullOrEmpty(line2) || line2.Length != 8 ? null : line2.Substring(1, 1);
|
||||
int check = slotMap == "1" ? 1 : 0;
|
||||
#pragma warning disable IDE0057
|
||||
int total = int.Parse(line1.Substring(1));
|
||||
foreach (char item in line2.Substring(2))
|
||||
#pragma warning restore IDE0057
|
||||
{
|
||||
switch (item)
|
||||
{
|
||||
case '0':
|
||||
check += 0;
|
||||
slotMap += "0000";
|
||||
break;
|
||||
case '1':
|
||||
check += 1;
|
||||
slotMap += "0001";
|
||||
break;
|
||||
case '2':
|
||||
check += 1;
|
||||
slotMap += "0010";
|
||||
break;
|
||||
case '3':
|
||||
check += 2;
|
||||
slotMap += "0011";
|
||||
break;
|
||||
case '4':
|
||||
check += 1;
|
||||
slotMap += "0100";
|
||||
break;
|
||||
case '5':
|
||||
check += 2;
|
||||
slotMap += "0101";
|
||||
break;
|
||||
case '6':
|
||||
check += 2;
|
||||
slotMap += "0110";
|
||||
break;
|
||||
case '7':
|
||||
check += 3;
|
||||
slotMap += "0111";
|
||||
break;
|
||||
case '8':
|
||||
check += 1;
|
||||
slotMap += "1000";
|
||||
break;
|
||||
case '9':
|
||||
check += 2;
|
||||
slotMap += "1001";
|
||||
break;
|
||||
case 'A':
|
||||
check += 2;
|
||||
slotMap += "1010";
|
||||
break;
|
||||
case 'B':
|
||||
check += 3;
|
||||
slotMap += "1011";
|
||||
break;
|
||||
case 'C':
|
||||
check += 2;
|
||||
slotMap += "1100";
|
||||
break;
|
||||
case 'D':
|
||||
check += 3;
|
||||
slotMap += "1101";
|
||||
break;
|
||||
case 'E':
|
||||
check += 3;
|
||||
slotMap += "1110";
|
||||
break;
|
||||
case 'F':
|
||||
check += 4;
|
||||
slotMap += "1111";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
result = new(check, total, slotMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
private Uri GetWaferSizeUri(string area, string waferSize) =>
|
||||
_FileShareRepository.Append(new Uri(_AppSettings.EcMesaFileShareCharacterizationSi), "WaferCounter", area, waferSize);
|
||||
|
||||
private string GetWaferSizeDirectory(string area, string waferSize, bool destination) =>
|
||||
destination ? Path.Combine(_AppSettings.WaferCounterDestinationDirectory, area, waferSize) : Path.Combine(_AppSettings.EcCharacterizationSi, "WaferCounter", area, waferSize);
|
||||
|
||||
string? IWaferCounterRepository.GetSlotMap(string line1, string line2) =>
|
||||
GetRecord(line1, line2).SlotMap;
|
||||
|
||||
private List<NginxFileSystemSortable> GetNginxFileSystemSortableCollection(HttpClient httpClient, Uri waferSizeUri)
|
||||
{
|
||||
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 > 0 || DateTime.Now.Ticks > ticks)
|
||||
break;
|
||||
Thread.Sleep(250);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private static WaferCounter GetLastQuantityAndSlotMap(string waferSize, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable)
|
||||
{
|
||||
WaferCounter result;
|
||||
string text = string.Empty;
|
||||
Task<string> value = httpClient.GetStringAsync(nginxFileSystemSortable.Uri);
|
||||
value.Wait();
|
||||
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 for slot-map!");
|
||||
if (record.Total != record.Check)
|
||||
throw new Exception($"Invalid {record.Total} != {record.Check}");
|
||||
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 result;
|
||||
Uri waferSizeUri = GetWaferSizeUri(area, waferSize);
|
||||
HttpClient httpClient = _HttpClientFactory.CreateClient();
|
||||
string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false);
|
||||
List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri);
|
||||
if (nginxFileSystemSortableCollection.Count < 1)
|
||||
throw new Exception("No files!");
|
||||
result = GetLastQuantityAndSlotMap(waferSize, httpClient, nginxFileSystemSortableCollection[0]);
|
||||
for (int i = 0; i < nginxFileSystemSortableCollection.Count; i++)
|
||||
MoveFile(waferSizeDirectory, nginxFileSystemSortableCollection[i]);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user