Exemple #1
0
        public void ImportData(IEnumerable <UserDefinedType> userDefinedTypes, IEnumerable <TypeMapping> typeMappings)
        {
            if (!userDefinedTypes.Any())
            {
                return;
            }

            UDTWriter udtWriter = new UDTWriter();

            foreach (UserDefinedType type in userDefinedTypes)
            {
                udtWriter.Types.Add(type);
            }

            lock (s_udtLock)
                udtWriter.WriteFiles(s_udtDirectory);

            if (!typeMappings.Any())
            {
                return;
            }

            MappingWriter mappingWriter = new MappingWriter();

            foreach (TypeMapping mapping in typeMappings)
            {
                mappingWriter.Mappings.Add(mapping);
            }

            lock (s_udmLock)
                mappingWriter.WriteFiles(s_udmDirectory);
        }
Exemple #2
0
        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());
        }
Exemple #3
0
        public void AddUDT(UserDefinedType udt)
        {
            UDTWriter udtWriter = new UDTWriter();

            udtWriter.Types.Add(udt);

            lock (s_udtLock)
                udtWriter.WriteFiles(s_udtDirectory);
        }
Exemple #4
0
        public void ExportUDTs(IEnumerable <UserDefinedType> list, string file)
        {
            UDTWriter udtWriter = new UDTWriter();

            foreach (UserDefinedType udt in list)
            {
                udtWriter.Types.Add(udt);
            }

            lock (s_udtLock)
                udtWriter.Write(file);
        }
Exemple #5
0
        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);
        }
        // Writes the UDT and mapping files to the specified path, containing the specified types and mappings.
        private void WriteMappingsTo(string path, IEnumerable <UserDefinedType> userDefinedTypes, IEnumerable <TypeMapping> userDefinedMappings)
        {
            // Determine the paths to the UDT and mapping files
            string udtFilePath     = Path.Combine(path, "UserDefinedTypes.ecaidl");
            string mappingFilePath = Path.Combine(path, "UserDefinedMappings.ecamap");

            // Create the writers to generate the files
            UDTWriter     udtWriter     = new UDTWriter();
            MappingWriter mappingWriter = new MappingWriter();

            // Add the UDTs and mappings to the writers
            udtWriter.Types.AddRange(userDefinedTypes);
            mappingWriter.Mappings.AddRange(userDefinedMappings);

            // Generate the files
            udtWriter.Write(udtFilePath);
            mappingWriter.Write(mappingFilePath);
        }
Exemple #7
0
        public void ImportData(IEnumerable <UserDefinedType> userDefinedTypes, IEnumerable <TypeMapping> inputTypeMappings, IEnumerable <TypeMapping> outputTypeMappings)
        {
            if (userDefinedTypes.Any())
            {
                UDTWriter udtWriter = new UDTWriter();

                foreach (UserDefinedType type in userDefinedTypes)
                {
                    udtWriter.Types.Add(type);
                }

                lock (s_udtLock)
                    udtWriter.WriteFiles(s_udtDirectory);
            }

            if (inputTypeMappings.Any())
            {
                MappingWriter mappingWriter = new MappingWriter();

                foreach (TypeMapping mapping in inputTypeMappings)
                {
                    mappingWriter.Mappings.Add(mapping);
                }

                lock (s_udimLock)
                    mappingWriter.WriteFiles(s_udimDirectory);
            }
            if (outputTypeMappings.Any())
            {
                MappingWriter mappingWriter = new MappingWriter();

                foreach (TypeMapping mapping in outputTypeMappings)
                {
                    mappingWriter.Mappings.Add(mapping);
                }

                lock (s_udomLock)
                    mappingWriter.WriteFiles(s_udomDirectory);
            }
        }
Exemple #8
0
        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();
        }
Exemple #9
0
        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();
        }