Exemplo n.º 1
0
        static async Task MainAsync(string[] args)
        {
            Console.WriteLine("Getting device certificate...");

            Parameters parameters = null;

            try
            {
                ValidateInput(args);

                parameters = ExtractParameters(args);

                if (parameters != null && !string.IsNullOrEmpty(parameters.DeviceId))
                {
                    string data = string.Empty;
                    if (parameters.CreateDevice)
                    {
                        Device device = await ProvisioningHelper.AddDeviceAsync(parameters.DeviceId);

                        if (device != null)
                        {
                            data = device.AuthenticationPrimaryKey;
                        }
                    }

                    //in case no device creation has ben requested
                    if (string.IsNullOrEmpty(data))
                    {
                        data = await GetCertificateASync(parameters.DeviceId);
                    }

                    if (string.IsNullOrEmpty(parameters.CertificateDestinationPath))
                    {
                        //display the response
                        Console.WriteLine($"Device: {parameters.DeviceId}");
                        Console.WriteLine($"Certificate: {data}");
                        Console.ReadKey();
                    }
                    else
                    {
                        //store the response in a file
                        if (!string.IsNullOrEmpty(data))
                        {
                            File.WriteAllText($"{parameters.CertificateDestinationPath}\\{parameters.DeviceId}.txt", data);
                        }
                        else
                        {
                            throw new Exception($"No key for device {parameters.DeviceId}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (parameters != null && !string.IsNullOrEmpty(parameters.CertificateDestinationPath))
                {
                    File.AppendAllText($"{parameters.CertificateDestinationPath}\\logs.txt", $"{Environment.NewLine}{DateTime.Now.ToString()}::ERROR::{ex.Message}");
                }
                else
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
            }
        }