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));
        }
Example #2
0
        public ActionResult SelectType(DeviceType deviceType)
        {
            // device type logic getdevicetypeasync
            var device = new UnregisteredDeviceModel {
                DeviceType = deviceType, IsDeviceIdSystemGenerated = true
            };

            return(PartialView("_AddDeviceCreate", device));
        }
        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));
        }
Example #4
0
        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));
        }
Example #5
0
        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));
        }
Example #6
0
        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);
        }
Example #7
0
        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));
        }
        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
                while (true)
                {
                    var    random   = BitConverter.ToUInt32(Guid.NewGuid().ToByteArray(), 0);
                    string deviceId = $"CoolingSampleDevice_{random % 10000:d4}";

                    if (!await GetDeviceExistsAsync(deviceId))
                    {
                        model.DeviceId = deviceId;
                        break;
                    }
                }

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

            if (isModelValid)
            {
                bool deviceExists = !model.IsDeviceIdSystemGenerated && 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));
        }
Example #10
0
        public async Task <ActionResult> AddDeviceCreate(string button, UnregisteredDeviceModel model)
        {
            bool isModelValid   = this.ModelState.IsValid;
            bool onlyValidating = (button != null && button.ToLower().Trim() == "check");

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

            if (this._apiRegistrationRepository.IsApiRegisteredInAzure())
            {
                try
                {
                    var devices = await this.GetDevices();

                    this.ViewBag.AvailableIccids = this._cellularService.GetListOfAvailableIccids(devices);
                    this.ViewBag.CanHaveIccid    = true;
                }
                catch (CellularConnectivityException)
                {
                    this.ViewBag.CanHaveIccid = false;
                }
            }
            else
            {
                this.ViewBag.CanHaveIccid = false;
            }

            //reset flag
            model.IsDeviceIdUnique = false;

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

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

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

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

                model.IsDeviceIdUnique = !deviceExists;

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

            return(this.PartialView("_AddDeviceCreate", model));
        }