Finding the unique ID of a time series - AquaticInformatics/aquarius-sdk-net GitHub Wiki
Most of the objects operated upon by the public APIs are identified by a unique ID (a .NET GUID) which never changes for the lifetime of the object.
A time-series identifier string shown in the browser as "Stage.Logger@Location1" on Tuesday might be renamed on Wednesday to appear as "Stage.DataLogger@Riverbed". The unique ID for the time-series will remain unchanged at "74d837a46f914b138798fb6dcf35da13".
While the browser does have a way of showing the unique ID of a time-series, many AQTS users will simply be familiar with the identifier string, with its familiar <Parameter>.<Label>@<Location>
syntax.
The GetTimeSeriesUniqueId()
method below will take a string identifier and return the unique ID GUID for the time-series.
Basic logic:
- If the string is actually a valid GUID, just return it in GUID form. This will allow the method to avoid an HTTP request if the GUID was already known.
- Use a regular expression to parse out the location indentifer from the string.
- Ask the Publish API for the
TimeSeriesDescriptions
of all the time-series at the location. - Search the results for a
TimeSeriesDescription
whoseIdentifier
property matches the desired text. - Return the
UniqueId
of the found time-series. - Throw an exception if something isn't found.
using System.Linq;
using System.Text.RegularExpressions;
using Aquarius.TimeSeries.Client;
using Aquarius.TimeSeries.Client.ServiceModels.Publish;
public Guid GetTimeSeriesUniqueId(string timeSeriesIdentifier)
{
Guid uniqueId;
if (Guid.TryParse(timeSeriesIdentifier, out uniqueId))
return uniqueId;
var location = ParseLocationIdentifier(timeSeriesIdentifier);
var response = _client.Publish.Get(new TimeSeriesDescriptionServiceRequest {LocationIdentifier = location});
var timeSeriesDescription = response.TimeSeriesDescriptions.FirstOrDefault(t => t.Identifier == timeSeriesIdentifier);
if (timeSeriesDescription == null)
throw new ArgumentException($"Can't find '{timeSeriesIdentifier}' at location '{location}'");
return timeSeriesDescription.UniqueId;
}
private static string ParseLocationIdentifier(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>");
return match.Groups["location"].Value;
}
private static readonly Regex IdentifierRegex = new Regex(@"^(?<parameter>[^.]+)\.(?<label>[^@]+)@(?<location>.*)$");