//******************************************************************************************************
// IMeasurement.cs - Gbtc
//
// Copyright © 2010, 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 Eclipse Public License -v 1.0 (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/eclipse-1.0.php
//
// 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:
// ----------------------------------------------------------------------------------------------------
// 09/02/2010 - J. Ritchie Carroll
// Generated original version of source code.
// 04/14/2011 - J. Ritchie Carroll
// Added received and published timestamps for measurements.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
namespace GSF.TimeSeries
{
///
/// Method signature for function used to apply a value filter over a sequence of values.
///
/// Sequence of values over which to apply filter.
/// Result of filter applied to sequence of values.
public delegate double MeasurementValueFilterFunction(IEnumerable source);
#region [ Enumerations ]
///
/// Measurement state flags.
///
[Flags]
public enum MeasurementStateFlags : uint
{
///
/// Defines normal state.
///
Normal = (uint)Bits.Nil,
///
/// Defines bad data state.
///
BadData = (uint)Bits.Bit00,
///
/// Defines suspect data state.
///
SuspectData = (uint)Bits.Bit01,
///
/// Defines over range error, i.e., unreasonable high value.
///
OverRangeError = (uint)Bits.Bit02,
///
/// Defines under range error, i.e., unreasonable low value.
///
UnderRangeError = (uint)Bits.Bit03,
///
/// Defines alarm for high value.
///
AlarmHigh = (uint)Bits.Bit04,
///
/// Defines alarm for low value.
///
AlarmLow = (uint)Bits.Bit05,
///
/// Defines warning for high value.
///
WarningHigh = (uint)Bits.Bit06,
///
/// Defines warning for low value.
///
WarningLow = (uint)Bits.Bit07,
///
/// Defines alarm for flat-lined value, i.e., latched value test alarm.
///
FlatlineAlarm = (uint)Bits.Bit08,
///
/// Defines comparision alarm, i.e., outside threshold of comparison with a real-time value.
///
ComparisonAlarm = (uint)Bits.Bit09,
///
/// Defines rate-of-change alarm.
///
ROCAlarm = (uint)Bits.Bit10,
///
/// Defines bad value received.
///
ReceivedAsBad = (uint)Bits.Bit11,
///
/// Defines calculated value state.
///
CalcuatedValue = (uint)Bits.Bit12,
///
/// Defines calculation error with the value.
///
CalculationError = (uint)Bits.Bit13,
///
/// Defines calculation warning with the value.
///
CalculationWarning = (uint)Bits.Bit14,
///
/// Defines reserved quality flag.
///
ReservedQualityFlag = (uint)Bits.Bit15,
///
/// Defines bad time state.
///
BadTime = (uint)Bits.Bit16,
///
/// Defines suspect time state.
///
SuspectTime = (uint)Bits.Bit17,
///
/// Defines late time alarm.
///
LateTimeAlarm = (uint)Bits.Bit18,
///
/// Defines future time alarm.
///
FutureTimeAlarm = (uint)Bits.Bit19,
///
/// Defines upsampled state.
///
UpSampled = (uint)Bits.Bit20,
///
/// Defines downsampled state.
///
DownSampled = (uint)Bits.Bit21,
///
/// Defines discarded value state.
///
DiscardedValue = (uint)Bits.Bit22,
///
/// Defines reserved time flag.
///
ReservedTimeFlag = (uint)Bits.Bit23,
///
/// Defines user defined flag 1.
///
UserDefinedFlag1 = (uint)Bits.Bit24,
///
/// Defines user defined flag 2.
///
UserDefinedFlag2 = (uint)Bits.Bit25,
///
/// Defines user defined flag 3.
///
UserDefinedFlag3 = (uint)Bits.Bit26,
///
/// Defines user defined flag 4.
///
UserDefinedFlag4 = (uint)Bits.Bit27,
///
/// Defines user defined flag 5.
///
UserDefinedFlag5 = (uint)Bits.Bit28,
///
/// Defines system error state.
///
SystemError = (uint)Bits.Bit29,
///
/// Defines system warning state.
///
SystemWarning = (uint)Bits.Bit30,
///
/// Defines measurement error flag.
///
MeasurementError = (uint)Bits.Bit31
}
#endregion
///
/// Represents an interface for an abstract measurement value measured by a device at an exact time.
///
///
/// This interface abstractly represents a measured value at an exact time interval.
///
public interface IMeasurement : ITimeSeriesValue, IEquatable, IComparable, IComparable
{
///
/// Gets or sets the primary key of this .
///
MeasurementKey Key
{
get;
set;
}
///
/// Gets or sets the text based tag name of this .
///
string TagName
{
get;
set;
}
///
/// Gets the adjusted numeric value of this , taking into account the specified and offsets.
///
///
/// Implementors need to account for and in return value, e.g.:
/// return * +
///
///
double AdjustedValue
{
get;
}
///
/// Defines an offset to add to the value.
///
///
/// Implementors should make sure this value defaults to zero.
///
double Adder
{
get;
set;
}
///
/// Defines a mulplicative offset to apply to the value.
///
///
/// Implementors should make sure this value defaults to one.
///
double Multiplier
{
get;
set;
}
///
/// Gets or sets associated with this .
///
MeasurementStateFlags StateFlags
{
get;
set;
}
///
/// Gets or sets exact timestamp, in ticks, of when this was received (i.e., created).
///
///
/// Implementors should set this timestamp to be the ticks of of when this class was created.
/// The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001.
///
Ticks ReceivedTimestamp
{
get;
set;
}
///
/// Gets or sets exact timestamp, in ticks, of when this was published (post-processing).
///
///
/// The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001.
///
Ticks PublishedTimestamp
{
get;
set;
}
///
/// Gets or sets function used to apply a downsampling filter over a sequence of values.
///
MeasurementValueFilterFunction MeasurementValueFilter
{
get;
set;
}
}
///
/// Defines static extension functions for implementations.
///
///
/// These helper functions map to the previously defined corresponding properties to help with the transition of .
///
public static class IMeasurementExtensions
{
///
/// Returns true if is not set.
///
/// instance to test.
/// true if is not set.
public static bool ValueQualityIsGood(this IMeasurement measurement)
{
return (measurement.StateFlags & MeasurementStateFlags.BadData) == 0;
}
///
/// Returns true if is not set.
///
/// instance to test.
/// true if is not set.
public static bool TimestampQualityIsGood(this IMeasurement measurement)
{
return (measurement.StateFlags & MeasurementStateFlags.BadTime) == 0;
}
///
/// Returns true if is set.
///
/// instance to test.
/// true if is not set.
public static bool IsDiscarded(this IMeasurement measurement)
{
return (measurement.StateFlags & MeasurementStateFlags.DiscardedValue) > 0;
}
///
/// Returns true if is set.
///
/// instance to test.
/// true if is not set.
public static bool IsCalculated(this IMeasurement measurement)
{
return (measurement.StateFlags & MeasurementStateFlags.CalcuatedValue) > 0;
}
///
/// Returns the measurement ID if defined, otherwise the run-time signal ID associated with the measurement key.
///
/// instance to test.
/// Measurement ID if defined, otherwise the run-time signal ID associated with the measurement key.
public static Guid RuntimeSignalID(this IMeasurement measurement)
{
if (measurement.ID == Guid.Empty)
return measurement.Key.SignalID;
return measurement.ID;
}
///
/// Returns the values of a enumeration.
///
/// enumeration to convert.
/// values of the enumeration.
public static IEnumerable MeasurementKeys(this IEnumerable measurements)
{
if (measurements == null)
return new MeasurementKey[0];
return measurements.Select(m => m.Key);
}
}
}