Ejemplo n.º 1
0
        public async Task <ActionResult> Index(ManageMessageId?message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
                : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
                : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
                : message == ManageMessageId.Error ? "An error has occurred."
                : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
                : message == ManageMessageId.ProfileInfoUpdateSucess ? "Профилът е обновен успешно."
                : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
                : "";

            var userId = User.Identity.GetUserId();
            var model  = new IndexViewModel
            {
                HasPassword       = HasPassword(),
                PhoneNumber       = await UserManager.GetPhoneNumberAsync(userId),
                TwoFactor         = await UserManager.GetTwoFactorEnabledAsync(userId),
                Logins            = await UserManager.GetLoginsAsync(userId),
                BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId)
            };

            if (User.IsInRole("Company"))
            {
                var company   = _companyService.GetAll().First(x => x.UserId == userId);
                var viewModel = new IndexCompanyViewModel {
                    UniqueNumber = company.UniqueNumber, Name = company.Name, MOL = company.MOL, Address = company.City, DDS = company.DDS, EIK = company.EIK
                };

                return(View("IndexCompany", viewModel));
            }
            else if (User.IsInRole("Driver"))
            {
                return(View("IndexDriver", model));
            }
            else if (User.IsInRole("Dispatcher"))
            {
                UserManager <Models.ApplicationUser> userManager = new UserManager <Models.ApplicationUser>(new UserStore <Models.ApplicationUser>(new Models.ApplicationDbContext()));
                var user      = userManager.FindById(User.Identity.GetUserId());
                var viewModel = new IndexDispatcherViewModel
                {
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Telephone = user.PhoneNumber
                };
                var dispatcher = _dispatcherService.GetAll().First(x => x.UserId == user.Id);
                var company    = _companyService.GetAll().First(x => x.Id == dispatcher.Company.Id);
                viewModel.CompanyName = company.Name;

                return(View("IndexDispatcher", viewModel));
            }
            else
            {
                return(View("IndexDriver", model));
            }
        }
Ejemplo n.º 2
0
        public void Pull(bool free = false, bool busy = false, bool absent = false, bool offline = false, bool offduty = false)
        {
            var userId = Context.User.Identity.GetUserId();

            var dashboard = _dashboardService.GetAll().Where(x => x.DispatcherUserId == userId).ToList();

            Clients.All.hey();
            // Remove those request which have been seen
            foreach (var item in dashboard)
            {
                if (item.LastSeenStatus != RequestStatusEnum.NoCarChosen)
                {
                    var diff = DateTime.Now - item.LastSeen;
                    if (diff.TotalSeconds > 60)
                    {
                        var dispatcherDashboardRequest = dashboard.First(x => x.Id == item.Id);
                        _data.DispatchersDashboard.Remove(dispatcherDashboardRequest);
                    }
                }
            }

            //Try to find appropriate car for the rest of it
            var requestsList = _dashboardService.GetAll().Where(x => x.DispatcherUserId == userId).ToList();

            for (int i = 0; i < requestsList.Count; i++)
            {
                if (requestsList[i].Request != null)
                {
                    var reqId = requestsList[i].Request.Id;
                    if (requestsList[i].Request.RequestStatus == RequestStatusEnum.NotTaken)
                    {
                        NotTakenRequest(reqId);
                    }
                    else if (requestsList[i].Request.RequestStatus == RequestStatusEnum.NoCarChosen)
                    {
                        NoCarChosen(reqId);
                    }


                    var requestId      = requestsList[i].Id;
                    var currentRequest = _dashboardService.GetAll().Where(x => x.Id == requestId).FirstOrDefault();
                    if (currentRequest != null && currentRequest.Request.RequestStatus != currentRequest.LastSeenStatus)
                    {
                        currentRequest.LastSeen       = DateTime.Now;
                        currentRequest.LastSeenStatus = requestsList[i].Request.RequestStatus;
                    }
                }
            }


            var requests = _dashboardService.GetRequests(userId);

            var dispatcher          = _dispatcherService.GetAll().First(x => x.UserId == userId);
            var dispatcherCompanyId = dispatcher.Company.Id;
            var cars = _carService.GetCars().Where(x => x.Company.Id == dispatcher.Company.Id).ToList();

            List <object> carsList = new List <object>();

            object[] carObjs            = new object[cars.Count];
            int      freeStatusCount    = 0;
            int      busyStatusCount    = 0;
            int      absentStatusCount  = 0;
            int      offlineStatusCount = 0;
            int      offdutyStatusCount = 0;
            var      addCar             = false;

            for (int i = 0; i < cars.Count; i++)
            {
                addCar = false;
                TimeSpan diff = DateTime.Now - cars[i].LastActiveDateTime;
                if (diff.TotalMinutes > 2 && cars[i].CarStatus != CarStatus.OffDuty)
                {
                    cars[i].CarStatus = CarStatus.Offline;
                    _carService.SaveChanges();
                }
                switch (cars[i].CarStatus)
                {
                case CarStatus.Free:
                    if (free)
                    {
                        addCar = true;
                    }
                    freeStatusCount++;
                    break;

                case CarStatus.Busy:
                    if (busy)
                    {
                        addCar = true;
                    }
                    busyStatusCount++;
                    break;

                case CarStatus.Absent:
                    if (absent)
                    {
                        addCar = true;
                    }
                    absentStatusCount++;
                    break;

                case CarStatus.OffDuty:
                    if (offduty)
                    {
                        addCar = true;
                    }
                    offdutyStatusCount++;
                    break;

                case CarStatus.Offline:
                    if (offline)
                    {
                        addCar = true;
                    }
                    offlineStatusCount++;
                    break;

                default:
                    break;
                }

                if (addCar)
                {
                    carsList.Add(new { lng = cars[i].Location.Longitude, lat = cars[i].Location.Latitude, id = cars[i].InternalNumber, carStatus = cars[i].CarStatus });
                }
            }
            carObjs = carsList.ToArray();
            Clients.Client(Context.ConnectionId).pullRespond(new { requests = requests, cars = carObjs, freeStatusCount = freeStatusCount, busyStatusCount = busyStatusCount, absentStatusCount = absentStatusCount, offlineStatusCount = offlineStatusCount, offdutyStatusCount = offdutyStatusCount });
        }
        /// <summary>
        /// Controls all requests of the dispacher, takes care of the communication between the drivers and dispatcher
        /// </summary>
        /// <param name="free"></param>
        /// <param name="busy"></param>
        /// <param name="absent"></param>
        /// <param name="offline"></param>
        /// <param name="offduty"></param>
        /// <returns>Retuens car locations and statuses, all active request and their statuses</returns>
        public JsonResult Pull(bool free = false, bool busy = false, bool absent = false, bool offline = false, bool offduty = false)
        {
            var userId = User.Identity.GetUserId();

            _dashboardService.UpdateRequestStatus(userId);
            var requests = _dashboardService.GetRequests(userId);

            var dispatcher          = _dispatcherService.GetAll().First(x => x.UserId == userId);
            var dispatcherCompanyId = dispatcher.Company.Id;
            var cars = _carService.GetCars().Where(x => x.Company.Id == dispatcher.Company.Id).ToList();

            List <object> carsList = new List <object>();

            object[] carObjs            = new object[cars.Count];
            int      freeStatusCount    = 0;
            int      busyStatusCount    = 0;
            int      absentStatusCount  = 0;
            int      offlineStatusCount = 0;
            int      offdutyStatusCount = 0;
            var      addCar             = false;

            for (int i = 0; i < cars.Count; i++)
            {
                addCar = false;
                TimeSpan diff = DateTime.Now - cars[i].LastActiveDateTime;
                if (diff.TotalMinutes > 2 && cars[i].CarStatus != CarStatus.OffDuty)
                {
                    cars[i].CarStatus = CarStatus.Offline;
                    _carService.SaveChanges();
                }
                switch (cars[i].CarStatus)
                {
                case CarStatus.Free:
                    if (free)
                    {
                        addCar = true;
                    }
                    freeStatusCount++;
                    break;

                case CarStatus.Busy:
                    if (busy)
                    {
                        addCar = true;
                    }
                    busyStatusCount++;
                    break;

                case CarStatus.Absent:
                    if (absent)
                    {
                        addCar = true;
                    }
                    absentStatusCount++;
                    break;

                case CarStatus.OffDuty:
                    if (offduty)
                    {
                        addCar = true;
                    }
                    offdutyStatusCount++;
                    break;

                case CarStatus.Offline:
                    if (offline)
                    {
                        addCar = true;
                    }
                    offlineStatusCount++;
                    break;

                default:
                    break;
                }

                if (addCar)
                {
                    carsList.Add(new { lng = cars[i].Location.Longitude, lat = cars[i].Location.Latitude, id = cars[i].InternalNumber, carStatus = cars[i].CarStatus });
                }
            }
            carObjs = carsList.ToArray();
            return(Json(new { requests = requests, cars = carObjs, freeStatusCount = freeStatusCount, busyStatusCount = busyStatusCount, absentStatusCount = absentStatusCount, offlineStatusCount = offlineStatusCount, offdutyStatusCount = offdutyStatusCount }));
        }