/// <summary>
        /// Closes the archive file once the statistics are no longer needed.
        /// </summary>
        public void Close()
        {
            if ((object)m_archiveReader == null)
                return;

            m_archiveReader.Dispose();
            m_archiveReader = null;
            m_metadataRecords = null;
        }
        /// <summary>
        /// Causes the <see cref="StatisticsReader"/> to open the archive file and retrieve the statistics.
        /// </summary>
        public void Open()
        {
            m_archiveReader = new ArchiveReader();
            m_archiveReader.Open(ArchiveFilePath);

            m_metadataRecords = m_archiveReader.MetadataFile.Read()
                .Where(record => !string.IsNullOrEmpty(record.Name))
                .ToList();
        }
        /// <summary>
        /// Attempts to disconnect from this <see cref="LocalInputAdapter"/>.
        /// </summary>
        protected override void AttemptDisconnection()
        {
            if ((object)m_readTimer != null)
            {
                m_readTimer.Enabled = false;

                lock (m_readTimer)
                    m_dataReader = null;
            }

            if ((object)m_archiveReader != null)
            {
                m_archiveReader.HistoricFileListBuildStart -= m_archiveReader_HistoricFileListBuildStart;
                m_archiveReader.HistoricFileListBuildComplete -= m_archiveReader_HistoricFileListBuildComplete;
                m_archiveReader.HistoricFileListBuildException -= m_archiveReader_HistoricFileListBuildException;
                m_archiveReader.DataReadException -= m_archiveReader_DataReadException;
                m_archiveReader.Dispose();
            }

            m_archiveReader = null;
        }
        /// <summary>
        /// Attempts to connect to this <see cref="LocalInputAdapter"/>.
        /// </summary>
        protected override void AttemptConnection()
        {
            // This adapter is only engaged for history, so we don't process any data unless a temporal constraint is defined
            if (this.TemporalConstraintIsDefined())
            {
                // Turn off read timer if it's active
                m_readTimer.Enabled = false;

                // Attempt to open historian files
                if (Directory.Exists(m_archiveLocation))
                {
                    // Specified directory is a valid one.
                    string[] matches = Directory.GetFiles(m_archiveLocation, "*_archive*.d");

                    if (matches.Length > 0)
                    {
                        // Capture the instance name
                        string fileName = matches[0].Remove(matches[0].IndexOf("_archive", StringComparison.OrdinalIgnoreCase)) + "_archive.d";

                        // Setup historian reader
                        m_archiveReader = new ArchiveReader();
                        m_archiveReader.HistoricFileListBuildStart += m_archiveReader_HistoricFileListBuildStart;
                        m_archiveReader.HistoricFileListBuildComplete += m_archiveReader_HistoricFileListBuildComplete;
                        m_archiveReader.HistoricFileListBuildException += m_archiveReader_HistoricFileListBuildException;
                        m_archiveReader.DataReadException += m_archiveReader_DataReadException;

                        // Open the active archive
                        m_archiveReader.Open(fileName, m_archiveLocation);

                        try
                        {
                            // Start the data reader on its own thread so connection attempt can complete in a timely fashion...
                            ThreadPool.QueueUserWorkItem(StartDataReader);
                        }
                        catch (Exception ex)
                        {
                            // Process exception for logging
                            OnProcessException(MessageLevel.Warning, new InvalidOperationException("Failed to start data reader due to exception: " + ex.Message, ex));
                        }
                    }
                }
                else
                {
                    OnProcessException(MessageLevel.Warning, new InvalidOperationException("Cannot open historian files, directory does not exist: " + m_archiveLocation));
                }
            }
        }
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="LocalInputAdapter"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    if (disposing)
                    {
                        if ((object)m_readTimer != null)
                        {
                            m_readTimer.Elapsed -= m_readTimer_Elapsed;
                            m_readTimer.Dispose();
                            m_readTimer = null;
                        }

                        if ((object)m_archiveReader != null)
                        {
                            m_archiveReader.HistoricFileListBuildStart -= m_archiveReader_HistoricFileListBuildStart;
                            m_archiveReader.HistoricFileListBuildComplete -= m_archiveReader_HistoricFileListBuildComplete;
                            m_archiveReader.HistoricFileListBuildException -= m_archiveReader_HistoricFileListBuildException;
                            m_archiveReader.DataReadException -= m_archiveReader_DataReadException;
                            m_archiveReader.Dispose();
                            m_archiveReader.Dispose();
                            m_archiveReader = null;
                        }
                    }
                }
                finally
                {
                    m_disposed = true;          // Prevent duplicate dispose.
                    base.Dispose(disposing);    // Call base class Dispose().
                }
            }
        }
Example #6
0
File: Main.cs Project: rmc00/gsf
        public Main()
        {
            InitializeComponent();

            // Initialize UI.
            EnableWatermark();
            StartTimeInput.Value = DateTime.UtcNow.AddMinutes(-5);
            EndTimeInput.Value = DateTime.UtcNow;

            // Add version number to title
            this.Text = string.Format(this.Text, AssemblyInfo.EntryAssembly.Version.ToString(3));

            foreach (string port in SerialPort.GetPortNames())
            {
                SerialPortInput.Items.Add(port);
            }

            if (SerialPortInput.Items.Count > 0)
            {
                SerialPortInput.SelectedIndex = 0;
                SerialBaudRateInput.SelectedIndex = 4;
                SerialParityInput.SelectedIndex = 0;
                SerialStopBitsInput.SelectedIndex = 1;
            }
            else
            {
                // No serial ports where found on this machine so the option for serial output will be removed
                OutputChannelTabs.TabPages.Remove(SerialSettingsTab);
            }

            Application.DoEvents();

            // Initialize member variables.
            m_activeThreads = new List<Thread>();
            m_archiveReader = new ArchiveReader();
            m_archiveReader.RolloverStart += m_archiveReader_RolloverStart;
            m_archiveReader.RolloverComplete += m_archiveReader_RolloverComplete;
            m_archiveReader.HistoricFileListBuildStart += m_archiveReader_HistoricFileListBuildStart;
            m_archiveReader.HistoricFileListBuildComplete += m_archiveReader_HistoricFileListBuildComplete;
            m_archiveReader.HistoricFileListBuildException += m_archiveReader_HistoricFileListBuildException;
            m_archiveReader.DataReadException += m_archiveReader_DataReadException;
            m_lastSelectedArchiveLocation = ConfigurationFile.Current.Settings.General["ArchiveLocation", true].ValueAs("");

            // If last selected archive is not defined, try to a few default selections
            if (string.IsNullOrWhiteSpace(m_lastSelectedArchiveLocation) || !Directory.Exists(m_lastSelectedArchiveLocation))
            {
                // See if a local archive folder exists with a valid archive
                m_lastSelectedArchiveLocation = FilePath.GetAbsolutePath("Archive");

                if (!Directory.Exists(m_lastSelectedArchiveLocation) || Directory.GetFiles(m_lastSelectedArchiveLocation, "*_archive.d").Length == 0)
                {
                    // See if a local statistics folder exists with a valid archive
                    m_lastSelectedArchiveLocation = FilePath.GetAbsolutePath("Statistics");

                    // If neither of these folders exist, just leave setting blank
                    if (!Directory.Exists(m_lastSelectedArchiveLocation) || Directory.GetFiles(m_lastSelectedArchiveLocation, "*_archive.d").Length == 0)
                        m_lastSelectedArchiveLocation = "";
                }
            }

            // Update archive location text box to contain the archive location from the configuration file.
            ArchiveLocationInput.Text = m_lastSelectedArchiveLocation;
        }