//******************************************************************************************************
// DataCell.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/27/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 09/15/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
// ReSharper disable VirtualMemberCallInConstructor
namespace GSF.PhasorProtocols.SelFastMessage
{
///
/// Represents the SEL Fast Message implementation of a that can be sent or received.
///
[Serializable]
public class DataCell : DataCellBase
{
#region [ Constructors ]
///
/// Creates a new .
///
/// The reference to parent of this .
/// The associated with this .
public DataCell(IDataFrame parent, IConfigurationCell configurationCell)
: base(parent, configurationCell, (ushort)(StatusFlags.TSOK | StatusFlags.PMDOK), Common.MaximumPhasorValues, Common.MaximumAnalogValues, Common.MaximumDigitalValues)
{
}
///
/// Creates a new from specified parameters.
///
/// The reference to parent of this .
/// The associated with this .
/// If true, adds empty values for each defined configuration cell definition.
public DataCell(DataFrame parent, ConfigurationCell configurationCell, bool addEmptyValues)
: this(parent, configurationCell)
{
if (!addEmptyValues)
return;
// Define needed phasor values
foreach (IPhasorDefinition phasorDefinition in configurationCell.PhasorDefinitions)
PhasorValues.Add(new PhasorValue(this, phasorDefinition));
// Define a frequency and df/dt
FrequencyValue = new FrequencyValue(this, configurationCell.FrequencyDefinition);
}
///
/// Creates a new from serialization parameters.
///
/// The with populated with data.
/// The source for this deserialization.
protected DataCell(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endregion
#region [ Properties ]
///
/// Gets or sets the reference to parent of this .
///
public new DataFrame Parent
{
get => base.Parent as DataFrame;
set => base.Parent = value;
}
///
/// Gets or sets the associated with this .
///
public new ConfigurationCell ConfigurationCell
{
get => base.ConfigurationCell as ConfigurationCell;
set => base.ConfigurationCell = value;
}
///
/// Gets the numeric ID code for this .
///
public new uint IDCode => ConfigurationCell.IDCode;
///
/// Gets or sets status flags for this .
///
public new StatusFlags StatusFlags
{
get => (StatusFlags)base.StatusFlags;
set => base.StatusFlags = (ushort)value;
}
///
/// Gets or sets flag that determines if data of this is valid.
///
public override bool DataIsValid
{
get => (StatusFlags & StatusFlags.PMDOK) > 0;
set
{
if (value)
StatusFlags |= StatusFlags.PMDOK;
else
StatusFlags &= ~StatusFlags.PMDOK;
}
}
///
/// Gets or sets flag that determines if timestamp of this is valid based on GPS lock.
///
public override bool SynchronizationIsValid
{
get => (StatusFlags & StatusFlags.TSOK) > 0;
set
{
if (value)
StatusFlags |= StatusFlags.TSOK;
else
StatusFlags &= ~StatusFlags.TSOK;
}
}
///
/// Gets or sets of this .
///
public override DataSortingType DataSortingType
{
get => SynchronizationIsValid ? DataSortingType.ByTimestamp : DataSortingType.ByArrival;
set
{
// We just ignore this value as we have defined data sorting type as a derived value based on synchronization validity
}
}
///
/// Gets or sets flag that determines if source device of this is reporting an error.
///
/// SEL Fast Message doesn't define bits for device error.
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool DeviceError
{
get => false;
set
{
// We just ignore this value as SEL Fast Message defines no flags for data errors
}
}
///
/// Gets of this .
///
///
/// SEL Fast Message doesn't define any analog values.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override AnalogValueCollection AnalogValues => base.AnalogValues;
///
/// Gets of this .
///
///
/// SEL Fast Message doesn't define any digital values.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override DigitalValueCollection DigitalValues => base.DigitalValues;
///
/// of string based property names and values for the object.
///
public override Dictionary Attributes
{
get
{
Dictionary baseAttributes = base.Attributes;
baseAttributes.Add("SEL Status Flags", $"{(int)StatusFlags}: {StatusFlags}");
return baseAttributes;
}
}
#endregion
#region [ Methods ]
///
/// Parses the binary body image.
///
/// Binary image to parse.
/// Start index into to begin parsing.
/// Length of valid data within .
/// The length of the data that was parsed.
protected override int ParseBodyImage(byte[] buffer, int startIndex, int length)
{
ConfigurationCell configCell = ConfigurationCell;
IPhasorValue phasorValue;
int x, index = startIndex;
// Parse out frequency value
FrequencyValue = SelFastMessage.FrequencyValue.CreateNewValue(this, configCell.FrequencyDefinition, buffer, index, out int parsedLength);
index += parsedLength;
// Parse out phasor values
for (x = 0; x < configCell.PhasorDefinitions.Count; x++)
{
phasorValue = PhasorValue.CreateNewValue(this, configCell.PhasorDefinitions[x], buffer, index, out parsedLength);
PhasorValues.Add(phasorValue);
index += parsedLength;
}
// Parse out status flags
StatusFlags = (StatusFlags)BigEndian.ToUInt16(buffer, index);
index += 2;
// Return total parsed length
return index - startIndex;
}
#endregion
#region [ Static ]
// Static Methods
// Delegate handler to create a new SEL Fast Message data cell
internal static IDataCell CreateNewCell(IChannelFrame parent, IChannelFrameParsingState state, int index, byte[] buffer, int startIndex, out int parsedLength)
{
DataCell dataCell = new(parent as IDataFrame, (state as IDataFrameParsingState).ConfigurationFrame.Cells[index]);
parsedLength = dataCell.ParseBinaryImage(buffer, startIndex, 0);
return dataCell;
}
#endregion
}
}