121 lines
5.0 KiB
C#

using Adaptation.FileHandlers.MoveAllFiles.OpenInsight;
using Adaptation.FileHandlers.TIBCO.Transport;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Adaptation.FileHandlers.MoveAllFiles.ApiController;
public class BarcodeHelper
{
#nullable enable
private static string? GetJson(HttpContent? httpContent)
{
string? result;
if (httpContent is null)
result = null;
else
{
Task<string> task = httpContent.ReadAsStringAsync();
task.Wait();
result = task.Result;
}
return result;
}
private static void Write(string barcodeHostFileShare, string cellInstanceConnectionName, Notification notification)
{
DateTime dateTime = DateTime.Now;
Calendar calendar = new CultureInfo("en-US").Calendar;
string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
string directory = Path.Combine(barcodeHostFileShare, weekOfYear, dateTime.ToString("yyyy-MM-dd_HH"));
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
File.WriteAllText(Path.Combine(directory, $"{cellInstanceConnectionName}.csv"), notification.LastScanServiceResultValue);
}
private static Job GetJob(string lsl2SQLConnectionString, string metrologyFileShare, string barcodeHostFileShare, string id, Notification notification)
{
Job result;
string lastScan = notification.LastScanServiceResultValue.Length < 3 || notification.LastScanServiceResultValue[1] is not 't' and not 'T' || notification.LastScanServiceResultValue[0] != '1' ? notification.LastScanServiceResultValue : notification.LastScanServiceResultValue.Substring(2);
string json = string.Concat("{\"Area\": \"Si\", \"EquipmentType\": \"MET08THFTIRQS408M\", \"MesEntity\": \"", id, "\", \"Sequence\": \"", notification.KeyPressEvent.DateTime.Ticks, "\", \"MID\": \"-", lastScan, "-\", \"Recipe\": \"Recipe\"}");
result = new(lsl2SQLConnectionString, metrologyFileShare, barcodeHostFileShare, json);
return result;
}
private static Root? GetRoot(string rds)
{
HttpClient httpClient = new();
string url = $"{FileRead.OpenInsightApplicationProgrammingInterface}/materials/rds/{rds}";
string httpClientResult = httpClient.GetStringAsync(url).Result;
Root? root = JsonSerializer.Deserialize<Root>(httpClientResult);
httpClient.Dispose();
return root;
}
private static void SetQaMetTests(string toolClass, Root root, Dictionary<string, List<QaMetTest>> qaMetTests)
{
List<QaMetTest>? collection;
foreach (PrsStage prsStage in root.Rds.ProdSpec.PrsStages)
{
if (prsStage.QaMetTests is null)
continue;
foreach (QaMetTest qaMetTest in prsStage.QaMetTests)
{
if (qaMetTest.ToolClass != toolClass)
continue;
if (!qaMetTests.TryGetValue(prsStage.Stage, out collection))
{
qaMetTests.Add(prsStage.Stage, new());
if (!qaMetTests.TryGetValue(prsStage.Stage, out collection))
throw new Exception();
}
collection.Add(qaMetTest);
}
}
}
internal static PostReplay? Post(string id, HttpContent? httpContent)
{
PostReplay? result;
string? json = GetJson(httpContent);
if (json is null)
result = null;
else
{
Notification? notification = JsonSerializer.Deserialize<Notification>(json);
if (notification is null)
result = null;
else
{
Job? job;
string? mid;
Dictionary<string, List<QaMetTest>> qaMetTests = new();
Write(TIBCO.FileRead.BarcodeHostFileShare, id, notification.Value);
try
{
const string hyphen = "-";
job = GetJob(TIBCO.FileRead.LSL2SQLConnectionString, TIBCO.FileRead.MetrologyFileShare, TIBCO.FileRead.BarcodeHostFileShare, id, notification.Value);
mid = job.SpecName == hyphen || job.ProcessSpecName == hyphen ? $"{job.ProcessType}" : $"{job.ProcessType}.{job.SpecName}.{job.ProcessSpecName}";
Root? root = GetRoot(job.LotName);
if (root is not null)
SetQaMetTests(notification.Value.ToolClass, root, qaMetTests);
}
catch (Exception ex)
{
mid = string.Concat(ex.Message, Environment.NewLine, ex.StackTrace);
}
result = new(mid, qaMetTests); // .ProdSpec.PrsStages[0].QaMetTests[0].Recipe);
}
}
return result;
}
}