using APCViewer.Models; using Infineon.Monitoring.MonA; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Shared; using Shared.Metrology; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Runtime.InteropServices; using System.Text; using System.Threading; namespace APCViewer.Singleton { public class Background : IBackground { public List Timers => _Timers; public string Message { get; private set; } public AppSettings AppSettings => _AppSettings; public WebClient WebClient { get; private set; } public string WorkingDirectory { get; private set; } public IsEnvironment IsEnvironment => _IsEnvironment; public List Exceptions { get; private set; } private bool _ShuttingDown; private readonly object _Lock; private readonly List _Timers; private readonly AppSettings _AppSettings; private readonly string[] _SiEquipmentTypes; private readonly string[] _GaNEquipmentTypes; private readonly IsEnvironment _IsEnvironment; private const string _Site = "sjc"; private Log _Log; private DateTime _PrimaryInstanceSetAt; private readonly Dictionary _GaNPDSFFiles; private readonly Dictionary _SiPDSFFiles; private readonly Dictionary _GaNIPDSFFiles; private readonly Dictionary _SiIPDSFFiles; private readonly Dictionary _GaNAPCLogistics; private readonly Dictionary _SiAPCLogistics; private readonly Dictionary _GaNEDALogistics; private readonly Dictionary _SiEDALogistics; private readonly Dictionary _GaNEAFLogLogistics; private readonly Dictionary _SiEAFLogLogistics; public Background(IsEnvironment isEnvironment, IConfiguration configuration, string workingDirectory) { _Log = null; _Lock = new object(); _ShuttingDown = false; if (_Lock is null) { } Message = string.Empty; AppSettings appSettings = new AppSettings(); configuration.Bind(appSettings); if (!isEnvironment.Production) appSettings.Server = appSettings.Server.Replace('.', '_'); _AppSettings = appSettings; _Timers = new List(); _IsEnvironment = isEnvironment; Exceptions = new List(); WorkingDirectory = workingDirectory; _PrimaryInstanceSetAt = DateTime.MinValue; _GaNPDSFFiles = new Dictionary(); _SiPDSFFiles = new Dictionary(); _GaNIPDSFFiles = new Dictionary(); _SiIPDSFFiles = new Dictionary(); _GaNAPCLogistics = new Dictionary(); _SiAPCLogistics = new Dictionary(); _GaNEDALogistics = new Dictionary(); _SiEDALogistics = new Dictionary(); _GaNEAFLogLogistics = new Dictionary(); _SiEAFLogLogistics = new Dictionary(); _GaNEquipmentTypes = GetEquipmentTypes(isGaN: true, isSi: false); _SiEquipmentTypes = GetEquipmentTypes(isGaN: false, isSi: true); } public void Dispose() { foreach (Timer timer in _Timers) timer.Dispose(); } void IBackground.Update(ILogger logger, WebClient webClient) { Update(logger, webClient); } internal void Update(ILogger logger, WebClient webClient) { WebClient = webClient; _Log = new Log(logger); //https://blog.magnusmontin.net/2018/11/05/platform-conditional-compilation-in-net-core/ if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) _Log.Debug("Running on Linux!"); else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) _Log.Debug("Running on macOS!"); else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) _Log.Debug("Running on Windows!"); #if Linux _Log.Debug("Built on Linux!"); #elif OSX _Log.Debug("Built on macOS!"); #elif Windows _Log.Debug("Built in Windows!"); #else ()("Built in unkown!"); #endif if (string.IsNullOrEmpty(_AppSettings.URLs) && !_IsEnvironment.Development) throw new Exception("Invalid Application Settings URLs!"); } internal void Catch(Exception exception) { Exceptions.Add(exception); _Log.Error(exception); if (!string.IsNullOrEmpty(_AppSettings.MonARessource)) { MonIn monIn = MonIn.GetInstance(); monIn.SendStatus(_Site, _AppSettings.MonARessource, "Heartbeat", State.Warning); } } public void SendStatusOk() { if (!string.IsNullOrEmpty(_AppSettings.MonARessource)) { MonIn monIn = MonIn.GetInstance(); monIn.SendStatus(_Site, _AppSettings.MonARessource, "Heartbeat", State.Ok); } } public string GetCountDirectory(string verb) { DateTime dateTime = DateTime.Now; CultureInfo cultureInfo = new("en-US"); Calendar calendar = cultureInfo.Calendar; string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00"); //string nameSpace = System.Reflection.Assembly.GetExecutingAssembly().EntryPoint.DeclaringType.Namespace; string nameSpace = GetType().Namespace.Split('.')[0]; if (!string.IsNullOrEmpty(_AppSettings.MonARessource)) { MonIn monIn = MonIn.GetInstance(); monIn.SendStatus(_Site, _AppSettings.MonARessource, "Heartbeat", State.Up); } return string.Concat(@"\\", _AppSettings.Server, @"\EC_APC\Counts\", dateTime.ToString("yyyy"), @"\", "Week_", weekOfYear, @"\", dateTime.ToString("yyyy - MM - dd"), @"\", "Application", @"\", nameSpace, @"\", verb, @"\", dateTime.Ticks); } public void Stop(bool immediate) { _ShuttingDown = true; foreach (Timer timer in _Timers) timer.Change(Timeout.Infinite, 0); if (!_IsEnvironment.Development) { if (!string.IsNullOrEmpty(_AppSettings.MonARessource)) { MonIn monIn = MonIn.GetInstance(); monIn.SendStatus(_Site, _AppSettings.MonARessource, "Heartbeat", State.Down); } string countDirectory = GetCountDirectory("Stop"); Directory.CreateDirectory(countDirectory); } } public void ClearMessage() { Message = string.Empty; } public void SetIsPrimaryInstance() { _PrimaryInstanceSetAt = DateTime.Now; } public void ClearIsPrimaryInstance() { _PrimaryInstanceSetAt = DateTime.MinValue; } public bool IsPrimaryInstance() { bool result; DateTime dateTime = DateTime.Now.AddDays(-1); result = _PrimaryInstanceSetAt > dateTime; return result; } public void LogisticsClear() { _GaNAPCLogistics.Clear(); _SiAPCLogistics.Clear(); _GaNEDALogistics.Clear(); _SiEDALogistics.Clear(); _GaNEAFLogLogistics.Clear(); _SiEAFLogLogistics.Clear(); } public void APCDataCallback() { Data(_AppSettings.Server, _SiEquipmentTypes, _SiAPCLogistics, _SiPDSFFiles, isAPC: true); Data(_AppSettings.Server, _GaNEquipmentTypes, _GaNAPCLogistics, _GaNPDSFFiles, isAPC: true); } public void EDADataCallback() { Data(_AppSettings.Server, _SiEquipmentTypes, _SiEDALogistics, _SiPDSFFiles, isEDA: true); Data(_AppSettings.Server, _GaNEquipmentTypes, _GaNEDALogistics, _GaNPDSFFiles, isEDA: true); } public void EAFLogDataCallback() { Data(_AppSettings.Server, _SiEquipmentTypes, _SiEAFLogLogistics, _SiIPDSFFiles, isEAFLog: true); Data(_AppSettings.Server, _GaNEquipmentTypes, _GaNEAFLogLogistics, _GaNIPDSFFiles, isEAFLog: true); } public string GetPDSF(long Sequence) { string result = string.Empty; if (string.IsNullOrEmpty(result)) { foreach (KeyValuePair element in _GaNEDALogistics) { if (((Logistics)element.Value).Sequence == Sequence) { result = element.Key; break; } } } if (string.IsNullOrEmpty(result)) { foreach (KeyValuePair element in _SiEDALogistics) { if (((Logistics)element.Value).Sequence == Sequence) { result = element.Key; break; } } } if (string.IsNullOrEmpty(result)) { foreach (KeyValuePair element in _GaNAPCLogistics) { if (((Logistics)element.Value).Sequence == Sequence) { result = element.Key; break; } } } if (string.IsNullOrEmpty(result)) { foreach (KeyValuePair element in _SiAPCLogistics) { if (((Logistics)element.Value).Sequence == Sequence) { result = element.Key; break; } } } return result; } public string GetIPDSF(long Sequence) { string result = string.Empty; if (string.IsNullOrEmpty(result)) { foreach (KeyValuePair element in _GaNEAFLogLogistics) { if (((Logistics)element.Value).Sequence == Sequence) { result = element.Key; break; } } } if (string.IsNullOrEmpty(result)) { foreach (KeyValuePair element in _SiEAFLogLogistics) { if (((Logistics)element.Value).Sequence == Sequence) { result = element.Key; break; } } } return result; } public Tuple SetViewBag(string directory, string filter, bool isGaN = false, bool isSi = false, bool forPDSF = false, bool forIPDSF = false) { Tuple result; if (isGaN && isSi) throw new Exception(); else if (!isGaN && !isSi) isGaN = true; if (isGaN) result = SetViewBag(_GaNAPCLogistics, _GaNEDALogistics, _GaNEAFLogLogistics, directory, filter, forPDSF, forIPDSF); else if (isSi) result = SetViewBag(_SiAPCLogistics, _SiEDALogistics, _SiEAFLogLogistics, directory, filter, forPDSF, forIPDSF); else throw new Exception(); return result; } public Tuple, List> GetTimePivot(bool isGaN = false, bool isSi = false) { List forPDSF; List forIPDSF; if (isGaN && isSi) throw new Exception(); else if (!isGaN && !isSi) isGaN = true; if (isGaN) { forIPDSF = GetTimePivot(_GaNEquipmentTypes, _GaNAPCLogistics, _GaNEDALogistics, _GaNEAFLogLogistics, forPDSF: false, forIPDSF: true); forPDSF = GetTimePivot(_GaNEquipmentTypes, _GaNAPCLogistics, _GaNEDALogistics, _GaNEAFLogLogistics, forPDSF: true, forIPDSF: false); } else if (isSi) { forIPDSF = GetTimePivot(_SiEquipmentTypes, _SiAPCLogistics, _SiEDALogistics, _SiEAFLogLogistics, forPDSF: false, forIPDSF: true); forPDSF = GetTimePivot(_SiEquipmentTypes, _SiAPCLogistics, _SiEDALogistics, _SiEAFLogLogistics, forPDSF: true, forIPDSF: false); } else throw new Exception(); return new Tuple, List>(forIPDSF, forPDSF); } public static string[] GetEquipmentTypes(bool isGaN = false, bool isSi = false) { string[] results; if (isGaN && isSi) throw new Exception(); else if (!isGaN && !isSi) isGaN = true; if (isSi) { results = new string[] { Shared.EquipmentType.MET08ANLYSDIFAAST230_Semi.ToString(), Shared.EquipmentType.MET08DDUPSFS6420.ToString(), Shared.EquipmentType.MET08DDUPSP1TBI.ToString(), Shared.EquipmentType.MET08RESIHGCV.ToString(), Shared.EquipmentType.MET08RESIMAPCDE.ToString(), Shared.EquipmentType.MET08THFTIRQS408M.ToString(), Shared.EquipmentType.MET08THFTIRSTRATUS.ToString() }; } else if (isGaN) { results = new string[] { Shared.EquipmentType.DEP08EGANAIXG5.ToString(), Shared.EquipmentType.MET08AFMD3100.ToString(), Shared.EquipmentType.MET08BVHGPROBE.ToString(), Shared.EquipmentType.MET08CVHGPROBE802B150.ToString(), Shared.EquipmentType.MET08CVHGPROBE802B150_Monthly.ToString(), Shared.EquipmentType.MET08CVHGPROBE802B150_Weekly.ToString(), Shared.EquipmentType.MET08DDINCAN8620.ToString(), Shared.EquipmentType.MET08DDINCAN8620_Daily.ToString(), Shared.EquipmentType.MET08EBEAMINTEGRITY26.ToString(), Shared.EquipmentType.MET08HALLHL5580.ToString(), Shared.EquipmentType.MET08HALLHL5580_Monthly.ToString(), Shared.EquipmentType.MET08HALLHL5580_Weekly.ToString(), Shared.EquipmentType.MET08MESMICROSCOPE.ToString(), Shared.EquipmentType.MET08NDFRESIMAP151C.ToString(), Shared.EquipmentType.MET08NDFRESIMAP151C_Verification.ToString(), Shared.EquipmentType.MET08PLMAPRPM.ToString(), Shared.EquipmentType.MET08PLMAPRPM_Daily.ToString(), Shared.EquipmentType.MET08PLMAPRPM_Verification.ToString(), Shared.EquipmentType.MET08PLMPPLATO.ToString(), Shared.EquipmentType.MET08PRFUSB4000.ToString(), Shared.EquipmentType.MET08UVH44GS100M.ToString(), Shared.EquipmentType.MET08VPDSUBCON.ToString(), Shared.EquipmentType.MET08WGEOMX203641Q.ToString(), Shared.EquipmentType.MET08WGEOMX203641Q_Verification.ToString(), Shared.EquipmentType.METBRXRAYJV7300L.ToString(), Shared.EquipmentType.MET08XRDXPERTPROMRDXL.ToString(), Shared.EquipmentType.MET08XRDXPERTPROMRDXL_Monthly.ToString(), Shared.EquipmentType.MET08XRDXPERTPROMRDXL_Weekly.ToString() }; } else throw new Exception(); return results; } public Tuple SetViewBag(Dictionary apcLogistics, Dictionary edaLogistics, Dictionary eafLogLogistics, string directory = null, string filter = null, bool forPDSF = false, bool forIPDSF = false) { Tuple result; int files = 0; string eaf = "EAF"; string eda = "EDA"; string apc = "APC"; string[] technologies = new string[] { eaf, eda, apc }; string root = "Root"; string staging = "Staging"; string production = "Production"; string development = "Development"; string[] enviroments = new string[] { root, staging, production, development }; string error = "Error !!!"; List> sorted = new List>(); Dictionary logisticsCollection = new Dictionary(); Dictionary> pairs = new Dictionary>(); Dictionary>> grouped = new Dictionary>>(); foreach (string t in technologies) { if (_ShuttingDown) break; foreach (string e in enviroments) { if (_ShuttingDown) break; grouped.Add(string.Concat(t, " - ", e), new Dictionary>()); grouped.Add(string.Concat(t, " - ", e, " ", error), new Dictionary>()); } } string searchPattern; if (forPDSF && forIPDSF) throw new Exception(); else if (!forPDSF && !forIPDSF) throw new Exception(); else if (forPDSF) searchPattern = "*.pdsf"; else if (forIPDSF) searchPattern = "*.ipdsf"; else throw new Exception(); if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory)) { Logistics logistics; Tuple pdsf; string[] pdsfFiles = Directory.GetFiles(directory, searchPattern, SearchOption.AllDirectories); foreach (string pdsfFile in pdsfFiles) { if (_ShuttingDown) break; pdsf = ProcessDataStandardFormat.GetLogisticsColumnsAndBody(pdsfFile); logistics = new Logistics(pdsfFile, pdsf.Item1); if (!logistics.Logistics2.Any()) logistics.Logistics2.Add(new Logistics2(string.Empty)); logisticsCollection.Add(pdsfFile, logistics); } } else if (forIPDSF) { if (!(eafLogLogistics is null)) { foreach (KeyValuePair element in eafLogLogistics) logisticsCollection.Add(element.Key, (Logistics)element.Value); } } else if (forPDSF) { if (!(edaLogistics is null)) { foreach (KeyValuePair element in edaLogistics) logisticsCollection.Add(element.Key, (Logistics)element.Value); } if (!(apcLogistics is null)) { foreach (KeyValuePair element in apcLogistics) logisticsCollection.Add(element.Key, (Logistics)element.Value); } } if (logisticsCollection.Any()) { string key; string technology; string environment; string equipmentType; List sequences = new List(); foreach (KeyValuePair element in logisticsCollection) { if (_ShuttingDown) break; if (element.Key.Contains(@"\EC_EAFLog\")) technology = eaf; else if (element.Key.Contains(@"\EC_EDA\")) technology = eda; else if (element.Key.Contains(@"\EC_APC\")) technology = apc; else technology = eaf; if (element.Key.Contains(@"\Production\")) environment = production; else if (element.Key.Contains(@"\Staging\")) environment = staging; else if (element.Key.Contains(@"\DEV\")) environment = development; else environment = root; equipmentType = Path.GetFileNameWithoutExtension(Path.GetDirectoryName(Path.GetDirectoryName(element.Key))); if (equipmentType == "ViewerPath") equipmentType = Path.GetFileNameWithoutExtension(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(element.Key)))); if (string.IsNullOrEmpty(filter) || technology == filter || environment == filter || equipmentType == filter) { files += 1; if (!element.Key.Contains(@"\Error\") && !element.Key.Contains(@"\BadPath\")) key = string.Concat(technology, " - ", environment); else key = string.Concat(technology, " - ", environment, " ", error); if (!grouped[key].ContainsKey(equipmentType)) grouped[key].Add(equipmentType, new List()); grouped[key][equipmentType].Add(element.Value); if (!sequences.Contains(element.Value.Sequence)) { sequences.Add(element.Value.Sequence); pairs.Add(element.Value.Sequence, new Tuple(new string[] { key, equipmentType }, element.Value)); } else { for (short i = 1; i < short.MaxValue; i++) { if (_ShuttingDown) break; if (sequences.Contains(element.Value.Sequence + i)) continue; else { sequences.Add(element.Value.Sequence + i); pairs.Add(element.Value.Sequence + i, new Tuple(new string[] { string.Concat("*", key), equipmentType }, element.Value)); break; } } } } } sequences = (from l in sequences orderby l descending select l).ToList(); foreach (long sequence in sequences) sorted.Add(pairs[sequence]); } result = new Tuple(files, grouped, sorted, directory); return result; } public List GetTimePivot(string[] equipmentTypes, Dictionary apcLogistics, Dictionary edaLogistics, Dictionary eafLogLogistics, bool forPDSF = false, bool forIPDSF = false) { List results = new List(); Dictionary logisticsCollection = new Dictionary(); if (forPDSF && forIPDSF) throw new Exception(); else if (!forPDSF && !forIPDSF) throw new Exception(); else if (forIPDSF) { if (!(eafLogLogistics is null)) { foreach (KeyValuePair element in eafLogLogistics) logisticsCollection.Add(element.Key, (Logistics)element.Value); } } else if (forPDSF) { if (!(edaLogistics is null)) { foreach (KeyValuePair element in edaLogistics) logisticsCollection.Add(element.Key, (Logistics)element.Value); } if (!(apcLogistics is null)) { foreach (KeyValuePair element in apcLogistics) logisticsCollection.Add(element.Key, (Logistics)element.Value); } } if (logisticsCollection.Any()) { int eT = 0; int jID = 1; int pJID = 2; int pN = 3; int k = 4; string key; string partNumber; string equipmentConnection; DateTime dateTime = DateTime.Now; List segments = new List(); StringBuilder stringBuilder = new StringBuilder(); long minutes15 = dateTime.AddMinutes(-15).Ticks; long minutes30 = dateTime.AddMinutes(-30).Ticks; long hours1 = dateTime.AddHours(-1).Ticks; long hours3 = dateTime.AddHours(-3).Ticks; long days1 = dateTime.AddDays(-1).Ticks; long days2 = dateTime.AddDays(-2).Ticks; long days4 = dateTime.AddDays(-4).Ticks; long days7 = dateTime.AddDays(-7).Ticks; List keys = new List(); List keySegments = new List(); List equipmentConnections = new List(); foreach (string item in equipmentTypes) equipmentConnections.Add(item.ToString()); Dictionary> m15 = new Dictionary>(); Dictionary> m30 = new Dictionary>(); Dictionary> h1 = new Dictionary>(); Dictionary> h3 = new Dictionary>(); Dictionary> d1 = new Dictionary>(); Dictionary> d2 = new Dictionary>(); Dictionary> d4 = new Dictionary>(); Dictionary> d7 = new Dictionary>(); Dictionary> more = new Dictionary>(); string[] si = new string[] { EquipmentType.MET08DDUPSFS6420.ToString(), EquipmentType.MET08DDUPSP1TBI.ToString(), EquipmentType.MET08RESIHGCV.ToString(), EquipmentType.MET08RESIMAPCDE.ToString(), EquipmentType.MET08THFTIRQS408M.ToString(), EquipmentType.MET08THFTIRSTRATUS.ToString() }; foreach (KeyValuePair element in logisticsCollection) { if (_ShuttingDown) break; equipmentConnection = Path.GetFileNameWithoutExtension(Path.GetDirectoryName(Path.GetDirectoryName(element.Key))); if (equipmentConnections.Contains(equipmentConnection)) equipmentConnections.Remove(equipmentConnection); if (!element.Value.Logistics2.Any()) partNumber = string.Empty; else partNumber = element.Value.Logistics2[0].PartNumber; if (string.IsNullOrEmpty(partNumber) && element.Value.Logistics1.Any()) { key = "PRODUCT="; if (element.Value.Logistics1[0].Contains(key)) { segments = element.Value.Logistics1[0].Split(new string[] { key }, StringSplitOptions.RemoveEmptyEntries).ToList(); partNumber = segments[1].Split(';')[0]; } } if (string.IsNullOrEmpty(partNumber)) partNumber = " "; segments.Clear(); segments.Add(equipmentConnection); segments.Add(element.Value.JobID); if (si.Contains(equipmentConnection)) segments.Add("Si"); else segments.Add(element.Value.ProcessJobID); segments.Add(partNumber); stringBuilder.Clear(); foreach (string segment in segments) stringBuilder.Append(segment).Append('|'); key = stringBuilder.ToString(); segments.Add(key); if (element.Value.Sequence > minutes15) { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } m15[key].Add(element.Value); } else if (element.Value.Sequence > minutes30) { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } m30[key].Add(element.Value); } else if (element.Value.Sequence > hours1) { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } h1[key].Add(element.Value); } else if (element.Value.Sequence > hours3) { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } h3[key].Add(element.Value); } else if (element.Value.Sequence > days1) { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } d1[key].Add(element.Value); } else if (element.Value.Sequence > days2) { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } d2[key].Add(element.Value); } else if (element.Value.Sequence > days4) { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } d4[key].Add(element.Value); } else if (element.Value.Sequence > days7) { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } d7[key].Add(element.Value); } else { if (!keys.Contains(key)) { keys.Add(key); keySegments.Add(segments.ToArray()); m15.Add(key, new List()); m30.Add(key, new List()); h1.Add(key, new List()); h3.Add(key, new List()); d1.Add(key, new List()); d2.Add(key, new List()); d4.Add(key, new List()); d7.Add(key, new List()); more.Add(key, new List()); } more[key].Add(element.Value); } } int count; List row; results.Add(new string[] { "ET", nameof(Logistics.JobID), nameof(Logistics.ProcessJobID), nameof(Logistics2.PartNumber), "m15", "m30", "h1", "h3", "d1", "d2", "d4", "d7", "more" }); for (int i = 0; i < equipmentConnections.Count; i++) results.Add(new string[] { equipmentConnections[i], " ", " ", " ", "0", "0", "0", "0", "0", "0", "0", "0", "0" }); keySegments = (from l in keySegments orderby l[eT], l[jID], l[pJID], l[pN] select l).ToList(); foreach (string[] keySegment in keySegments) { if (_ShuttingDown) break; count = 0; key = keySegment[k]; row = new List(new string[] { keySegment[eT], keySegment[pJID], keySegment[jID], keySegment[pN] }); count += m15[key].Count; row.Add(count.ToString()); count += m30[key].Count; row.Add(count.ToString()); count += h1[key].Count; row.Add(count.ToString()); count += h3[key].Count; row.Add(count.ToString()); count += d1[key].Count; row.Add(count.ToString()); count += d2[key].Count; row.Add(count.ToString()); count += d4[key].Count; row.Add(count.ToString()); count += d7[key].Count; row.Add(count.ToString()); count += more[key].Count; row.Add(count.ToString()); results.Add(row.ToArray()); } } return results; } public void Data(string server, string[] equipmentTypes, Dictionary inputLogistics, Dictionary inputPDSFFiles, bool isEAFLog = false, bool isEDA = false, bool isAPC = false) { DateTime dateTime; string[] pdsfFiles; string checkDirectory; DateTime creationTime; DateTime lastWriteTime; string[] cellInstanceDirectories; string[] equipmentTypeDirectories; long ticks = DateTime.Now.AddDays(-5).Ticks; List pdsfFileCollection = new List(); long pollPathTicks = DateTime.Now.AddMinutes(-5).Ticks; Tuple pdsf; if (inputLogistics is null) inputLogistics = new Dictionary(); if (inputPDSFFiles is null) inputPDSFFiles = new Dictionary(); if (inputLogistics.Any()) { string[] fileKeys = inputLogistics.Keys.ToArray(); foreach (string fileKey in fileKeys) { if (_ShuttingDown) break; if (!File.Exists(fileKey)) continue; creationTime = new FileInfo(fileKey).CreationTime; lastWriteTime = new FileInfo(fileKey).LastWriteTime; if (creationTime < lastWriteTime) dateTime = creationTime; else dateTime = lastWriteTime; if (dateTime.Ticks < ticks) { if (inputLogistics.ContainsKey(fileKey)) inputLogistics.Remove(fileKey); continue; } } } string key; int loopEnd; int loopStart; string searchPattern; string equipmentType; string rootDirectoryPath; bool isDuplicatorDirectory; if (isEAFLog && isEDA && isAPC) throw new Exception(); else if (!isEAFLog && !isEDA && !isAPC) throw new Exception(); else if (isEAFLog) { loopEnd = 4; loopStart = 1; searchPattern = "*.ipdsf"; rootDirectoryPath = string.Concat(@"\\", server, @"\EC_EAFLog"); } else if (isEDA) { loopEnd = 6; loopStart = 2; searchPattern = "*.pdsf"; rootDirectoryPath = string.Concat(@"\\", server, @"\EC_EDA"); } else if (isAPC) { loopEnd = 7; loopStart = 6; searchPattern = "*.pdsf"; rootDirectoryPath = string.Concat(@"\\", server, @"\EC_APC"); } else throw new Exception(); string[] rootDirectories = Directory.GetDirectories(rootDirectoryPath, "*", SearchOption.TopDirectoryOnly); foreach (string rootDirectory in rootDirectories) { if (_ShuttingDown) break; if (rootDirectory.Contains(@"\DEV")) continue; checkDirectory = string.Concat(rootDirectory, @"\Traces"); if (!Directory.Exists(checkDirectory)) continue; equipmentTypeDirectories = Directory.GetDirectories(checkDirectory, "*", SearchOption.TopDirectoryOnly); foreach (string equipmentTypeDirectory in equipmentTypeDirectories) { if (_ShuttingDown) break; equipmentType = Path.GetFileName(equipmentTypeDirectory); if (!equipmentTypes.Contains(equipmentType)) continue; for (int i = loopStart; i < loopEnd; i++) { if (_ShuttingDown) break; checkDirectory = i switch { 1 => string.Concat(equipmentTypeDirectory, @"\Source"), 2 => string.Concat(equipmentTypeDirectory, @"\Error"), 3 => string.Concat(equipmentTypeDirectory, @"\Target"), 4 => string.Concat(equipmentTypeDirectory, @"\BadPath"), 5 => string.Concat(equipmentTypeDirectory, @"\PollPath"), 6 => string.Concat(equipmentTypeDirectory, @"\ViewerPath"), _ => throw new Exception(), }; if (!Directory.Exists(checkDirectory)) continue; pdsfFiles = Directory.GetFiles(checkDirectory, searchPattern, SearchOption.AllDirectories); if (isAPC && !pdsfFiles.Any() && i == 6) { pdsfFileCollection.Clear(); cellInstanceDirectories = Directory.GetDirectories(checkDirectory, "*", SearchOption.TopDirectoryOnly); foreach (string cellInstanceDirectory in cellInstanceDirectories) pdsfFileCollection.AddRange(Directory.GetFiles(cellInstanceDirectory, searchPattern, SearchOption.TopDirectoryOnly)); pdsfFiles = pdsfFileCollection.ToArray(); } foreach (string pdsfFile in pdsfFiles) { if (_ShuttingDown) break; creationTime = new FileInfo(pdsfFile).CreationTime; lastWriteTime = new FileInfo(pdsfFile).LastWriteTime; isDuplicatorDirectory = Path.GetDirectoryName(pdsfFile) != checkDirectory && Path.GetFileName(Path.GetDirectoryName(Path.GetDirectoryName(pdsfFile))) == equipmentType; if (creationTime < lastWriteTime) dateTime = creationTime; else dateTime = lastWriteTime; if (dateTime.Ticks < ticks) { if (inputLogistics.ContainsKey(pdsfFile)) { inputLogistics.Remove(pdsfFile); continue; } } else if (isDuplicatorDirectory) continue; else if (checkDirectory.Contains("PollPath") && dateTime.Ticks > pollPathTicks) continue; else if (!inputLogistics.ContainsKey(pdsfFile)) { pdsf = ProcessDataStandardFormat.GetLogisticsColumnsAndBody(pdsfFile); Logistics logistics = new Logistics(pdsfFile, pdsf.Item1); if (!logistics.Logistics2.Any()) logistics.Logistics2.Add(new Logistics2(string.Empty)); key = Path.GetFileName(pdsfFile); if (!inputPDSFFiles.ContainsKey(key)) inputPDSFFiles.Add(key, pdsfFile); else { foreach (KeyValuePair element in inputLogistics) { if (Path.GetFileName(element.Key) == key) { if (!File.Exists(element.Key)) inputLogistics.Remove(element.Key); } } } inputLogistics.Add(pdsfFile, logistics); } } } } } } } }