//****************************************************************************************************** // DataSourceRecord.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: // ---------------------------------------------------------------------------------------------------- // 05/03/2012 - Stephen C. Wills, Grid Protection Alliance // Generated original version of source code. // 12/17/2012 - Starlynn Danyelle Gilliam // Generated original version of source code. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Linq; using System.Text; using GSF.PQDIF.Physical; namespace GSF.PQDIF.Logical { /// /// Types of data sources. /// public static class DataSourceType { /// /// The ID for data source type Measure. /// public static readonly Guid Measure = new Guid("e6b51730-f747-11cf-9d89-0080c72e70a3"); /// /// The ID for data source type Manual. /// public static readonly Guid Manual = new Guid("e6b51731-f747-11cf-9d89-0080c72e70a3"); /// /// The ID for data source type Simulate. /// public static readonly Guid Simulate = new Guid("e6b51732-f747-11cf-9d89-0080c72e70a3"); /// /// The ID for data source type Benchmark. /// public static readonly Guid Benchmark = new Guid("e6b51733-f747-11cf-9d89-0080c72e70a3"); /// /// The ID for data source type Debug. /// public static readonly Guid Debug = new Guid("e6b51734-f747-11cf-9d89-0080c72e70a3"); /// /// Gets information about the data source type identified by the given ID. /// /// Globally unique identifier for the data source type. /// The information about the data source type. public static Identifier GetInfo(Guid dataSourceTypeID) { Identifier identifier; return DataSourceTypeLookup.TryGetValue(dataSourceTypeID, out identifier) ? identifier : null; } /// /// Converts the given data source type ID to a string containing the name of the data source type. /// /// The ID of the data source type to be converted to a string. /// A string containing the name of the data source type with the given ID. public static string ToString(Guid dataSourceTypeID) { return GetInfo(dataSourceTypeID)?.Name ?? dataSourceTypeID.ToString(); } private static Dictionary DataSourceTypeLookup { get { Tag dataSourceTypeTag = Tag.GetTag(DataSourceRecord.VendorIDTag); if (s_dataSourceTypeTag != dataSourceTypeTag) { s_dataSourceTypeLookup = dataSourceTypeTag.ValidIdentifiers.ToDictionary(id => Guid.Parse(id.Value)); s_dataSourceTypeTag = dataSourceTypeTag; } return s_dataSourceTypeLookup; } } private static Tag s_dataSourceTypeTag; private static Dictionary s_dataSourceTypeLookup; } /// /// Represents a data source record in a PQDIF file. The data source /// record contains information about the source of the data in an /// . /// public class DataSourceRecord { #region [ Members ] // Fields private readonly Record m_physicalRecord; #endregion #region [ Constructors ] /// /// Creates a new instance of the class. /// /// The physical structure of the data source record. private DataSourceRecord(Record physicalRecord) { m_physicalRecord = physicalRecord; } #endregion #region [ Properties ] /// /// Gets the physical structure of the data source record. /// public Record PhysicalRecord { get { return m_physicalRecord; } } /// /// Gets or sets the ID of the type of the data source. /// public Guid DataSourceTypeID { get { return m_physicalRecord.Body.Collection .GetScalarByTag(DataSourceTypeIDTag) .GetGuid(); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; ScalarElement dataSourceTypeIDElement = collectionElement.GetOrAddScalar(DataSourceTypeIDTag); dataSourceTypeIDElement.TypeOfValue = PhysicalType.Guid; dataSourceTypeIDElement.SetGuid(value); } } /// /// Gets or sets the ID of the vendor of the data source. /// public Guid VendorID { get { CollectionElement collectionElement = m_physicalRecord.Body.Collection; ScalarElement vendorIDElement = collectionElement.GetScalarByTag(VendorIDTag); if ((object)vendorIDElement == null) return Vendor.None; return vendorIDElement.GetGuid(); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; ScalarElement vendorIDElement = collectionElement.GetOrAddScalar(VendorIDTag); vendorIDElement.TypeOfValue = PhysicalType.Guid; vendorIDElement.SetGuid(value); } } /// /// Gets or sets the ID of the equipment. /// public Guid EquipmentID { get { CollectionElement collectionElement = m_physicalRecord.Body.Collection; ScalarElement equipmentIDElement = collectionElement.GetScalarByTag(EquipmentIDTag); if ((object)equipmentIDElement == null) return Guid.Empty; return equipmentIDElement.GetGuid(); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; ScalarElement equipmentIDElement = collectionElement.GetOrAddScalar(EquipmentIDTag); equipmentIDElement.TypeOfValue = PhysicalType.Guid; equipmentIDElement.SetGuid(value); } } /// /// Gets or sets the name of the data source. /// public string DataSourceName { get { CollectionElement collectionElement = m_physicalRecord.Body.Collection; VectorElement dataSourceNameElement = collectionElement.GetVectorByTag(DataSourceNameTag); return Encoding.ASCII.GetString(dataSourceNameElement.GetValues()).Trim((char)0); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; byte[] bytes = Encoding.ASCII.GetBytes(value + (char)0); collectionElement.AddOrUpdateVector(DataSourceNameTag, PhysicalType.Char1, bytes); } } /// /// Gets or sets the name of the data source Owner. /// public string DataSourceOwner { get { CollectionElement collectionElement = m_physicalRecord.Body.Collection; VectorElement dataSourceNameElement = collectionElement.GetVectorByTag(DataSourceOwnerTag); if ((object)dataSourceNameElement == null) return null; return Encoding.ASCII.GetString(dataSourceNameElement.GetValues()).Trim((char)0); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; byte[] bytes = Encoding.ASCII.GetBytes(value + (char)0); collectionElement.AddOrUpdateVector(DataSourceOwnerTag, PhysicalType.Char1, bytes); } } /// /// Gets or sets the name of the data source Location. /// public string DataSourceLocation { get { CollectionElement collectionElement = m_physicalRecord.Body.Collection; VectorElement dataSourceNameElement = collectionElement.GetVectorByTag(DataSourceLocationTag); return Encoding.ASCII.GetString(dataSourceNameElement.GetValues()).Trim((char)0); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; byte[] bytes = Encoding.ASCII.GetBytes(value + (char)0); collectionElement.AddOrUpdateVector(DataSourceLocationTag, PhysicalType.Char1, bytes); } } /// /// Gets or sets the longitude at which the data source is located. /// public uint Longitude { get { CollectionElement collectionElement = m_physicalRecord.Body.Collection; VectorElement dataSourceCoordinatesElement = collectionElement.GetVectorByTag(DataSourceCoordinatesTag); if ((object)dataSourceCoordinatesElement == null) return uint.MaxValue; return dataSourceCoordinatesElement.GetUInt4(0); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; VectorElement dataSourceCoordinatesElement = collectionElement.GetOrAddVector(DataSourceCoordinatesTag); dataSourceCoordinatesElement.TypeOfValue = PhysicalType.UnsignedInteger4; dataSourceCoordinatesElement.Size = 2; dataSourceCoordinatesElement.SetUInt4(0, value); } } /// /// Gets or sets the latitude at which the device is located. /// public uint Latitude { get { CollectionElement collectionElement = m_physicalRecord.Body.Collection; VectorElement dataSourceCoordinatesElement = collectionElement.GetVectorByTag(DataSourceCoordinatesTag); if ((object)dataSourceCoordinatesElement == null) return uint.MaxValue; return dataSourceCoordinatesElement.GetUInt4(1); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; VectorElement dataSourceCoordinatesElement = collectionElement.GetOrAddVector(DataSourceCoordinatesTag); dataSourceCoordinatesElement.TypeOfValue = PhysicalType.UnsignedInteger4; dataSourceCoordinatesElement.Size = 2; dataSourceCoordinatesElement.SetUInt4(1, value); } } /// /// Gets the definitions for the channels defined in the data source. /// public IList ChannelDefinitions { get { return m_physicalRecord.Body.Collection .GetCollectionByTag(ChannelDefinitionsTag) .GetElementsByTag(OneChannelDefinitionTag) .Cast() .Select(collection => new ChannelDefinition(collection, this)) .ToList(); } } /// /// Gets or sets the time that this data source record became effective. /// public DateTime Effective { get { return m_physicalRecord.Body.Collection .GetScalarByTag(EffectiveTag) .GetTimestamp(); } set { CollectionElement collectionElement = m_physicalRecord.Body.Collection; ScalarElement effectiveElement = collectionElement.GetOrAddScalar(EffectiveTag); effectiveElement.TypeOfValue = PhysicalType.Timestamp; effectiveElement.SetTimestamp(value); } } #endregion #region [ Methods ] /// /// Adds a new channel definition to the collection /// of channel definitions in this data source record. /// public ChannelDefinition AddNewChannelDefinition() { CollectionElement channelDefinitionElement = new CollectionElement() { TagOfElement = OneChannelDefinitionTag }; ChannelDefinition channelDefinition = new ChannelDefinition(channelDefinitionElement, this); channelDefinition.Phase = Phase.None; channelDefinition.QuantityMeasured = QuantityMeasured.None; channelDefinitionElement.AddElement(new CollectionElement() { TagOfElement = ChannelDefinition.SeriesDefinitionsTag }); CollectionElement channelDefinitionsElement = m_physicalRecord.Body.Collection.GetCollectionByTag(ChannelDefinitionsTag); if ((object)channelDefinitionsElement == null) { channelDefinitionsElement = new CollectionElement() { TagOfElement = ChannelDefinitionsTag }; m_physicalRecord.Body.Collection.AddElement(channelDefinitionsElement); } channelDefinitionsElement.AddElement(channelDefinitionElement); return channelDefinition; } /// /// Removes the given channel definition from the collection of channel definitions. /// /// The channel definition to be removed. public void Remove(ChannelDefinition channelDefinition) { CollectionElement channelDefinitionsElement = m_physicalRecord.Body.Collection.GetCollectionByTag(ChannelDefinitionsTag); List channelDefinitionElements; ChannelDefinition definition; if ((object)channelDefinitionsElement == null) return; channelDefinitionElements = channelDefinitionsElement.GetElementsByTag(OneChannelDefinitionTag).Cast().ToList(); foreach (CollectionElement channelDefinitionElement in channelDefinitionElements) { definition = new ChannelDefinition(channelDefinitionElement, this); if (Equals(channelDefinition, definition)) channelDefinitionsElement.RemoveElement(channelDefinitionElement); } } /// /// Removes the element identified by the given tag from the record. /// /// The tag of the element to be removed. public void RemoveElement(Guid tag) { m_physicalRecord.Body.Collection.RemoveElementsByTag(tag); } #endregion #region [ Static ] // Static Fields /// /// Tag that identifies the data source type. /// public static readonly Guid DataSourceTypeIDTag = new Guid("b48d8581-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the vendor ID. /// public static readonly Guid VendorIDTag = new Guid("b48d8582-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the equipment ID. /// public static readonly Guid EquipmentIDTag = new Guid("b48d8583-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the data source name. /// public static readonly Guid DataSourceNameTag = new Guid("b48d8587-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the data source owner. /// public static readonly Guid DataSourceOwnerTag = new Guid("b48d8588-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the data source owner. /// public static readonly Guid DataSourceLocationTag = new Guid("b48d8589-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the physical location of the data source. /// public static readonly Guid DataSourceCoordinatesTag = new Guid("b48d858b-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the channel definitions collection. /// public static readonly Guid ChannelDefinitionsTag = new Guid("b48d858d-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the a single channel definition in the collection. /// public static readonly Guid OneChannelDefinitionTag = new Guid("b48d858e-f5f5-11cf-9d89-0080c72e70a3"); /// /// Tag that identifies the time that the data source record becomes effective. /// public static readonly Guid EffectiveTag = new Guid("62f28183-f9c4-11cf-9d89-0080c72e70a3"); // Static Methods /// /// Creates a new data source record from scratch. /// /// The name of the data source to be created. /// The new data source record. public static DataSourceRecord CreateDataSourceRecord(string dataSourceName) { Guid recordTypeTag = Record.GetTypeAsTag(RecordType.DataSource); Record physicalRecord = new Record(recordTypeTag); DataSourceRecord dataSourceRecord = new DataSourceRecord(physicalRecord); DateTime now = DateTime.UtcNow; dataSourceRecord.DataSourceTypeID = DataSourceType.Simulate; dataSourceRecord.DataSourceName = dataSourceName; dataSourceRecord.Effective = now; CollectionElement bodyElement = physicalRecord.Body.Collection; bodyElement.AddElement(new CollectionElement() { TagOfElement = ChannelDefinitionsTag }); return dataSourceRecord; } /// /// Creates a new data source record from the given physical record /// if the physical record is of type data source. Returns null if /// it is not. /// /// The physical record used to create the data source record. /// The new data source record, or null if the physical record does not define a data source record. public static DataSourceRecord CreateDataSourceRecord(Record physicalRecord) { bool isValidDataSourceRecord = physicalRecord.Header.TypeOfRecord == RecordType.DataSource; return isValidDataSourceRecord ? new DataSourceRecord(physicalRecord) : null; } #endregion } }