Example #1
0
        /// <summary>
        /// Device Update for IoT Hub Sample: Get device
        /// </summary>
        /// <param name="device">Device identifier.</param>
        static async Task Main(string device)
        {
            Console.WriteLine("Device Update for IoT Hub Sample: Get device");
            Console.WriteLine();

            if (string.IsNullOrWhiteSpace(device))
            {
                throw new ArgumentException("You have to provider a valid device identifier.");
            }

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

            Console.WriteLine("Retrieve device information:");
            Console.WriteLine($"    Device: {device}");

            Console.WriteLine();
            Console.WriteLine("Information:");
            try
            {
                var response = await client.GetDeviceAsync(device);

                Console.WriteLine(response.Content.ToString());
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Example #2
0
        private async Task CheckDeviceUpdateStepAsync(string groupId, string provider, string name, string version)
        {
            ConsoleEx.WriteLine(ConsoleColor.Yellow, $"Checking device {_deviceId} status...");
            Console.WriteLine("Waiting for the update to be installed...");
            var repeat = true;

            while (repeat)
            {
                Response deviceResponse = await _managementClient.GetDeviceAsync(_deviceId, new RequestContext());

                var deviceResponseDoc = JsonDocument.Parse(deviceResponse.Content.ToMemory());

                if (deviceResponseDoc.RootElement.TryGetProperty("installedUpdateId", out var installedUpdateId))
                {
                    if (installedUpdateId.GetProperty("provider").GetString() == provider &&
                        installedUpdateId.GetProperty("name").GetString() == name &&
                        installedUpdateId.GetProperty("version").GetString() == version)
                    {
                        repeat = false;
                    }
                    else
                    {
                        Console.Write(".");
                        await Task.Delay(DefaultRetryAfterValue);
                    }
                }
            }

            Console.WriteLine();
        }