//****************************************************************************************************** // ArchivistOutputAdapter.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: // ---------------------------------------------------------------------------------------------------- // 10/31/2012 - J. Adam Crain // 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.Text; using Automatak.Archivist.Client; using Automatak.Archivist.Client.Impl; using Automatak.Archivist.Protocol; using GSF; using GSF.Diagnostics; using GSF.TimeSeries; using GSF.TimeSeries.Adapters; using Measurement = Automatak.Archivist.Protocol.Measurement; using Type = Automatak.Archivist.Protocol.Type; namespace ArchivistAdapters { /// /// Represents an output adapter that writes measurements to a CSV file. /// [Description("Archivist: Archives measurements to a an archivist/openArchiveMediator instance")] public class ArchivistOutputAdapter : OutputAdapterBase { #region [ Members ] // Fields private string m_host; private ushort m_port; private readonly int m_measurementCount; private IArchivistClient m_client; #endregion #region [ Constructors ] /// /// Initializes a new instance of the class. /// public ArchivistOutputAdapter() { m_host = "127.0.0.1"; m_port = 20000; m_measurementCount = 0; m_client = null; } #endregion #region [ Properties ] /// /// Gets or sets the host address /// [ConnectionStringParameter, Description("THe host address to which the adapter will attempt to connect"), DefaultValue("127.0.0.1")] public string Host { get { return m_host; } set { m_host = value; } } /// /// Gets or sets the host address /// [ConnectionStringParameter, Description("The port to which the adapter will attempt to connect"), DefaultValue(20000)] public ushort Port { get { return m_port; } set { m_port = value; } } /// /// Returns a flag that determines if measurements sent to this /// are destined for archival. /// public override bool OutputIsForArchive => true; /// /// Gets a flag that determines if this /// uses an asynchronous connection. /// protected override bool UseAsyncConnect => false; /// /// Returns the detailed status of this . /// public override string Status { get { StringBuilder status = new StringBuilder(); status.Append(base.Status); status.AppendFormat(" Host: {0}", m_host); status.AppendFormat(" Port: {0}", m_port); 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("host", out setting)) m_host = setting; if (settings.TryGetValue("port", out setting)) m_port = ushort.Parse(setting); PoolingMessageClient msgClient = new PoolingMessageClient(m_host, m_port); m_client = new DefaultArchivistClient(msgClient); } /// /// Attempts to connect to this . /// protected override void AttemptConnection() { } /// /// Attempts to disconnect from this . /// protected override void AttemptDisconnection() { m_client.Shutdown(); } /// /// Archives to remote archivist instance. /// /// Measurements to be archived. protected override void ProcessMeasurements(IMeasurement[] measurements) { if ((object)measurements != null) { try { ICollection meas = Convert(measurements); m_client.Insert(meas).Await().Get(); } catch (Exception ex) { OnProcessException(MessageLevel.Warning, ex); } } } /// /// 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 $"Archived {m_measurementCount} measurements to Archive.".CenterText(maxLength); } #endregion #region [ Static ] private static ICollection Convert(IMeasurement[] m) { List list = new List(); for (int i = 0; i < m.Length; ++i) list.Add(Convert(m[i])); return list; } private static MeasurementWithId Convert(IMeasurement m) { Measurement.Builder builder = Measurement.CreateBuilder(); builder.SetTime(m.Timestamp); builder.SetType(Type.FLOAT64); builder.SetDoubleValue(m.Value); builder.SetQuality(0); // TODO - convert quality types return MeasurementWithId.CreateBuilder().SetId(m.TagName).SetMeas(builder.Build()).Build(); } #endregion } }