/// <summary>
 /// Returns true if this registration matches the application
 /// model provided.
 /// </summary>
 /// <param name="registration"></param>
 /// <param name="model"></param>
 /// <returns></returns>
 public static bool Matches(this ApplicationRegistration registration,
                            ApplicationInfoModel model)
 {
     if (registration == null)
     {
         return(model == null);
     }
     return(model != null &&
            registration.ApplicationId == model.ApplicationId &&
            registration.ApplicationType == model.ApplicationType &&
            registration.ApplicationUri == model.ApplicationUri &&
            registration.HostAddresses.DecodeAsList().SequenceEqualsSafe(
                model.HostAddresses) &&
            registration.CreateAuthorityId == model.Created?.AuthorityId &&
            registration.UpdateAuthorityId == model.Updated?.AuthorityId &&
            registration.CreateTime == model.Created?.Time &&
            registration.UpdateTime == model.Updated?.Time &&
            registration.DiscoveryProfileUri == model.DiscoveryProfileUri &&
            registration.GatewayServerUri == model.GatewayServerUri &&
            registration.NotSeenSince == model.NotSeenSince &&
            registration.DiscovererId == model.DiscovererId &&
            registration.SiteId == model.SiteId &&
            registration.Capabilities.DecodeAsSet().SetEqualsSafe(
                model.Capabilities?.Select(x =>
                                           VariantValueEx.SanitizePropertyName(x).ToUpperInvariant())) &&
            registration.DiscoveryUrls.DecodeAsList().SequenceEqualsSafe(
                model.DiscoveryUrls));
 }
        /// <summary>
        /// Convert string set to queryable dictionary
        /// </summary>
        /// <param name="set"></param>
        /// <param name="upperCase"></param>
        /// <returns></returns>
        public static Dictionary <string, bool> EncodeAsDictionary(this ISet <string> set,
                                                                   bool?upperCase = null)
        {
            if (set == null)
            {
                return(null);
            }
            var result = new Dictionary <string, bool>();

            foreach (var s in set)
            {
                var add = VariantValueEx.SanitizePropertyName(s);
                if (upperCase != null)
                {
                    add = (bool)upperCase ? add.ToUpperInvariant() : add.ToLowerInvariant();
                }
                result.Add(add, true);
            }
            return(result);
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public async Task <ApplicationInfoListModel> QueryAsync(
            ApplicationRegistrationQueryModel model, int?pageSize, CancellationToken ct)
        {
            var query = "SELECT * FROM devices WHERE " +
                        $"tags.{nameof(EntityRegistration.DeviceType)} = '{IdentityType.Application}' ";

            if (!(model?.IncludeNotSeenSince ?? false))
            {
                // Scope to non deleted applications
                query += $"AND NOT IS_DEFINED(tags.{nameof(EntityRegistration.NotSeenSince)}) ";
            }

            if (model?.Locale != null)
            {
                if (model?.ApplicationName != null)
                {
                    // If application name provided, include it in search
                    query += $"AND tags.{nameof(ApplicationRegistration.LocalizedNames)}" +
                             $".{model.Locale} = '{model.ApplicationName}' ";
                }
                else
                {
                    // Just search for locale
                    query += $"AND IS_DEFINED(tags.{nameof(ApplicationRegistration.LocalizedNames)}" +
                             $".{model.Locale}) ";
                }
            }
            else if (model?.ApplicationName != null)
            {
                // If application name provided, search for default name
                query += $"AND tags.{nameof(ApplicationRegistration.ApplicationName)} = " +
                         $"'{model.ApplicationName}' ";
            }
            if (model?.DiscovererId != null)
            {
                // If discoverer provided, include it in search
                query += $"AND tags.{nameof(ApplicationRegistration.DiscovererId)} = " +
                         $"'{model.DiscovererId}' ";
            }
            if (model?.ProductUri != null)
            {
                // If product uri provided, include it in search
                query += $"AND tags.{nameof(ApplicationRegistration.ProductUri)} = " +
                         $"'{model.ProductUri}' ";
            }
            if (model?.GatewayServerUri != null)
            {
                // If gateway uri provided, include it in search
                query += $"AND tags.{nameof(ApplicationRegistration.GatewayServerUri)} = " +
                         $"'{model.GatewayServerUri}' ";
            }
            if (model?.DiscoveryProfileUri != null)
            {
                // If discovery profile uri provided, include it in search
                query += $"AND tags.{nameof(ApplicationRegistration.DiscoveryProfileUri)} = " +
                         $"'{model.DiscoveryProfileUri}' ";
            }
            if (model?.ApplicationUri != null)
            {
                // If ApplicationUri provided, include it in search
                query += $"AND tags.{nameof(ApplicationRegistration.ApplicationUriLC)} = " +
                         $"'{model.ApplicationUri.ToLowerInvariant()}' ";
            }
            if (model?.ApplicationType == ApplicationType.Client ||
                model?.ApplicationType == ApplicationType.ClientAndServer)
            {
                // If searching for clients include it in search
                query += $"AND tags.{nameof(ApplicationType.Client)} = true ";
            }
            if (model?.ApplicationType == ApplicationType.Server ||
                model?.ApplicationType == ApplicationType.ClientAndServer)
            {
                // If searching for servers include it in search
                query += $"AND tags.{nameof(ApplicationType.Server)} = true ";
            }
            if (model?.ApplicationType == ApplicationType.DiscoveryServer)
            {
                // If searching for servers include it in search
                query += $"AND tags.{nameof(ApplicationType.DiscoveryServer)} = true ";
            }
            if (model?.Capability != null)
            {
                // If Capabilities provided, filter results
                var tag = VariantValueEx.SanitizePropertyName(model.Capability)
                          .ToUpperInvariant();
                query += $"AND tags.{tag} = true ";
            }
            if (model?.SiteOrGatewayId != null)
            {
                // If site or gateway id search provided, include it in search
                query += $"AND tags.{nameof(EntityRegistration.SiteOrGatewayId)} = " +
                         $"'{model.SiteOrGatewayId}' ";
            }

            var queryResult = await _iothub.QueryDeviceTwinsAsync(query, null, pageSize);

            return(new ApplicationInfoListModel {
                ContinuationToken = queryResult.ContinuationToken,
                Items = queryResult.Items
                        .Select(t => t.ToApplicationRegistration())
                        .Select(s => s.ToServiceModel())
                        .ToList()
            });
        }