net6.0 to net7.0 Infineon.EAF.Runtime 2.49.0 MesEntity Placeholder Not Tested connectionCount AssemblyVersion SRP2100 = 53, //Largest nuget server name v2.49.2 IDescription.GetDescriptions with body Viewer support tasks.json kanbn initialize WSRequest alignment Back to x64 mesfs.infineon.com Infineon.EAF.Runtime 2.49.3 pool name Kanban net8.0 v2_52_0-Tests editorconfig yml ec fix yml explicit contents dotnet_diagnostic CA1862 and GetWeekOfYear for WritePDSF gitignore cellInstanceVersion.EdaConnection.PortNumber Delete 2.52.3 Added Climatec to Test.cs GetJobIdDirectory Remove and 5-Other-Small
348 lines
16 KiB
C#
348 lines
16 KiB
C#
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.IO.Compression;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
|
|
namespace Adaptation.FileHandlers.ZipFilesByDate;
|
|
|
|
public class FileRead : Shared.FileRead, IFileRead
|
|
{
|
|
|
|
private readonly Timer _Timer;
|
|
|
|
public FileRead(ISMTP smtp, Dictionary<string, string> fileParameter, string cellInstanceName, int? connectionCount, string cellInstanceConnectionName, FileConnectorConfiguration fileConnectorConfiguration, string equipmentTypeName, string parameterizedModelObjectDefinitionType, IList<ModelObjectParameterDefinition> modelObjectParameters, string equipmentDictionaryName, Dictionary<string, List<long>> dummyRuns, Dictionary<long, List<string>> 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);
|
|
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<string, Test[], JsonElement[], List<FileInfo>> extractResults, Exception exception)
|
|
{
|
|
bool isErrorFile = exception is not null;
|
|
if (!isErrorFile && !string.IsNullOrEmpty(_Logistics.ReportFullPath))
|
|
{
|
|
FileInfo fileInfo = new(_Logistics.ReportFullPath);
|
|
if (fileInfo.Exists && fileInfo.LastWriteTime < fileInfo.CreationTime)
|
|
File.SetLastWriteTime(_Logistics.ReportFullPath, fileInfo.CreationTime);
|
|
}
|
|
Move(extractResults);
|
|
}
|
|
|
|
void IFileRead.WaitForThread() => WaitForThread(thread: null, threadExceptions: null);
|
|
|
|
string IFileRead.GetEventDescription()
|
|
{
|
|
string result = _Description.GetEventDescription();
|
|
return result;
|
|
}
|
|
|
|
List<string> IFileRead.GetHeaderNames()
|
|
{
|
|
List<string> results = _Description.GetHeaderNames();
|
|
return results;
|
|
}
|
|
|
|
string[] IFileRead.Move(Tuple<string, Test[], JsonElement[], List<FileInfo>> 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<string, string> IFileRead.GetDisplayNamesJsonElement()
|
|
{
|
|
Dictionary<string, string> results = _Description.GetDisplayNamesJsonElement(this);
|
|
return results;
|
|
}
|
|
|
|
List<IDescription> IFileRead.GetDescriptions(IFileRead fileRead, List<Test> tests, IProcessData processData)
|
|
{
|
|
List<IDescription> results = _Description.GetDescriptions(fileRead, _Logistics, tests, processData);
|
|
return results;
|
|
}
|
|
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.GetExtractResult(string reportFullPath, string eventName)
|
|
{
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> 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<string, Test[], JsonElement[], List<FileInfo>>(results.Item1, Array.Empty<Test>(), JsonSerializer.Deserialize<JsonElement[]>("[]"), results.Item4);
|
|
if (results.Item3.Length > 0 && _IsEAFHosted)
|
|
WritePDSF(this, results.Item3);
|
|
UpdateLastTicksDuration(DateTime.Now.Ticks - dateTime.Ticks);
|
|
return results;
|
|
}
|
|
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> IFileRead.ReExtract()
|
|
{
|
|
Tuple<string, Test[], JsonElement[], List<FileInfo>> results;
|
|
List<string> headerNames = _Description.GetHeaderNames();
|
|
Dictionary<string, string> keyValuePairs = _Description.GetDisplayNamesJsonElement(this);
|
|
results = ReExtract(this, headerNames, keyValuePairs);
|
|
return results;
|
|
}
|
|
|
|
private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime) => throw new Exception(string.Concat("See ", nameof(Callback)));
|
|
|
|
private static bool DeleteEmptyDirectories(string rootDirectory)
|
|
{
|
|
bool result;
|
|
if (!Directory.Exists(rootDirectory))
|
|
result = false;
|
|
else
|
|
{
|
|
string[] files;
|
|
string[] directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
if (directories.Length > 0)
|
|
files = Array.Empty<string>();
|
|
else
|
|
files = Directory.GetFiles(rootDirectory, "*", SearchOption.AllDirectories);
|
|
if (directories.Length == 0 && files.Length == 0)
|
|
{
|
|
result = true;
|
|
Directory.Delete(rootDirectory);
|
|
}
|
|
else
|
|
{
|
|
result = false;
|
|
foreach (string directory in directories)
|
|
{
|
|
result = DeleteEmptyDirectories(directory);
|
|
if (result)
|
|
result = DeleteEmptyDirectories(directory);
|
|
}
|
|
if (files is null)
|
|
{
|
|
directories = Directory.GetDirectories(rootDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
result = directories.Length == 0;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void ZipFilesByDate(string sourceDirectory, SearchOption searchOption, string dayFormat)
|
|
{
|
|
#nullable enable
|
|
string key;
|
|
bool addFile;
|
|
string? zipPath;
|
|
string fileName;
|
|
string[] segments;
|
|
string[] subFiles;
|
|
string weekOfYear;
|
|
FileInfo fileInfo;
|
|
string zipDirectory;
|
|
DateTime creationTime;
|
|
string? directoryName;
|
|
DateTime lastWriteTime;
|
|
DateTime nowDateTime = DateTime.Now;
|
|
Regex regex = new("[a-zA-Z0-9]{1,}");
|
|
DateTime dateTime = DateTime.MinValue;
|
|
DateTime firstEmail = new(2019, 3, 8);
|
|
CultureInfo cultureInfo = new("en-US");
|
|
Calendar calendar = cultureInfo.Calendar;
|
|
Dictionary<string, DateTime> weeks = new();
|
|
int ticksLength = nowDateTime.AddDays(-6).Ticks.ToString().Length;
|
|
for (int i = 0; i < int.MaxValue; i++)
|
|
{
|
|
dateTime = firstEmail.AddDays(i);
|
|
if (dateTime > nowDateTime)
|
|
break;
|
|
weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
key = string.Concat(dateTime.ToString("yyyy"), "_Week_", weekOfYear);
|
|
if (!weeks.ContainsKey(key))
|
|
weeks.Add(key, dateTime);
|
|
}
|
|
weekOfYear = calendar.GetWeekOfYear(nowDateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
string skipKey = string.Concat(nowDateTime.ToString("yyyy"), "_Week_", weekOfYear);
|
|
Dictionary<string, List<string>> keyValuePairs = new();
|
|
string[] topDirectories = Directory.GetDirectories(sourceDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
if (topDirectories.Length == 0)
|
|
topDirectories = new string[] { sourceDirectory };
|
|
foreach (string topDirectory in topDirectories)
|
|
{
|
|
keyValuePairs.Clear();
|
|
directoryName = Path.GetDirectoryName(topDirectory);
|
|
subFiles = Directory.GetFiles(topDirectory, "*", searchOption);
|
|
zipPath = string.IsNullOrEmpty(directoryName) ? null : Path.Combine(directoryName, "ZipPath");
|
|
zipDirectory = zipPath is not null && Directory.Exists(zipPath) ? zipPath : topDirectory;
|
|
foreach (string subFile in subFiles)
|
|
{
|
|
addFile = false;
|
|
if (subFile.EndsWith(".zip"))
|
|
continue;
|
|
fileName = Path.GetFileName(subFile);
|
|
fileInfo = new FileInfo(subFile);
|
|
creationTime = fileInfo.CreationTime;
|
|
if (creationTime > dateTime)
|
|
continue;
|
|
lastWriteTime = fileInfo.LastWriteTime;
|
|
if (fileName.Contains(lastWriteTime.ToString("yyyyMMdd")) || fileName.Contains(lastWriteTime.ToString("yyyy-MM-dd")) ||
|
|
fileName.Contains(creationTime.ToString("yyyyMMdd")) || fileName.Contains(creationTime.ToString("yyyy-MM-dd")) ||
|
|
fileName.Contains(lastWriteTime.ToString("yyMMdd")) || fileName.Contains(lastWriteTime.ToString("yy-MM-dd")) ||
|
|
fileName.Contains(creationTime.ToString("yyMMdd")) || fileName.Contains(creationTime.ToString("yy-MM-dd")) ||
|
|
fileName.Contains(lastWriteTime.AddDays(-1).ToString("yyyyMMdd")) || fileName.Contains(lastWriteTime.AddDays(-1).ToString("yyyy-MM-dd")) ||
|
|
fileName.Contains(creationTime.AddDays(-1).ToString("yyyyMMdd")) || fileName.Contains(creationTime.AddDays(-1).ToString("yyyy-MM-dd")) ||
|
|
fileName.Contains(lastWriteTime.AddDays(-1).ToString("yyMMdd")) || fileName.Contains(lastWriteTime.AddDays(-1).ToString("yy-MM-dd")) ||
|
|
fileName.Contains(creationTime.AddDays(-1).ToString("yyMMdd")) || fileName.Contains(creationTime.AddDays(-1).ToString("yy-MM-dd")))
|
|
addFile = true;
|
|
if (!addFile && fileName.Length > ticksLength)
|
|
{
|
|
MatchCollection matches = regex.Matches(fileName);
|
|
foreach (Match match in matches.Cast<Match>())
|
|
{
|
|
if (match.Value.Length != ticksLength)
|
|
continue;
|
|
if (!long.TryParse(match.Value, out long ticks))
|
|
continue;
|
|
addFile = true;
|
|
break;
|
|
}
|
|
if (addFile)
|
|
break;
|
|
}
|
|
if (addFile)
|
|
{
|
|
weekOfYear = calendar.GetWeekOfYear(lastWriteTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
|
if (string.IsNullOrEmpty(dayFormat))
|
|
key = string.Concat(lastWriteTime.ToString("yyyy"), "_Week_", weekOfYear);
|
|
else
|
|
key = string.Concat(lastWriteTime.ToString("yyyy"), "_Week_", weekOfYear, "_", lastWriteTime.ToString(dayFormat));
|
|
if (key == skipKey)
|
|
continue;
|
|
if (!keyValuePairs.ContainsKey(key))
|
|
keyValuePairs.Add(key, new List<string>());
|
|
keyValuePairs[key].Add(subFile);
|
|
}
|
|
}
|
|
foreach (KeyValuePair<string, List<string>> element in keyValuePairs)
|
|
{
|
|
key = Path.Combine(zipDirectory, $"{element.Key}.zip");
|
|
if (File.Exists(key))
|
|
{
|
|
for (short i = 101; i < short.MaxValue; i++)
|
|
{
|
|
key = Path.Combine(zipDirectory, $"{element.Key}_{i}.zip");
|
|
if (!File.Exists(key))
|
|
break;
|
|
}
|
|
}
|
|
using ZipArchive zip = ZipFile.Open(key, ZipArchiveMode.Create);
|
|
foreach (string file in element.Value)
|
|
{
|
|
_ = zip.CreateEntryFromFile(file, Path.GetFileName(file));
|
|
File.Delete(file);
|
|
}
|
|
if (zipPath is not null && Directory.Exists(zipPath) && !string.IsNullOrEmpty(directoryName))
|
|
{
|
|
try
|
|
{ Directory.SetLastWriteTime(directoryName, DateTime.Now); }
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
subFiles = Directory.GetFiles(zipDirectory, "*.zip", SearchOption.TopDirectoryOnly);
|
|
foreach (string subFile in subFiles)
|
|
{
|
|
fileName = Path.GetFileNameWithoutExtension(subFile);
|
|
segments = fileName.Split('_');
|
|
if (segments.Length > 2)
|
|
fileName = string.Concat(segments[0], '_', segments[1], '_', segments[2]);
|
|
if (weeks.TryGetValue(fileName, out DateTime value))
|
|
{
|
|
try
|
|
{ File.SetLastWriteTime(subFile, value); }
|
|
catch (Exception) { }
|
|
}
|
|
}
|
|
if (topDirectory != sourceDirectory)
|
|
{
|
|
try
|
|
{ _ = DeleteEmptyDirectories(topDirectory); }
|
|
catch (Exception) { }
|
|
}
|
|
_Log.Info(topDirectory);
|
|
}
|
|
#nullable disable
|
|
}
|
|
|
|
private void ZipFilesByDate()
|
|
{
|
|
string checkDirectory;
|
|
string dayFormat = string.Empty;
|
|
string[] equipmentDirectories = Directory.GetDirectories(_FileConnectorConfiguration.SourceFileLocation, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (string equipmentDirectory in equipmentDirectories)
|
|
{
|
|
checkDirectory = Path.Combine(equipmentDirectory, "PollPath");
|
|
if (!Directory.Exists(checkDirectory))
|
|
continue;
|
|
if (_IsEAFHosted)
|
|
ZipFilesByDate(checkDirectory, SearchOption.TopDirectoryOnly, dayFormat);
|
|
}
|
|
}
|
|
|
|
private void Callback(object state)
|
|
{
|
|
try
|
|
{
|
|
ZipFilesByDate();
|
|
}
|
|
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) { }
|
|
}
|
|
}
|
|
|
|
} |