예제 #1
0
        private async Task <string> CreateDeploymentGroupStepAsync()
        {
            string groupId        = _deviceTag;
            bool   createNewGroup = false;

            ConsoleEx.WriteLine(ConsoleColor.Yellow, "Querying deployment group...");
            try
            {
                Response <Group> groupResponse = await _devicesClient.GetGroupAsync(groupId);

                Console.WriteLine($"Deployment group {groupId} already exists.");
            }
            catch (RequestFailedException e)
            {
                if (e.Status == (int)HttpStatusCode.NotFound)
                {
                    createNewGroup = true;
                }
            }

            if (createNewGroup)
            {
                ConsoleEx.WriteLine(ConsoleColor.Yellow, "Creating deployment group...");
                Response <Group> groupResponse = await _devicesClient.CreateOrUpdateGroupAsync(
                    groupId,
                    new Group(
                        groupId,
                        GroupType.IoTHubTag,
                        new[]
                {
                    groupId
                },
                        DateTimeOffset.UtcNow.ToString()));

                if (groupResponse.Value != null)
                {
                    Console.WriteLine($"Group {groupId} created.");
                    Console.WriteLine();

                    ConsoleEx.WriteLine(ConsoleColor.Yellow, "Waiting for the group to be populated with devices...");
                    Console.WriteLine("(this may take about five minutes to complete)");
                    bool repeat = true;
                    while (repeat)
                    {
                        groupResponse = await _devicesClient.GetGroupAsync(groupId);

                        if (groupResponse.Value.DeviceCount > 0)
                        {
                            Console.WriteLine($"Deployment group {groupId} now has {groupResponse.Value.DeviceCount} devices.");
                            repeat = false;
                        }
                        else
                        {
                            Console.Write(".");
                            await Task.Delay(DefaultRetryAfterValue);
                        }
                    }
                }
            }

            Console.WriteLine();
            return(groupId);
        }