Exemple #1
0
        public async Task <DeviceStatisticsServiceModel> GetDeviceStatisticsAsync(string query)
        {
            ResultWithContinuationToken <List <DeviceConnectionStatusCountModel> > data = null;

            if (this.kustoEnabled)
            {
                if (!string.IsNullOrWhiteSpace(query))
                {
                    // Try to translate clauses to query
                    query = QueryConditionTranslator.ToADXQueryString(query);
                }

                data = await this.GetTwinDataADXQueryAsync <DeviceConnectionStatusCountModel>(
                    KustoQueryPrefix,
                    query,
                    DeviceConnectionStateCountKustoQuery);
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(query))
                {
                    // Try to translate clauses to query
                    query = QueryConditionTranslator.ToQueryString(query);
                }

                data = await this.GetIotDataQueryAsync <DeviceConnectionStatusCountModel>(
                    DeviceConnectionStateCountQueryPrefix,
                    query,
                    DeviceConnectionState,
                    null,
                    MaximumGetList); // Currently data does not show correct edge device connected status count. Will be supported in future.
            }

            return(new DeviceStatisticsServiceModel(data.Result));
        }
Exemple #2
0
        public async Task <DeviceServiceListModel> GetDeviceListAsync(string query, string continuationToken)
        {
            if (!string.IsNullOrWhiteSpace(query))
            {
                // Try to translate clauses to query
                query = QueryConditionTranslator.ToQueryString(query);
            }

            var twins = await this.GetTwinByQueryAsync(
                QueryPrefix,
                query,
                continuationToken,
                MaximumGetList);

            var connectedEdgeDevices = await this.GetConnectedEdgeDevices(twins.Result);

            var resultModel = new DeviceServiceListModel(
                twins.Result.Select(azureTwin => new DeviceServiceModel(
                                        azureTwin,
                                        this.tenantConnectionHelper.GetIotHubName(),
                                        connectedEdgeDevices.ContainsKey(azureTwin.DeviceId))),
                twins.ContinuationToken);

            return(resultModel);
        }
Exemple #3
0
        public static Configuration ToHubConfiguration(DeploymentServiceModel model)
        {
            var packageConfiguration = JsonConvert.DeserializeObject <Configuration>(model.PackageContent);

            if (model.PackageType.Equals(PackageType.EdgeManifest) &&
                packageConfiguration.Content?.DeviceContent != null)
            {
                throw new InvalidInputException("Deployment type does not match with package contents.");
            }
            else if (model.PackageType.Equals(PackageType.DeviceConfiguration) &&
                     packageConfiguration.Content?.ModulesContent != null)
            {
                throw new InvalidInputException("Deployment type does not match with package contents.");
            }

            var deploymentId  = Guid.NewGuid().ToString().ToLower();
            var configuration = new Configuration(deploymentId);

            configuration.Content = packageConfiguration.Content;

            var targetCondition = QueryConditionTranslator.ToQueryString(model.DeviceGroupQuery);

            configuration.TargetCondition = string.IsNullOrEmpty(targetCondition) ? "*" : targetCondition;
            configuration.Priority        = model.Priority;
            configuration.ETag            = string.Empty;
            configuration.Labels          = packageConfiguration.Labels ?? new Dictionary <string, string>();

            // Required labels
            configuration.Labels[PackageTypeLabel]       = model.PackageType.ToString();
            configuration.Labels[DeploymentNameLabel]    = model.Name;
            configuration.Labels[DeploymentGroupIdLabel] = model.DeviceGroupId;
            configuration.Labels[RmCreatedLabel]         = bool.TrueString;
            if (!string.IsNullOrEmpty(model.ConfigType))
            {
                configuration.Labels[ConfigTypeLabel] = model.ConfigType;
            }

            var customMetrics = packageConfiguration.Metrics?.Queries;

            if (customMetrics != null)
            {
                configuration.Metrics.Queries = SubstituteDeploymentIdIfPresent(
                    customMetrics,
                    deploymentId);
            }

            // Add optional labels
            if (model.DeviceGroupName != null)
            {
                configuration.Labels[DeploymentGroupNameLabel] = model.DeviceGroupName;
            }

            if (model.PackageName != null)
            {
                configuration.Labels[DeploymentPackageNameLabel] = model.PackageName;
            }

            return(configuration);
        }
Exemple #4
0
        public async Task <DeviceServiceListModel> GetListFromADXAsync(string inputQuery)
        {
            string querytoBeCached = inputQuery;

            if (!string.IsNullOrWhiteSpace(inputQuery))
            {
                // Try to translate clauses to query
                inputQuery = QueryConditionTranslator.ToADXQueryString(inputQuery);
            }

            DeviceServiceListModel resultModel = null;

            // Commented cache.
            // string tenantId = this.tenantConnectionHelper.TenantId;
            // resultModel = await this.deviceQueryCache.GetCachedQueryResultAsync(tenantId, querytoBeCached);
            // if (resultModel != null)
            // {
            //     return resultModel;
            // }
            string query           = string.Empty;
            string deviceListValue = string.Empty;
            var    allTwins        = await this.GetTwinDataADXQueryAsync <DeviceTwinMirrorModel>(
                KustoQueryPrefix,
                inputQuery,
                KustoOrderByQuery);

            var connectedEdgeDevices = await this.GetConnectedEdgeDevices(allTwins.Result.Select(x => x.Twin).ToList());

            resultModel = new DeviceServiceListModel(
                allTwins.Result.Select(azureTwin => new DeviceServiceModel(
                                           azureTwin.Twin,
                                           this.tenantConnectionHelper.GetIotHubName(),
                                           connectedEdgeDevices.ContainsKey(azureTwin.DeviceId),
                                           azureTwin.DeviceCreatedDate,
                                           azureTwin.TimeStamp)),
                allTwins.ContinuationToken);

            // Commented cache.
            // this.deviceQueryCache.SetTenantQueryResult(
            //    this.tenantConnectionHelper.TenantId,
            //    querytoBeCached,
            //    new DeviceQueryCacheResultServiceModel
            //    {
            //        Result = resultModel,
            //        ResultTimestamp = DateTimeOffset.Now,
            //    });
            return(resultModel);
        }
Exemple #5
0
        public async Task <DeviceServiceListModel> GetListFromIoTHubAsync(string inputQuery, string continuationToken)
        {
            string querytoBeCached = inputQuery;
            IEnumerable <QueryConditionClause> clauses         = null;
            IEnumerable <QueryConditionClause> deviceIdClauses = null;

            if (!string.IsNullOrWhiteSpace(inputQuery))
            {
                try
                {
                    clauses         = JsonConvert.DeserializeObject <IEnumerable <QueryConditionClause> >(inputQuery);
                    deviceIdClauses = clauses.Where(x => x.Key == "deviceId" && x.Operator == "LK").ToList();

                    if (deviceIdClauses != null && deviceIdClauses.Count() > 0)
                    {
                        clauses    = clauses.Where(x => x.Key != "deviceId" && x.Operator != "LK");
                        inputQuery = JsonConvert.SerializeObject(clauses);
                    }
                }
                catch
                {
                    // Any exception raised in deserializing will be ignored
                }

                if (!string.IsNullOrWhiteSpace(inputQuery))
                {
                    // Try to translate clauses to query
                    inputQuery = QueryConditionTranslator.ToQueryString(inputQuery);
                }
            }

            DeviceServiceListModel resultModel = null;
            string tenantId = this.tenantConnectionHelper.TenantId;

            if (string.IsNullOrWhiteSpace(continuationToken))
            {
                resultModel = await this.deviceQueryCache.GetCachedQueryResultAsync(tenantId, querytoBeCached);

                if (resultModel != null)
                {
                    return(resultModel);
                }
            }

            string query           = string.Empty;
            int    iotHublimit     = 500;
            string deviceListValue = string.Empty;
            ResultWithContinuationToken <List <Twin> > allTwins = new ResultWithContinuationToken <List <Twin> >(new List <Twin>(), continuationToken);

            if (deviceIdClauses != null && deviceIdClauses.Count() > 0)
            {
                foreach (var deviceIdClause in deviceIdClauses)
                {
                    List <string> deviceIds = await this.GetDevicesBasedOnInputDeviceString(deviceIdClause.Value.ToString().ToLower(), tenantId);

                    for (int i = 0; i < (deviceIds.Count / iotHublimit) + 1; i++)
                    {
                        if (i != 0 && (deviceIds.Count % (i * iotHublimit)) <= 0)
                        {
                            break;
                        }

                        List <string> batchDeviceIds = deviceIds.Skip(i * iotHublimit).Take(iotHublimit).ToList();
                        if (batchDeviceIds != null && batchDeviceIds.Count > 0)
                        {
                            // deviceListValue = $"({string.Join(" or ", deviceIds.Select(v => $"deviceId = '{v}'"))})";
                            deviceListValue = string.Join(",", batchDeviceIds.Select(p => $"'{p}'"));
                        }

                        if (!string.IsNullOrWhiteSpace(inputQuery))
                        {
                            // Try to translate clauses to query
                            query = $"{inputQuery} AND deviceId IN [{deviceListValue}]";
                        }
                        else
                        {
                            query = $" deviceId IN [{deviceListValue}]";
                        }

                        int countOfDevicestoFetch = string.IsNullOrWhiteSpace(deviceListValue) ? MaximumGetList : deviceIds.Count();

                        var twins = await this.GetTwinByQueryAsync(
                            QueryPrefix,
                            query,
                            continuationToken,
                            countOfDevicestoFetch);

                        allTwins.Result.AddRange(twins.Result.Except(allTwins.Result));
                        while (!string.IsNullOrWhiteSpace(twins.ContinuationToken))
                        {
                            twins = await this.GetTwinByQueryAsync(
                                QueryPrefix,
                                query,
                                continuationToken,
                                countOfDevicestoFetch);

                            allTwins.Result.AddRange(twins.Result);
                        }
                    }
                }
            }
            else
            {
                allTwins = await this.GetTwinByQueryAsync(
                    QueryPrefix,
                    inputQuery,
                    continuationToken,
                    MaximumGetList);
            }

            var connectedEdgeDevices = await this.GetConnectedEdgeDevices(allTwins.Result);

            resultModel = new DeviceServiceListModel(
                allTwins.Result.Select(azureTwin => new DeviceServiceModel(
                                           azureTwin,
                                           this.tenantConnectionHelper.GetIotHubName(),
                                           connectedEdgeDevices.ContainsKey(azureTwin.DeviceId))),
                allTwins.ContinuationToken);

            if (string.IsNullOrWhiteSpace(continuationToken))
            {
                this.deviceQueryCache.SetTenantQueryResult(
                    this.tenantConnectionHelper.TenantId,
                    querytoBeCached,
                    new DeviceQueryCacheResultServiceModel
                {
                    Result          = resultModel,
                    ResultTimestamp = DateTimeOffset.Now,
                });
            }

            return(resultModel);
        }
        public static Configuration ToHubConfiguration(DeploymentServiceModel model)
        {
            var packageConfiguration = JsonConvert.DeserializeObject <Configuration>(model.PackageContent);

            if (model.PackageType.Equals(PackageType.EdgeManifest) &&
                packageConfiguration.Content?.DeviceContent != null)
            {
                throw new InvalidInputException("Deployment type does not match with package contents.");
            }
            else if (model.PackageType.Equals(PackageType.DeviceConfiguration) &&
                     packageConfiguration.Content?.ModulesContent != null)
            {
                throw new InvalidInputException("Deployment type does not match with package contents.");
            }

            var deploymentId  = string.IsNullOrWhiteSpace(model.Id) ? Guid.NewGuid().ToString().ToLower() : model.Id;
            var configuration = new Configuration(deploymentId);

            configuration.Content = packageConfiguration.Content;

            // Model.TargetCondition will not be empty when synced from IoTHub to Cosmos, which will be used
            // while reactivating the deployment
            if (string.IsNullOrWhiteSpace(model.TargetCondition))
            {
                var targetCondition = QueryConditionTranslator.ToQueryString(model.DeviceGroupQuery);
                if (model.DeviceIds != null && model.DeviceIds.Any())
                {
                    string deviceIdCondition = $"({string.Join(" or ", model.DeviceIds.Select(v => $"deviceId = '{v}'"))})";
                    if (!string.IsNullOrWhiteSpace(targetCondition))
                    {
                        string[] conditions = { targetCondition, deviceIdCondition };
                        targetCondition = string.Join(" or ", conditions);
                    }
                    else
                    {
                        targetCondition = deviceIdCondition;
                    }
                }

                configuration.TargetCondition = string.IsNullOrEmpty(targetCondition) ? "*" : targetCondition;
            }
            else
            {
                configuration.TargetCondition = model.TargetCondition;
            }

            configuration.Priority = model.Priority;
            configuration.ETag     = string.Empty;
            configuration.Labels   = packageConfiguration.Labels ?? new Dictionary <string, string>();

            // Required labels
            configuration.Labels[PackageTypeLabel]       = model.PackageType.ToString();
            configuration.Labels[DeploymentNameLabel]    = model.Name;
            configuration.Labels[DeploymentGroupIdLabel] = model.DeviceGroupId;
            configuration.Labels[RmCreatedLabel]         = bool.TrueString;
            if (!string.IsNullOrEmpty(model.ConfigType))
            {
                configuration.Labels[ConfigTypeLabel] = model.ConfigType;
            }

            var customMetrics = packageConfiguration.Metrics?.Queries;

            if (customMetrics != null)
            {
                configuration.Metrics.Queries = SubstituteDeploymentIdIfPresent(
                    customMetrics,
                    deploymentId);
            }

            // Add optional labels
            if (model.DeviceGroupName != null)
            {
                configuration.Labels[DeploymentGroupNameLabel] = model.DeviceGroupName;
            }

            if (model.PackageName != null)
            {
                configuration.Labels[DeploymentPackageNameLabel] = model.PackageName;
            }

            return(configuration);
        }