コード例 #1
0
        public async Task <IActionResult> RegisterNewSensor
            ([Bind(include: WebConstants.UserSensorViewModelBindingString)] RegisterSensorModel model)
        {
            if (!ModelState.IsValid)
            {
                this.TempData["Lng-Lat"] = "Please set the location of the sensor.";
                return(View(model));
            }
            if (model.UserMinValue >= model.UserMaxValue)
            {
                this.TempData["Invalid-Min-Max-Value"] = "The minimum value can not be equal to or greater than the maximum value.";
                return(View(model));
            }
            if (model.UserId == null)
            {
                var currentUser = await this.userManager.GetUserAsync(HttpContext.User);

                model.UserId = currentUser.Id;
            }

            var registrationData = new UserSensor()
            {
                Name             = model.Name,
                PollingInterval  = model.UserPollingInterval,
                SampleSensorId   = model.SampleSensorId,
                UserId           = model.UserId,
                UserMinValue     = model.UserMinValue,
                UserMaxValue     = model.UserMaxValue,
                Latitude         = model.Latitude,
                Longitude        = model.Longitude,
                SendNotification = model.SendNotification,
                IsPrivate        = model.IsPrivate
            };

            UserSensor sensor;

            try
            {
                sensor = await this.sensorsService.RegisterSensorAsync(registrationData);
            }
            catch (SensorNullableException ex)
            {
                this.TempData["Service-Error"] = ex.Message;
                return(View("ServiceError"));
            }

            this.TempData["Success-Message"] = $"Sensor {sensor.Name} was registered successfully!";

            return(this.RedirectToAction("ListSampleSensors", new { userId = sensor.UserId }));
        }
コード例 #2
0
        public async Task <IActionResult> RegisterNewSensor(Guid sampleSensorId, string userId)
        {
            SampleSensor sensor;

            try
            {
                sensor = await this.sensorsService.GetSampleSensorAsync(sampleSensorId);
            }
            catch (SensorNullableException ex)
            {
                this.TempData["Service-Error"] = ex.Message;
                return(View("ServiceError"));
            }

            var model = new RegisterSensorModel(sensor, userId);

            return(View("RegisterNewSensor", model));
        }