public ActionResult Create(DeviceCreate model)
        {
            var userId = User.Identity.GetUserId();
            var userData = _uow.UserDataRepository.GetById(userId);
            var profiles = _uow.ProfileRepository.GetAll().Where(p => p.UserDataId == null || p.UserDataId == userId);
            var profilesList = new SelectList(profiles, "Id", "Title");
            ViewBag.Profiles = profilesList;

            if (ModelState.IsValid)
            {
                var checkCodes = _uow.CheckCodeRepository.GetAll().Where(cc => cc.Code == model.Code);

                if (checkCodes.Any())
                {
                    var checkCode = checkCodes.First();

                    if (checkCode.Time.AddMinutes(15) >= DateTime.Now)
                    {
                        if (userData.UserDevices.All(ud => ud.DeviceId != checkCode.Id))
                        {
                            var userDevice = new UserDevice
                            {
                                DeviceId = checkCode.Id,
                                Title = model.Title,
                                ProfileId = model.ProfileId,
                                UserDataId = userId,
                                Period = model.Period
                            };
                            _uow.UserDeviceRepository.Insert(userDevice);
                            _uow.CheckCodeRepository.Delete(checkCode);
                            _uow.Save();

                            return RedirectToAction("Index");
                        }

                        ModelState.AddModelError("", "You already added this device.");
                    }

                    ModelState.AddModelError("", "Time expired. Please, press button on device again.");
                    _uow.CheckCodeRepository.Delete(model.Code);
                    _uow.Save();
                }

                ModelState.AddModelError("", "Code is encorrect.");
            }

            return View(model);
        }
        private object GetTemperaturesForDevice(UserDevice userDevice)
        {
            var deviceTemperatures = new List<object> { new IComparable[] { "Time", "Measured", "Predicted" } };

            var temperatures = userDevice.Device.Temperatures.Reverse().Take(10).Reverse().ToArray();
            List<double> arguments = new List<double>();
            List<double> temperatureValues = new List<double>();

            DateTime time;
            double temperature;

            var count = temperatures.Count();
            var extend = new double[5];

            for (int i = 0; i < 5; i++)
            {
                extend[i] = i + 10;
            }

            for (var i = 0; i < count - 1; ++i)
            {
                temperature = temperatures[i].Value;
                time = temperatures[i].Time;
                arguments.Add(i);
                temperatureValues.Add(temperature);
                deviceTemperatures.Add(new IComparable[] {time.ToShortTimeString(), temperature, null});
            }

            while (temperatureValues.Count < 9)
            {
                temperatureValues.Insert(0, 0D);
                DateTime deviceTemperature;
                DateTime temp;

                if (arguments.Count > 0)
                    arguments.Add(arguments.Last() + 1);
                else
                    arguments.Add(0);

                if (temperatureValues.Count < 2)
                {
                    deviceTemperature = DateTime.Now;
                    temp = deviceTemperature.AddMinutes(-1);
                    deviceTemperatures.Insert(1, new IComparable[] {temp.ToShortTimeString(), 0, null});
                }
                else
                {
                    var temp1 = deviceTemperatures[1] as IComparable[];
                    if (temp1 == null) continue;

                    var tempTemperature = temp1[0] as string;
                    var success = DateTime.TryParse(tempTemperature, out deviceTemperature);
                    if (!success)
                        deviceTemperature = DateTime.Now;
                    temp = deviceTemperature.AddMinutes(-1);

                    deviceTemperatures.Insert(1, new IComparable[] { temp.ToShortTimeString(), 0, null });
                }
            }

            if (count == 10)
            {
                temperature = temperatures[count-1].Value;
                time = temperatures[count-1].Time;
                arguments.Add(count);
                temperatureValues.Add(temperature);
                deviceTemperatures.Add(new IComparable[] {time.ToShortTimeString(), temperature, temperature });
            }
            else
            {
                time = DateTime.Now;
                deviceTemperatures.Add(new IComparable[] { time.ToShortTimeString(), 0, 0});
            }

            var interpolantValue = Interpolant(arguments.ToArray(), temperatureValues.ToArray(), extend);
            for (var i = 0; i < interpolantValue.Length; ++i)
            {
                var time2 = time.AddMinutes(i + 1);
                deviceTemperatures.Add(new IComparable[] {time2.ToShortTimeString(), null, interpolantValue[i] });
            }

            return deviceTemperatures;
        }