示例#1
0
        public async Task <IActionResult> AddCarToUser(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var user = await _context.Users.FirstOrDefaultAsync(m => m.Id == id);

            if (user == null)
            {
                return(NotFound());
            }

            var userCarViewModel = new UserCarViewModel()
            {
                UserId       = user.Id,
                AllCarBrands = LicensePlateSearcher.GetAllBrands().Select(x => new SelectListItem
                {
                    Text  = x,
                    Value = x.ToString()
                }),
                AllCarModels = LicensePlateSearcher.GetAllModels().Select(x => new SelectListItem
                {
                    Text  = x,
                    Value = x.ToString()
                })
            };

            return(View(userCarViewModel));
        }
示例#2
0
        public IActionResult CreatePath(UserViewModel user)
        {
            var path = new Path()
            {
                User = new User()
                {
                    Name = ""
                },
                AllCarBrands = LicensePlateSearcher.GetAllBrands().Select(x => new SelectListItem
                {
                    Text  = x,
                    Value = x.ToString()
                }),
                AllCarModels = LicensePlateSearcher.GetAllModels().Select(x => new SelectListItem
                {
                    Text  = x,
                    Value = x.ToString()
                }),
                AllCars = LicensePlateSearcher.GetAllCars().Select(x => new SelectListItem
                {
                    Text  = x.Brand,
                    Value = x.Make.ToString().Replace(';', ' ') + $" - ({x.Range}km)"
                }),
                Cars = _context.Cars.ToList()
            };

            if (user.Name == null)
            {
                path.User.Name = "Gästläge";
            }
            else
            {
                path.User = _context.Users.Where(x => x.Name == user.Name && x.Phone == user.Phone && x.Password == user.Password).Include(x => x.UserCars).ThenInclude(x => x.Car).FirstOrDefault();
            }

            return(View(path));
        }
示例#3
0
        //public async Task<IActionResult> CreatePath(Path path)
        public async Task <IActionResult> CreatePath(Path path)
        {
            path.Car = LicensePlateSearcher.CheckForCarInDatabase(path.CarBrand, path.CarMake);

            //If car couldnt be found set it to a default one
            if (path.Car == null)
            {
                path.Car = LicensePlateSearcher.CheckForCarInDatabase("BMW", "iX3");
            }


            path.RangeKm = path.Car.Range;

            path.MaxRangeM       = (path.RangeKm * 1000);                       //km -> m
            path.MinRangeM       = (path.RangeKm * 1000 * 0.2);                 //20% of MaxRangeM
            path.EffectiveRangeM = path.MaxRangeM - path.MinRangeM;             //diff
            path.CurrentRangeM   = path.MaxRangeM * (path.CurrentRangeM / 100); //50 * 1000;

            //If no entered destination set it to the closest charging station
            if (path.Destination == null || path.Destination == string.Empty)
            {
                try
                {
                    path.IgnoreRange = true;
                    await GetChargingStationLocationAsDestination(path);
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }

            var direction = await Google_GetDirection(path);

            try
            {
                int loop = 0;

                while (await EvaluateDirection(direction, path) == true)
                {
                    //A path could have more than 20 waypoints
                    //Temp solution to prevent infinite loops
                    if (loop >= 9)
                    {
                        throw new Exception("LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOP");
                    }

                    loop++;
                    direction = await Google_GetDirection(path);
                }

                if (path.Arrived == false)
                {
                    throw new Exception("Couldn't make a path");
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return(RedirectToAction(nameof(DisplayPath), path));
        }