protected DataValue ComputeDelta(TimeSlice slice)
{
// get the values in the slice.
List<DataValue> values = GetValues(slice);
// check for empty slice.
if (values == null || values.Count == 0)
{
return GetNoDataValue(slice);
}
// find start value.
DataValue start = null;
double startValue = 0;
TypeInfo originalType = null;
bool badDataSkipped = false;
for (int ii = 0; ii < values.Count; ii++)
{
start = values[ii];
if (StatusCode.IsGood(start.StatusCode))
{
try
{
startValue = CastToDouble(start);
originalType = start.WrappedValue.TypeInfo;
break;
}
catch (Exception)
{
startValue = Double.NaN;
}
}
start = null;
badDataSkipped = true;
}
// find end value.
DataValue end = null;
double endValue = 0;
for (int ii = values.Count - 1; ii >= 0; ii--)
{
end = values[ii];
if (StatusCode.IsGood(end.StatusCode))
{
try
{
endValue = CastToDouble(end);
break;
}
catch (Exception)
{
endValue = Double.NaN;
}
break;
}
end = null;
badDataSkipped = true;
}
// check if no good data.
if (Double.IsNaN(startValue) || Double.IsNaN(endValue))
{
return GetNoDataValue(slice);
}
DataValue value = new DataValue();
value.SourceTimestamp = GetTimestamp(slice);
value.ServerTimestamp = GetTimestamp(slice);
// set status code.
if (badDataSkipped)
{
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;
}