using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; namespace Shared.Metrology { public class ConfigDataBase { public bool EafHosted { get; protected set; } public string EquipmentElementName { get; protected set; } public bool UseCyclicalForDescription { get; protected set; } public Dictionary CellNames { get; protected set; } public Dictionary MesEntities { get; protected set; } public EquipmentConnection? EquipmentConnection { get; protected set; } protected readonly string _CellName; protected readonly EventName? _EventName; protected readonly Dictionary _Reactors; protected IProcessDataDescription _ProcessDataDescription; public ConfigDataBase(string equipmentElementName, EquipmentConnection? equipmentConnection, string cellName, EventName? eventName, bool eafHosted) { _CellName = cellName; EafHosted = eafHosted; _EventName = eventName; EquipmentConnection = equipmentConnection; EquipmentElementName = equipmentElementName; _Reactors = new Dictionary(); CellNames = new Dictionary(); MesEntities = new Dictionary(); if (equipmentConnection is null && Enum.TryParse(equipmentElementName.Replace("-", string.Empty), out EquipmentConnection equipmentConnectionValue)) EquipmentConnection = equipmentConnectionValue; } public string GetEventName() { string result = string.Concat(_EventName); return result; } public EventName GetEventNameValue() { EventName result = _EventName.Value; return result; } public string GetEventDescription() { string result = _ProcessDataDescription.GetEventDescription(); return result; } public string GetCurrentReactor(ILogic logic) { string result = string.Empty; foreach (KeyValuePair keyValuePair in _Reactors) { foreach (string filePrefix in keyValuePair.Value.Split('|')) { if (logic.Logistics.MID.StartsWith(filePrefix) || (_EventName != EventName.FileRead && MesEntities.ContainsKey(logic.Logistics.JobID) && keyValuePair.Value == MesEntities[logic.Logistics.JobID])) { result = keyValuePair.Key; break; } } } if (string.IsNullOrEmpty(result) && _Reactors.Count == 1) result = _Reactors.ElementAt(0).Key; return result; } public IProcessDataDescription GetDefault(ILogic logic) { IProcessDataDescription result = _ProcessDataDescription.GetDefault(logic, this); return result; } public IProcessDataDescription GetDisplayNames(ILogic logic) { IProcessDataDescription result = _ProcessDataDescription.GetDisplayNames(logic, this); return result; } protected JsonElement GetDefaultJsonElement(ILogic logic) { JsonElement result; IProcessDataDescription processDataDescription = _ProcessDataDescription.GetDefault(logic, this); string json = JsonSerializer.Serialize(processDataDescription, processDataDescription.GetType()); object @object = JsonSerializer.Deserialize(json); result = (JsonElement)@object; return result; } public Dictionary>> GetParameterInfo(ILogic logic, bool allowNull) { Dictionary>> results = new Dictionary>>(); string description; Description.Param param; Tuple tuple; JsonElement defaultJsonElement = GetDefaultJsonElement(logic); Dictionary keyValuePairs = GetDisplayNamesJsonElement(logic); foreach (JsonProperty jsonProperty in defaultJsonElement.EnumerateObject()) { if (jsonProperty.Value.ValueKind == JsonValueKind.Null && !allowNull) throw new Exception(); if (jsonProperty.Value.ValueKind == JsonValueKind.Object || jsonProperty.Value.ValueKind == JsonValueKind.Array) { description = string.Empty; param = Description.Param.StructuredType; //jValue = jObject.Value("Item1"); throw new NotImplementedException("Item1"); } else { switch (jsonProperty.Value.ValueKind) { case JsonValueKind.String: param = Description.Param.String; break; case JsonValueKind.Number: param = Description.Param.Double; break; case JsonValueKind.True: case JsonValueKind.False: param = Description.Param.Boolean; break; case JsonValueKind.Null: param = Description.Param.String; break; default: param = Description.Param.StructuredType; break; } } if (!keyValuePairs.ContainsKey(jsonProperty.Name)) description = string.Empty; else description = keyValuePairs[jsonProperty.Name]; tuple = new Tuple(param, jsonProperty.Name, description, jsonProperty.Value.ToString()); if (!results.ContainsKey(jsonProperty.Name)) results.Add(jsonProperty.Name, new List>()); results[jsonProperty.Name].Add(tuple); } return results; } protected void WriteExportAliases(ILogic logic, string cellName) { int i = 0; object value; string description; Description.Param param; Description.Param[] @params; StringBuilder stringBuilder = new StringBuilder(); string shareRoot = @"\\messv02ecc1.ec.local\EC_EDA"; string shareDirectory = string.Concat(shareRoot, @"\Staging\Pdsf\", cellName, @"\ExportAliases"); Dictionary>> keyValuePairs = GetParameterInfo(logic, allowNull: false); stringBuilder.AppendLine("\"AliasName\";\"Condition\";\"EventId\";\"ExceptionId\";\"Formula\";\"HardwareId\";\"OrderId\";\"ParameterName\";\"Remark\";\"ReportName\";\"SourceId\";\"Use\""); if (!Directory.Exists(shareRoot)) return; if (!Directory.Exists(shareDirectory)) Directory.CreateDirectory(shareDirectory); string shareFile = string.Concat(shareDirectory, @"\", DateTime.Now.Ticks, ".csv"); foreach (KeyValuePair>> keyValuePair in keyValuePairs) { i += 1; @params = (from l in keyValuePair.Value select l.Item1).Distinct().ToArray(); if (@params.Length != 1) throw new Exception(); if (keyValuePair.Value[0].Item2 != keyValuePair.Key) throw new Exception(); param = @params[0]; if (param != Description.Param.String) stringBuilder.AppendLine($"\"{keyValuePair.Key}\";\"\";\"\";\"\";\"\";\"\";\"{i}\";\"{cellName}/{EquipmentElementName}/{keyValuePair.Key}\";\"\";\"{cellName}/{EquipmentElementName}/{_EventName.Value}\";\"\";\"True\""); else { description = keyValuePair.Value[0].Item3.Split('|')[0]; if (string.IsNullOrEmpty(description)) continue; value = keyValuePair.Value[0].Item4; stringBuilder.AppendLine($"\"'{description}'\";\"\";\"\";\"\";\"\";\"\";\"{i}\";\"{cellName}/{EquipmentElementName}/{value}\";\"\";\"{cellName}/{EquipmentElementName}/{_EventName.Value}\";\"\";\"True\""); } } File.WriteAllText(shareFile, stringBuilder.ToString()); } public Dictionary GetDisplayNamesJsonElement(ILogic logic) { Dictionary results = new Dictionary(); IProcessDataDescription processDataDescription = _ProcessDataDescription.GetDisplayNames(logic, this); string json = JsonSerializer.Serialize(processDataDescription, processDataDescription.GetType()); JsonElement jsonElement = JsonSerializer.Deserialize(json); foreach (JsonProperty jsonProperty in jsonElement.EnumerateObject()) { if (!results.ContainsKey(jsonProperty.Name)) results.Add(jsonProperty.Name, string.Empty); if (jsonProperty.Value is JsonElement jsonPropertyValue) results[jsonProperty.Name] = jsonPropertyValue.ToString(); } return results; } public List GetDetailNames(ILogic logic) { List results = _ProcessDataDescription.GetDetailNames(logic, this); return results; } public List GetHeaderNames(ILogic logic) { List results = _ProcessDataDescription.GetHeaderNames(logic, this); return results; } public List GetIgnoreParameterNames(ILogic logic, Test test) { List results = _ProcessDataDescription.GetIgnoreParameterNames(logic, this, test); string value; List pairedParameterNames = _ProcessDataDescription.GetPairedParameterNames(logic, this); IProcessDataDescription processDataDescription = _ProcessDataDescription.GetDisplayNames(logic, this); string json = JsonSerializer.Serialize(processDataDescription, processDataDescription.GetType()); object @object = JsonSerializer.Deserialize(json); if (!(@object is JsonElement jsonElement)) throw new Exception(); foreach (JsonProperty jsonProperty in jsonElement.EnumerateObject()) { if (jsonProperty.Value.ValueKind == JsonValueKind.Object || jsonProperty.Value.ValueKind == JsonValueKind.Array) throw new Exception(); value = jsonProperty.Value.ToString(); if (!results.Contains(jsonProperty.Name) && pairedParameterNames.Contains(jsonProperty.Name) && (string.IsNullOrEmpty(value) || value[0] == '|')) results.Add(jsonProperty.Name); } return results; } public List GetNames(ILogic logic) { List results = _ProcessDataDescription.GetNames(logic, this); return results; } public List GetPairedParameterNames(ILogic logic) { List results = _ProcessDataDescription.GetPairedParameterNames(logic, this); return results; } public List GetParameterNames(ILogic logic) { List results = _ProcessDataDescription.GetParameterNames(logic, this); return results; } public List GetProcessDataDescriptions(JsonElement jsonElement) { List results; if (jsonElement.ValueKind != JsonValueKind.Array) throw new Exception(); JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString }; results = JsonSerializer.Deserialize>(jsonElement.ToString(), jsonSerializerOptions); return results; } public Dictionary> GetKeyValuePairs(List processDataDescriptions) { Dictionary> results = new Dictionary>(); Test testKey; for (int i = 0; i < processDataDescriptions.Count; i++) { testKey = (Test)processDataDescriptions[i].Test; if (!results.ContainsKey(testKey)) results.Add(testKey, new List()); results[testKey].Add(processDataDescriptions[i]); } return results; } public Dictionary> GetKeyValuePairs(JsonElement jsonElement, List processDataDescriptions, Test test) { Dictionary> results = new Dictionary>(); Test testKey; if (jsonElement.ValueKind != JsonValueKind.Array) throw new Exception(); JsonElement[] jsonElements = jsonElement.EnumerateArray().ToArray(); if (processDataDescriptions.Count != jsonElements.Length) throw new Exception(); for (int i = 0; i < processDataDescriptions.Count; i++) { testKey = (Test)processDataDescriptions[i].Test; if (testKey != test) continue; foreach (JsonProperty jsonProperty in jsonElements[0].EnumerateObject()) { if (jsonProperty.Value.ValueKind == JsonValueKind.Object || jsonProperty.Value.ValueKind == JsonValueKind.Array) throw new Exception(); if (!results.ContainsKey(jsonProperty.Name)) results.Add(jsonProperty.Name, new List()); results[jsonProperty.Name].Add(jsonProperty.Value.ToString()); } } return results; } public List GetIProcessDataDescriptions(JsonElement jsonElement) { List results = new List(); if (jsonElement.ValueKind != JsonValueKind.Array) throw new Exception(); object @object; Type type = _ProcessDataDescription.GetType(); JsonElement[] jsonElements = jsonElement.EnumerateArray().ToArray(); JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString }; for (int i = 0; i < jsonElements.Length; i++) { @object = JsonSerializer.Deserialize(jsonElements[i].ToString(), type, jsonSerializerOptions); if (!(@object is IProcessDataDescription processDataDescription)) continue; results.Add(processDataDescription); } return results; } } }