public string UpdateMapping(string udtFileContents, string mappingFileContents, string identifier, string newident) { StringReader udtsr = new StringReader(udtFileContents); StringReader mappingsr = new StringReader(mappingFileContents); UDTCompiler udtCompiler = new UDTCompiler(); udtCompiler.Compile(udtsr); MappingCompiler mappingCompiler = new MappingCompiler(udtCompiler); mappingCompiler.Compile(mappingsr); foreach (TypeMapping tm in mappingCompiler.DefinedMappings) { if (tm.Identifier == identifier) { tm.Identifier = newident; } } MappingWriter mappingWriter = new MappingWriter(); mappingWriter.Mappings.AddRange(mappingCompiler.DefinedMappings); StringBuilder sb = new StringBuilder(); mappingWriter.Write(new StringWriter(sb)); return(sb.ToString()); }
/// <summary> /// Creates a new <see cref="MapperBase"/>. /// </summary> /// <param name="framework">Container object for framework elements.</param> /// <param name="unmapper">Object that handles conversion of data from data structures into measurements.</param> /// <param name="inputMapping">Input mapping name.</param> protected MapperBase(Framework framework, string inputMapping) { m_framework = framework; m_minimumRetentionLock = new object(); m_minimumRetentionTimes = new Dictionary <MeasurementKey, TimeSpan>(); m_mappingRetentionTimes = new Dictionary <MeasurementKey, TimeSpan>(); m_retentionTimes = new Dictionary <MeasurementKey, TimeSpan>(); UDTCompiler udtCompiler = new UDTCompiler(); m_mappingCompiler = new MappingCompiler(udtCompiler); string udtPath = Path.Combine("Model", "UserDefinedTypes.ecaidl"); string mappingPath = Path.Combine("Model", "UserDefinedMappings.ecamap"); udtCompiler.Compile(udtPath); m_mappingCompiler.Compile(mappingPath); m_keys = new List <MeasurementKey[]>(); m_timeWindowKeys = new List <MeasurementKey[]>(); m_mappingCollections = new List <TypeMapping[]>(); m_inputMapping = inputMapping; if ((object)m_mappingCompiler.GetTypeMapping(inputMapping) == null) { throw new InvalidOperationException($"Unable to find input mapping \"{inputMapping}\" in mapping file ({mappingPath})!"); } }
public string UpdateUDT(string udtFileContents, string category, string identifier, string newcat, string newident) { StringReader udtsr = new StringReader(udtFileContents); UDTCompiler udtCompiler = new UDTCompiler(); udtCompiler.Compile(udtsr); foreach (DataType dt in udtCompiler.DefinedTypes) { if (dt.Category == category && dt.Identifier == identifier) { if (newcat != null) { dt.Category = newcat; } if (newident != null) { dt.Identifier = newident; } } } UDTWriter udtWriter = new UDTWriter(); udtWriter.Types.AddRange(udtCompiler.DefinedTypes.OfType <UserDefinedType>()); StringBuilder sb = new StringBuilder(); udtWriter.Write(new StringWriter(sb)); return(sb.ToString()); }
public void FixUDT(string filePath, string contents) { UDTCompiler udtCompiler = CreateUDTCompiler(); if (udtCompiler.BatchErrors.Any(ex => ex.FilePath == filePath)) { File.WriteAllText(filePath, contents); } }
public List <DataType> GetEnumeratedReferenceTypes(DataType dataType) { List <DataType> referenceTypes = new List <DataType>(); UDTCompiler udtCompiler = CreateUDTCompiler(); referenceTypes.Add(dataType); GetEnumeratedReferenceTypes(dataType, referenceTypes, udtCompiler); return(referenceTypes); }
public List <DataType> ReadUDTFile(string udtfileContents) { StringReader udtsr = new StringReader(udtfileContents); UDTCompiler compiler = new UDTCompiler(); compiler.Compile(udtsr); return(compiler.DefinedTypes.Where(x => x.IsUserDefined).ToList()); }
public void UpdateUDT(UserDefinedType udt, string oldCat, string oldIdent) { UDTCompiler udtCompiler = CreateUDTCompiler(); MappingCompiler mappingCompiler = new MappingCompiler(udtCompiler); mappingCompiler.Scan(s_udmDirectory); foreach (UserDefinedType dt in udtCompiler.DefinedTypes.OfType <UserDefinedType>()) { if (dt.Category == oldCat && dt.Identifier == oldIdent) { dt.Fields.Clear(); foreach (UDTField dataType in udt.Fields) { dt.Fields.Add(dataType); } dt.Category = udt.Category; dt.Identifier = udt.Identifier; } } string categoryPath = Path.Combine(s_udtDirectory, oldCat); string typePath = Path.Combine(categoryPath, oldIdent + ".ecaidl"); lock (s_udtLock) { File.Delete(typePath); if (!Directory.EnumerateFileSystemEntries(categoryPath).Any()) { Directory.Delete(categoryPath); } } UDTWriter udtWriter = new UDTWriter(); udtWriter.Types.AddRange(udtCompiler.DefinedTypes.OfType <UserDefinedType>()); lock (s_udtLock) udtWriter.WriteFiles(s_udtDirectory); MappingWriter mappingWriter = new MappingWriter(); mappingWriter.Mappings.AddRange(mappingCompiler.DefinedMappings); lock (s_udmLock) mappingWriter.WriteFiles(s_udmDirectory); }
public List <TypeMapping> ReadMappingFile(string udtfileContents, string mappingFileContents) { StringReader udtsr = new StringReader(udtfileContents); StringReader mappingsr = new StringReader(mappingFileContents); UDTCompiler comp = new UDTCompiler(); comp.Compile(udtsr); MappingCompiler compiler = new MappingCompiler(comp); compiler.Compile(mappingsr); return(compiler.DefinedMappings); }
private UDTCompiler CreateUDTCompiler() { UDTCompiler udtCompiler = new UDTCompiler(); lock (s_udtLock) { if (Directory.Exists(s_udtDirectory)) { udtCompiler.Scan(s_udtDirectory); } } return(udtCompiler); }
private MappingCompiler CreateOutputMappingCompiler() { UDTCompiler udtCompiler = CreateUDTCompiler(); MappingCompiler mappingCompiler = new MappingCompiler(udtCompiler); lock (s_udomLock) { if (Directory.Exists(s_udomDirectory)) { mappingCompiler.Scan(s_udomDirectory); } } return(mappingCompiler); }
/// <summary> /// Creates a new <see cref="MapperBase"/>. /// </summary> /// <param name="framework">Container object for framework elements.</param> /// <param name="inputMapping">Input mapping name.</param> protected MapperBase(Framework framework, string inputMapping) { m_signalLookup = framework.SignalLookup; m_alignmentCoordinator = framework.AlignmentCoordinator; m_signalBuffers = framework.SignalBuffers; UDTCompiler udtCompiler = new UDTCompiler(); m_mappingCompiler = new MappingCompiler(udtCompiler); udtCompiler.Compile(Path.Combine("Model", "UserDefinedTypes.ecaidl")); m_mappingCompiler.Compile(Path.Combine("Model", "UserDefinedMappings.ecamap")); m_keys = new List <MeasurementKey[]>(); m_readonlyKeys = m_keys.AsReadOnly(); m_inputMapping = inputMapping; }
public void RemoveUDT(UserDefinedType udt) { UDTCompiler udtCompiler = CreateUDTCompiler(); MappingCompiler mappingInputCompiler = new MappingCompiler(udtCompiler); mappingInputCompiler.Scan(s_udimDirectory); MappingCompiler mappingOutputCompiler = new MappingCompiler(udtCompiler); mappingOutputCompiler.Scan(s_udomDirectory); List <DataType> dataTypes = GetEnumeratedReferenceTypes(udt); dataTypes.Reverse(); foreach (DataType type in dataTypes) { foreach (TypeMapping mapping in mappingInputCompiler.GetMappings((UserDefinedType)type)) { RemoveInputMapping(mapping); } foreach (TypeMapping mapping in mappingOutputCompiler.GetMappings((UserDefinedType)type)) { RemoveOutputMapping(mapping); } string categoryPath = Path.Combine(s_udtDirectory, type.Category); string typePath = Path.Combine(categoryPath, type.Identifier + ".ecaidl"); lock (s_udtLock) { File.Delete(typePath); if (!Directory.EnumerateFileSystemEntries(categoryPath).Any()) { Directory.Delete(categoryPath); } } } }
public string UpdateMappingForUDT(string udtFileContents, string mappingFileContents, string category, string identifier, string newcat, string newident) { StringReader udtsr = new StringReader(udtFileContents); StringReader mappingsr = new StringReader(mappingFileContents); UDTCompiler udtCompiler = new UDTCompiler(); udtCompiler.Compile(udtsr); MappingCompiler mappingCompiler = new MappingCompiler(udtCompiler); mappingCompiler.Compile(mappingsr); foreach (DataType dt in udtCompiler.DefinedTypes) { if (dt.Category == category && dt.Identifier == identifier) { if (newcat != null) { dt.Category = newcat; } if (newident != null) { dt.Identifier = newident; } } } MappingWriter mappingWriter = new MappingWriter(); mappingWriter.Mappings.AddRange(mappingCompiler.DefinedMappings); StringBuilder sb = new StringBuilder(); mappingWriter.Write(new StringWriter(sb)); return(sb.ToString()); }
// Client-side script functionality #region [ User Defined Types ] public IEnumerable <DataType> GetDefinedTypes() { UDTCompiler udtCompiler = CreateUDTCompiler(); return(udtCompiler.DefinedTypes); }
public void GetEnumeratedReferenceTypes(DataType dataType, List <DataType> dataTypes, UDTCompiler compiler) { IEnumerable <DataType> referencingTypes = compiler.EnumerateReferencingTypes(compiler.GetType(dataType.Category, dataType.Identifier)); foreach (DataType referencingType in referencingTypes) { dataTypes.Add(referencingType); GetEnumeratedReferenceTypes(referencingType, dataTypes, compiler); } }
public static void CheckPhasorTypesAndMappings() { string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string ecaClientDataPath = Path.Combine(appData, "Grid Protection Alliance", "openECAClient"); string udtDirectory = Path.Combine(ecaClientDataPath, "UserDefinedTypes"); string udmDirectory = Path.Combine(ecaClientDataPath, "UserDefinedInputMappings"); UDTCompiler udtCompiler = new UDTCompiler(); if (Directory.Exists(udtDirectory)) { udtCompiler.Scan(udtDirectory); } if (!udtCompiler.DefinedTypes.Where(x => x.IsUserDefined).ToList().Any(x => x.Category == "ECA" && x.Identifier == "Phasor")) { UserDefinedType udt = new UserDefinedType(); udt.Identifier = "Phasor"; udt.Category = "ECA"; udt.Fields = new List <UDTField>(); UDTField magnitude = new UDTField(); magnitude.Type = new DataType { Category = "FloatingPoint", Identifier = "Double" }; magnitude.Identifier = "Magnitude"; udt.Fields.Add(magnitude); UDTField angle = new UDTField(); angle.Type = new DataType { Category = "FloatingPoint", Identifier = "Double" }; angle.Identifier = "Angle"; udt.Fields.Add(angle); UDTWriter udtWriter = new UDTWriter(); udtWriter.Types.Add(udt); udtWriter.WriteFiles(udtDirectory); } if (!udtCompiler.DefinedTypes.Where(x => x.IsUserDefined).ToList().Any(x => x.Category == "ECA" && x.Identifier == "VIPair")) { UserDefinedType udt = new UserDefinedType(); udt.Identifier = "VIPair"; udt.Category = "ECA"; udt.Fields = new List <UDTField>(); UDTField voltage = new UDTField(); voltage.Type = new DataType { Category = "ECA", Identifier = "Phasor" }; voltage.Identifier = "Voltage"; udt.Fields.Add(voltage); UDTField current = new UDTField(); current.Type = new DataType { Category = "ECA", Identifier = "Phasor" }; current.Identifier = "Current"; udt.Fields.Add(current); UDTWriter udtWriter = new UDTWriter(); udtWriter.Types.Add(udt); udtWriter.WriteFiles(udtDirectory); } udtCompiler = new UDTCompiler(); if (Directory.Exists(udtDirectory)) { udtCompiler.Scan(udtDirectory); } MappingCompiler mappingCompiler = new MappingCompiler(udtCompiler); if (Directory.Exists(udmDirectory)) { mappingCompiler.Scan(udmDirectory); } DataHub dataHub = new DataHub(); dataHub.Context = new HubCallerContext(null, Guid.NewGuid().ToString()); dataHub.RegisterMetadataReceivedHandler(() => { try { Program.LogStatus("Synchronizing ECA.Phasor mappings with accessible phasor meta-data..."); Dictionary <Guid, string> mappingLookup = new Dictionary <Guid, string>(); IEnumerable <PhasorDetail> phasorDetails = dataHub.GetPhasorDetails() ?? new PhasorDetail[0]; IEnumerable <PowerCalculation> powerCalculations = dataHub.GetPowerCalculation() ?? new PowerCalculation[0]; IEnumerable <MeasurementDetail> measurementDetails = dataHub.GetMeasurementDetails() ?? new MeasurementDetail[0]; MappingWriter mappingWriter = new MappingWriter(); foreach (PhasorDetail detail in phasorDetails) { Guid magnitudeID = measurementDetails.FirstOrDefault(measurement => measurement.DeviceAcronym == detail.DeviceAcronym && measurement.PhasorSourceIndex == detail.SourceIndex && (measurement.SignalAcronym?.Contains("PHM") ?? false))?.SignalID ?? Guid.Empty; Guid angleID = measurementDetails.FirstOrDefault(measurement => measurement.DeviceAcronym == detail.DeviceAcronym && measurement.PhasorSourceIndex == detail.SourceIndex && (measurement.SignalAcronym?.Contains("PHA") ?? false))?.SignalID ?? Guid.Empty; if (magnitudeID == Guid.Empty || angleID == Guid.Empty) { continue; } string identifier = (detail.DeviceAcronym + '_' + detail.Label + '_' + detail.Phase?.Replace(" ", "_").Replace("+", "pos").Replace("-", "neg") + '_' + detail.Type) .Replace("\\", "_").Replace("#", "").Replace("'", "").Replace("(", "").Replace(")", "").ReplaceCharacters('_', x => !char.IsLetterOrDigit(x)); if (mappingCompiler.DefinedMappings.All(typeMapping => typeMapping.Identifier != identifier)) { TypeMapping mapping = new TypeMapping { Identifier = identifier, Type = (UserDefinedType)udtCompiler.GetType("ECA", "Phasor") }; if (mapping.Type.Fields.Count > 1) { mapping.FieldMappings.Add(new FieldMapping { Field = mapping.Type.Fields[0], Expression = magnitudeID.ToString() }); mapping.FieldMappings.Add(new FieldMapping { Field = mapping.Type.Fields[1], Expression = angleID.ToString() }); mappingWriter.Mappings.Add(mapping); } } mappingLookup.Add(angleID, identifier); } foreach (PowerCalculation calculation in powerCalculations) { Guid voltageAngleID = calculation.VoltageAngleID; Guid currentAngleID = calculation.CurrentAngleID; string voltageMappingIdentifier; string currentMappingIdentifier; if (mappingLookup.TryGetValue(voltageAngleID, out voltageMappingIdentifier) && mappingLookup.TryGetValue(currentAngleID, out currentMappingIdentifier) && !string.IsNullOrEmpty(voltageMappingIdentifier) && !string.IsNullOrEmpty(currentMappingIdentifier)) { TypeMapping mapping = new TypeMapping { Identifier = $"{voltageMappingIdentifier}__{currentMappingIdentifier}" }; mapping.Identifier = mapping.Identifier.Replace("+", "pos").Replace("-", "neg").Replace("\\", "_").Replace("#", "").Replace("'", "").Replace("(", "").Replace(")", "").ReplaceCharacters('_', x => !char.IsLetterOrDigit(x)); mapping.Type = (UserDefinedType)udtCompiler.GetType("ECA", "VIPair"); if (mapping.Type.Fields.Count > 1) { mapping.FieldMappings.Add(new FieldMapping { Field = mapping.Type.Fields[0], Expression = voltageMappingIdentifier }); mapping.FieldMappings.Add(new FieldMapping { Field = mapping.Type.Fields[1], Expression = currentMappingIdentifier }); mappingWriter.Mappings.Add(mapping); } } } mappingWriter.WriteFiles(udmDirectory); Program.LogStatus("Completed synchronization of mappings with accessible phasor meta-data.", true); } catch (Exception ex) { Program.LogException(new InvalidOperationException($"Failed while synchronizing ECA.Phasor mappings with accessible phasor meta-data: {ex.Message}", ex), true); } finally { dataHub.OnDisconnected(true); } }); dataHub.InitializeSubscriptions(); }
public static void CheckPhasorTypesAndMappings() { string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string ecaClientDataPath = Path.Combine(appData, "Grid Protection Alliance", "openECAClient"); string udtDirectory = Path.Combine(ecaClientDataPath, "UserDefinedTypes"); string udmDirectory = Path.Combine(ecaClientDataPath, "UserDefinedMappings"); UDTCompiler udtCompiler = new UDTCompiler(); if (Directory.Exists(udtDirectory)) { udtCompiler.Scan(udtDirectory); } if (!udtCompiler.DefinedTypes.Where(x => x.IsUserDefined).ToList().Any(x => x.Category == "ECA" && x.Identifier == "Phasor")) { UserDefinedType udt = new UserDefinedType(); udt.Identifier = "Phasor"; udt.Category = "ECA"; udt.Fields = new List <UDTField>(); UDTField magnitude = new UDTField(); magnitude.Type = new DataType() { Category = "FloatingPoint", Identifier = "Double" }; magnitude.Identifier = "Magnitude"; udt.Fields.Add(magnitude); UDTField angle = new UDTField(); angle.Type = new DataType() { Category = "FloatingPoint", Identifier = "Double" }; angle.Identifier = "Angle"; udt.Fields.Add(angle); UDTWriter udtWriter = new UDTWriter(); udtWriter.Types.Add(udt); udtWriter.WriteFiles(udtDirectory); } udtCompiler = new UDTCompiler(); if (Directory.Exists(udtDirectory)) { udtCompiler.Scan(udtDirectory); } MappingCompiler mappingCompiler = new MappingCompiler(udtCompiler); if (Directory.Exists(udmDirectory)) { mappingCompiler.Scan(udmDirectory); } DataHub dataHub = new DataHub(); dataHub.Context = new HubCallerContext(null, Guid.NewGuid().ToString()); dataHub.RegisterMetadataRecieved(() => { IEnumerable <PhasorDetail> phasorDetails = dataHub.GetPhasorDetails(); List <MeasurementDetail> measurementDetails = dataHub.GetMeasurementDetails().ToList(); MappingWriter mappingWriter = new MappingWriter(); foreach (PhasorDetail pd in phasorDetails) { string identifier = (pd.DeviceAcronym + '_' + pd.Label + '_' + pd.Phase.Replace(" ", "_").Replace("+", "pos").Replace("-", "neg") + '_' + pd.Type) .Replace(" ", "_").Replace("\\", "_").Replace("/", "_").Replace("!", "_").Replace("-", "_").Replace("#", "").Replace("'", "").Replace("(", "").Replace(")", ""); if (!mappingCompiler.DefinedMappings.Any(x => x.Identifier == identifier)) { TypeMapping tm = new TypeMapping(); tm.Identifier = identifier; tm.Type = (UserDefinedType)udtCompiler.DefinedTypes.Find(x => x.Category == "ECA" && x.Identifier == "Phasor"); tm.FieldMappings.Add(new FieldMapping() { Field = tm.Type.Fields[0], Expression = measurementDetails.Find(x => x.DeviceAcronym == pd.DeviceAcronym && x.PhasorSourceIndex == pd.SourceIndex && x.SignalAcronym.Contains("PHM")).SignalID.ToString() }); tm.FieldMappings.Add(new FieldMapping() { Field = tm.Type.Fields[1], Expression = measurementDetails.Find(x => x.DeviceAcronym == pd.DeviceAcronym && x.PhasorSourceIndex == pd.SourceIndex && x.SignalAcronym.Contains("PHA")).SignalID.ToString() }); mappingWriter.Mappings.Add(tm); } } mappingWriter.WriteFiles(udmDirectory); }); dataHub.InitializeSubscriptions(); }
public List <InvalidUDTException> GetUDTCompilerErrors() { UDTCompiler udtCompiler = CreateUDTCompiler(); return(udtCompiler.BatchErrors); }