Esempio n. 1
0
 public override Task OnConnected()
 {
     if (Context.User.Identity.GetUserId() == null)
     {
         var userId = _accessTokenService.GetUserId(Context.QueryString["accessToken"]);
         if (userId != null)
         {
             if (_driverService.GetAll().Where(x => x.UserId == userId).FirstOrDefault() != null)
             {
                 _driverService.AddOnlineDriver(userId, Context.ConnectionId);
             }
             else if (_clientService.GetAll().Where(x => x.UserId == userId) != null)
             {
                 _clientService.AddOnlineClient(userId, Context.ConnectionId);
             }
         }
         else
         {
             return(base.OnDisconnected(true));
         }
     }
     else
     {
         _dispatcherService.AddOnlineDispatcher(Context.User.Identity.GetUserId(), Context.ConnectionId);
     }
     Clients.All.hey();
     return(base.OnConnected());
 }
        /// <summary>
        /// Updates the location of the car and takes care of the status as well as returns appropriate requests
        /// </summary>
        /// <param name="lon"></param>
        /// <param name="lat"></param>
        /// <param name="accessToken"></param>
        /// <param name="absent"></param>
        /// <param name="free"></param>
        /// <param name="onAddress"></param>
        /// <returns></returns>
        public JsonResult Pull(double lon, double lat, string accessToken, bool absent = false, bool free = false,
                               bool onAddress = false)
        {
            if (HttpContext.Request.RequestType == "POST")
            {
                var newAccessToken = _accessTokenService.GenerateAccessToken(accessToken);

                if (newAccessToken == null)
                {
                    return(Json(new { status = "INVALID ACCESSTOKEN" }));
                }

                var userId = _accessTokenService.GetUserId(newAccessToken);

                var driver = _driverService.GetDriverByUserId(userId);
                var car    = _carService.GetCarByDriver(driver);
                _carService.UpdateCarInfo(car, new Models.Models.Location {
                    Latitude = lat, Longitude = lon
                }, absent, free);

                if (onAddress)
                {
                    if (_carService.CarOnAddress(car))
                    {
                        return
                            (Json(
                                 new
                        {
                            status = "OK",
                            onAddress = true,
                            carStatus = car.CarStatus,
                            accessToken = newAccessToken
                        }));
                    }
                }

                if (_carService.CarOnAddressCheck(car))
                {
                    return
                        (Json(
                             new
                    {
                        status = "OK",
                        onAddress = true,
                        carStatus = car.CarStatus,
                        accessToken = newAccessToken
                    }));
                }

                var requestObj = _requestService.AppropriateRequest(car);
                if (requestObj != null)
                {
                    return
                        (Json(
                             new
                    {
                        status = "OK",
                        accessToken = newAccessToken,
                        carStatus = car.CarStatus,
                        request = requestObj
                    }));
                }

                return(Json(new { status = "OK", carStatus = car.CarStatus, accessToken = newAccessToken }));
            }
            return(Json(new { }));
        }