コード例 #1
0
        public async Task <DeviceServiceListModel> GetListAsync(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);
        }
コード例 #2
0
        public async Task <List <DeviceDeploymentStatusServiceModel> > GetDeploymentStatusReport(string id, bool isLatest = true)
        {
            List <DeviceDeploymentStatusServiceModel> deviceDeploymentStatuses = new List <DeviceDeploymentStatusServiceModel>();
            var deploymentDetails = await this.GetAsync(id, true, isLatest);

            if (deploymentDetails != null && deploymentDetails.DeploymentMetrics != null && deploymentDetails.DeploymentMetrics.DeviceStatuses != null && deploymentDetails.DeploymentMetrics.DeviceStatuses.Keys.Count > 0)
            {
                string deviceQuery    = @"deviceId IN [{0}]";
                var    deviceIdsQuery = string.Join(",", deploymentDetails.DeploymentMetrics.DeviceStatuses.Keys.Select(d => $"'{d}'"));
                var    query          = string.Format(deviceQuery, deviceIdsQuery);

                DeviceServiceListModel devices = await this.GetDeviceListAsync(id, query, isLatest);

                foreach (var item in deploymentDetails.DeploymentMetrics.DeviceStatuses)
                {
                    var reportedProperties = devices.Items.First(x => x.Id == item.Key).Twin.ReportedProperties;

                    var json            = JToken.Parse(JsonConvert.SerializeObject(reportedProperties));
                    var fieldsCollector = new JsonFieldsCollector(json);
                    var fields          = fieldsCollector.GetAllFields();
                    deviceDeploymentStatuses.Add(new DeviceDeploymentStatusServiceModel(item.Key, item.Value, fields));
                }
            }

            return(deviceDeploymentStatuses);
        }
        public async Task GetCachedResultAsyncReturnsNullWhenStorageHasValueTest()
        {
            var queryString = this.random.NextString();
            var deviceId    = this.random.NextString();

            var mockResult = new DeviceServiceListModel(
                new List <DeviceServiceModel>
            {
                new DeviceServiceModel(
                    null,
                    deviceId,
                    0,
                    new DateTime(DateTimeOffset.Now.ToUnixTimeSeconds()),
                    false,
                    false,
                    false,
                    new DateTime(DateTimeOffset.Now.ToUnixTimeSeconds()),
                    new TwinServiceModel(),
                    new AuthenticationMechanismServiceModel(),
                    this.random.NextString()),
            });

            this.deviceQueryCache.SetTenantQueryResult(
                MockTenantId,
                queryString,
                new DeviceQueryCacheResultServiceModel
            {
                Result          = mockResult,
                ResultTimestamp = DateTimeOffset.Now,
            });

            this.mockStorage
            .Setup(x => x.QueryDocumentsAsync(
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <FeedOptions>(),
                       It.IsAny <SqlQuerySpec>(),
                       It.IsAny <int>(),
                       It.IsAny <int>()))
            .ReturnsAsync(new List <Document>
            {
                new Document(),
            });

            var result = await this.deviceQueryCache.GetCachedQueryResultAsync(MockTenantId, queryString);

            this.mockStorage
            .Verify(
                x => x.QueryDocumentsAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <FeedOptions>(),
                    It.IsAny <SqlQuerySpec>(),
                    It.IsAny <int>(),
                    It.IsAny <int>()),
                Times.Once);

            Assert.NotEqual(mockResult, result);
        }
コード例 #4
0
        private async Task <DeviceServiceListModel> GetDeviceListAsync(string deploymentId, List <string> deviceIds, string tenantId)
        {
            string query           = string.Empty;
            int    iotHublimit     = 500;
            string deviceListValue = string.Empty;

            var deploymentDeviceTask = this.GetDeploymentDevicesAsync(deploymentId, tenantId);

            DeviceServiceListModel allDevices = new DeviceServiceListModel(new List <DeviceServiceModel>(), null);

            if (deviceIds?.Count > 0)
            {
                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(",", batchDeviceIds.Select(p => $"'{p}'"));
                    }

                    query = $" deviceId IN [{deviceListValue}]";

                    var devices = await this.devices.GetListFromIoTHubAsync(query, null);

                    allDevices.Items.AddRange(devices.Items);

                    while (!string.IsNullOrWhiteSpace(devices.ContinuationToken))
                    {
                        devices = await this.devices.GetListFromIoTHubAsync(query, null);

                        allDevices.Items.AddRange(devices.Items);
                    }
                }

                var deploymentDeviceHistory = await deploymentDeviceTask;
                if (deploymentDeviceHistory != null && deploymentDeviceHistory.Items.Count > 0)
                {
                    Parallel.ForEach(allDevices.Items, item =>
                    {
                        var twin = deploymentDeviceHistory.Items.FirstOrDefault(i => i.DeviceId == item.Id)?.Twin;

                        if (twin != null)
                        {
                            item.Twin = twin;
                        }

                        item.PreviousTwin = deploymentDeviceHistory.Items.FirstOrDefault(i => i.DeviceId == item.Id)?.PreviousFirmwareTwin;
                    });
                }
            }

            return(allDevices);
        }
コード例 #5
0
 public DeviceListApiModel(DeviceServiceListModel devices)
 {
     this.Items             = new List <DeviceRegistryApiModel>();
     this.ContinuationToken = devices.ContinuationToken;
     foreach (var d in devices.Items)
     {
         this.Items.Add(new DeviceRegistryApiModel(d));
     }
 }
コード例 #6
0
        private async Task GetDevices(string query, string continuationToken, List <DeviceReportServiceModel> devices)
        {
            DeviceServiceListModel devicesFromQuery = null;

            devicesFromQuery = await this.GetListAsync(query, continuationToken);

            if (devicesFromQuery != null && devicesFromQuery.Items.Count() > 0)
            {
                devices.AddRange(devicesFromQuery.Items.Select(i => new DeviceReportServiceModel(i)));
                if (!string.IsNullOrWhiteSpace(devicesFromQuery.ContinuationToken))
                {
                    await this.GetDevices(query, devicesFromQuery.ContinuationToken, devices);
                }
            }
        }
コード例 #7
0
        private async Task GetDeviceTwins(string query, string continuationToken, List <TwinServiceModel> twins)
        {
            DeviceServiceListModel devices = null;

            devices = await this.devices.GetListAsync(query, null);

            if (devices != null && devices.Items.Count() > 0)
            {
                twins.AddRange(devices.Items.Select(i => i.Twin));
                if (!string.IsNullOrWhiteSpace(devices.ContinuationToken))
                {
                    await this.GetDeviceTwins(query, continuationToken, twins);
                }
            }
        }
コード例 #8
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);
        }
コード例 #9
0
        public async Task <DeviceServiceListModel> GetListAsync(string query, string continuationToken)
        {
            if (!string.IsNullOrWhiteSpace(query))
            {
                // Try to translate clauses to query
                query = QueryConditionTranslator.ToQueryString(query);
            }

            var resultModel = await this.deviceQueryCache.GetCachedQueryResultAsync(this.tenantConnectionHelper.TenantId, query);

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

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

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

            resultModel = new DeviceServiceListModel(
                twins.Result.Select(azureTwin => new DeviceServiceModel(
                                        azureTwin,
                                        this.tenantConnectionHelper.GetIotHubName(),
                                        connectedEdgeDevices.ContainsKey(azureTwin.DeviceId))),
                twins.ContinuationToken);
            this.deviceQueryCache.SetTenantQueryResult(
                this.tenantConnectionHelper.TenantId,
                query,
                new DeviceQueryCacheResultServiceModel
            {
                Result          = resultModel,
                ResultTimestamp = DateTimeOffset.Now,
            });

            return(resultModel);
        }
コード例 #10
0
        private async Task <List <TwinServiceModel> > GetDeviceProperties(IEnumerable <string> deviceIds)
        {
            List <TwinServiceModel> twins   = null;
            DeviceServiceListModel  devices = null;
            string deviceQuery = @"deviceId IN [{0}]";

            if (deviceIds != null && deviceIds.Count() > 0)
            {
                var deviceIdsQuery = string.Join(",", deviceIds.Select(d => $"'{d}'"));
                var query          = string.Format(deviceQuery, deviceIdsQuery);

                devices = await this.devices.GetListAsync(query, null);

                if (devices != null && devices.Items.Count() > 0)
                {
                    twins = new List <TwinServiceModel>();

                    twins.AddRange(devices.Items.Select(i => i.Twin));
                }
            }

            return(twins);
        }
コード例 #11
0
        /// <summary>
        /// Query devices
        /// </summary>
        /// <param name="query">
        /// Two types of query supported:
        /// 1. Serialized Clause list in JSON. Each clause includes three parts: key, operator and value
        /// 2. The "Where" clause of official IoTHub query string, except keyword "WHERE"
        /// </param>
        /// <param name="continuationToken">Continuation token. Not in use yet</param>
        /// <returns>List of devices</returns>
        public async Task <DeviceServiceListModel> GetListAsync(string query, string continuationToken)
        {
            if (!string.IsNullOrWhiteSpace(query))
            {
                // Try to translate clauses to query
                query = QueryConditionTranslator.ToQueryString(query);
            }

            var twins = await this.GetTwinByQueryAsync(QUERY_PREFIX,
                                                       query,
                                                       continuationToken,
                                                       MAX_GET_LIST);

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

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

            return(resultModel);
        }
コード例 #12
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);
        }
コード例 #13
0
        /// <summary>
        /// Query devices
        /// </summary>
        /// <returns>DeviceTwinName</returns>
        public async Task <DeviceTwinName> GetDeviceTwinNamesAsync()
        {
            DeviceServiceListModel content = await GetListAsync(string.Empty, string.Empty);

            return(content.GetDeviceTwinNames());
        }