/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System.Collections.Generic; namespace Opc.Ua.Server { /// /// Calculates the value of an aggregate. /// public class StatusAggregateCalculator : AggregateCalculator { /// /// Initializes the aggregate calculator. /// /// The aggregate function to apply. /// The start time. /// The end time. /// The processing interval. /// Whether to use stepped interpolation. /// The aggregate configuration. /// The telemetry context to use to create obvservability instruments public StatusAggregateCalculator( NodeId aggregateId, DateTimeUtc startTime, DateTimeUtc endTime, double processingInterval, bool stepped, AggregateConfiguration configuration, ITelemetryContext telemetry) : base(aggregateId, startTime, endTime, processingInterval, stepped, configuration, telemetry) { SetPartialBit = true; } /// /// Computes the value for the timeslice. /// protected override DataValue ComputeValue(TimeSlice slice) { if (!AggregateId.TryGetValue(out uint numericId)) { return base.ComputeValue(slice); } switch (numericId) { case Objects.AggregateFunction_DurationGood: return ComputeDurationGoodBad(slice, false, false); case Objects.AggregateFunction_DurationBad: return ComputeDurationGoodBad(slice, true, false); case Objects.AggregateFunction_PercentGood: return ComputeDurationGoodBad(slice, false, true); case Objects.AggregateFunction_PercentBad: return ComputeDurationGoodBad(slice, true, true); case Objects.AggregateFunction_WorstQuality: return ComputeWorstQuality(slice, false); case Objects.AggregateFunction_WorstQuality2: return ComputeWorstQuality(slice, true); default: return base.ComputeValue(slice); } } /// /// Calculates the DurationGood and DurationBad aggregates for the timeslice. /// protected DataValue ComputeDurationGoodBad(TimeSlice slice, bool isBad, bool usePercent) { // get the values in the slice. List? values = GetValuesWithSimpleBounds(slice); // check for empty slice. if (values == null || values.Count == 0) { return GetNoDataValue(slice); } // get the regions. List? regions = GetRegionsInValueSet(values, false, true); double duration = 0; double total = 0; for (int ii = 0; ii < regions!.Count; ii++) { total += regions[ii].Duration; if (isBad) { if (StatusCode.IsBad(regions[ii].StatusCode)) { duration += regions[ii].Duration; } } else if (StatusCode.IsGood(regions[ii].StatusCode)) { duration += regions[ii].Duration; } } if (usePercent) { duration = duration / total * 100; } // set the timestamp and status. var value = new DataValue( Variant.From(duration), StatusCodes.Good, GetTimestamp(slice), GetTimestamp(slice)); // return result. return value.WithStatus(value.StatusCode.WithAggregateBits(AggregateBits.Calculated)); } /// /// Calculates the DurationGood and DurationBad aggregates for the timeslice. /// protected DataValue ComputeWorstQuality(TimeSlice slice, bool includeBounds) { // get the values in the slice. List? values; if (!includeBounds) { values = GetValues(slice); } else { values = GetValuesWithSimpleBounds(slice); } // check for empty slice. if (values == null || values.Count == 0) { return GetNoDataValue(slice); } // get the regions. _ = GetRegionsInValueSet(values, false, true); StatusCode worstQuality = StatusCodes.Good; int badQualityCount = 0; int uncertainQualityCount = 0; for (int ii = 0; ii < values.Count; ii++) { StatusCode quality = values[ii].StatusCode; if (StatusCode.IsBad(quality)) { badQualityCount++; if (StatusCode.IsNotBad(worstQuality)) { worstQuality = quality.CodeBits; } continue; } if (StatusCode.IsUncertain(quality)) { uncertainQualityCount++; if (StatusCode.IsGood(worstQuality)) { worstQuality = quality.CodeBits; } } } // set the timestamp and status. var value = new DataValue( Variant.From(worstQuality), StatusCodes.Good, GetTimestamp(slice), GetTimestamp(slice)); value = value.WithStatus(value.StatusCode.WithAggregateBits(AggregateBits.Calculated)); if ((StatusCode.IsBad(worstQuality) && badQualityCount > 1) || (StatusCode.IsUncertain(worstQuality) && uncertainQualityCount > 1)) { value = value.WithStatus(value.StatusCode.WithAggregateBits( value.StatusCode.AggregateBits | AggregateBits.MultipleValues)); } // return result. return value; } } }