public async Task <bool> AddOtaaDevice(Program.AddOtaaOptions opts, ConfigurationHelper configurationHelper)
        {
            Console.WriteLine($"Adding OTAA device to IoT Hub: {opts.DevEui} ...");

            bool isSuccess      = false;
            var  twinProperties = new TwinProperties();

            twinProperties.Desired["AppSKey"] = opts.AppSKey;

            twinProperties.Desired["NwkSKey"] = opts.NwkSKey;

            twinProperties.Desired["DevAddr"] = opts.DevAddr;

            twinProperties.Desired["GatewayID"] = opts.GatewayId;

            twinProperties.Desired["SensorDecoder"] = opts.SensorDecoder;

            if (!string.IsNullOrEmpty(opts.ClassType))
            {
                twinProperties.Desired["ClassType"] = opts.ClassType;
            }

            var twin = new Twin();

            twin.Properties = twinProperties;

            var device = new Device(opts.DevEui);

            BulkRegistryOperationResult result;

            try
            {
                result = await configurationHelper.RegistryManager.AddDeviceWithTwinAsync(device, twin);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}\n");
                return(false);
            }

            if (result.IsSuccessful)
            {
                Console.WriteLine($"Done!\n");
                isSuccess = true;
            }
            else
            {
                Console.WriteLine($"Error adding device:");
                foreach (var error in result.Errors)
                {
                    Console.WriteLine($"Device Id: {error.DeviceId}, Code: {error.ErrorCode}, Error: {error.ErrorStatus}");
                }
                Console.WriteLine();
                isSuccess = false;
            }
            return(isSuccess);
        }
        public Program.AddOtaaOptions CompleteMissingOtaaOptionsAndClean(Program.AddOtaaOptions opts)
        {
            if (string.IsNullOrEmpty(opts.DevEui))
            {
                opts.DevEui = Keygen.Generate(16);
                Console.WriteLine($"Info: Generating missing DevEUI: {opts.DevEui}");
            }
            else
            {
                opts.DevEui = ValidationHelper.CleanString(opts.DevEui);
            }

            if (string.IsNullOrEmpty(opts.NwkSKey))
            {
                opts.NwkSKey = Keygen.Generate(16);
                Console.WriteLine($"Info: Generating missing NwkSKey: {opts.NwkSKey}");
            }
            {
                opts.NwkSKey = ValidationHelper.CleanString(opts.NwkSKey);
            }

            if (string.IsNullOrEmpty(opts.AppSKey))
            {
                opts.AppSKey = Keygen.Generate(16);
                Console.WriteLine($"Info: Generating missing AppSKey: {opts.AppSKey}");
            }
            {
                opts.AppSKey = ValidationHelper.CleanString(opts.AppSKey);
            }

            if (string.IsNullOrEmpty(opts.DevAddr))
            {
                opts.DevAddr = Keygen.Generate(4);
                Console.WriteLine($"Info: Generating missing DevAddr: {opts.DevAddr}");
            }
            {
                opts.DevAddr = ValidationHelper.CleanString(opts.DevAddr);
            }

            if (!string.IsNullOrEmpty(opts.GatewayId))
            {
                opts.GatewayId = ValidationHelper.CleanString(opts.GatewayId);
            }

            if (!string.IsNullOrEmpty(opts.SensorDecoder))
            {
                opts.SensorDecoder = ValidationHelper.CleanString(opts.SensorDecoder);
            }

            if (!string.IsNullOrEmpty(opts.ClassType))
            {
                opts.ClassType = ValidationHelper.CleanString(opts.ClassType);
            }

            return(opts);
        }
        public bool ValidateOtaaDevice(Program.AddOtaaOptions opts)
        {
            var  validationError = string.Empty;
            bool isValid         = true;

            if (string.IsNullOrEmpty(opts.NwkSKey))
            {
                Console.WriteLine("Error: NwkSKey is missing.");
                isValid = false;
            }
            if (!ValidationHelper.ValidateKey(opts.NwkSKey, 16, out validationError))
            {
                Console.WriteLine($"Error: NwkSKey is invalid. {validationError}");
                isValid = false;
            }
            else
            {
                Console.WriteLine($"Info: NwkSKey is valid: {opts.NwkSKey}");
            }

            if (string.IsNullOrEmpty(opts.AppSKey))
            {
                Console.WriteLine("Error: AppSKey is missing.");
                isValid = false;
            }
            if (!ValidationHelper.ValidateKey(opts.AppSKey, 16, out validationError))
            {
                Console.WriteLine($"Error: AppSKey is invalid. {validationError}");
                isValid = false;
            }
            else
            {
                Console.WriteLine($"Info: AppSKey is valid: {opts.AppSKey}");
            }

            if (string.IsNullOrEmpty(opts.DevAddr))
            {
                Console.WriteLine("Error: DevAddr is missing.");
                isValid = false;
            }
            if (!ValidationHelper.ValidateKey(opts.DevAddr, 4, out validationError))
            {
                Console.WriteLine($"Error: DevAddr is invalid. {validationError}");
                isValid = false;
            }
            else
            {
                Console.WriteLine($"Info: DevAddr is valid: {opts.DevAddr}");
            }

            if (!ValidationHelper.ValidateSensorDecoder(opts.SensorDecoder, out validationError))
            {
                isValid = false;
            }
            if (!string.IsNullOrEmpty(validationError))
            {
                Console.WriteLine(validationError);
            }
            else
            {
                Console.WriteLine($"Info: SensorDecoder is valid: {opts.SensorDecoder}");
            }

            if (!string.IsNullOrEmpty(opts.ClassType))
            {
                if (!string.Equals(opts.ClassType, "C", StringComparison.CurrentCultureIgnoreCase))
                {
                    isValid = false;
                    Console.WriteLine("Error: If ClassType is set, it needs to be \"C\".");
                }
                else
                {
                    Console.WriteLine($"Info: ClassType is valid: {opts.ClassType}");
                }
            }
            else
            {
                Console.WriteLine($"Info: ClassType is empty");
            }

            return(isValid);
        }