示例#1
0
        private async Task <string> DeployUpdateStepAsync(string provider, string name, string version, string groupId)
        {
            ConsoleEx.WriteLine(ConsoleColor.Yellow, "Deploying the update to a device...");
            string groupid      = "groupid";
            string deploymentId = $"{groupid}-{version.Replace(".", "-")}";
            var    data         = new
            {
                deploymentId,
                startDateTime = DateTime.UtcNow.ToString("O"),
                groupId,
                updateId = new
                {
                    manufacturer = provider,
                    name,
                    version
                }
            };
            Response deployment = await _managementClient.CreateOrUpdateDeploymentAsync(deploymentId, groupid, RequestContent.Create(data), new RequestContext());

            var deploymentDoc = JsonDocument.Parse(deployment.Content.ToMemory());

            Console.WriteLine($"Deployment '{deploymentDoc.RootElement.GetProperty("deploymentId").GetString()}' is created.");
            await Task.Delay(DefaultRetryAfterValue);

            Console.WriteLine("Checking the deployment status...");
            bool repeat = true;

            while (repeat)
            {
                var deploymentStatusResponse = await _managementClient.GetDeploymentStatusAsync(groupId, deploymentId);

                var deploymentStatusDoc = JsonDocument.Parse(deploymentStatusResponse.Content.ToMemory());
                if (deploymentStatusDoc.RootElement.TryGetProperty("devicesCompletedSucceededCount", out var deviceCountValue) && deviceCountValue.GetInt32() > 0)
                {
                    Console.WriteLine($"\nDeployment {deploymentId} successfully deployed to {deviceCountValue.GetInt32()} devices.");
                    repeat = false;
                }
                else
                {
                    Console.Write(".");
                    await Task.Delay(DefaultRetryAfterValue);
                }
            }
            Console.WriteLine();
            return(deploymentId);
        }
示例#2
0
        public async Task GroupAndDeployment()
        {
            DeviceManagementClient client = CreateClient();
            string groupid = "joegroup";

            /* list groups. */
            AsyncPageable <BinaryData> fetchResponse = client.GetGroupsAsync(new RequestContext());
            int counter = 0;

            await foreach (var item in fetchResponse)
            {
                Assert.IsNotNull(item);
                counter++;
            }

            Assert.IsTrue(counter > 0);

            /* create a group. */
            var body = new
            {
                groupId         = groupid,
                tags            = new string[] { groupid },
                createdDateTime = "2021-11-17T16:29:56.5770502+00:00",
                groupType       = "DeviceClassIdAndIoTHubTag",
                deviceClassId   = "0919e3ae422a2bfa8c84ff905813e60351e456d1"
            };
            Response createResponse = await client.CreateOrUpdateGroupAsync(groupid, RequestContent.Create(body), new RequestContext());

            Assert.IsTrue(createResponse.Status == 200);

            /* get a group. */
            Response getResponse = await client.GetGroupAsync(groupid, new RequestContext());

            Assert.IsTrue(getResponse.Status == 200);

            /* create a deployment. */
            string deploymentid   = "testdeployment1";
            var    deploymentBody = new
            {
                deploymentId  = deploymentid,
                startDateTime = "2021-09-02T16:29:56.5770502Z",
                groupId       = groupid,
                updateId      = new
                {
                    provider = "fabrikam",
                    name     = "vacuum",
                    version  = "2021.1117.1036.48"
                }
            };
            Response createDeploymentResponse = await client.CreateOrUpdateDeploymentAsync(groupid, deploymentid, RequestContent.Create(deploymentBody), new RequestContext());

            Assert.IsTrue(createDeploymentResponse.Status == 200);
            /* get deployment. */
            Response getDepoloymentResponse = await client.GetDeploymentAsync(groupid, deploymentid, new RequestContext());

            Assert.IsTrue(getDepoloymentResponse.Status == 200);

            /* delete deployment. */
            Response deleteDeploymentResponse = await client.DeleteDeploymentAsync(groupid, deploymentid, new RequestContext());

            Assert.IsTrue(deleteDeploymentResponse.Status == 204);

            /* delete group. */
            Response deleteGroupResponse = await client.DeleteGroupAsync(groupid, new RequestContext());

            Assert.IsTrue(deleteGroupResponse.Status == 204);
        }
示例#3
0
        /// <summary>
        /// Device Update for IoT Hub Sample: Deploy update to a test device
        /// </summary>
        /// <param name="updateVersion">Update version to retrieve.</param>
        /// <param name="deviceGroup">Device group to deploy the update to.</param>
        static async Task Main(string updateVersion, string deviceGroup)
        {
            Console.WriteLine("Device Update for IoT Hub Sample: Deploy update to a test device");
            Console.WriteLine();

            if (string.IsNullOrWhiteSpace(updateVersion))
            {
                throw new ArgumentException("You have to provider a valid update version.");
            }

            var credentials = new InteractiveBrowserCredential(Constant.TenantId, Constant.ClientId);
            var client      = new DeviceManagementClient(Constant.AccountEndpoint, Constant.Instance, credentials);

            Console.WriteLine("Retrieve update:");
            Console.WriteLine($"    Provider: {Constant.Provider}");
            Console.WriteLine($"    Name    : {Constant.Name}");
            Console.WriteLine($"    Version : {updateVersion}");

            Console.WriteLine();
            Console.WriteLine($"Checking existence of device group '{deviceGroup}'...");
            try
            {
                var response = await client.GetGroupAsync(deviceGroup);

                if (response.Status == (int)HttpStatusCode.NotFound)
                {
                    throw new ApplicationException($"Group '{deviceGroup}' doesn't exist. Create it before you create a deployment.");
                }
                Console.WriteLine($"Group '{deviceGroup}' already exists.");
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e);
                throw;
            }

            Console.WriteLine();
            Console.WriteLine($"Creating deployment of update '{updateVersion}' to device group '{deviceGroup}'...");
            var deploymentId = $"test{DateTime.UtcNow.ToString("MMddhhmmss")}";
            var deployment   = new
            {
                deploymentId,
                startDateTime = DateTime.UtcNow.ToString("O"),
                groupId       = deviceGroup,
                updateId      = new
                {
                    manufacturer = Constant.Provider,
                    name         = Constant.Name,
                    version      = updateVersion
                }
            };
            var body = JsonSerializer.Serialize(deployment);

            Console.WriteLine();
            Console.WriteLine($"Deploying update '{updateVersion}' to device group '{deviceGroup}'...");
            Console.WriteLine($"(this may take a long time to finish)");
            try
            {
                var response = await client.CreateOrUpdateDeploymentAsync(deviceGroup, deploymentId, RequestContent.Create(body));

                bool repeat = true;
                while (repeat)
                {
                    response = await client.GetDeploymentStatusAsync(deviceGroup, deploymentId);

                    var doc = JsonDocument.Parse(response.Content.ToMemory());
                    if (doc.RootElement.TryGetProperty("devicesCompletedSucceededCount", out var deviceCountValue) && deviceCountValue.GetInt32() > 0)
                    {
                        Console.WriteLine($"\nSuccessfully deployed to {deviceCountValue.GetInt32()} devices.");
                        repeat = false;
                    }
                    else
                    {
                        Console.Write(".");
                        await Task.Delay(DefaultRetryAfterValue);
                    }
                }
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }