Example #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);
        }
Example #2
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);
            }

            // normally we need deviceTwins for all devices to show device list
            var devices = await this.registry.GetDevicesAsync(MAX_GET_LIST);

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

            var twinsMap = twins.Result.ToDictionary(twin => twin.DeviceId, twin => twin);

            var devicesList          = devices.Where(dvc => twinsMap.ContainsKey(dvc.Id)).ToList();
            var connectedEdgeDevices = this.GetConnectedEdgeDevices(devicesList, twinsMap).Result;

            // since deviceAsync does not support continuationToken for now, we need to ignore those devices which does not shown in twins
            return(new DeviceServiceListModel(devicesList
                                              .Select(azureDevice => new DeviceServiceModel(azureDevice,
                                                                                            twinsMap[azureDevice.Id],
                                                                                            this.ioTHubHostName,
                                                                                            connectedEdgeDevices.ContainsKey(azureDevice.Id))),
                                              twins.ContinuationToken));
        }
        private EventData GetDeviceGroupsForADX(DeviceGroup deviceGroup, bool isDeleted)
        {
            JObject deviceGroupDeviceMappingJson = new JObject();

            deviceGroupDeviceMappingJson.Add(DeviceGroupId, deviceGroup.Id);
            deviceGroupDeviceMappingJson.Add(DeviceGroupName, deviceGroup.DisplayName);
            deviceGroupDeviceMappingJson.Add(DeviceGroupConditions, QueryConditionTranslator.ToADXQueryString(JsonConvert.SerializeObject(deviceGroup.Conditions)));
            deviceGroupDeviceMappingJson.Add(TimeStamp, DateTime.UtcNow);
            deviceGroupDeviceMappingJson.Add(IsDeleted, isDeleted);
            var byteMessage            = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(deviceGroupDeviceMappingJson));
            var deviceMappingEventData = new EventData(byteMessage);

            return(deviceMappingEventData);
        }
Example #4
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);
            }

            // normally we need deviceTwins for all devices to show device list
            var devices = await this.registry.GetDevicesAsync(MAX_GET_LIST);

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

            // since deviceAsync does not support continuationToken for now, we need to ignore those devices which does not shown in twins
            return(new DeviceServiceListModel(devices
                                              .Where(d => twins.Result.Exists(t => d.Id == t.DeviceId))
                                              .Select(azureDevice => new DeviceServiceModel(azureDevice, twins.Result.SingleOrDefault(t => t.DeviceId == azureDevice.Id), this.ioTHubHostName)),
                                              twins.ContinuationToken));
        }
Example #5
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);
        }
Example #6
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);
        }
Example #7
0
        private Configuration CreateEdgeConfiguration(DeploymentServiceModel model)
        {
            var deploymentId             = Guid.NewGuid().ToString().ToLower();
            var edgeConfiguration        = new Configuration(deploymentId);
            var packageEdgeConfiguration = JsonConvert.DeserializeObject <Configuration>(model.PackageContent);

            edgeConfiguration.Content = packageEdgeConfiguration.Content;

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

            edgeConfiguration.TargetCondition = string.IsNullOrEmpty(targetCondition) ? "*" : targetCondition;
            edgeConfiguration.Priority        = model.Priority;
            edgeConfiguration.ETag            = string.Empty;

            if (edgeConfiguration.Labels == null)
            {
                edgeConfiguration.Labels = new Dictionary <string, string>();
            }

            // Required labels
            edgeConfiguration.Labels.Add(DEPLOYMENT_NAME_LABEL, model.Name);
            edgeConfiguration.Labels.Add(DEPLOYMENT_GROUP_ID_LABEL, model.DeviceGroupId);
            edgeConfiguration.Labels.Add(RM_CREATED_LABEL, bool.TrueString);

            // Add optional labels
            if (model.DeviceGroupName != null)
            {
                edgeConfiguration.Labels.Add(DEPLOYMENT_GROUP_NAME_LABEL, model.DeviceGroupName);
            }
            if (model.PackageName != null)
            {
                edgeConfiguration.Labels.Add(DEPLOYMENT_PACKAGE_NAME_LABEL, model.PackageName);
            }

            return(edgeConfiguration);
        }
Example #8
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)
        {
            var response = await this.client.GetAllAsync(DEVICE_COLLECTION_ID);

            var requireddevicedata = response.Items.Select(CreateDeviceServiceModel);

            //// normally we need deviceTwins for all devices to show device list
            //var devices = await this.registry.GetDevicesAsync(MAX_GET_LIST);

            //var twins = await this.GetTwinByQueryAsync(QUERY_PREFIX,
            //                                           query,
            //                                           continuationToken,
            //                                           MAX_GET_LIST);
            //var twinsMap = twins.Result.ToDictionary(twin => twin.DeviceId, twin => twin);

            //var devicesList = devices.Where(dvc => twinsMap.ContainsKey(dvc.Id)).ToList();



            var twins = await this.client.GetAllAsync(DEVICE_TWIN_COLLECTION_ID);

            var requiredtwindata = twins.Items.Select(CreateTwinServiceModel);
            List <TwinServiceModel> filteredTwins = new List <TwinServiceModel>();

            if ((!string.IsNullOrWhiteSpace(query)) && (query != "[]"))
            {
                // Try to translate clauses to query

                query = QueryConditionTranslator.ToQueryString(query);
                var dictionary = query.Split(';').Select(part => part.Split('=')).Where(part => part.Length == 2).ToDictionary(sp => sp[0].Trim(), sp => JToken.Parse(sp[1].ToString().Trim()));
                foreach (var newitem in requiredtwindata)
                {
                    if (newitem.Tags != null)
                    {
                        var sum = dictionary.Keys.Intersect(newitem.Tags.Keys).ToDictionary(t => t, t => dictionary[t]);
                        if (sum.Count == dictionary.Count)
                        {
                            filteredTwins.Add(newitem);
                        }
                    }
                }
            }
            else
            {
                filteredTwins = requiredtwindata.ToList();
            }

            var twinsMap = filteredTwins.ToDictionary(twin => twin.DeviceId, twin => twin);

            var devicesList = requireddevicedata.Where(dvc => twinsMap.ContainsKey(dvc.Id)).ToList();

            // return new DeviceServiceListModel(devicesList);

            //var connectedEdgeDevices = this.GetConnectedEdgeDevices(devicesList, twinsMap).Result;

            //// since deviceAsync does not support continuationToken for now, we need to ignore those devices which does not shown in twins
            return(new DeviceServiceListModel(devicesList
                                              .Select(azureDevice => new DeviceServiceModel(azureDevice,
                                                                                            twinsMap[azureDevice.Id],
                                                                                            this.ioTHubHostName,
                                                                                            false, "text")),
                                              null));
        }