//******************************************************************************************************
// ChannelDefinition.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/04/2012 - Stephen C. Wills, Grid Protection Alliance
// Generated original version of source code.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GSF.PQDIF.Physical;
namespace GSF.PQDIF.Logical
{
#region [ Enumerations ]
///
/// Phase types.
///
public enum Phase : uint
{
///
/// Phase is not applicable.
///
None = 0,
///
/// A-to-neutral.
///
AN = 1,
///
/// B-to-neutral.
///
BN = 2,
///
/// C-to-neutral.
///
CN = 3,
///
/// Neutral-to-ground.
///
NG = 4,
///
/// A-to-B.
///
AB = 5,
///
/// B-to-C.
///
BC = 6,
///
/// C-to-A.
///
CA = 7,
///
/// Residual - the vector or point-on-wave sum of Phases A, B, and C.
/// Should be zero in a perfectly balanced system.
///
Residual = 8,
///
/// Net - the vector or point-on-wave sum of Phases A, B, C and the
/// Neutral phase. Should be zero in a 4-wire system with no earth
/// return path.
///
Net = 9,
///
/// Positive sequence.
///
PositiveSequence = 10,
///
/// Negative sequence.
///
NegativeSequence = 11,
///
/// Zero sequence.
///
ZeroSequence = 12,
///
/// The value representing a total or other
/// summarizing value in a multi-phase system.
///
Total = 13,
///
/// The value representing average of 3 line-neutral values.
///
LineToNeutralAverage = 14,
///
/// The value representing average of 3 line-line values.
///
LineToLineAverage = 15,
///
/// The value representing the "worst" of the 3 phases.
///
Worst = 16,
///
/// DC Positive.
///
Plus = 17,
///
/// DC Negative.
///
Minus = 18,
///
/// Generic Phase 1.
///
General1 = 19,
///
/// Generic Phase 2.
///
General2 = 20,
///
/// Generic Phase 3.
///
General3 = 21,
///
/// Generic Phase 4.
///
General4 = 22,
///
/// Generic Phase 5.
///
General5 = 23,
///
/// Generic Phase 6.
///
General6 = 24,
///
/// Generic Phase 7.
///
General7 = 25,
///
/// Generic Phase 8.
///
General8 = 26,
///
/// Generic Phase 9.
///
General9 = 27,
///
/// Generic Phase 10.
///
General10 = 28,
///
/// Generic Phase 11.
///
General11 = 29,
///
/// Generic Phase 12.
///
General12 = 30,
///
/// Generic Phase 13.
///
General13 = 31,
///
/// Generic Phase 14.
///
General14 = 32,
///
/// Generic Phase 15.
///
General15 = 33,
///
/// Generic Phase 16.
///
General16 = 34
}
///
/// Physical quantity under measurement.
///
public enum QuantityMeasured : uint
{
///
/// None or not applicable.
///
None = 0,
///
/// Voltage.
///
Voltage = 1,
///
/// Current.
///
Current = 2,
///
/// Power - includes all data for a quantity or characteristic
/// derived from multiplying voltage and current components.
///
Power = 3,
///
/// Energy - includes all data from an integration of a quantity
/// or characteristic derived from multiplying voltage and current
/// components together.
///
Energy = 4,
///
/// Temperature.
///
Temperature = 5,
///
/// Pressure.
///
Pressure = 6,
///
/// Charge.
///
Charge = 7,
///
/// Electrical field.
///
ElectricalField = 8,
///
/// Magnetic field.
///
MagneticField = 9,
///
/// Velocity.
///
Velocity = 10,
///
/// Compass bearing.
///
Bearing = 11,
///
/// Applied force, electrical, mechanical etc.
///
Force = 12,
///
/// Torque.
///
Torque = 13,
///
/// Spatial position.
///
Position = 14,
///
/// Flux linkage Weber Turns.
///
FluxLinkage = 15,
///
/// Magnetic field density.
///
FluxDensity = 16,
///
/// Status data.
///
Status = 17
}
#endregion
///
/// Represents a channel definition in a PQDIF file. The channel
/// definition resides within a and
/// defines a .
///
public class ChannelDefinition : IEquatable
{
#region [ Members ]
// Fields
private readonly CollectionElement m_physicalStructure;
private readonly DataSourceRecord m_dataSource;
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the class.
///
/// The collection element which is the physical structure of the channel definition.
/// The data source in which the channel definition resides.
public ChannelDefinition(CollectionElement physicalStructure, DataSourceRecord dataSource)
{
m_dataSource = dataSource;
m_physicalStructure = physicalStructure;
}
#endregion
#region [ Properties ]
///
/// Gets the physical structure of the channel definition.
///
public CollectionElement PhysicalStructure
{
get
{
return m_physicalStructure;
}
}
///
/// Gets the data source record in which
/// the channel definition resides.
///
public DataSourceRecord DataSource
{
get
{
return m_dataSource;
}
}
///
/// Gets a string identifier for the channel definition.
///
public string ChannelName
{
get
{
VectorElement channelNameElement = m_physicalStructure.GetVectorByTag(ChannelNameTag);
if ((object)channelNameElement == null)
return null;
return Encoding.ASCII.GetString(channelNameElement.GetValues()).Trim((char)0);
}
set
{
byte[] bytes = Encoding.ASCII.GetBytes(value + (char)0);
m_physicalStructure.AddOrUpdateVector(ChannelNameTag, PhysicalType.Char1, bytes);
}
}
///
/// Gets the phase measured by the device.
///
public Phase Phase
{
get
{
return (Phase)m_physicalStructure
.GetScalarByTag(PhaseIDTag)
.GetUInt4();
}
set
{
ScalarElement phaseIDElement = m_physicalStructure.GetOrAddScalar(PhaseIDTag);
phaseIDElement.TypeOfValue = PhysicalType.UnsignedInteger4;
phaseIDElement.SetUInt4((uint)value);
}
}
///
/// Gets the quantity type ID, which specifies how the data
/// inside instances of this definition should be interpreted.
///
public Guid QuantityTypeID
{
get
{
return m_physicalStructure
.GetScalarByTag(QuantityTypeIDTag)
.GetGuid();
}
set
{
ScalarElement quantityTypeIDElement = m_physicalStructure.GetOrAddScalar(QuantityTypeIDTag);
quantityTypeIDElement.TypeOfValue = PhysicalType.Guid;
quantityTypeIDElement.SetGuid(value);
}
}
///
/// Gets the physical quantity under measurement
/// (Voltage, Current Power, etc).
///
public QuantityMeasured QuantityMeasured
{
get
{
return (QuantityMeasured)m_physicalStructure
.GetScalarByTag(QuantityMeasuredIDTag)
.GetUInt4();
}
set
{
ScalarElement quantityMeasuredIDElement = m_physicalStructure.GetOrAddScalar(QuantityMeasuredIDTag);
quantityMeasuredIDElement.TypeOfValue = PhysicalType.UnsignedInteger4;
quantityMeasuredIDElement.SetUInt4((uint)value);
}
}
///
/// Gets the name of the quantity.
///
public string QuantityName
{
get
{
VectorElement quantityNameElement = m_physicalStructure.GetVectorByTag(QuantityNameTag);
if ((object)quantityNameElement == null)
return null;
return Encoding.ASCII.GetString(quantityNameElement.GetValues()).Trim((char)0);
}
set
{
byte[] bytes = Encoding.ASCII.GetBytes(value + (char)0);
m_physicalStructure.AddOrUpdateVector(QuantityNameTag, PhysicalType.Char1, bytes);
}
}
///
/// Gets the series definitions defined in this channel definition.
///
public IList SeriesDefinitions
{
get
{
return m_physicalStructure.GetCollectionByTag(SeriesDefinitionsTag)
.GetElementsByTag(OneSeriesDefinitionTag)
.Cast()
.Select(collection => new SeriesDefinition(collection, this))
.ToList();
}
}
#endregion
#region [ Methods ]
///
/// Adds a new series definition to the collection
/// of series definitions in this channel definition.
///
public SeriesDefinition AddNewSeriesDefinition()
{
CollectionElement seriesDefinitionElement = new CollectionElement() { TagOfElement = OneSeriesDefinitionTag };
SeriesDefinition seriesDefinition = new SeriesDefinition(seriesDefinitionElement, this);
seriesDefinition.ValueTypeID = SeriesValueType.Val;
seriesDefinition.QuantityUnits = QuantityUnits.None;
seriesDefinition.QuantityCharacteristicID = QuantityCharacteristic.None;
seriesDefinition.StorageMethodID = StorageMethods.Values;
CollectionElement seriesDefinitionsElement = m_physicalStructure.GetCollectionByTag(SeriesDefinitionsTag);
if ((object)seriesDefinitionsElement == null)
{
seriesDefinitionsElement = new CollectionElement() { TagOfElement = SeriesDefinitionsTag };
m_physicalStructure.AddElement(seriesDefinitionsElement);
}
seriesDefinitionsElement.AddElement(seriesDefinitionElement);
return seriesDefinition;
}
///
/// Removes the given series definition from the collection of series definitions.
///
/// The series definition to be removed.
public void Remove(SeriesDefinition seriesDefinition)
{
CollectionElement seriesDefinitionsElement = m_physicalStructure.GetCollectionByTag(SeriesDefinitionsTag);
List seriesDefinitionElements;
SeriesDefinition definition;
if ((object)seriesDefinitionsElement == null)
return;
seriesDefinitionElements = seriesDefinitionsElement.GetElementsByTag(OneSeriesDefinitionTag).Cast().ToList();
foreach (CollectionElement seriesDefinitionElement in seriesDefinitionElements)
{
definition = new SeriesDefinition(seriesDefinitionElement, this);
if (Equals(seriesDefinition, definition))
seriesDefinitionsElement.RemoveElement(seriesDefinitionElement);
}
}
///
/// Indicates whether the current object is equal to another object of the same type.
///
/// An object to compare with this object.
/// true if the current object is equal to the parameter; otherwise, false.
public bool Equals(ChannelDefinition other)
{
if ((object)other == null)
return false;
return ReferenceEquals(m_physicalStructure, other.m_physicalStructure);
}
///
/// Determines whether the specified is equal to the current .
///
/// The object to compare with the current object.
/// true if the specified object is equal to the current object; otherwise, false.
/// 2
public override bool Equals(object obj)
{
return Equals(obj as ChannelDefinition);
}
///
/// Serves as a hash function for a particular type.
///
/// A hash code for the current .
/// 2
public override int GetHashCode()
{
return m_physicalStructure.GetHashCode();
}
#endregion
#region [ Static ]
// Static Fields
///
/// Tag that identifies the channel definition index.
///
public static readonly Guid ChannelDefinitionIndexTag = new Guid("b48d858f-f5f5-11cf-9d89-0080c72e70a3");
///
/// Tag that identifies the channel name.
///
public static readonly Guid ChannelNameTag = new Guid("b48d8590-f5f5-11cf-9d89-0080c72e70a3");
///
/// Tag that identifies the phase ID.
///
public static readonly Guid PhaseIDTag = new Guid("b48d8591-f5f5-11cf-9d89-0080c72e70a3");
///
/// Tag that identifies the quantity type.
///
public static readonly Guid QuantityTypeIDTag = new Guid("b48d8592-f5f5-11cf-9d89-0080c72e70a3");
///
/// Tag that identifies the quantity measured ID.
///
public static readonly Guid QuantityMeasuredIDTag = new Guid("c690e872-f755-11cf-9d89-0080c72e70a3");
///
/// Tag that identifies the quantity name.
///
public static readonly Guid QuantityNameTag = new Guid("b48d8595-f5f5-11cf-9d89-0080c72e70a3");
///
/// Tag that identifies the series definitions collection.
///
public static readonly Guid SeriesDefinitionsTag = new Guid("b48d8598-f5f5-11cf-9d89-0080c72e70a3");
///
/// Tag that identifies a single series definition within the collection.
///
public static readonly Guid OneSeriesDefinitionTag = new Guid("b48d859a-f5f5-11cf-9d89-0080c72e70a3");
// Static Methods
///
/// Creates a new channel definition belonging to the given data source record.
///
/// The data source record that the new channel definition belongs to.
/// The new channel definition.
public static ChannelDefinition CreateChannelDefinition(DataSourceRecord dataSourceRecord)
{
ChannelDefinition channelDefinition = dataSourceRecord.AddNewChannelDefinition();
channelDefinition.Phase = Phase.None;
channelDefinition.QuantityMeasured = QuantityMeasured.None;
CollectionElement physicalStructure = channelDefinition.PhysicalStructure;
physicalStructure.AddElement(new CollectionElement() { TagOfElement = SeriesDefinitionsTag });
return channelDefinition;
}
#endregion
}
}