public LocationInfo GetLocationByUniqueId(string uniqueIdText) { if (!Guid.TryParse(uniqueIdText, out var uniqueId)) { throw new ArgumentException($"Can't parse '{uniqueIdText}' as a unique ID"); } var locationInfo = LocationCache.SingleOrDefault(l => Guid.Parse(l.UniqueId) == uniqueId); if (locationInfo != null) { return(locationInfo); } try { var location = Client.Provisioning.Get(new GetLocation { LocationUniqueId = uniqueId }); return(AddLocationInfo(location)); } catch (WebServiceException) { Log.Error($"LocationUniqueId {uniqueId:N} does not exist"); throw new ArgumentException($"LocationUniqueId {uniqueId:N} does not exist"); } }
public LocationInfo GetLocationByIdentifier(string locationIdentifier) { var locationInfo = LocationCache.SingleOrDefault(l => l.LocationIdentifier.Equals(locationIdentifier, StringComparison.InvariantCultureIgnoreCase)); if (locationInfo != null) { return(locationInfo); } if (LocationAliases.TryGetValue(locationIdentifier, out var aliasedIdentifier)) { locationInfo = LocationCache.SingleOrDefault(l => l.LocationIdentifier.Equals(aliasedIdentifier, StringComparison.InvariantCultureIgnoreCase)); if (locationInfo != null) { return(locationInfo); } locationIdentifier = aliasedIdentifier; } var locationDescriptions = Client.Publish.Get(new LocationDescriptionListServiceRequest { LocationIdentifier = locationIdentifier }) .LocationDescriptions; var locationDescription = locationDescriptions .FirstOrDefault(l => l.Identifier == locationIdentifier); if (locationDescription == null) { var closeMatches = locationDescriptions .Where(l => l.Identifier.Equals(locationIdentifier, StringComparison.InvariantCultureIgnoreCase)) .ToList(); if (closeMatches.Count == 1) { locationDescription = closeMatches.Single(); } } if (locationDescription == null) { Log.Error(!locationDescriptions.Any() ? $"Location '{locationIdentifier}' does not exist." : $"Location '{locationIdentifier}' has ambiguously found {locationDescriptions.Count} matches: {string.Join(", ", locationDescriptions.Select(l => l.Identifier))}"); return(AddUnknownLocationInfo(locationIdentifier)); } var location = Client.Provisioning.Get(new GetLocation { LocationUniqueId = locationDescription.UniqueId }); return(AddLocationInfo(location)); }
private LocationInfo AddLocationInfo(string locationName, string locationIdentifier, Offset utcOffset, Guid?uniqueId = null) { const long dummyId = 0; var locationInfo = InternalConstructor <LocationInfo> .Invoke( locationName, locationIdentifier, dummyId, uniqueId ?? Guid.Empty, utcOffset.ToTimeSpan().TotalHours); if (LocationCache.SingleOrDefault(l => l.LocationIdentifier == locationInfo.LocationIdentifier) == null) { LocationCache.Add(locationInfo); } return(locationInfo); }