Creating a time series - AquaticInformatics/aquarius-sdk-net GitHub Wiki
Uses the Provisioning API to create a basic time-series.
The CreateBasicTimeSeries()
method below will take a <Parameter>.<Label>@<Location>
string identifier an create a basic time-series at the named location, using the named parameter and the given label.
Basic logic:
- Use a regular expression to parse the identifier string into its parameter, label, and location string components
- Get the matching location uniqueID from the Publish API
- Get the default monitoring method code from the Provisioning API
- Get the matching parameter DTO from the Provisioning API
- Use the parameter's default unit and interpolation type when creating the time-series
- Use a configurable default gap-tolerance when possible
- Make the POST request on the Provisioning API to create the time-series
- Return the unique ID of the created time-series.
- Throw an exception if something goes wrong
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Aquarius.TimeSeries.Client;
using Aquarius.TimeSeries.Client.ServiceModels.Provisioning;
using Aquarius.TimeSeries.Client.ServiceModels.Publish;
using NodaTime;
using InterpolationType = Aquarius.TimeSeries.Client.ServiceModels.Provisioning.InterpolationType;
using MonitoringMethod = Aquarius.TimeSeries.Client.ServiceModels.Provisioning.MonitoringMethod;
private IAquariusClient _client;
private static readonly Regex IdentifierRegex = new Regex(@"^(?<parameter>[^.]+)\.(?<label>[^@]+)@(?<location>.*)$");
public Guid CreateBasicTimeSeries(string timeSeriesIdentifier)
{
var match = IdentifierRegex.Match(timeSeriesIdentifier);
if (!match.Success)
throw new ArgumentException($"Can't parse '{timeSeriesIdentifier}' as time-series identifier. Expecting <Parameter>.<Label>@<Location>");
var locationIdentifier = match.Groups["location"].Value;
var parmeterIdentifier = match.Groups["parameter"].Value;
var label = match.Groups["label"].Value;
return CreateBasicTimeSeries(parmeterIdentifier, label, locationIdentifier);
}
private Guid CreateBasicTimeSeries(string parameterIdentifier, string label, string locationIdentifier)
{
var locationUniqueId = GetLocationUniqueId(locationIdentifier);
var defaultMonitoringMethod = GetDefaultMonitoringMethod();
var parameter = GetParameter(parameterIdentifier);
return _client.Provisioning
.Post(new PostBasicTimeSeries
{
LocationUniqueId = locationUniqueId,
Parameter = parameter.ParameterId,
Label = label,
Unit = parameter.UnitIdentifier,
InterpolationType = parameter.InterpolationType,
Method = defaultMonitoringMethod.MethodCode,
GapTolerance = GapToleranceForInterpolationType(parameter.InterpolationType)
})
.UniqueId;
}
private Guid GetLocationUniqueId(string locationIdentifier)
{
return _client.Publish
.Get(new LocationDescriptionListServiceRequest {LocationIdentifier = locationIdentifier})
.LocationDescriptions
.Single(locationDescription => locationDescription.Identifier == locationIdentifier)
.UniqueId;
}
private MonitoringMethod GetDefaultMonitoringMethod()
{
return _client.Provisioning
.Get(new GetMonitoringMethods())
.Results
.Single(monitoringMethod => monitoringMethod.ParameterUniqueId == Guid.Empty);
}
private Parameter GetParameter(string parameterIdentifier)
{
return _client.Provisioning
.Get(new GetParameters())
.Results
.Single(parameter => parameter.Identifier == parameterIdentifier);
}
private static Duration GapToleranceForInterpolationType(InterpolationType interpolationType)
{
return _defaultGapToleranceMinutes < 1 || InterpolationTypesWithNoGaps.Contains(interpolationType)
? NoGaps
: Duration.FromMinutes(_defaultGapToleranceMinutes);
}
private static int _defaultGapToleranceMinutes = 0; // TODO: change this as needed
private static readonly Duration NoGaps = DurationExtensions.MaxGapDuration; // Equivalent to the JSON "MaxDuration" constant.
private static readonly InterpolationType[] InterpolationTypesWithNoGaps =
{
InterpolationType.InstantaneousTotals,
InterpolationType.DiscreteValues,
};
Use the same logic as above, except use the PostReflectedTimeSeries
request DTO instead of the PostBasicTimeSeries
request DTO.
The TimeSeriesCreator.cs class contains code for both creating basic or reflected time-series, and for creating a location.