using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Text.Json; using System.Text.RegularExpressions; namespace Adaptation.FileHandlers.TIBCO.Transport; public class Job { public string AutomationMode { get; } public string BasicType { get; } public string Equipment { get; } public string JobName { get; } public string LotName { get; } public string PackageName { get; } public string ProcessSpecName { get; } public string ProcessType { get; } public string ProductName { get; } public string Qty { get; } public string RecipeName { get; } public string StateModel { get; } // public bool IsAreaSi { get; } public DateTime DateTime { get; } public List Items { get; } public Job(string lsl2SQLConnectionString, Dictionary> _RDS, string mid) { Items = new List(); if (string.IsNullOrEmpty(mid) || mid[0] != '{' || mid[mid.Length - 1] != '}' || !mid.Contains("\"Si\"")) IsAreaSi = false; else { string psn; int rds = 0; string reactor; Input input = JsonSerializer.Deserialize(mid); IsAreaSi = input.Area == "Si"; if (input.MID is null) input.MID = string.Empty; if (!long.TryParse(input.Sequence, out long sequence)) DateTime = DateTime.Now; else DateTime = new DateTime(sequence); string[] segments = Regex.Split(input.MID, @"[^0-9']"); List> orderedMatches = new(); if (segments.Length < 3 || (segments.Length > 1 && !int.TryParse(segments[1], out rds))) { psn = string.Empty; reactor = string.Empty; } else if (rds is < 100000 or > 100000000) { psn = string.Empty; reactor = string.Empty; } else { psn = segments[2]; reactor = segments[0]; lock (_RDS) { if (!_RDS.ContainsKey(rds)) _RDS.Add(rds, new()); bool rdsCollectionCountIsZero = _RDS[rds].Count == 0; if (rdsCollectionCountIsZero) { if (!string.IsNullOrEmpty(reactor) && !string.IsNullOrEmpty(psn)) _RDS[rds].Add(new(input.MID, 1)); else { string lsl2SQLMID = GetMID(lsl2SQLConnectionString, rds); if (string.IsNullOrEmpty(lsl2SQLMID)) _RDS[rds].Add(new(input.MID, 1)); else _RDS[rds].Add(new(lsl2SQLMID, 1)); } } IEnumerable> grouped = _RDS[rds].GroupBy(l => l.MID); orderedMatches.AddRange(from l in grouped orderby l.Count() descending select l); if (!rdsCollectionCountIsZero) { int length = (from l in orderedMatches where l.Key == input.MID select true).Count(); _RDS[rds].Add(new(input.MID, length + 1)); } if (_RDS.Count > 1000) _ = _RDS.Remove(_RDS.ElementAt(0).Key); } } AutomationMode = string.Concat(DateTime.Ticks, ".", input.MesEntity); Equipment = input.MesEntity; JobName = DateTime.Ticks.ToString(); if (orderedMatches.Any() && (string.IsNullOrEmpty(reactor) || string.IsNullOrEmpty(psn))) { segments = Regex.Split(orderedMatches[0].Key, @"[^0-9']"); psn = segments[2]; reactor = segments[0]; } const string hyphen = "-"; if (rds is < 100000 or > 100000000) { BasicType = hyphen; LotName = input.MID; } else { LotName = rds.ToString(); BasicType = GetBasicType(lsl2SQLConnectionString, hyphen, rds); } PackageName = hyphen; //WAFER_ID WaferLot ProcessSpecName = hyphen; //WAFER_POS PocketNumber ProcessType = reactor; ProductName = psn; Qty = "1"; RecipeName = input.Recipe; StateModel = input.EquipmentType; Items.Add(new Item { Name = "0", Type = "NA", Number = (0 + 1).ToString(), Qty = "1", CarrierName = hyphen }); } } private static string GetBasicType(string lsl2SQLConnectionString, string hyphen, int rds) { string result; // string json; // string loadLock; // JsonElement jsonElement; // DateTime dateTime = DateTime.Now; // string rdsFile = Path.Combine(configData.OIContextDataResultsPath, $"{DateTime.Ticks}.rds"); // string jsonFile = Path.Combine(configData.OIContextDataResultsPath, $"{DateTime.Ticks}.json"); // File.WriteAllText(Path.Combine(configData.OIContextDataSearchPath, $"{DateTime.Ticks}.rds"), rds); // CultureInfo cultureInfo = new CultureInfo("en-US"); // Calendar calendar = cultureInfo.Calendar; // string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00"); // string yearWeek = string.Concat(dateTime.ToString("yyyy"), "___Week_", weekOfYear); // string resultsDirectory = Path.Combine(configData.OIContextDataResultsPath, yearWeek); // if (!Directory.Exists(resultsDirectory)) // Directory.CreateDirectory(resultsDirectory); // long breakAfter = dateTime.AddSeconds(60).Ticks; // for (int i = 0; i < short.MaxValue; i++) // { // if (File.Exists(rdsFile) && File.Exists(jsonFile)) // { // loadLock = string.Empty; // json = File.ReadAllText(jsonFile); // jsonElement = JsonSerializer.Deserialize(json); // if (jsonElement.ValueKind == JsonValueKind.Object) // { // foreach (JsonProperty jsonProperty in jsonElement.EnumerateObject()) // { // if (jsonProperty.Name != "LoadLock") // continue; // loadLock = jsonProperty.Value.ToString(); // } // } // if (string.IsNullOrEmpty(loadLock)) // File.Move(jsonFile, Path.Combine(configData.OIContextDataResultsPath, $"{DateTime.Ticks}.err")); // else // { // File.Move(rdsFile, Path.Combine(configData.OIContextDataResultsPath, yearWeek, $"{DateTime.Ticks}.rds")); // File.Move(jsonFile, Path.Combine(configData.OIContextDataResultsPath, yearWeek, $"{DateTime.Ticks}.json")); // result = loadLock; // } // break; // } // if (DateTime.Now.Ticks > breakAfter) // break; // } object scalar = null; StringBuilder sql = new(); _ = sql.Append(" SELECT "). Append(" CASE "). Append(" WHEN LOAD_LOCK_SIDE = 'L' THEN 'Left - ' "). Append(" WHEN LOAD_LOCK_SIDE = 'R' THEN 'Right - ' "). Append(" ELSE LOAD_LOCK_SIDE "). Append(" END + REACTOR_TYPE AS LOAD_LOCK "). Append(" FROM [LSL2SQL].[dbo].[REACT_RUN] "). Append($" WHERE RDS_NO = '{rds}' "); //Append(" AND LOAD_SIG != '' "); try { using SqlConnection sqlConnection = new(lsl2SQLConnectionString); sqlConnection.Open(); using (SqlCommand sqlCommand = new(sql.ToString(), sqlConnection)) scalar = sqlCommand.ExecuteScalar(); sqlConnection.Close(); } catch (Exception) { } if (scalar is null) result = hyphen; else result = scalar.ToString(); return result; } private static string GetMID(string lsl2SQLConnectionString, int rds) { string result; object scalar = null; StringBuilder sql = new(); _ = sql.Append(" SELECT CONCAT(REACTOR, '-', RDS_NO, '-', PS_NO) [MID] "). Append(" FROM [LSL2SQL].[dbo].[REACT_RUN] "). Append($" WHERE RDS_NO = '{rds}' "); try { using SqlConnection sqlConnection = new(lsl2SQLConnectionString); sqlConnection.Open(); using (SqlCommand sqlCommand = new(sql.ToString(), sqlConnection)) scalar = sqlCommand.ExecuteScalar(); sqlConnection.Close(); } catch (Exception) { } if (scalar is null) result = string.Empty; else result = scalar.ToString(); return result; } }