public async Task<ActionResult> SelectType(DeviceType deviceType)
        {
            if (_apiRegistrationRepository.IsApiRegisteredInAzure())
            {
                try
                {
                    List<DeviceModel> devices = await GetDevices();
                    ViewBag.AvailableIccids = _cellularExtensions.GetListOfAvailableIccids(devices);
                    ViewBag.CanHaveIccid = true;
                }
                catch (CellularConnectivityException)
                {
                    ViewBag.CanHaveIccid = false;
                }
            }
            else
            {
                ViewBag.CanHaveIccid = false;
            }

            // device type logic getdevicetypeasync
            var device = new UnregisteredDeviceModel
            {
                DeviceType = deviceType,
                IsDeviceIdSystemGenerated = true
            };
            return PartialView("_AddDeviceCreate", device);
        }
        public async Task<ActionResult> AddDeviceCreate(string button, UnregisteredDeviceModel model)
        {
            bool isModelValid = ModelState.IsValid;
            bool onlyValidating = (button != null && button.ToLower().Trim() == "check");

            if (object.ReferenceEquals(null, model) ||
                (model.GetType() == typeof(object)))
            {
                model = new UnregisteredDeviceModel();
            }

            //reset flag
            model.IsDeviceIdUnique = false;

            if (model.IsDeviceIdSystemGenerated)
            {
                //clear the model state of errors prior to modifying the model
                ModelState.Clear();

                //assign a system generated device Id
                model.DeviceId = Guid.NewGuid().ToString();

                //validate the model
                isModelValid = TryValidateModel(model);
            }

            if (isModelValid)
            {
                bool deviceExists = await GetDeviceExistsAsync(model.DeviceId);

                model.IsDeviceIdUnique = !deviceExists;

                if (model.IsDeviceIdUnique)
                {
                    if (!onlyValidating) 
                    {
                        return await Add(model);
                    }
                }
                else
                {
                    ModelState.AddModelError("DeviceId", Strings.DeviceIdInUse);
                }
            }

            return PartialView("_AddDeviceCreate", model);
        }
        public async Task<ActionResult> AddDeviceCreate(string button, UnregisteredDeviceModel model)
        {
            bool isModelValid = ModelState.IsValid;
            bool onlyValidating = (button != null && button.ToLower().Trim() == "check");

            if (ReferenceEquals(null, model) ||
                (model.GetType() == typeof (object)))
            {
                model = new UnregisteredDeviceModel();
            }

            if (_apiRegistrationRepository.IsApiRegisteredInAzure())
            {
                try
                {
                    List<DeviceModel> devices = await GetDevices();
                    ViewBag.AvailableIccids = _cellularExtensions.GetListOfAvailableIccids(devices);
                    ViewBag.CanHaveIccid = true;
                }
                catch (CellularConnectivityException)
                {
                    ViewBag.CanHaveIccid = false;
                }
            }
            else
            {
                ViewBag.CanHaveIccid = false;
            }

            //reset flag
            model.IsDeviceIdUnique = false;

            if (model.IsDeviceIdSystemGenerated)
            {
                //clear the model state of errors prior to modifying the model
                ModelState.Clear();

                //assign a system generated device Id
                model.DeviceId = Guid.NewGuid().ToString();

                //validate the model
                isModelValid = TryValidateModel(model);
            }

            if (isModelValid)
            {
                bool deviceExists = await GetDeviceExistsAsync(model.DeviceId);

                model.IsDeviceIdUnique = !deviceExists;

                if (model.IsDeviceIdUnique)
                {
                    if (!onlyValidating)
                    {
                        return await Add(model);
                    }
                }
                else
                {
                    ModelState.AddModelError("DeviceId", Strings.DeviceIdInUse);
                }
            }

            return PartialView("_AddDeviceCreate", model);
        }
        private async Task<DeviceWithKeys> AddDeviceAsync(UnregisteredDeviceModel unregisteredDeviceModel)
        {
            Debug.Assert(
                unregisteredDeviceModel != null,
                "unregisteredDeviceModel is a null reference.");

            Debug.Assert(
                unregisteredDeviceModel.DeviceType != null,
                "unregisteredDeviceModel.DeviceType is a null reference.");

	        DeviceModel device = DeviceCreatorHelper.BuildDeviceStructure(unregisteredDeviceModel.DeviceId,
                unregisteredDeviceModel.DeviceType.IsSimulatedDevice, unregisteredDeviceModel.Iccid);
            
            DeviceWithKeys addedDevice = await this._deviceLogic.AddDeviceAsync(device);
            return addedDevice;
        }
        private async Task<ActionResult> Add(UnregisteredDeviceModel model)
        {
            var deviceWithKeys = await AddDeviceAsync(model);
            var newDevice = new RegisteredDeviceModel
            {
                HostName = _iotHubName,
                DeviceType = model.DeviceType,
                DeviceId = deviceWithKeys.Device.DeviceProperties.DeviceID,
                PrimaryKey = deviceWithKeys.SecurityKeys.PrimaryKey,
                SecondaryKey = deviceWithKeys.SecurityKeys.SecondaryKey,
                InstructionsUrl = model.DeviceType.InstructionsUrl
            };

            return PartialView("_AddDeviceCopy", newDevice);
        }
 public ActionResult SelectType(DeviceType deviceType)
 {
     // device type logic getdevicetypeasync
     var device = new UnregisteredDeviceModel { DeviceType = deviceType, IsDeviceIdSystemGenerated = true };
     return PartialView("_AddDeviceCreate", device);
 }
        private async Task<dynamic> AddDeviceAsync(UnregisteredDeviceModel unregisteredDeviceModel)
        {
            dynamic device;

            Debug.Assert(
                unregisteredDeviceModel != null, 
                "unregisteredDeviceModel is a null reference.");

            Debug.Assert(
                unregisteredDeviceModel.DeviceType != null,
                "unregisteredDeviceModel.DeviceType is a null reference.");

            device = DeviceSchemaHelper.BuildDeviceStructure(unregisteredDeviceModel.DeviceId, unregisteredDeviceModel.DeviceType.IsSimulatedDevice);

            return await this._deviceLogic.AddDeviceAsync(device);
        }
        private async Task<ActionResult> Add(UnregisteredDeviceModel model)
        {
            Debug.Assert(model != null, "model is a null reference.");
            Debug.Assert(
                model.DeviceType != null, 
                "model.DeviceType is a null reference.");

            dynamic deviceWithKeys = await AddDeviceAsync(model);

            var newDevice = new RegisteredDeviceModel
            { 
                HostName = this.iotHubName,
                DeviceType = model.DeviceType,
                DeviceId = DeviceSchemaHelper.GetDeviceID(deviceWithKeys.Device),
                PrimaryKey = deviceWithKeys.SecurityKeys.PrimaryKey,
                SecondaryKey = deviceWithKeys.SecurityKeys.SecondaryKey,
                InstructionsUrl = model.DeviceType.InstructionsUrl
            };
            
            return PartialView("_AddDeviceCopy", newDevice);
        }
        private async Task<dynamic> AddDeviceAsync(
            UnregisteredDeviceModel unregisteredDeviceModel,
            string userName)
        {
            dynamic device;

            Debug.Assert(
                unregisteredDeviceModel != null, 
                "unregisteredDeviceModel is a null reference.");

            Debug.Assert(
                unregisteredDeviceModel.DeviceType != null,
                "unregisteredDeviceModel.DeviceType is a null reference.");

            Debug.Assert(
                !string.IsNullOrEmpty(userName),
                "userName is a null reference or empty string.");

            device = DeviceSchemaHelper.BuildDeviceStructure(unregisteredDeviceModel.DeviceId, unregisteredDeviceModel.DeviceType.IsSimulatedDevice);

            return await this._deviceLogic.AddDeviceAsync(device, userName);
        }