using Adaptation.Eaf.Management.ConfigurationData.CellAutomation; using Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration; using Adaptation.Shared; using Adaptation.Shared.Duplicator; using Adaptation.Shared.Methods; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Net.Http; using System.Text.Json; using System.Threading; using System.Threading.Tasks; namespace Adaptation.FileHandlers.DownloadTXTFile; public class FileRead : Shared.FileRead, IFileRead { private readonly Timer _Timer; private readonly HttpClient _HttpClient; private readonly string _StaticFileServer; public FileRead(ISMTP smtp, Dictionary fileParameter, string cellInstanceName, int? connectionCount, string cellInstanceConnectionName, FileConnectorConfiguration fileConnectorConfiguration, string equipmentTypeName, string parameterizedModelObjectDefinitionType, IList modelObjectParameters, string equipmentDictionaryName, Dictionary> dummyRuns, Dictionary> staticRuns, bool useCyclicalForDescription, bool isEAFHosted) : base(new Description(), false, smtp, fileParameter, cellInstanceName, connectionCount, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, dummyRuns, staticRuns, useCyclicalForDescription, isEAFHosted: connectionCount is null) { _MinFileLength = 10; _NullData = string.Empty; _Logistics = new(this); if (_FileParameter is null) throw new Exception(cellInstanceConnectionName); if (_ModelObjectParameterDefinitions is null) throw new Exception(cellInstanceConnectionName); if (_IsDuplicator) throw new Exception(cellInstanceConnectionName); _HttpClient = new(); _StaticFileServer = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, string.Concat("CellInstance.", cellInstanceName, ".StaticFileServer")); if (_IsEAFHosted) NestExistingFiles(_FileConnectorConfiguration); if (!Debugger.IsAttached && fileConnectorConfiguration.PreProcessingMode != FileConnectorConfiguration.PreProcessingModeEnum.Process) _Timer = new Timer(Callback, null, (int)(fileConnectorConfiguration.FileScanningIntervalInSeconds * 1000), Timeout.Infinite); else { _Timer = new Timer(Callback, null, Timeout.Infinite, Timeout.Infinite); Callback(null); } } void IFileRead.Move(Tuple> extractResults, Exception exception) => Move(extractResults); void IFileRead.WaitForThread() => WaitForThread(thread: null, threadExceptions: null); string IFileRead.GetEventDescription() { string result = _Description.GetEventDescription(); return result; } List IFileRead.GetHeaderNames() { List results = _Description.GetHeaderNames(); return results; } string[] IFileRead.Move(Tuple> extractResults, string to, string from, string resolvedFileLocation, Exception exception) { string[] results = Move(extractResults, to, from, resolvedFileLocation, exception); return results; } JsonProperty[] IFileRead.GetDefault() { JsonProperty[] results = _Description.GetDefault(this, _Logistics); return results; } Dictionary IFileRead.GetDisplayNamesJsonElement() { Dictionary results = _Description.GetDisplayNamesJsonElement(this); return results; } List IFileRead.GetDescriptions(IFileRead fileRead, List tests, IProcessData processData) { List results = _Description.GetDescriptions(fileRead, _Logistics, tests, processData); return results; } Tuple> IFileRead.GetExtractResult(string reportFullPath, string eventName) { Tuple> results; if (string.IsNullOrEmpty(eventName)) throw new Exception(); _ReportFullPath = reportFullPath; DateTime dateTime = DateTime.Now; results = GetExtractResult(reportFullPath, dateTime); if (results.Item3 is null) results = new Tuple>(results.Item1, Array.Empty(), JsonSerializer.Deserialize("[]"), results.Item4); if (results.Item3.Length > 0 && _IsEAFHosted) WritePDSF(this, results.Item3); UpdateLastTicksDuration(DateTime.Now.Ticks - dateTime.Ticks); return results; } Tuple> IFileRead.ReExtract() { Tuple> results; List headerNames = _Description.GetHeaderNames(); Dictionary keyValuePairs = _Description.GetDisplayNamesJsonElement(this); results = ReExtract(this, headerNames, keyValuePairs); return results; } private Tuple> GetExtractResult(string reportFullPath, DateTime dateTime) => throw new Exception(string.Concat("See ", nameof(Callback))); private static DateTime GetFileAgeThresholdDateTime(string fileAgeThreshold) { DateTime result = DateTime.Now; string[] segments = fileAgeThreshold.Split(':'); for (int i = 0; i < segments.Length; i++) { result = i switch { 0 => result.AddDays(double.Parse(segments[i]) * -1), 1 => result.AddHours(double.Parse(segments[i]) * -1), 2 => result.AddMinutes(double.Parse(segments[i]) * -1), 3 => result.AddSeconds(double.Parse(segments[i]) * -1), _ => throw new Exception(), }; } return result; } private static string[] GetValidWeeks(DateTime fileAgeThresholdDateTime) { 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 lastWeekOfYear = $"{fileAgeThresholdDateTime:yyyy}_Week_{calendar.GetWeekOfYear(fileAgeThresholdDateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}"; return new string[] { weekOfYear, lastWeekOfYear }.Distinct().ToArray(); } private static string[] GetValidDays(DateTime fileAgeThresholdDateTime) { DateTime dateTime = DateTime.Now; return new string[] { dateTime.ToString("yyyy-MM-dd"), fileAgeThresholdDateTime.ToString("yyyy-MM-dd") }.Distinct().ToArray(); } private ReadOnlyCollection GetDayNginxFileSystemCollection(DateTime fileAgeThresholdDateTime, string week, string day, string dayUrl, NginxFileSystem[] dayNginxFileSystemCollection) { List results = new(); DateTime dateTime; string nginxFormat = "ddd, dd MMM yyyy HH:mm:ss zzz"; foreach (NginxFileSystem dayNginxFileSystem in dayNginxFileSystemCollection) { if (!DateTime.TryParseExact(dayNginxFileSystem.MTime.Replace("GMT", "+00:00"), nginxFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) continue; if (dateTime < fileAgeThresholdDateTime) continue; results.Add(new( Path.GetFullPath(Path.Combine(_FileConnectorConfiguration.TargetFileLocation, week, day, dayNginxFileSystem.Name)), string.Concat(dayUrl, '/', dayNginxFileSystem.Name), dateTime.ToString(), dayNginxFileSystem.Size)); } return results.AsReadOnly(); } private ReadOnlyCollection GetDayNginxFileSystemCollection(DateTime fileAgeThresholdDateTime) { #nullable enable List results = new(); string dayUrl; string dayJson; string weekJson; string checkWeek; Task task; NginxFileSystem[]? dayNginxFileSystemCollection; NginxFileSystem[]? weekNginxFileSystemCollection; string[] days = GetValidDays(fileAgeThresholdDateTime); string[] weeks = GetValidWeeks(fileAgeThresholdDateTime); JsonSerializerOptions propertyNameCaseInsensitiveJsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; foreach (string week in weeks) { checkWeek = string.Concat("http://", _StaticFileServer, '/', week); task = _HttpClient.GetAsync(checkWeek); task.Wait(); if (!task.Result.IsSuccessStatusCode) continue; weekJson = _HttpClient.GetStringAsync(checkWeek).Result; weekNginxFileSystemCollection = JsonSerializer.Deserialize(weekJson, propertyNameCaseInsensitiveJsonSerializerOptions); if (weekNginxFileSystemCollection is null) continue; foreach (NginxFileSystem weekNginxFileSystem in weekNginxFileSystemCollection) { if (!(from l in days where weekNginxFileSystem.Name == l select false).Any()) continue; dayUrl = string.Concat(checkWeek, '/', weekNginxFileSystem.Name); dayJson = _HttpClient.GetStringAsync(dayUrl).Result; dayNginxFileSystemCollection = JsonSerializer.Deserialize(dayJson, propertyNameCaseInsensitiveJsonSerializerOptions); if (dayNginxFileSystemCollection is null) continue; results.AddRange(GetDayNginxFileSystemCollection(fileAgeThresholdDateTime, week, weekNginxFileSystem.Name, dayUrl, dayNginxFileSystemCollection)); } } return results.AsReadOnly(); #nullable disable } private ReadOnlyCollection> GetPossible() { List> results = new(); string fileName; DateTime dateTime; FileInfo targetFileInfo; FileInfo alternateFileInfo; DateTime fileAgeThresholdDateTime = GetFileAgeThresholdDateTime(_FileConnectorConfiguration.FileAgeThreshold); ReadOnlyCollection dayNginxFileSystemCollection = GetDayNginxFileSystemCollection(fileAgeThresholdDateTime); foreach (NginxFileSystem nginxFileSystem in dayNginxFileSystemCollection) { targetFileInfo = new FileInfo(nginxFileSystem.Name); if (targetFileInfo.Directory is null) continue; if (!Directory.Exists(targetFileInfo.Directory.FullName)) _ = Directory.CreateDirectory(targetFileInfo.Directory.FullName); if (!DateTime.TryParse(nginxFileSystem.MTime, out dateTime)) continue; if (targetFileInfo.Exists && targetFileInfo.LastWriteTime == dateTime) continue; fileName = Path.GetFileName(targetFileInfo.Name); alternateFileInfo = new(Path.Combine(_FileConnectorConfiguration.AlternateTargetFolder, fileName.Split(new string[] { "~" }, StringSplitOptions.None).Last())); results.Add(new(dateTime, targetFileInfo, alternateFileInfo, nginxFileSystem.Type)); } return results.AsReadOnly(); } private void DownloadTXTFileAsync() { #nullable enable if (_HttpClient is null) throw new Exception(); if (string.IsNullOrEmpty(_StaticFileServer)) throw new Exception(); ReadOnlyCollection> possibleDownload = GetPossible(); if (possibleDownload.Count > 0) { string targetFileName = possibleDownload[0].Item4; FileInfo targetFileInfo = possibleDownload[0].Item2; FileInfo alternateFileInfo = possibleDownload[0].Item3; DateTime matchNginxFileSystemDateTime = possibleDownload[0].Item1; if (targetFileInfo.Exists) File.Delete(targetFileInfo.FullName); string targetJson = _HttpClient.GetStringAsync(targetFileName).Result; File.WriteAllText(targetFileInfo.FullName, targetJson); targetFileInfo.LastWriteTime = matchNginxFileSystemDateTime; File.AppendAllText(alternateFileInfo.FullName, targetJson); } #nullable disable } private TimeSpan GetNextTimeSpan() { TimeSpan result; string checkFile = Path.Combine(_FileConnectorConfiguration.AlternateTargetFolder, _FileConnectorConfiguration.SourceFileFilter.Split('|')[0]); if (File.Exists(checkFile)) result = new(DateTime.Now.AddMilliseconds(500).Ticks - DateTime.Now.Ticks); else result = new(DateTime.Now.AddSeconds(_FileConnectorConfiguration.FileScanningIntervalInSeconds.Value).Ticks - DateTime.Now.Ticks); return result; } private void Callback(object state) { try { if (_IsEAFHosted) DownloadTXTFileAsync(); } catch (Exception exception) { string subject = string.Concat("Exception:", _CellInstanceConnectionName); string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace); try { _SMTP.SendHighPriorityEmailMessage(subject, body); } catch (Exception) { } } try { TimeSpan timeSpan = GetNextTimeSpan(); _ = _Timer.Change((long)timeSpan.TotalMilliseconds, Timeout.Infinite); } catch (Exception exception) { string subject = string.Concat("Exception:", _CellInstanceConnectionName); string body = string.Concat(exception.Message, Environment.NewLine, Environment.NewLine, exception.StackTrace); try { _SMTP.SendHighPriorityEmailMessage(subject, body); } catch (Exception) { } } } }