2022-06-13 14:09:59 -07:00

230 lines
9.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
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<Item> Items { get; }
public Job(string oiContextDataPendingPath, string oiContextDataResultsPath, string oiContextDataSearchPath, string lsl2SQLConnectionString, Dictionary<int, List<(string MID, int Count)>> _RDS, string mid)
{
Items = new List<Item>();
if (mid[0] != '{' || mid[mid.Length - 1] != '}' || !mid.Contains("\"Si\""))
IsAreaSi = false;
else
{
string psn;
int rds = 0;
string reactor;
Input input = JsonSerializer.Deserialize<Input>(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<IGrouping<string, (string MID, int Count)>> orderedMatches = new();
if (segments.Length < 3 || (segments.Length > 1 && !int.TryParse(segments[1], out rds)))
{
psn = string.Empty;
reactor = string.Empty;
}
else if (rds > 0)
{
lock (_RDS)
{
if (!_RDS.ContainsKey(rds))
_RDS.Add(rds, new());
List<(string MID, int Count)> collection = _RDS[rds];
if (collection.Count == 0)
_RDS[rds].Add(new(input.MID, 1));
else
{
IEnumerable<IGrouping<string, (string MID, int Count)>> grouped = collection.GroupBy(l => l.MID);
orderedMatches.AddRange(from l in grouped orderby l.Count() descending select l);
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);
}
psn = segments[2];
reactor = segments[0];
}
else
{
psn = string.Empty;
reactor = string.Empty;
}
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 < 1)
{
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 });
MoveOldFiles(oiContextDataSearchPath, oiContextDataPendingPath, oiContextDataResultsPath);
}
}
public 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<JsonElement>(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 void MoveOldFiles(string oiContextDataPendingPath, string oiContextDataResultsPath, string oiContextDataSearchPath)
{
string yearWeek;
string[] oldFiles;
FileInfo fileInfo;
string weekOfYear;
string moveDirectory;
DateTime daysOld = DateTime.Now.AddDays(-2);
CultureInfo cultureInfo = new("en-US");
Calendar calendar = cultureInfo.Calendar;
string[] directories = new string[] { oiContextDataSearchPath, oiContextDataPendingPath, oiContextDataResultsPath };
foreach (string directory in directories)
{
try
{
oldFiles = Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly);
foreach (string oldFile in oldFiles)
{
fileInfo = new FileInfo(oldFile);
if (!fileInfo.Exists || fileInfo.LastWriteTime > daysOld)
continue;
weekOfYear = calendar.GetWeekOfYear(fileInfo.LastWriteTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
yearWeek = string.Concat(fileInfo.LastWriteTime.ToString("yyyy"), "___Week_", weekOfYear);
moveDirectory = Path.Combine(fileInfo.DirectoryName, yearWeek);
if (!Directory.Exists(moveDirectory))
_ = Directory.CreateDirectory(moveDirectory);
try
{ File.Move(oldFile, Path.Combine(moveDirectory, fileInfo.Name)); }
catch (Exception) { }
}
}
catch (Exception)
{
}
}
}
}