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.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 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; } 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(json); if (notification is null) result = null; else { Job? job; string? mid; List? collection; Dictionary> qaMetTests = new(); Write(TIBCO.FileRead.BarcodeHostFileShare, id, notification.Value); try { const string hyphen = "-"; HttpClient httpClient = new(); 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}"; string httpClientResult = httpClient.GetStringAsync($"{FileRead.OpenInsightApplicationProgrammingInterface}/materials/rds/{job.LotName}").Result; Root? root = JsonSerializer.Deserialize(httpClientResult); httpClient.Dispose(); if (root is not null) { foreach (PrsStage prsStage in root.Rds.ProdSpec.PrsStages) { if (prsStage.QaMetTests is null) continue; foreach (QaMetTest qaMetTest in prsStage.QaMetTests) { if (qaMetTest.ToolClass != notification.Value.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); } } } } catch (Exception) { mid = null; } result = new(mid, qaMetTests); // .ProdSpec.PrsStages[0].QaMetTests[0].Recipe); } } return result; } }