Files

386 lines
17 KiB
C#

/* ========================================================================
* 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;
namespace Opc.Ua.Server
{
/// <summary>
/// Creates a new instance of an aggregate factory.
/// </summary>
public delegate IAggregateCalculator? AggregatorFactory(
NodeId aggregateId,
DateTimeUtc startTime,
DateTimeUtc endTime,
double processingInterval,
bool stepped,
AggregateConfiguration configuration,
ITelemetryContext telemetry);
/// <summary>
/// The set of built-in aggregate factories.
/// </summary>
public static class Aggregators
{
/// <summary>
/// Stores the mapping for a aggregate id to the calculator.
/// </summary>
private class FactoryMapping
{
public NodeId AggregateId { get; set; }
public QualifiedName AggregateName { get; set; }
public CreateInstance Calculator { get; set; } = null!;
}
private delegate IAggregateCalculator CreateInstance(
NodeId aggregateId,
DateTimeUtc startTime,
DateTimeUtc endTime,
double processingInterval,
bool stepped,
AggregateConfiguration configuration,
ITelemetryContext telemetry);
/// <summary>
/// Mapping for all of the standard aggregates.
/// </summary>
private static readonly FactoryMapping[] s_mappings =
[
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Interpolative,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Interpolative),
Calculator = (a, s, e, p, b, c, t) => new AggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Average,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Average),
Calculator = (a, s, e, p, b, c, t) => new AverageAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_TimeAverage,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_TimeAverage),
Calculator = (a, s, e, p, b, c, t) => new AverageAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_TimeAverage2,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_TimeAverage2),
Calculator = (a, s, e, p, b, c, t) => new AverageAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Total,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Total),
Calculator = (a, s, e, p, b, c, t) => new AverageAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Total2,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Total2),
Calculator = (a, s, e, p, b, c, t) => new AverageAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Minimum,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Minimum),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Maximum,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Maximum),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_MinimumActualTime,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_MinimumActualTime),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_MaximumActualTime,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_MaximumActualTime),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Range,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Range),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Minimum2,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Minimum2),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Maximum2,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Maximum2),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_MinimumActualTime2,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_MinimumActualTime2),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_MaximumActualTime2,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_MaximumActualTime2),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Range2,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Range2),
Calculator = (a, s, e, p, b, c, t) => new MinMaxAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Count,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Count),
Calculator = (a, s, e, p, b, c, t) => new CountAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_AnnotationCount,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_AnnotationCount),
Calculator = (a, s, e, p, b, c, t) => new CountAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_DurationInStateZero,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_DurationInStateZero),
Calculator = (a, s, e, p, b, c, t) => new CountAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_DurationInStateNonZero,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_DurationInStateNonZero),
Calculator = (a, s, e, p, b, c, t) => new CountAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_NumberOfTransitions,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_NumberOfTransitions),
Calculator = (a, s, e, p, b, c, t) => new CountAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Start,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Start),
Calculator = (a, s, e, p, b, c, t) => new StartEndAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_End,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_End),
Calculator = (a, s, e, p, b, c, t) => new StartEndAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_Delta,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_Delta),
Calculator = (a, s, e, p, b, c, t) => new StartEndAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_StartBound,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_StartBound),
Calculator = (a, s, e, p, b, c, t) => new StartEndAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_EndBound,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_EndBound),
Calculator = (a, s, e, p, b, c, t) => new StartEndAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_DeltaBounds,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_DeltaBounds),
Calculator = (a, s, e, p, b, c, t) => new StartEndAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_DurationGood,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_DurationGood),
Calculator = (a, s, e, p, b, c, t) => new StatusAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_DurationBad,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_DurationBad),
Calculator = (a, s, e, p, b, c, t) => new StatusAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_PercentGood,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_PercentGood),
Calculator = (a, s, e, p, b, c, t) => new StatusAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_PercentBad,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_PercentBad),
Calculator = (a, s, e, p, b, c, t) => new StatusAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_WorstQuality,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_WorstQuality),
Calculator = (a, s, e, p, b, c, t) => new StatusAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_WorstQuality2,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_WorstQuality2),
Calculator = (a, s, e, p, b, c, t) => new StatusAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_StandardDeviationPopulation,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_StandardDeviationPopulation),
Calculator = (a, s, e, p, b, c, t) => new StdDevAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_VariancePopulation,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_VariancePopulation),
Calculator = (a, s, e, p, b, c, t) => new StdDevAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_StandardDeviationSample,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_StandardDeviationSample),
Calculator = (a, s, e, p, b, c, t) => new StdDevAggregateCalculator(a, s, e, p, b, c, t)
},
new FactoryMapping
{
AggregateId = ObjectIds.AggregateFunction_VarianceSample,
AggregateName = QualifiedName.From(BrowseNames.AggregateFunction_VarianceSample),
Calculator = (a, s, e, p, b, c, t) => new StdDevAggregateCalculator(a, s, e, p, b, c, t)
}
];
/// <summary>
/// Returns the name for a standard aggregates.
/// </summary>
public static QualifiedName GetNameForStandardAggregate(NodeId aggregateId)
{
for (int ii = 0; ii < s_mappings.Length; ii++)
{
if (s_mappings[ii].AggregateId == aggregateId)
{
return s_mappings[ii].AggregateName;
}
}
return default;
}
/// <summary>
/// Returns the id for a standard aggregates.
/// </summary>
public static NodeId GetIdForStandardAggregate(QualifiedName aggregateName)
{
for (int ii = 0; ii < s_mappings.Length; ii++)
{
if (s_mappings[ii].AggregateName == aggregateName)
{
return s_mappings[ii].AggregateId;
}
}
return default;
}
/// <summary>
/// Creates a calculator for one of the standard aggregates.
/// </summary>
[Obsolete("Use CreateStandardCalculator with telemetry context.")]
public static IAggregateCalculator CreateStandardCalculator(
NodeId aggregateId,
DateTimeUtc startTime,
DateTimeUtc endTime,
double processingInterval,
bool stepped,
AggregateConfiguration configuration)
{
return CreateStandardCalculator(
aggregateId,
startTime,
endTime,
processingInterval,
stepped,
configuration,
null!)!;
}
/// <summary>
/// Creates a calculator for one of the standard aggregates.
/// </summary>
public static IAggregateCalculator? CreateStandardCalculator(
NodeId aggregateId,
DateTimeUtc startTime,
DateTimeUtc endTime,
double processingInterval,
bool stepped,
AggregateConfiguration configuration,
ITelemetryContext telemetry)
{
for (int ii = 0; ii < s_mappings.Length; ii++)
{
if (s_mappings[ii].AggregateId == aggregateId)
{
return s_mappings[ii].Calculator(
aggregateId,
startTime,
endTime,
processingInterval,
stepped,
configuration,
telemetry);
}
}
return null;
}
}
}