//****************************************************************************************************** // CsvOutputAdapter.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 04/06/2010 - Stephen C. Wills // Generated original version of source code. // 12/13/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Text; using GSF; using GSF.TimeSeries; using GSF.TimeSeries.Adapters; namespace CsvAdapters { /// /// Represents an output adapter that writes measurements to a CSV file. /// [Description("CSV: Archives measurements to a CSV file")] public class CsvOutputAdapter : OutputAdapterBase { #region [ Members ] // Fields private string m_fileName; private StreamWriter m_outStream; private int m_measurementCount; #endregion #region [ Constructors ] /// /// Initializes a new instance of the class. /// public CsvOutputAdapter() { m_fileName = "measurements.csv"; m_measurementCount = 0; } #endregion #region [ Properties ] /// /// Gets or sets the name of the CSV file. /// [ConnectionStringParameter, Description("Define the name of the CSV file to which measurements will be archived."), DefaultValue("measurements.csv"), CustomConfigurationEditor("GSF.TimeSeries.UI.WPF.dll", "GSF.TimeSeries.UI.Editors.FileDialogEditor", "type=save; defaultExt=.csv; filter=CSV files|*.csv|All files|*.*")] public string FileName { get { return m_fileName; } set { m_fileName = value; } } /// /// Returns a flag that determines if measurements sent to this /// are destined for archival. /// public override bool OutputIsForArchive { get { return true; } } /// /// Gets a flag that determines if this /// uses an asynchronous connection. /// protected override bool UseAsyncConnect { get { return false; } } /// /// Returns the detailed status of this . /// public override string Status { get { StringBuilder status = new StringBuilder(); status.Append(base.Status); status.AppendFormat(" File name: {0}", m_fileName); status.AppendLine(); return status.ToString(); } } #endregion #region [ Methods ] /// /// Initializes this . /// public override void Initialize() { base.Initialize(); Dictionary settings = Settings; string setting; // Load optional parameters if (settings.TryGetValue("fileName", out setting)) m_fileName = setting; } /// /// Attempts to connect to this . /// protected override void AttemptConnection() { m_outStream = new StreamWriter(m_fileName); m_outStream.WriteLine(s_fileHeader); } /// /// Attempts to disconnect from this . /// protected override void AttemptDisconnection() { m_outStream.Close(); } /// /// Archives locally. /// /// Measurements to be archived. protected override void ProcessMeasurements(IMeasurement[] measurements) { if ((object)measurements != null) { StringBuilder builder = new StringBuilder(); foreach (IMeasurement measurement in measurements) { builder.Append(measurement.ID); builder.Append(','); builder.Append(measurement.Key); builder.Append(','); builder.Append((long)measurement.Timestamp); builder.Append(','); builder.Append(measurement.AdjustedValue); builder.Append(Environment.NewLine); } m_outStream.Write(builder.ToString()); m_measurementCount += measurements.Length; } } /// /// Gets a short one-line status of this . /// /// Maximum length of the status message. /// Text of the status message. public override string GetShortStatus(int maxLength) { return string.Format("Archived {0} measurements to CSV file.", m_measurementCount).CenterText(maxLength); } #endregion #region [ Static ] // Static Fields private static string s_fileHeader = "Signal ID,Measurement Key,Timestamp,Value"; #endregion } }