Пример #1
0
        internal BestAvailableCompany FindSpecificCompany(string market, CreateReportOrder createReportOrder, string orderCompanyKey = null, int?orderFleetId = null, double?latitude = null, double?longitude = null)
        {
            if (!orderCompanyKey.HasValue() && !orderFleetId.HasValue)
            {
                Exception createOrderException = new ArgumentNullException("You must at least provide a value for orderCompanyKey or orderFleetId");
                createReportOrder.Error = createOrderException.ToString();
                _commandBus.Send(createReportOrder);
                throw createOrderException;
            }

            var homeCompanyKey = _serverSettings.ServerData.TaxiHail.ApplicationKey;

            var fleets = market.HasValue()
                ? _taxiHailNetworkServiceClient.GetMarketFleets(homeCompanyKey, market).ToArray()
                : _taxiHailNetworkServiceClient.GetNetworkFleet(homeCompanyKey, latitude, longitude).ToArray();

            if (orderCompanyKey.HasValue())
            {
                var match = fleets.FirstOrDefault(f => f.CompanyKey == orderCompanyKey);
                if (match != null)
                {
                    return(new BestAvailableCompany
                    {
                        CompanyKey = match.CompanyKey,
                        CompanyName = match.CompanyName,
                        FleetId = match.FleetId
                    });
                }
            }

            if (orderFleetId.HasValue)
            {
                var match = fleets.FirstOrDefault(f => f.FleetId == orderFleetId.Value);
                if (match != null)
                {
                    return(new BestAvailableCompany
                    {
                        CompanyKey = match.CompanyKey,
                        CompanyName = match.CompanyName,
                        FleetId = match.FleetId
                    });
                }
            }

            // Nothing found
            return(new BestAvailableCompany());
        }
Пример #2
0
        private List <NetworkFleetResponse> GetMarketFleets(string market, string currentCompanyKey, double pickupLatitude, double pickupLongitude)
        {
            const int searchExpendLimit = 10;
            var       searchRadius      = 2000; // In meters

            for (var i = 1; i < searchExpendLimit; i++)
            {
                var marketVehicles = GetAvailableVehiclesServiceClient(market)
                                     .GetAvailableVehicles(market, pickupLatitude, pickupLongitude, searchRadius, null, true)
                                     .ToArray();

                if (marketVehicles.Any())
                {
                    // Group and order fleet from max vehicle count to min
                    var orderedFleets = marketVehicles.GroupBy(v => v.FleetId)
                                        .OrderBy(v => v.Count())
                                        .Select(g => g.First());

                    // Fetch all NetworkFleet objects for this market
                    var allMarketFleets = _taxiHailNetworkServiceClient.GetMarketFleets(null, market).ToArray();

                    // All NetworkFleet objects for this market except the current one
                    var marketFleets = allMarketFleets.Where(f => f.CompanyKey != currentCompanyKey);
                    var currentFleet = allMarketFleets.First(f => f.CompanyKey == currentCompanyKey);

                    // Return only those from the ordered fleet
                    var nonNullOrdered = orderedFleets
                                         .Select(v => marketFleets.FirstOrDefault(f => f.FleetId == v.FleetId))
                                         .Where(f => f != null)
                                         .ToList();

                    nonNullOrdered.Insert(0, currentFleet);
                    return(nonNullOrdered);
                }

                // Nothing found, extend search radius (total radius after 10 iterations: 3375m)
                searchRadius += (i * 25);
            }

            return(new List <NetworkFleetResponse>());
        }
Пример #3
0
        public AvailableVehiclesResponse Post(AvailableVehicles request)
        {
            var vehicleType = _dao.GetAll().FirstOrDefault(v => v.ReferenceDataVehicleId == request.VehicleTypeId);
            var logoName    = vehicleType != null ? vehicleType.LogoName : null;

            IbsVehiclePosition[] ibsVehicles;
            string market = null;

            try
            {
                market = _taxiHailNetworkServiceClient.GetCompanyMarket(request.Latitude, request.Longitude);
            }
            catch
            {
                // Do nothing. If we fail to contact Customer Portal, we continue as if we are in a local market.
                _logger.LogMessage("VehicleService: Error while trying to get company Market to find available vehicles.");
            }

            if (!market.HasValue() &&
                _serverSettings.ServerData.LocalAvailableVehiclesMode == LocalAvailableVehiclesModes.IBS)
            {
                // LOCAL market IBS
                ibsVehicles = _ibsServiceProvider.Booking().GetAvailableVehicles(request.Latitude, request.Longitude, request.VehicleTypeId);
            }
            else
            {
                string      availableVehiclesMarket;
                IList <int> availableVehiclesFleetIds = null;

                if (!market.HasValue() && _serverSettings.ServerData.LocalAvailableVehiclesMode == LocalAvailableVehiclesModes.HoneyBadger)
                {
                    // LOCAL market Honey Badger
                    availableVehiclesMarket = _serverSettings.ServerData.HoneyBadger.AvailableVehiclesMarket;

                    if (request.FleetIds != null)
                    {
                        // Use fleet ids specified in the request first
                        availableVehiclesFleetIds = request.FleetIds;
                    }
                    else if (_serverSettings.ServerData.HoneyBadger.AvailableVehiclesFleetId.HasValue)
                    {
                        // Or fleet id specified in the settings
                        availableVehiclesFleetIds = new[] { _serverSettings.ServerData.HoneyBadger.AvailableVehiclesFleetId.Value };
                    }
                }
                else if (!market.HasValue() && _serverSettings.ServerData.LocalAvailableVehiclesMode == LocalAvailableVehiclesModes.Geo)
                {
                    // LOCAL market Geo
                    availableVehiclesMarket = _serverSettings.ServerData.CmtGeo.AvailableVehiclesMarket;

                    if (_serverSettings.ServerData.CmtGeo.AvailableVehiclesFleetId.HasValue)
                    {
                        availableVehiclesFleetIds = new[] { _serverSettings.ServerData.CmtGeo.AvailableVehiclesFleetId.Value };
                    }
                }
                else
                {
                    // EXTERNAL market Honey Badger or Geo
                    availableVehiclesMarket = market;

                    try
                    {
                        // Only get available vehicles for dispatchable companies in market
                        var roamingCompanies = _taxiHailNetworkServiceClient.GetMarketFleets(_serverSettings.ServerData.TaxiHail.ApplicationKey, market);
                        if (roamingCompanies != null)
                        {
                            var roamingFleetIds = roamingCompanies.Select(r => r.FleetId);

                            if (request.FleetIds != null)
                            {
                                // From the fleets accessible by that company, only return vehicles from the fleets specified in the request
                                availableVehiclesFleetIds = roamingFleetIds
                                                            .Where(fleetId => request.FleetIds.Contains(fleetId))
                                                            .ToArray();
                            }
                            else
                            {
                                // Return vehicles from all fleets accessible by that company
                                availableVehiclesFleetIds = roamingFleetIds.ToArray();
                            }
                        }
                        else
                        {
                            availableVehiclesFleetIds = request.FleetIds;
                        }
                    }
                    catch
                    {
                        // Do nothing. If we fail to contact Customer Portal, we return an unfiltered list of available vehicles.
                        _logger.LogMessage("VehicleService: Error while trying to get Market fleets.");
                    }
                }

                var vehicleResponse = GetAvailableVehiclesServiceClient(market).GetAvailableVehicles(
                    market: availableVehiclesMarket,
                    latitude: request.Latitude,
                    longitude: request.Longitude,
                    searchRadius: request.SearchRadius,
                    fleetIds: availableVehiclesFleetIds,
                    wheelchairAccessibleOnly: (vehicleType != null && vehicleType.IsWheelchairAccessible));

                ibsVehicles = vehicleResponse.Select(v => new IbsVehiclePosition
                {
                    Latitude      = v.Latitude,
                    Longitude     = v.Longitude,
                    PositionDate  = v.Timestamp,
                    VehicleNumber = v.Medallion,
                    FleetId       = v.FleetId,
                    Eta           = (int?)v.Eta,
                    VehicleType   = v.VehicleType,
                    CompassCourse = v.CompassCourse,
                }).ToArray();
            }

            var isAuthenticated = this.GetSession().IsAuthenticated;

            var availableVehicles = new List <AvailableVehicle>();

            foreach (var ibsVehicle in ibsVehicles)
            {
                var vehicle = new AvailableVehicle
                {
                    Latitude    = ibsVehicle.Latitude,
                    Longitude   = ibsVehicle.Longitude,
                    LogoName    = logoName,
                    Eta         = ibsVehicle.Eta,
                    VehicleType = ibsVehicle.VehicleType
                };

                if (isAuthenticated)
                {
                    vehicle.CompassCourse = ibsVehicle.CompassCourse ?? 0;
                    vehicle.VehicleName   = ibsVehicle.VehicleNumber;
                    vehicle.FleetId       = ibsVehicle.FleetId;
                }

                availableVehicles.Add(vehicle);
            }

            return(new AvailableVehiclesResponse(availableVehicles));
        }