Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            //To use this sample, uncomment the bits you want to run in ImportExportDevicesSample.RunSampleAsync

            //The size of the hub you are using should be able to manage the number of devices
            //  you want to create and test with.

            // Check and see if the environment variables were read. If not, check
            //   for command-line arguments -- if found, load them into the connection strings.
            // This is just another way to run the sample that lets you run it w/o
            //   putting the connection strings in the code.
            if (string.IsNullOrEmpty(_IoTHubConnectionString) && args.Length > 0)
            {
                _IoTHubConnectionString = args[0];
            }
            if (string.IsNullOrEmpty(_DestIoTHubConnectionString) && args.Length > 1)
            {
                _DestIoTHubConnectionString = args[1];
            }
            if (string.IsNullOrEmpty(_storageAccountConnectionString) && args.Length > 2)
            {
                _storageAccountConnectionString = args[2];
            }

            ImportExportDevicesSample importExportDevicesSample =
                new ImportExportDevicesSample(_IoTHubConnectionString, _DestIoTHubConnectionString,
                                              _storageAccountConnectionString);

            try
            {
                importExportDevicesSample.RunSampleAsync().GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                Debug.Print("Error. Description = {0}", ex.Message);
            }

            Console.WriteLine("Finished.");
            Console.WriteLine();
            Console.Write("Press any key to continue.");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public static async Task Main(string[] args)
        {
            //The size of the hub you are using should be able to manage the number of devices
            //  you want to create and test with.

            // When retrieving the variables, it works like this:
            // For the 5 options, try to read the environment variable. These will be put in the
            //   private static strings above, like _copy_Devices.
            // If the environment variable is blank and there is a command-line argument available,
            //   read that argument into a string (the private static strings above, like _copy_Devices).
            // Convert the private static string to boolean or integer (depending on how it's defined)
            //   and store the result in the class-level camelcase variables (like copyDevices).

            // For the connection strings, try to read the environment variable. These will be put in the
            //   private static strings above, like _IoTHubConnectionString.
            // If the connection string environment variable is blank, try to get it from the
            //   command line argument. These don't require any conversions because they are all text.

            // Note that because of the order of the arguments, you can set the connection strings
            //   using environment variables and then pass in the five options using command line args.
            // The only reason you might want to do this is because the connection strings are so long,
            //   it makes it hard to distinguish them at the command line.

            // If you want to look at the raw arg values, uncomment this and they will print in the console window.
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine($"args({i}) = {args[i]}");
            }
            // **addDevices**
            if (string.IsNullOrEmpty(_envAddDevices) && args.Length > 0)
            {
                _envAddDevices = args[0];
            }
            if (!string.IsNullOrEmpty(_envAddDevices))
            {
                _addDevices = _envAddDevices.Trim().ToUpper() == "TRUE" ? true : false;
            }

            // **numToAdd**
            if (_addDevices)
            {
                if (string.IsNullOrEmpty(_envNumToAdd) && args.Length > 1)
                {
                    _envNumToAdd = args[1];
                }
                if (!string.IsNullOrEmpty(_envNumToAdd))
                {
                    _ = int.TryParse(_envNumToAdd, out _numToAdd);
                }
            }

            // **copyDevices**
            if (string.IsNullOrEmpty(_envCopyDevices) && args.Length > 2)
            {
                _envCopyDevices = args[2];
            }
            if (!string.IsNullOrEmpty(_envCopyDevices))
            {
                _copyDevices = _envCopyDevices.Trim().ToUpper() == "TRUE" ? true : false;
            }
            // **deleteSourceDevices**
            if (string.IsNullOrEmpty(_envDeleteSourceDevices) && args.Length > 3)
            {
                _envDeleteSourceDevices = args[3].Trim().ToUpper();
            }
            if (!string.IsNullOrEmpty(_envDeleteSourceDevices))
            {
                _deleteSourceDevices = _envDeleteSourceDevices.Trim().ToUpper() == "TRUE" ? true : false;
            }

            // **deleteDestDevices**
            if (string.IsNullOrEmpty(_envDeleteDestDevices) && args.Length > 4)
            {
                _envDeleteDestDevices = args[4];
            }
            if (!string.IsNullOrEmpty(_envDeleteDestDevices))
            {
                _deleteDestDevices = _envDeleteDestDevices.Trim().ToUpper() == "TRUE" ? true : false;
            }

            // ** IoTHubConnectionString **
            if (string.IsNullOrEmpty(_envIotHubConnectionString) && args.Length > 5)
            {
                _envIotHubConnectionString = args[5];
            }

            // ** DestIoTHubConnectionString **
            if (string.IsNullOrEmpty(_envIotHubConnectionString) && args.Length > 5)
            {
                _envDestIotHubConnectionString = args[5];
            }

            // ** storageAccountConnectionString **
            if (string.IsNullOrEmpty(_envStorageAccountConnectionString) && args.Length > 5)
            {
                _envStorageAccountConnectionString = args[5];
            }

            // Show the data passed in and what it thinks it was.
            Console.WriteLine("Input:");
            Console.WriteLine($"  add devices = {_addDevices}.");
            Console.WriteLine($"  num to add = {_numToAdd}.");
            Console.WriteLine($"  copy devices = {_copyDevices}.");
            Console.WriteLine($"  delete source devices = {_deleteSourceDevices}.");
            Console.WriteLine($"  delete dest devices = {_deleteDestDevices}.");
            Console.WriteLine($"  IoTHubConnString = '{_envIotHubConnectionString}'.");
            Console.WriteLine($"  IoTHubDestString = '{_envDestIotHubConnectionString}'.");
            Console.WriteLine($"  storage connection string  = '{_envStorageAccountConnectionString}'.");

            try
            {
                // Instantiate the class and run the sample.
                var importExportDevicesSample = new ImportExportDevicesSample(
                    _envIotHubConnectionString,
                    _envDestIotHubConnectionString,
                    _envStorageAccountConnectionString);

                await importExportDevicesSample
                .RunSampleAsync(_addDevices, _numToAdd, _copyDevices, _deleteSourceDevices, _deleteDestDevices)
                .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Debug.Print($"Error. Description = {ex.Message}");
                Console.WriteLine($"Error. Description = {ex.Message}\n{ex.StackTrace}");
            }

            Console.WriteLine("Finished. Press any key to continue.");
            Console.ReadKey(true);
        }