protected DataValue ComputeMinMax2(TimeSlice slice, int valueType, bool returnActualTime)
{
// get the values in the slice.
List<DataValue> values = GetValuesWithSimpleBounds(slice);
// check for empty slice.
if (values == null || values.Count == 0)
{
return GetNoDataValue(slice);
}
double minimumGoodValue = Double.MaxValue;
double maximumGoodValue = Double.MinValue;
DateTime minimumGoodTimestamp = DateTime.MinValue;
DateTime maximumGoodTimestamp = DateTime.MinValue;
StatusCode minimumGoodStatusCode = StatusCodes.Good;
StatusCode maximumGoodStatusCode = StatusCodes.Good;
TypeInfo minimumOriginalType = null;
TypeInfo maximumOriginalType = null;
bool duplicatesMinimumsExist = false;
bool duplicatesMaximumsExist = false;
bool goodValueExists = false;
for (int ii = 0; ii < values.Count; ii++)
{
double currentValue = 0;
DateTime currentTime = values[ii].SourceTimestamp;
StatusCode currentStatus = values[ii].StatusCode;
// ignore bad values (as determined by the TreatUncertainAsBad parameter).
if (!IsGood(values[ii]))
{
continue;
}
// convert to double.
try
{
currentValue = CastToDouble(values[ii]);
}
catch (Exception)
{
continue;
}
// skip endpoint if stepped.
if (currentTime == slice.EndTime)
{
if (Stepped)
{
break;
}
}
// check for new minimum.
if (minimumGoodValue > currentValue)
{
minimumGoodValue = currentValue;
minimumGoodTimestamp = currentTime;
minimumGoodStatusCode = currentStatus;
minimumOriginalType = values[ii].WrappedValue.TypeInfo;
duplicatesMinimumsExist = false;
goodValueExists = true;
}
// check for duplicate minimums.
else if (minimumGoodValue == currentValue)
{
duplicatesMinimumsExist = true;
}
// check for new maximum.
if (maximumGoodValue < currentValue)
{
maximumGoodValue = currentValue;
maximumGoodTimestamp = currentTime;
maximumGoodStatusCode = currentStatus;
maximumOriginalType = values[ii].WrappedValue.TypeInfo;
duplicatesMaximumsExist = false;
goodValueExists = true;
}
// check for duplicate maximums.
else if (maximumGoodValue == currentValue)
{
duplicatesMaximumsExist = true;
}
}
// check if at least on good value exists.
if (!goodValueExists)
{
return GetNoDataValue(slice);
}
// determine the calculated value to return.
object processedValue = null;
TypeInfo processedType = null;
DateTime processedTimestamp = DateTime.MinValue;
StatusCode processedStatusCode = StatusCodes.Good;
bool duplicatesExist = false;
if (valueType == 1)
{
processedValue = minimumGoodValue;
processedTimestamp = minimumGoodTimestamp;
processedStatusCode = minimumGoodStatusCode;
processedType = minimumOriginalType;
duplicatesExist = duplicatesMinimumsExist;
}
else if (valueType == 2)
{
processedValue = maximumGoodValue;
processedTimestamp = maximumGoodTimestamp;
processedStatusCode = maximumGoodStatusCode;
processedType = maximumOriginalType;
duplicatesExist = duplicatesMaximumsExist;
}
else if (valueType == 3)
{
processedValue = Math.Abs(maximumGoodValue - minimumGoodValue);
processedType = TypeInfo.Scalars.Double;
}
// set the status code.
StatusCode statusCode = processedStatusCode;
// set calculated if not returning actual time and value is not at the start time.
if (!returnActualTime && processedTimestamp != slice.StartTime && (statusCode.AggregateBits & AggregateBits.Interpolated) == 0)
{
statusCode = statusCode.SetAggregateBits(statusCode.AggregateBits | AggregateBits.Calculated);
}
// set the multiple values flags.
if (duplicatesExist)
{
statusCode = statusCode.SetAggregateBits(statusCode.AggregateBits | AggregateBits.MultipleValues);
}
// convert back to original datatype.
if (processedType != null && processedType.BuiltInType != BuiltInType.Double)
{
processedValue = TypeInfo.Cast(processedValue, TypeInfo.Scalars.Double, processedType.BuiltInType);
}
else
{
processedType = TypeInfo.Scalars.Double;
}
// create processed value.
DataValue value = new DataValue();
value.WrappedValue = new Variant(processedValue, processedType);
value.StatusCode = GetTimeBasedStatusCode(slice, values, statusCode);
// zero value if status is bad.
if (StatusCode.IsBad(value.StatusCode))
{
value.WrappedValue = Variant.Null;
}
if (returnActualTime)
{
// calculate effective time if end bound is used.
if (TimeFlowsBackward)
{
if (processedTimestamp == slice.StartTime)
{
processedTimestamp = processedTimestamp.AddMilliseconds(+1);
value.StatusCode = value.StatusCode.SetAggregateBits(value.StatusCode.AggregateBits | AggregateBits.Interpolated);
}
}
else
{
if (processedTimestamp == slice.EndTime)
{
processedTimestamp = processedTimestamp.AddMilliseconds(-1);
value.StatusCode = value.StatusCode.SetAggregateBits(value.StatusCode.AggregateBits | AggregateBits.Interpolated);
}
}
value.SourceTimestamp = processedTimestamp;
value.ServerTimestamp = processedTimestamp;
}
else
{
value.SourceTimestamp = GetTimestamp(slice);
value.ServerTimestamp = GetTimestamp(slice);
}
return value;
}
#endregion