diff --git a/Adaptation/Shared/Metrology/WS.cs b/Adaptation/Shared/Metrology/WS.cs index ff666ba..11bbccf 100644 --- a/Adaptation/Shared/Metrology/WS.cs +++ b/Adaptation/Shared/Metrology/WS.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net.Http; +using System.Linq; using System.Text; using System.Text.Json; @@ -9,8 +10,6 @@ namespace Adaptation.Shared.Metrology; public partial class WS { -#pragma warning disable CA1847 - public static Tuple SendData(string url, object payload, int timeoutSeconds = 120) { Results results = new(); @@ -18,7 +17,7 @@ public partial class WS try { string json = JsonSerializer.Serialize(payload, payload.GetType()); - if (string.IsNullOrEmpty(url) || !url.Contains(":") || !url.Contains(".")) + if (string.IsNullOrEmpty(url) || !url.Contains(':') || !url.Contains('.')) throw new Exception("Invalid URL"); using (HttpClient httpClient = new()) { diff --git a/FileHandlers/FileRead.cs b/FileHandlers/FileRead.cs index bc7f46b..fa76069 100644 --- a/FileHandlers/FileRead.cs +++ b/FileHandlers/FileRead.cs @@ -24,333 +24,327 @@ using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; -namespace MET08RESIMAPCDE.FileHandlers +namespace MET08RESIMAPCDE.FileHandlers; + +public partial class FileRead : FileReaderHandler, ISMTP { - public partial class FileRead : FileReaderHandler, ISMTP + private readonly ILog _Log; + private IFileRead _FileRead; + private EquipmentEvent _EquipmentEvent; + private readonly bool _UseCyclicalForDescription; + private FilePathGenerator _FilePathGeneratorForError; + private FilePathGenerator _FilePathGeneratorForTarget; + private readonly List _EquipmentParameters; + private static readonly Dictionary> _DummyRuns; + + static FileRead() => _DummyRuns = new Dictionary>(); + + public FileRead() { + if (FileParameter is null) + FileParameter = new Dictionary(); + _FileRead = null; + _UseCyclicalForDescription = false; + _Log = LogManager.GetLogger(typeof(FileRead)); + _EquipmentParameters = new List(); + } - private readonly ILog _Log; - private IFileRead _FileRead; - private EquipmentEvent _EquipmentEvent; - private readonly bool _UseCyclicalForDescription; - private FilePathGenerator _FilePathGeneratorForError; - private FilePathGenerator _FilePathGeneratorForTarget; - private readonly List _EquipmentParameters; - private static readonly Dictionary> _DummyRuns; - - static FileRead() + public override void CreateSelfDescription() + { + MethodBase methodBase = new StackFrame().GetMethod(); + _Log.Debug(string.Concat(methodBase.Name, " - Entry")); + try { - _DummyRuns = new Dictionary>(); - } - - public FileRead() - { - if (FileParameter is null) - FileParameter = new Dictionary(); - _FileRead = null; - _UseCyclicalForDescription = false; - _Log = LogManager.GetLogger(typeof(FileRead)); - _EquipmentParameters = new List(); - } - - public override void CreateSelfDescription() - { - MethodBase methodBase = new StackFrame().GetMethod(); - _Log.Debug(string.Concat(methodBase.Name, " - Entry")); - try - { - if (Equipment is null) - throw new Exception(); - CreateSelfDescription(methodBase); - _Log.Debug(string.Concat(methodBase.Name, " - Try - Exit")); - } - catch (Exception ex) - { - _Log.Error(string.Concat(methodBase.Name, " - Catch - Entry {", ex.Message, "}", Environment.NewLine, Environment.NewLine, ex.StackTrace)); - long breakAfter = DateTime.Now.AddSeconds(30).Ticks; - for (short i = 0; i < short.MaxValue; i++) - { - if (DateTime.Now.Ticks > breakAfter) - break; - Thread.Sleep(500); - } - throw; - } - } - - public override bool Extract(string reportFullPath, string eventName) - { - MethodBase methodBase = new StackFrame().GetMethod(); - _Log.Debug(string.Concat(methodBase.Name, " - Entry - {", reportFullPath, "}")); - Tuple> extractResults = null; - try - { - extractResults = _FileRead.GetExtractResult(reportFullPath, eventName); - TriggerEvents(extractResults); - _FileRead.Move(extractResults); - FilePathGeneratorInfoMove(extractResults); - _FileRead.WaitForThread(); - _Log.Debug(string.Concat(methodBase.Name, " - Try - Exit")); - return true; - } - catch (Exception ex) - { - _Log.Error(string.Concat(methodBase.Name, " - Catch - Entry {", ex.Message, "}", Environment.NewLine, Environment.NewLine, ex.StackTrace)); - _FileRead.Move(extractResults, exception: ex); - FilePathGeneratorInfoMove(extractResults, exception: ex); - return false; - } - } - - private void Send(EmailMessage emailMessage) - { - ISmtp smtp = Backbone.Instance.GetBackboneComponentsOfType().SingleOrDefault(); - if (!(smtp is null)) - smtp.Send(emailMessage); - } - - void ISMTP.SendLowPriorityEmailMessage(string subject, string body) - { - EmailMessage emailMessage = new(subject, body, MailPriority.Low); - Send(emailMessage); - } - - void ISMTP.SendHighPriorityEmailMessage(string subject, string body) - { - EmailMessage emailMessage = new(subject, body, MailPriority.High); - Send(emailMessage); - } - - void ISMTP.SendNormalPriorityEmailMessage(string subject, string body) - { - EmailMessage emailMessage = new(subject, body, MailPriority.Normal); - Send(emailMessage); - } - - private void AddParameterRangeAndEvent() - { - string description; - bool allowNull = false; - EquipmentParameter equipmentParameter; - JsonProperty[] jsonProperties = _FileRead.GetDefault(); - Dictionary keyValuePairs = _FileRead.GetDisplayNamesJsonElement(); - Dictionary parameterTypeDefinitions = new(); - FileConnectorParameterTypeDefinitionProvider fileConnectorParameterTypeDefinitionProvider = new(); - foreach (ParameterTypeDefinition parameterTypeDefinition in fileConnectorParameterTypeDefinitionProvider.GetAllParameterTypeDefinition()) - { - switch (parameterTypeDefinition.Name) - { - case nameof(String): - parameterTypeDefinitions.Add(JsonValueKind.String, parameterTypeDefinition); - break; - case nameof(Double): - parameterTypeDefinitions.Add(JsonValueKind.Number, parameterTypeDefinition); - break; - case nameof(Boolean): - parameterTypeDefinitions.Add(JsonValueKind.True, parameterTypeDefinition); - parameterTypeDefinitions.Add(JsonValueKind.False, parameterTypeDefinition); - break; - default: - break; - } - } - foreach (JsonProperty jsonProperty in jsonProperties) - { - if (jsonProperty.Value.ValueKind == JsonValueKind.Object && !parameterTypeDefinitions.ContainsKey(JsonValueKind.Object)) - { - StructuredType structuredType = new(nameof(StructuredType), string.Empty, new List()); - parameterTypeDefinitions.Add(JsonValueKind.Object, structuredType); - Equipment.SelfDescriptionBuilder.AddParameterTypeDefinition(structuredType); - } - if (!parameterTypeDefinitions.ContainsKey(jsonProperty.Value.ValueKind)) - throw new Exception(string.Concat('{', jsonProperty.Value.ValueKind, "} is not mapped!")); - } - foreach (JsonProperty jsonProperty in jsonProperties) - { - if (jsonProperty.Value.ValueKind == JsonValueKind.Null && !allowNull) - throw new Exception(); - if (jsonProperty.Value.ValueKind != JsonValueKind.String || !keyValuePairs.ContainsKey(jsonProperty.Name)) - description = string.Empty; - else - description = keyValuePairs[jsonProperty.Name].Split('|')[0]; - equipmentParameter = new EquipmentParameter(jsonProperty.Name, parameterTypeDefinitions[jsonProperty.Value.ValueKind], description); - _EquipmentParameters.Add(equipmentParameter); - } - Equipment.SelfDescriptionBuilder.RootEquipmentElementBuilder.AddParameterRange(_EquipmentParameters); - _EquipmentEvent = new EquipmentEvent(_FileRead.EventName, _FileRead.GetEventDescription(), _EquipmentParameters); - Equipment.SelfDescriptionBuilder.RootEquipmentElementBuilder.AddEvent(_EquipmentEvent); - } - - private void CreateSelfDescription(MethodBase methodBase) - { - string cellInstanceName; - string equipmentTypeName = string.Empty; - string equipmentDictionaryName = string.Empty; - EquipmentElement equipmentElement = Equipment.SelfDescriptionBuilder.RootEquipmentElementBuilder.Item; - if (Backbone.Instance?.CellName is null) - cellInstanceName = string.Empty; - else - cellInstanceName = Backbone.Instance.CellName; - string cellInstanceConnectionName = equipmentElement.Name; - FileConnectorConfiguration fileConnectorConfiguration = Mapper.Map(Configuration); - string parameterizedModelObjectDefinitionType = methodBase.DeclaringType.FullName; - IList modelObjectParameters = Mapper.Map(ConfiguredParameters); - _FileRead = CellInstanceConnectionName.Get(this, FileParameter, cellInstanceName, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, _DummyRuns, _UseCyclicalForDescription, isEAFHosted: true); - if (_FileRead.IsEvent) - AddParameterRangeAndEvent(); - } - - private object GetFilePathGeneratorInfo(string reportFullPath, bool isErrorFile) - { - FilePathGeneratorInfo result; - FilePathGenerator filePathGeneratorOriginal; - if (Configuration is null) - result = null; - else - { - if (isErrorFile) - { - if (_FilePathGeneratorForError is null) - _FilePathGeneratorForError = new FilePathGenerator(Configuration, reportFullPath, isErrorFile: true); - filePathGeneratorOriginal = _FilePathGeneratorForError; - } - else - { - if (_FilePathGeneratorForTarget is null) - _FilePathGeneratorForTarget = new FilePathGenerator(Configuration, reportFullPath, isErrorFile: false); - filePathGeneratorOriginal = _FilePathGeneratorForTarget; - } - result = new FilePathGeneratorInfo(filePathGeneratorOriginal, reportFullPath, isErrorFile, FileParameter); - } - return result; - } - - private void FilePathGeneratorInfoMove(Tuple> extractResults, Exception exception = null) - { - bool isErrorFile = !(exception is null); - object filePathGeneratorInfo = GetFilePathGeneratorInfo(_FileRead.ReportFullPath, isErrorFile); - if (!(filePathGeneratorInfo is null) && filePathGeneratorInfo is FilePathGeneratorInfo filePathGenerator) - { - string[] exceptionLines = _FileRead.Move(extractResults, filePathGenerator.To, filePathGenerator.From, filePathGenerator.ResolvedFileLocation, exception); - if (isErrorFile) - { - try - { - StringBuilder stringBuilder = new(); - foreach (string item in exceptionLines) - stringBuilder.Append("<").Append(item).AppendLine(">"); - ISmtp smtp = Backbone.Instance.GetBackboneComponentsOfType().SingleOrDefault(); - EmailMessage emailMessage = new(_FileRead.ExceptionSubject, stringBuilder.ToString(), MailPriority.High); - smtp.Send(emailMessage); - } - catch (Exception) { } - } - } - } - - private List GetParameterValues(List headerNames, JsonElement[] jsonElements, int i) - { - List results = new(); - if (_UseCyclicalForDescription && i > 0) + if (Equipment is null) throw new Exception(); - object value; - List list; - JsonProperty[] jsonProperties = jsonElements[i].EnumerateObject().ToArray(); - if (jsonProperties.Length != _EquipmentParameters.Count) - throw new Exception(); - for (int p = 0; p < jsonProperties.Length; p++) - { - if (!_UseCyclicalForDescription || headerNames.Contains(jsonProperties[p].Name)) - value = jsonProperties[p].Value.ToString(); - else - { - list = new List(); - for (int z = 0; z < jsonElements.Length; z++) - list.Add(new object[] { z, jsonElements[z].GetProperty(jsonProperties[p].Name).ToString() }); - value = list; - } - results.Add(new ParameterValue(_EquipmentParameters[p], value, DateTime.Now)); - } - return results; + CreateSelfDescription(methodBase); + _Log.Debug(string.Concat(methodBase.Name, " - Try - Exit")); } - - private void TriggerEvents(Tuple> extractResults) + catch (Exception ex) { - List headerNames; - List parameters; - if (!_UseCyclicalForDescription) - headerNames = null; - else - headerNames = _FileRead.GetHeaderNames(); - for (int i = 0; i < extractResults.Item3.Length; i++) + _Log.Error(string.Concat(methodBase.Name, " - Catch - Entry {", ex.Message, "}", Environment.NewLine, Environment.NewLine, ex.StackTrace)); + long breakAfter = DateTime.Now.AddSeconds(30).Ticks; + for (short i = 0; i < short.MaxValue; i++) { + if (DateTime.Now.Ticks > breakAfter) + break; + Thread.Sleep(500); + } + throw; + } + } + + public override bool Extract(string reportFullPath, string eventName) + { + MethodBase methodBase = new StackFrame().GetMethod(); + _Log.Debug(string.Concat(methodBase.Name, " - Entry - {", reportFullPath, "}")); + Tuple> extractResults = null; + try + { + extractResults = _FileRead.GetExtractResult(reportFullPath, eventName); + TriggerEvents(extractResults); + _FileRead.Move(extractResults); + FilePathGeneratorInfoMove(extractResults); + _FileRead.WaitForThread(); + _Log.Debug(string.Concat(methodBase.Name, " - Try - Exit")); + return true; + } + catch (Exception ex) + { + _Log.Error(string.Concat(methodBase.Name, " - Catch - Entry {", ex.Message, "}", Environment.NewLine, Environment.NewLine, ex.StackTrace)); + _FileRead.Move(extractResults, exception: ex); + FilePathGeneratorInfoMove(extractResults, exception: ex); + return false; + } + } + + private void Send(EmailMessage emailMessage) + { + ISmtp smtp = Backbone.Instance.GetBackboneComponentsOfType().SingleOrDefault(); + if (smtp is not null) + smtp.Send(emailMessage); + } + + void ISMTP.SendLowPriorityEmailMessage(string subject, string body) + { + EmailMessage emailMessage = new(subject, body, MailPriority.Low); + Send(emailMessage); + } + + void ISMTP.SendHighPriorityEmailMessage(string subject, string body) + { + EmailMessage emailMessage = new(subject, body, MailPriority.High); + Send(emailMessage); + } + + void ISMTP.SendNormalPriorityEmailMessage(string subject, string body) + { + EmailMessage emailMessage = new(subject, body, MailPriority.Normal); + Send(emailMessage); + } + + private void AddParameterRangeAndEvent() + { + string description; + bool allowNull = false; + EquipmentParameter equipmentParameter; + JsonProperty[] jsonProperties = _FileRead.GetDefault(); + Dictionary keyValuePairs = _FileRead.GetDisplayNamesJsonElement(); + Dictionary parameterTypeDefinitions = new(); + FileConnectorParameterTypeDefinitionProvider fileConnectorParameterTypeDefinitionProvider = new(); + foreach (ParameterTypeDefinition parameterTypeDefinition in fileConnectorParameterTypeDefinitionProvider.GetAllParameterTypeDefinition()) + { + switch (parameterTypeDefinition.Name) + { + case nameof(String): + parameterTypeDefinitions.Add(JsonValueKind.String, parameterTypeDefinition); + break; + case nameof(Double): + parameterTypeDefinitions.Add(JsonValueKind.Number, parameterTypeDefinition); + break; + case nameof(Boolean): + parameterTypeDefinitions.Add(JsonValueKind.True, parameterTypeDefinition); + parameterTypeDefinitions.Add(JsonValueKind.False, parameterTypeDefinition); + break; + default: + break; + } + } + foreach (JsonProperty jsonProperty in jsonProperties) + { + if (jsonProperty.Value.ValueKind == JsonValueKind.Object && !parameterTypeDefinitions.ContainsKey(JsonValueKind.Object)) + { + StructuredType structuredType = new(nameof(StructuredType), string.Empty, new List()); + parameterTypeDefinitions.Add(JsonValueKind.Object, structuredType); + Equipment.SelfDescriptionBuilder.AddParameterTypeDefinition(structuredType); + } + if (!parameterTypeDefinitions.ContainsKey(jsonProperty.Value.ValueKind)) + throw new Exception(string.Concat('{', jsonProperty.Value.ValueKind, "} is not mapped!")); + } + foreach (JsonProperty jsonProperty in jsonProperties) + { + if (jsonProperty.Value.ValueKind == JsonValueKind.Null && !allowNull) + throw new Exception(); + if (jsonProperty.Value.ValueKind != JsonValueKind.String || !keyValuePairs.ContainsKey(jsonProperty.Name)) + description = string.Empty; + else + description = keyValuePairs[jsonProperty.Name].Split('|')[0]; + equipmentParameter = new EquipmentParameter(jsonProperty.Name, parameterTypeDefinitions[jsonProperty.Value.ValueKind], description); + _EquipmentParameters.Add(equipmentParameter); + } + _ = Equipment.SelfDescriptionBuilder.RootEquipmentElementBuilder.AddParameterRange(_EquipmentParameters); + _EquipmentEvent = new EquipmentEvent(_FileRead.EventName, _FileRead.GetEventDescription(), _EquipmentParameters); + _ = Equipment.SelfDescriptionBuilder.RootEquipmentElementBuilder.AddEvent(_EquipmentEvent); + } + + private void CreateSelfDescription(MethodBase methodBase) + { + string cellInstanceName; + string equipmentTypeName = string.Empty; + string equipmentDictionaryName = string.Empty; + EquipmentElement equipmentElement = Equipment.SelfDescriptionBuilder.RootEquipmentElementBuilder.Item; + if (Backbone.Instance?.CellName is null) + cellInstanceName = string.Empty; + else + cellInstanceName = Backbone.Instance.CellName; + string cellInstanceConnectionName = equipmentElement.Name; + FileConnectorConfiguration fileConnectorConfiguration = Mapper.Map(Configuration); + string parameterizedModelObjectDefinitionType = methodBase.DeclaringType.FullName; + IList modelObjectParameters = Mapper.Map(ConfiguredParameters); + _FileRead = CellInstanceConnectionName.Get(this, FileParameter, cellInstanceName, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, _DummyRuns, _UseCyclicalForDescription, isEAFHosted: true); + if (_FileRead.IsEvent) + AddParameterRangeAndEvent(); + } + + private object GetFilePathGeneratorInfo(string reportFullPath, bool isErrorFile) + { + FilePathGeneratorInfo result; + FilePathGenerator filePathGeneratorOriginal; + if (Configuration is null) + result = null; + else + { + if (isErrorFile) + { + if (_FilePathGeneratorForError is null) + _FilePathGeneratorForError = new FilePathGenerator(Configuration, reportFullPath, isErrorFile: true); + filePathGeneratorOriginal = _FilePathGeneratorForError; + } + else + { + if (_FilePathGeneratorForTarget is null) + _FilePathGeneratorForTarget = new FilePathGenerator(Configuration, reportFullPath, isErrorFile: false); + filePathGeneratorOriginal = _FilePathGeneratorForTarget; + } + result = new FilePathGeneratorInfo(filePathGeneratorOriginal, reportFullPath, isErrorFile, FileParameter); + } + return result; + } + + private void FilePathGeneratorInfoMove(Tuple> extractResults, Exception exception = null) + { + bool isErrorFile = exception is not null; + object filePathGeneratorInfo = GetFilePathGeneratorInfo(_FileRead.ReportFullPath, isErrorFile); + if (filePathGeneratorInfo is not null and FilePathGeneratorInfo filePathGenerator) + { + string[] exceptionLines = _FileRead.Move(extractResults, filePathGenerator.To, filePathGenerator.From, filePathGenerator.ResolvedFileLocation, exception); + if (isErrorFile) + { + try + { + StringBuilder stringBuilder = new(); + foreach (string item in exceptionLines) + _ = stringBuilder.Append("<").Append(item).AppendLine(">"); + ISmtp smtp = Backbone.Instance.GetBackboneComponentsOfType().SingleOrDefault(); + EmailMessage emailMessage = new(_FileRead.ExceptionSubject, stringBuilder.ToString(), MailPriority.High); + smtp.Send(emailMessage); + } + catch (Exception) { } + } + } + } + + private List GetParameterValues(List headerNames, JsonElement[] jsonElements, int i) + { + List results = new(); + if (_UseCyclicalForDescription && i > 0) + throw new Exception(); + object value; + List list; + JsonProperty[] jsonProperties = jsonElements[i].EnumerateObject().ToArray(); + if (jsonProperties.Length != _EquipmentParameters.Count) + throw new Exception(); + for (int p = 0; p < jsonProperties.Length; p++) + { + if (!_UseCyclicalForDescription || headerNames.Contains(jsonProperties[p].Name)) + value = jsonProperties[p].Value.ToString(); + else + { + list = new List(); + for (int z = 0; z < jsonElements.Length; z++) + list.Add(new object[] { z, jsonElements[z].GetProperty(jsonProperties[p].Name).ToString() }); + value = list; + } + results.Add(new ParameterValue(_EquipmentParameters[p], value, DateTime.Now)); + } + return results; + } + + private void TriggerEvents(Tuple> extractResults) + { + List headerNames; + List parameters; + if (!_UseCyclicalForDescription) + headerNames = null; + else + headerNames = _FileRead.GetHeaderNames(); + for (int i = 0; i < extractResults.Item3.Length; i++) + { + if (_EquipmentEvent is not null) + { + _Log.Debug(string.Concat("TriggerEvent - {", _FileRead.ReportFullPath, "} ", i, " of ", extractResults.Item3.Length)); + parameters = GetParameterValues(headerNames, extractResults.Item3, i); if (_EquipmentEvent is not null) - { - _Log.Debug(string.Concat("TriggerEvent - {", _FileRead.ReportFullPath, "} ", i, " of ", extractResults.Item3.Length)); - parameters = GetParameterValues(headerNames, extractResults.Item3, i); - if (!(_EquipmentEvent is null)) - Equipment.DataCollection.TriggerEvent(_EquipmentEvent, parameters); - if (_UseCyclicalForDescription) - break; - } + Equipment.DataCollection.TriggerEvent(_EquipmentEvent, parameters); + if (_UseCyclicalForDescription) + break; } } + } - public Tuple> ReExtract(string json) + public Tuple> ReExtract(string json) + { + Tuple> results; + string cellInstanceName; + if (!json.Contains(nameof(cellInstanceName))) + throw new Exception(); + string equipmentTypeName; + if (!json.Contains(nameof(equipmentTypeName))) + throw new Exception(); + string equipmentDictionaryName; + if (!json.Contains(nameof(equipmentDictionaryName))) + throw new Exception(); + string cellInstanceConnectionName; + if (!json.Contains(nameof(cellInstanceConnectionName))) + throw new Exception(); + if (!json.Contains(nameof(FileConnectorConfiguration))) + throw new Exception(); + if (!json.Contains(nameof(IList))) + throw new Exception(); + string parameterizedModelObjectDefinitionType; + if (!json.Contains(nameof(parameterizedModelObjectDefinitionType))) + throw new Exception(); + Dictionary> dummyRuns = new(); + Dictionary fileParameter = new(); + MethodBase methodBase = new StackFrame().GetMethod(); + JsonElement jsonElement = JsonSerializer.Deserialize(json); + cellInstanceName = jsonElement.GetProperty(nameof(cellInstanceName)).ToString(); + equipmentTypeName = jsonElement.GetProperty(nameof(equipmentTypeName)).ToString(); + equipmentDictionaryName = jsonElement.GetProperty(nameof(equipmentDictionaryName)).ToString(); + cellInstanceConnectionName = jsonElement.GetProperty(nameof(cellInstanceConnectionName)).ToString(); + JsonElement fileConnectorConfigurationJsonElement = jsonElement.GetProperty(nameof(FileConnectorConfiguration)); + parameterizedModelObjectDefinitionType = jsonElement.GetProperty(nameof(parameterizedModelObjectDefinitionType)).ToString(); + if (fileConnectorConfigurationJsonElement.ValueKind != JsonValueKind.Object) + throw new Exception(); + JsonSerializerOptions jsonSerializerOptions = new() { Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } }; + FileConnectorConfiguration fileConnectorConfiguration = JsonSerializer.Deserialize(fileConnectorConfigurationJsonElement.ToString(), jsonSerializerOptions); + JsonElement modelObjectParameterDefinitionJsonElement = jsonElement.GetProperty(nameof(IList)); + if (modelObjectParameterDefinitionJsonElement.ValueKind != JsonValueKind.Array) + throw new Exception(); + IList modelObjectParameters = JsonSerializer.Deserialize>(modelObjectParameterDefinitionJsonElement.ToString(), jsonSerializerOptions); + _FileRead = CellInstanceConnectionName.Get(this, FileParameter, cellInstanceName, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, _DummyRuns, _UseCyclicalForDescription, isEAFHosted: false); + results = _FileRead.ReExtract(); + if (results?.Item2 is null) + throw new Exception(); + else { - Tuple> results; - string cellInstanceName; - if (!json.Contains(nameof(cellInstanceName))) - throw new Exception(); - string equipmentTypeName; - if (!json.Contains(nameof(equipmentTypeName))) - throw new Exception(); - string equipmentDictionaryName; - if (!json.Contains(nameof(equipmentDictionaryName))) - throw new Exception(); - string cellInstanceConnectionName; - if (!json.Contains(nameof(cellInstanceConnectionName))) - throw new Exception(); - if (!json.Contains(nameof(FileConnectorConfiguration))) - throw new Exception(); - if (!json.Contains(nameof(IList))) - throw new Exception(); - string parameterizedModelObjectDefinitionType; - if (!json.Contains(nameof(parameterizedModelObjectDefinitionType))) - throw new Exception(); - Dictionary> dummyRuns = new(); - Dictionary fileParameter = new(); - MethodBase methodBase = new StackFrame().GetMethod(); - JsonElement jsonElement = JsonSerializer.Deserialize(json); - cellInstanceName = jsonElement.GetProperty(nameof(cellInstanceName)).ToString(); - equipmentTypeName = jsonElement.GetProperty(nameof(equipmentTypeName)).ToString(); - equipmentDictionaryName = jsonElement.GetProperty(nameof(equipmentDictionaryName)).ToString(); - cellInstanceConnectionName = jsonElement.GetProperty(nameof(cellInstanceConnectionName)).ToString(); - JsonElement fileConnectorConfigurationJsonElement = jsonElement.GetProperty(nameof(FileConnectorConfiguration)); - parameterizedModelObjectDefinitionType = jsonElement.GetProperty(nameof(parameterizedModelObjectDefinitionType)).ToString(); - if (fileConnectorConfigurationJsonElement.ValueKind != JsonValueKind.Object) - throw new Exception(); - JsonSerializerOptions jsonSerializerOptions = new() { Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) } }; - FileConnectorConfiguration fileConnectorConfiguration = JsonSerializer.Deserialize(fileConnectorConfigurationJsonElement.ToString(), jsonSerializerOptions); - JsonElement modelObjectParameterDefinitionJsonElement = jsonElement.GetProperty(nameof(IList)); - if (modelObjectParameterDefinitionJsonElement.ValueKind != JsonValueKind.Array) - throw new Exception(); - IList modelObjectParameters = JsonSerializer.Deserialize>(modelObjectParameterDefinitionJsonElement.ToString(), jsonSerializerOptions); - _FileRead = CellInstanceConnectionName.Get(this, FileParameter, cellInstanceName, cellInstanceConnectionName, fileConnectorConfiguration, equipmentTypeName, parameterizedModelObjectDefinitionType, modelObjectParameters, equipmentDictionaryName, _DummyRuns, _UseCyclicalForDescription, isEAFHosted: false); - results = _FileRead.ReExtract(); - if (results?.Item2 is null) - throw new Exception(); - else - { - TriggerEvents(results); - _FileRead.Move(results); - FilePathGeneratorInfoMove(results); - } - return results; + TriggerEvents(results); + _FileRead.Move(results); + FilePathGeneratorInfoMove(results); } - + return results; } } -// 2021-12-16 -> FileRead \ No newline at end of file +// 2022-02-15 -> FileRead \ No newline at end of file diff --git a/Shared/FilePathGenerator.cs b/Shared/FilePathGenerator.cs index c8d1fcd..da97777 100644 --- a/Shared/FilePathGenerator.cs +++ b/Shared/FilePathGenerator.cs @@ -2,34 +2,19 @@ using System.Collections.Generic; using Ifx.Eaf.EquipmentConnector.File.Component; using Ifx.Eaf.EquipmentConnector.File.Configuration; -namespace Shared +namespace Shared; + +public class FilePathGenerator : Ifx.Eaf.EquipmentConnector.File.Component.FilePathGenerator { - public class FilePathGenerator : Ifx.Eaf.EquipmentConnector.File.Component.FilePathGenerator - { + public FileConnectorConfiguration FileConnectorConfiguration { get; private set; } - public FileConnectorConfiguration FileConnectorConfiguration { get; private set; } + public FilePathGenerator(FileConnectorConfiguration config, Dictionary customPattern = null) : base(config, customPattern) => FileConnectorConfiguration = config; - public FilePathGenerator(FileConnectorConfiguration config, Dictionary customPattern = null) : base(config, customPattern) - { - FileConnectorConfiguration = config; - } + public FilePathGenerator(FileConnectorConfiguration config, File file, bool isErrorFile = false, Dictionary customPattern = null) : base(config, file, isErrorFile, customPattern) => FileConnectorConfiguration = config; - public FilePathGenerator(FileConnectorConfiguration config, File file, bool isErrorFile = false, Dictionary customPattern = null) : base(config, file, isErrorFile, customPattern) - { - FileConnectorConfiguration = config; - } + public FilePathGenerator(FileConnectorConfiguration config, string sourceFilePath, bool isErrorFile = false, Dictionary customPattern = null) : base(config, sourceFilePath, isErrorFile, customPattern) => FileConnectorConfiguration = config; - public FilePathGenerator(FileConnectorConfiguration config, string sourceFilePath, bool isErrorFile = false, Dictionary customPattern = null) : base(config, sourceFilePath, isErrorFile, customPattern) - { - FileConnectorConfiguration = config; - } - - public string GetSubFolderPath() - { - return SubFolderPath; - } - - } + public string GetSubFolderPath() => SubFolderPath; } diff --git a/Shared/FilePathGeneratorInfo.cs b/Shared/FilePathGeneratorInfo.cs index 827a893..8d5e287 100644 --- a/Shared/FilePathGeneratorInfo.cs +++ b/Shared/FilePathGeneratorInfo.cs @@ -1,66 +1,63 @@ using System.IO; -namespace Shared +namespace Shared; + +public class FilePathGeneratorInfo { - public class FilePathGeneratorInfo + public string To { get; protected set; } + public string From { get; protected set; } + public bool IsErrorFile { get; protected set; } + public string SubFolderPath { get; protected set; } + public string FirstDirectory { get; protected set; } + public string ReportFullPath { get; protected set; } + public string ResolvedFileLocation { get; protected set; } + + public FilePathGeneratorInfo(object originalFilePathGenerator, string reportFullPath, bool isErrorFile, System.Collections.Generic.Dictionary fileParameter) { - - public string To { get; protected set; } - public string From { get; protected set; } - public bool IsErrorFile { get; protected set; } - public string SubFolderPath { get; protected set; } - public string FirstDirectory { get; protected set; } - public string ReportFullPath { get; protected set; } - public string ResolvedFileLocation { get; protected set; } - - public FilePathGeneratorInfo(object originalFilePathGenerator, string reportFullPath, bool isErrorFile, System.Collections.Generic.Dictionary fileParameter) + ReportFullPath = reportFullPath; + IsErrorFile = isErrorFile; + if (originalFilePathGenerator is null || originalFilePathGenerator is not FilePathGenerator original) { - ReportFullPath = reportFullPath; - IsErrorFile = isErrorFile; - if (originalFilePathGenerator is null || originalFilePathGenerator is not FilePathGenerator original) - { - FirstDirectory = string.Empty; - ResolvedFileLocation = string.Empty; - To = string.Empty; - From = string.Empty; - } - else - { - string directorySeparatorChar = Path.DirectorySeparatorChar.ToString(); - FilePathGenerator filePathGenerator = new(original.FileConnectorConfiguration, reportFullPath, isErrorFile); - SubFolderPath = filePathGenerator.GetSubFolderPath(); - if (string.IsNullOrEmpty(SubFolderPath)) - FirstDirectory = SubFolderPath; - else - FirstDirectory = SubFolderPath.Split(Path.DirectorySeparatorChar)[0]; - ResolvedFileLocation = filePathGenerator.GetTargetFolder(); - if (string.IsNullOrEmpty(ResolvedFileLocation) && string.IsNullOrEmpty(SubFolderPath)) - To = string.Empty; - else if (string.IsNullOrEmpty(SubFolderPath)) - To = ResolvedFileLocation; - else - To = string.Concat(ResolvedFileLocation.Replace(SubFolderPath, string.Empty), Path.DirectorySeparatorChar, FirstDirectory); -#if (true) - if (string.IsNullOrEmpty(original.FileConnectorConfiguration.DefaultPlaceHolderValue)) - original.FileConnectorConfiguration.DefaultPlaceHolderValue = "NA"; - if (!(fileParameter is null) && fileParameter.Count == 1 && To.Contains(original.FileConnectorConfiguration.DefaultPlaceHolderValue)) - { - foreach (System.Collections.Generic.KeyValuePair keyValuePair in fileParameter) - To = To.Replace(string.Concat(original.FileConnectorConfiguration.DefaultPlaceHolderValue), keyValuePair.Value); - } -#endif - if (original.FileConnectorConfiguration.SourceFileLocation.EndsWith(directorySeparatorChar)) - From = string.Concat(original.FileConnectorConfiguration.SourceFileLocation, FirstDirectory); - else - From = string.Concat(original.FileConnectorConfiguration.SourceFileLocation, Path.DirectorySeparatorChar, FirstDirectory); - if (From.EndsWith(directorySeparatorChar) && !To.EndsWith(directorySeparatorChar)) - To = string.Concat(To, Path.DirectorySeparatorChar); - else if (To.EndsWith(directorySeparatorChar) && !From.EndsWith(directorySeparatorChar)) - To = To.Remove(To.Length - 1, 1); - } + FirstDirectory = string.Empty; + ResolvedFileLocation = string.Empty; + To = string.Empty; + From = string.Empty; + } + else + { + string directorySeparatorChar = Path.DirectorySeparatorChar.ToString(); + FilePathGenerator filePathGenerator = new(original.FileConnectorConfiguration, reportFullPath, isErrorFile); + SubFolderPath = filePathGenerator.GetSubFolderPath(); + if (string.IsNullOrEmpty(SubFolderPath)) + FirstDirectory = SubFolderPath; + else + FirstDirectory = SubFolderPath.Split(Path.DirectorySeparatorChar)[0]; + ResolvedFileLocation = filePathGenerator.GetTargetFolder(); + if (string.IsNullOrEmpty(ResolvedFileLocation) && string.IsNullOrEmpty(SubFolderPath)) + To = string.Empty; + else if (string.IsNullOrEmpty(SubFolderPath)) + To = ResolvedFileLocation; + else + To = string.Concat(ResolvedFileLocation.Replace(SubFolderPath, string.Empty), Path.DirectorySeparatorChar, FirstDirectory); +#if (true) + if (string.IsNullOrEmpty(original.FileConnectorConfiguration.DefaultPlaceHolderValue)) + original.FileConnectorConfiguration.DefaultPlaceHolderValue = "NA"; + if (fileParameter is not null && fileParameter.Count == 1 && To.Contains(original.FileConnectorConfiguration.DefaultPlaceHolderValue)) + { + foreach (System.Collections.Generic.KeyValuePair keyValuePair in fileParameter) + To = To.Replace(string.Concat(original.FileConnectorConfiguration.DefaultPlaceHolderValue), keyValuePair.Value); + } +#endif + if (original.FileConnectorConfiguration.SourceFileLocation.EndsWith(directorySeparatorChar)) + From = string.Concat(original.FileConnectorConfiguration.SourceFileLocation, FirstDirectory); + else + From = string.Concat(original.FileConnectorConfiguration.SourceFileLocation, Path.DirectorySeparatorChar, FirstDirectory); + if (From.EndsWith(directorySeparatorChar) && !To.EndsWith(directorySeparatorChar)) + To = string.Concat(To, Path.DirectorySeparatorChar); + else if (To.EndsWith(directorySeparatorChar) && !From.EndsWith(directorySeparatorChar)) + To = To.Remove(To.Length - 1, 1); } - } } diff --git a/Shared/Mapper.cs b/Shared/Mapper.cs index 4408cc8..fe5c923 100644 --- a/Shared/Mapper.cs +++ b/Shared/Mapper.cs @@ -2,76 +2,73 @@ using System.Collections.Generic; using Eaf.Management.ConfigurationData.CellAutomation; using Ifx.Eaf.EquipmentConnector.File.Configuration; -namespace Shared +namespace Shared; + +public static class Mapper { - public static class Mapper + internal static Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration Map(FileConnectorConfiguration configuration) { - - internal static Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration Map(FileConnectorConfiguration configuration) + Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration result = new() { - Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration result = new() - { - AlternateTargetFolder = configuration.AlternateTargetFolder, - ConnectionRetryInterval = configuration.ConnectionRetryInterval, - ConnectionSettings = new List(), - CopySourceFolderStructure = configuration.CopySourceFolderStructure, - DefaultPlaceHolderValue = configuration.DefaultPlaceHolderValue, - //DeleteAbandonedEmptySourceSubFolders = configuration.DeleteAbandonedEmptySourceSubFolders, - DeleteEmptySourceSubFolders = configuration.DeleteEmptySourceSubFolders, - ErrorPostProcessingMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.PostProcessingModeEnum)configuration.ErrorPostProcessingMode, - ErrorTargetFileLocation = configuration.ErrorTargetFileLocation, - ErrorTargetFileName = configuration.ErrorTargetFileName, - FileAgeFilterMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.FileAgeFilterEnum)configuration.FileAgeFilterMode, - FileAgeThreshold = configuration.FileAgeThreshold, - //FileHandlers = configuration.FileHandlers, - FileHandleTimeout = (long)configuration.FileHandleTimeout, - FileHandleWaitTime = configuration.FileHandleWaitTime, - FileScanningIntervalInSeconds = configuration.FileScanningIntervalInSeconds, - FileScanningOption = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.FileScanningOptionEnum)configuration.FileScanningOption, - FolderAgeCheckIndividualSubFolders = configuration.FolderAgeCheckIndividualSubFolders, - FolderAgeThreshold = configuration.FolderAgeThreshold, - //FolderOperationsSubFolderLevel = configuration.FolderOperationsSubFolderLevel, - IdleEventWaitTimeInSeconds = (long)configuration.IdleEventWaitTimeInSeconds, - IfFileExistAction = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.IfFileExistEnum)configuration.IfFileExistAction, - IfPostProcessingFailsAction = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.IfPostProcessingFailsEnum)configuration.IfPostProcessingFailsAction, - IncludeSubDirectories = configuration.IncludeSubDirectories, - PostProcessingRetries = configuration.PostProcessingRetries, - PreProcessingMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.PreProcessingModeEnum)configuration.PreProcessingMode, - PostProcessingMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.PostProcessingModeEnum)configuration.PostProcessingMode, - SourceDirectoryCloaking = configuration.SourceDirectoryCloaking, - SourceFileFilter = configuration.SourceFileFilter, - SourceFileFilters = configuration.SourceFileFilters, - SourceFileLocation = configuration.SourceFileLocation, - TargetFileLocation = configuration.TargetFileLocation, - TargetFileName = configuration.TargetFileName, - TriggerOnChanged = configuration.TriggerOnChanged, - TriggerOnCreated = configuration.TriggerOnCreated, - UseZip64Mode = configuration.UseZip64Mode, - ZipErrorTargetFileName = configuration.ZipErrorTargetFileName, - ZipFileAmount = configuration.ZipFileAmount, - ZipFileSubFolderLevel = configuration.ZipFileSubFolderLevel, - ZipFileTime = configuration.ZipFileTime, - ZipMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.ZipModeEnum)configuration.ZipMode, - ZipTargetFileName = configuration.ZipTargetFileName, - }; - foreach (Ifx.Eaf.Common.Configuration.ConnectionSetting connectionSetting in configuration.ConnectionSettings) - result.ConnectionSettings.Add(new Adaptation.Ifx.Eaf.Common.Configuration.ConnectionSetting(connectionSetting.Name, connectionSetting.Value)); - return result; - } + AlternateTargetFolder = configuration.AlternateTargetFolder, + ConnectionRetryInterval = configuration.ConnectionRetryInterval, + ConnectionSettings = new List(), + CopySourceFolderStructure = configuration.CopySourceFolderStructure, + DefaultPlaceHolderValue = configuration.DefaultPlaceHolderValue, + //DeleteAbandonedEmptySourceSubFolders = configuration.DeleteAbandonedEmptySourceSubFolders, + DeleteEmptySourceSubFolders = configuration.DeleteEmptySourceSubFolders, + ErrorPostProcessingMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.PostProcessingModeEnum)configuration.ErrorPostProcessingMode, + ErrorTargetFileLocation = configuration.ErrorTargetFileLocation, + ErrorTargetFileName = configuration.ErrorTargetFileName, + FileAgeFilterMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.FileAgeFilterEnum)configuration.FileAgeFilterMode, + FileAgeThreshold = configuration.FileAgeThreshold, + //FileHandlers = configuration.FileHandlers, + FileHandleTimeout = (long)configuration.FileHandleTimeout, + FileHandleWaitTime = configuration.FileHandleWaitTime, + FileScanningIntervalInSeconds = configuration.FileScanningIntervalInSeconds, + FileScanningOption = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.FileScanningOptionEnum)configuration.FileScanningOption, + FolderAgeCheckIndividualSubFolders = configuration.FolderAgeCheckIndividualSubFolders, + FolderAgeThreshold = configuration.FolderAgeThreshold, + //FolderOperationsSubFolderLevel = configuration.FolderOperationsSubFolderLevel, + IdleEventWaitTimeInSeconds = (long)configuration.IdleEventWaitTimeInSeconds, + IfFileExistAction = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.IfFileExistEnum)configuration.IfFileExistAction, + IfPostProcessingFailsAction = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.IfPostProcessingFailsEnum)configuration.IfPostProcessingFailsAction, + IncludeSubDirectories = configuration.IncludeSubDirectories, + PostProcessingRetries = configuration.PostProcessingRetries, + PreProcessingMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.PreProcessingModeEnum)configuration.PreProcessingMode, + PostProcessingMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.PostProcessingModeEnum)configuration.PostProcessingMode, + SourceDirectoryCloaking = configuration.SourceDirectoryCloaking, + SourceFileFilter = configuration.SourceFileFilter, + SourceFileFilters = configuration.SourceFileFilters, + SourceFileLocation = configuration.SourceFileLocation, + TargetFileLocation = configuration.TargetFileLocation, + TargetFileName = configuration.TargetFileName, + TriggerOnChanged = configuration.TriggerOnChanged, + TriggerOnCreated = configuration.TriggerOnCreated, + UseZip64Mode = configuration.UseZip64Mode, + ZipErrorTargetFileName = configuration.ZipErrorTargetFileName, + ZipFileAmount = configuration.ZipFileAmount, + ZipFileSubFolderLevel = configuration.ZipFileSubFolderLevel, + ZipFileTime = configuration.ZipFileTime, + ZipMode = (Adaptation.Ifx.Eaf.EquipmentConnector.File.Configuration.FileConnectorConfiguration.ZipModeEnum)configuration.ZipMode, + ZipTargetFileName = configuration.ZipTargetFileName, + }; + foreach (Ifx.Eaf.Common.Configuration.ConnectionSetting connectionSetting in configuration.ConnectionSettings) + result.ConnectionSettings.Add(new Adaptation.Ifx.Eaf.Common.Configuration.ConnectionSetting(connectionSetting.Name, connectionSetting.Value)); + return result; + } - internal static IList Map(IList configuredParameters) + internal static IList Map(IList configuredParameters) + { + List results = new(); + Adaptation.Eaf.Management.ConfigurationData.CellAutomation.ModelObjectParameterType modelObjectParameterType; + for (int i = 0; i < configuredParameters.Count; i++) { - List results = new(); - Adaptation.Eaf.Management.ConfigurationData.CellAutomation.ModelObjectParameterType modelObjectParameterType; - for (int i = 0; i < configuredParameters.Count; i++) - { - modelObjectParameterType = (Adaptation.Eaf.Management.ConfigurationData.CellAutomation.ModelObjectParameterType)configuredParameters[i].ValueType; - results.Add(new Adaptation.Eaf.Management.ConfigurationData.CellAutomation.ModelObjectParameterDefinition() { Id = i, Name = configuredParameters[i].Name, Value = configuredParameters[i].Value, ValueType = modelObjectParameterType, EnumType = modelObjectParameterType.ToString() }); - } - return results; + modelObjectParameterType = (Adaptation.Eaf.Management.ConfigurationData.CellAutomation.ModelObjectParameterType)configuredParameters[i].ValueType; + results.Add(new Adaptation.Eaf.Management.ConfigurationData.CellAutomation.ModelObjectParameterDefinition() { Id = i, Name = configuredParameters[i].Name, Value = configuredParameters[i].Value, ValueType = modelObjectParameterType, EnumType = modelObjectParameterType.ToString() }); } - + return results; } }