예제 #1
0
        // Attempts to cast given frame into a Macrodyne configuration frame - theoretically this will
        // allow the same configuration frame to be used for any protocol implementation
        internal static ConfigurationFrame CastToDerivedConfigurationFrame(IConfigurationFrame sourceFrame, string configurationFileName, string deviceLabel)
        {
            // See if frame is already a Macrodyne frame (if so, we don't need to do any work)
            ConfigurationFrame derivedFrame = sourceFrame as ConfigurationFrame;

            if (derivedFrame == null)
            {
                // Create a new Macrodyne configuration frame converted from equivalent configuration information; Macrodyne only supports one device
                if (sourceFrame.Cells.Count > 0)
                {
                    IConfigurationCell sourceCell  = sourceFrame.Cells[0];
                    string             stationName = sourceCell.StationName;

                    if (string.IsNullOrEmpty(stationName))
                    {
                        stationName = "Unit " + sourceCell.IDCode.ToString();
                    }

                    stationName         = stationName.TruncateLeft(8);
                    derivedFrame        = new ConfigurationFrame(Common.GetFormatFlagsFromPhasorCount(sourceFrame.Cells[0].PhasorDefinitions.Count), stationName, configurationFileName, deviceLabel);
                    derivedFrame.IDCode = sourceFrame.IDCode;

                    // Create new derived configuration cell
                    ConfigurationCell    derivedCell = new ConfigurationCell(derivedFrame);
                    IFrequencyDefinition sourceFrequency;

                    // Create equivalent derived phasor definitions
                    foreach (IPhasorDefinition sourcePhasor in sourceCell.PhasorDefinitions)
                    {
                        derivedCell.PhasorDefinitions.Add(new PhasorDefinition(derivedCell, sourcePhasor.Label, sourcePhasor.PhasorType, null));
                    }

                    // Create equivalent derived frequency definition
                    sourceFrequency = sourceCell.FrequencyDefinition;

                    if (sourceFrequency != null)
                    {
                        derivedCell.FrequencyDefinition = new FrequencyDefinition(derivedCell)
                        {
                            Label = sourceFrequency.Label
                        };
                    }

                    // Create equivalent derived digital definitions
                    foreach (IDigitalDefinition sourceDigital in sourceCell.DigitalDefinitions)
                    {
                        derivedCell.DigitalDefinitions.Add(new DigitalDefinition(derivedCell, sourceDigital.Label));
                    }

                    // Add cell to frame
                    derivedFrame.Cells.Add(derivedCell);
                }
            }

            return(derivedFrame);
        }
예제 #2
0
        /// <summary>
        /// Reload Macrodyne INI based configuration file.
        /// </summary>
        public void Refresh()
        {
            if ((object)m_iniFile == null)
            {
                return;
            }

            // The only time we need an access lock is when we reload the config file...
            lock (m_iniFile)
            {
                if (File.Exists(m_iniFile.FileName))
                {
                    ConfigurationCell pmuCell;
                    int phasorCount, pmuCount, x, y;

                    m_defaultPhasorV   = new PhasorDefinition(null, 0, m_iniFile["DEFAULT", "PhasorV", DefaultVoltagePhasorEntry]);
                    m_defaultPhasorI   = new PhasorDefinition(null, 0, m_iniFile["DEFAULT", "PhasorI", DefaultCurrentPhasorEntry]);
                    m_defaultFrequency = new FrequencyDefinition(null, m_iniFile["DEFAULT", "Frequency", DefaultFrequencyEntry]);
                    FrameRate          = ushort.Parse(m_iniFile["CONFIG", "SampleRate", "30"]);

                    // We read all cells in the config file into their own configuration cell collection - cells parsed
                    // from the configuration frame will be mapped to their associated config file cell by ID label
                    // when the configuration cell is parsed from the configuration frame
                    if (m_configurationFileCells == null)
                    {
                        m_configurationFileCells = new ConfigurationCellCollection(int.MaxValue);
                    }

                    m_configurationFileCells.Clear();

                    // Load phasor data for each section in config file...
                    foreach (string section in m_iniFile.GetSectionNames())
                    {
                        if (section.Length > 0)
                        {
                            // Make sure this is not a special section
                            if (string.Compare(section, "DEFAULT", true) != 0 && string.Compare(section, "CONFIG", true) != 0)
                            {
                                // Create new PMU entry structure from config file settings...
                                phasorCount = int.Parse(m_iniFile[section, "NumberPhasors", "0"]);

                                // Check for PDC code
                                int pdcID = int.Parse(m_iniFile[section, "PDC", "-1"]);

                                if (pdcID == -1)
                                {
                                    // No PDC entry exists, assume this is a PMU
                                    pmuCell              = new ConfigurationCell(this);
                                    pmuCell.IDCode       = ushort.Parse(m_iniFile[section, "PMU", Cells.Count.ToString()]);
                                    pmuCell.SectionEntry = section; // This will automatically assign ID label as first 4 digits of section
                                    pmuCell.StationName  = m_iniFile[section, "Name", section];

                                    pmuCell.PhasorDefinitions.Clear();

                                    for (x = 0; x < phasorCount; x++)
                                    {
                                        pmuCell.PhasorDefinitions.Add(new PhasorDefinition(pmuCell, x + 1, m_iniFile[section, "Phasor" + (x + 1), DefaultVoltagePhasorEntry]));
                                    }

                                    pmuCell.FrequencyDefinition = new FrequencyDefinition(pmuCell, m_iniFile[section, "Frequency", DefaultFrequencyEntry]);
                                    m_configurationFileCells.Add(pmuCell);
                                }
                                else
                                {
                                    // This is a PDC, need to define one virtual entry for each PMU
                                    pmuCount = int.Parse(m_iniFile[section, "NumberPMUs", "0"]);

                                    for (x = 0; x < pmuCount; x++)
                                    {
                                        // Create a new PMU cell for each PDC entry that exists
                                        pmuCell = new ConfigurationCell(this);

                                        // For BPA INI files, PMUs tradionally have an ID number indexed starting at zero or one - so we multiply
                                        // ID by 1000 and add index to attempt to create a fairly unique ID to help optimize downstream parsing
                                        pmuCell.IDCode       = unchecked ((ushort)(pdcID * 1000 + x));
                                        pmuCell.SectionEntry = string.Format("{0}pmu{1}", section, x); // This will automatically assign ID label as first 4 digits of section
                                        pmuCell.StationName  = string.Format("{0} - Device {1}", m_iniFile[section, "Name", section], (x + 1));

                                        pmuCell.PhasorDefinitions.Clear();

                                        for (y = 0; y < 2; y++)
                                        {
                                            pmuCell.PhasorDefinitions.Add(new PhasorDefinition(pmuCell, y + 1, m_iniFile[section, "Phasor" + ((x * 2) + (y + 1)), DefaultVoltagePhasorEntry]));
                                        }

                                        pmuCell.FrequencyDefinition = new FrequencyDefinition(pmuCell, m_iniFile[section, "Frequency", DefaultFrequencyEntry]);
                                        m_configurationFileCells.Add(pmuCell);
                                    }
                                }
                            }
                        }
                    }

                    // Associate single Macrodyne cell with its associated cell hopefully defined in INI file
                    if (m_configurationFileCells.Count > 0 && (object)Cells != null && Cells.Count > 0)
                    {
                        ConfigurationCell configurationFileCell = null;

                        // Assign INI file cell associating by section entry
                        ConfigurationCell cell = Cells[0];

                        // Attempt to associate this configuration cell with information read from external INI based configuration file
                        m_configurationFileCells.TryGetBySectionEntry(cell.SectionEntry, ref configurationFileCell);
                        cell.ConfigurationFileCell = configurationFileCell;
                        m_onlineDataFormatFlags    = Common.GetFormatFlagsFromPhasorCount(cell.PhasorDefinitions.Count);
                        m_stationName = cell.StationName;
                    }
                }
                else
                {
                    throw new InvalidOperationException("Macrodyne config file \"" + m_iniFile.FileName + "\" does not exist.");
                }
            }

            // In case other classes want to know, we send out a notification that the config file has been reloaded (make sure
            // you do this after the write lock has been released to avoid possible dead-lock situations)
            if (ConfigurationFileReloaded != null)
            {
                ConfigurationFileReloaded(this, EventArgs.Empty);
            }
        }