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.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Text.Json; using System.Threading; namespace Adaptation.FileHandlers.DownloadRsMFile { public class FileRead : Shared.FileRead, IFileRead { private readonly Timer _Timer; private readonly WebClient _WebClient; private readonly string _StaticFileServer; public FileRead(ISMTP smtp, Dictionary fileParameter, string cellInstanceName, string cellInstanceConnectionName, FileConnectorConfiguration fileConnectorConfiguration, string equipmentTypeName, string parameterizedModelObjectDefinitionType, IList modelObjectParameters, string equipmentDictionaryName, Dictionary> dummyRuns, bool useCyclicalForDescription, bool isEAFHosted, int hyphenXToArchive, int hyphenIsArchive) : base(new Description(), false, smtp, fileParameter, cellInstanceName, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, dummyRuns, useCyclicalForDescription, isEAFHosted, hyphenXToArchive, hyphenIsArchive) { _MinFileLength = 10; _NullData = string.Empty; _Logistics = new Logistics(this); if (_FileParameter is null) throw new Exception(cellInstanceConnectionName); if (_ModelObjectParameterDefinitions is null) throw new Exception(cellInstanceConnectionName); if (_IsDuplicator) throw new Exception(cellInstanceConnectionName); _WebClient = new WebClient(); _StaticFileServer = GetPropertyValue(cellInstanceConnectionName, modelObjectParameters, string.Concat("CellInstance.", cellInstanceName, ".StaticFileServer")); 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(this, extractResults, exception); } 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, new Test[] { }, 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; } void IFileRead.CheckTests(Test[] tests, bool extra) { throw new Exception(string.Concat("Not ", nameof(_IsDuplicator))); } void IFileRead.MoveArchive() { throw new Exception(string.Concat("Not ", nameof(_IsDuplicator))); } void IFileRead.Callback(object state) { Callback(state); } private Tuple> GetExtractResult(string reportFullPath, DateTime dateTime) { if (reportFullPath is null) { } if (dateTime == DateTime.MinValue) { } throw new Exception(string.Concat("See ", nameof(Callback))); } private void DownloadRsMFile() { if (_WebClient is null) throw new Exception(); if (string.IsNullOrEmpty(_StaticFileServer)) throw new Exception(); string logText; string runJson; string rootJson; string[] logLines; string runFileName; string[] logSegments; string targetFileName; string runFullFileName; FileInfo targetFileInfo; FileInfo alternateFileInfo; List runFullFileNameSegments; string dateTimeFormat = "yy/MM/dd HH:mm:ss"; NginxFileSystem[] runNginxFileSystemCollection; NginxFileSystem[] rootNginxFileSystemCollection; DateTime fileAgeThresholdDateTime = DateTime.Now; string nginxFormat = "ddd, dd MMM yyyy HH:mm:ss zzz"; List> possibleDownload = new(); string[] segments = _FileConnectorConfiguration.FileAgeThreshold.Split(':'); JsonSerializerOptions propertyNameCaseInsensitiveJsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; for (int i = 0; i < segments.Length; i++) { fileAgeThresholdDateTime = i switch { 0 => fileAgeThresholdDateTime.AddDays(double.Parse(segments[i]) * -1), 1 => fileAgeThresholdDateTime.AddHours(double.Parse(segments[i]) * -1), 2 => fileAgeThresholdDateTime.AddMinutes(double.Parse(segments[i]) * -1), 3 => fileAgeThresholdDateTime.AddSeconds(double.Parse(segments[i]) * -1), _ => throw new Exception(), }; } rootJson = _WebClient.DownloadString(string.Concat("http://", _StaticFileServer)); rootNginxFileSystemCollection = JsonSerializer.Deserialize(rootJson, propertyNameCaseInsensitiveJsonSerializerOptions); foreach (NginxFileSystem rootNginxFileSystem in rootNginxFileSystemCollection) { if (!(from l in _FileConnectorConfiguration.SourceFileFilters where rootNginxFileSystem.Name == l select false).Any()) continue; logText = _WebClient.DownloadString(string.Concat("http://", _StaticFileServer, '/', rootNginxFileSystem.Name)); logLines = logText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); foreach (string logLine in logLines) { if (string.IsNullOrEmpty(logLine)) continue; logSegments = logLine.Split('<'); if (logSegments.Length < 1 || !DateTime.TryParseExact(logSegments[0].Trim(), dateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime tvsDateTime)) continue; if (tvsDateTime < fileAgeThresholdDateTime) continue; if (logSegments.Length < 2) continue; runFullFileName = logSegments[1].Split('>')[0]; if (!(from l in _FileConnectorConfiguration.SourceFileFilters where runFullFileName.EndsWith(l) select false).Any()) continue; runFullFileNameSegments = runFullFileName.Split('\\').ToList(); runFileName = runFullFileNameSegments[runFullFileNameSegments.Count - 1]; runFullFileNameSegments.RemoveAt(runFullFileNameSegments.Count - 1); runJson = _WebClient.DownloadString(string.Concat("http://", _StaticFileServer, '/', string.Join("/", runFullFileNameSegments))); runFullFileNameSegments.Add(runFileName); runNginxFileSystemCollection = JsonSerializer.Deserialize(runJson, propertyNameCaseInsensitiveJsonSerializerOptions); foreach (NginxFileSystem matchNginxFileSystem in runNginxFileSystemCollection) { if (matchNginxFileSystem.Name != runFileName) continue; if (!(from l in _FileConnectorConfiguration.SourceFileFilters where matchNginxFileSystem.Name.EndsWith(l) select false).Any()) continue; if (!DateTime.TryParseExact(matchNginxFileSystem.MTime.Replace("GMT", "+00:00"), nginxFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime matchNginxFileSystemDateTime)) continue; if (matchNginxFileSystemDateTime < fileAgeThresholdDateTime) continue; targetFileInfo = new FileInfo(Path.Combine(_FileConnectorConfiguration.TargetFileLocation, runFullFileName)); if (!Directory.Exists(targetFileInfo.Directory.FullName)) Directory.CreateDirectory(targetFileInfo.Directory.FullName); if (targetFileInfo.Exists && targetFileInfo.LastWriteTime == matchNginxFileSystemDateTime) continue; alternateFileInfo = new(Path.Combine(_FileConnectorConfiguration.AlternateTargetFolder, matchNginxFileSystem.Name)); targetFileName = string.Concat("http://", _StaticFileServer, '/', string.Join("/", runFullFileNameSegments)); possibleDownload.Add(new(matchNginxFileSystemDateTime, targetFileInfo, alternateFileInfo, targetFileName)); break; } if (possibleDownload.Any()) break; } if (possibleDownload.Any()) break; } if (possibleDownload.Any()) { possibleDownload = (from l in possibleDownload orderby l.Item1 select l).ToList(); alternateFileInfo = possibleDownload[0].Item3; targetFileName = possibleDownload[0].Item4; targetFileInfo = possibleDownload[0].Item2; DateTime matchNginxFileSystemDateTime = possibleDownload[0].Item1; if (alternateFileInfo.Exists) File.Delete(alternateFileInfo.FullName); if (targetFileInfo.Exists) File.Delete(targetFileInfo.FullName); _WebClient.DownloadFile(targetFileName, targetFileInfo.FullName); targetFileInfo.LastWriteTime = matchNginxFileSystemDateTime; File.Copy(targetFileInfo.FullName, alternateFileInfo.FullName); } } private void Callback(object state) { try { if (_IsEAFHosted) DownloadRsMFile(); } 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 = new(DateTime.Now.AddSeconds(_FileConnectorConfiguration.FileScanningIntervalInSeconds.Value).Ticks - DateTime.Now.Ticks); _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) { } } } } }