protected DataValue ComputeDelta2(TimeSlice slice)
{
// get the values in the slice.
List<DataValue> values = GetValuesWithSimpleBounds(slice);
// check for empty slice.
if (values == null || values.Count == 0)
{
return GetNoDataValue(slice);
}
DataValue start = values[0];
DataValue end = values[values.Count-1];
// check for bad bounds.
if (StatusCode.IsBad(start.StatusCode) || StatusCode.IsBad(end.StatusCode))
{
return GetNoDataValue(slice);
}
// convert to doubles.
double startValue = 0;
TypeInfo originalType = null;
try
{
startValue = CastToDouble(start);
originalType = start.WrappedValue.TypeInfo;
}
catch (Exception)
{
startValue = Double.NaN;
}
double endValue = 0;
try
{
endValue = CastToDouble(end);
}
catch (Exception)
{
endValue = Double.NaN;
}
// check for bad bounds.
if (Double.IsNaN(startValue) || Double.IsNaN(endValue))
{
return GetNoDataValue(slice);
}
DataValue value = new DataValue();
value.SourceTimestamp = GetTimestamp(slice);
value.ServerTimestamp = GetTimestamp(slice);
if (StatusCode.IsNotGood(start.StatusCode) || StatusCode.IsNotGood(end.StatusCode))
{
value.StatusCode = StatusCodes.UncertainDataSubNormal;
}
value.StatusCode = value.StatusCode.SetAggregateBits(AggregateBits.Calculated);
// calculate delta.
double delta = endValue - startValue;
if (originalType != null && originalType.BuiltInType != BuiltInType.Double)
{
object delta2 = TypeInfo.Cast(delta, TypeInfo.Scalars.Double, originalType.BuiltInType);
value.WrappedValue = new Variant(delta2, originalType);
}
else
{
value.WrappedValue = new Variant(delta, TypeInfo.Scalars.Double);
}
// return result.
return value;
}
#endregion