private BusTripDto GetBusTrip(Group from, Group to, DateTime travelTime, TripSchedule schedule) { string link = string.Empty; var trip = this.SearchTrip(from.N, to.N, out link, travelTime, schedule).Trips.Trip.FirstOrDefault(); if (trip == null) { return(null); } var allStops = trip.I.Where(i => i.N != String.Empty && i.N != i.N2).ToList(); var stops = allStops.Where(i => i.N2 != String.Empty); var busTripDto = new BusTripDto { Start = Convert.ToDateTime(trip.Start), Stop = Convert.ToDateTime(trip.Stop), Duration = Convert.ToInt32(trip.Duration), ChangeNb = Convert.ToInt32(trip.Changecount), Link = link, StartCoordinates = new Coordinates { Latitude = Convert.ToDouble(stops.First().Y.Replace(',', '.'), CultureInfo.InvariantCulture), Longitude = Convert.ToDouble(stops.First().X.Replace(',', '.'), CultureInfo.InvariantCulture) }, EndCoordinates = new Coordinates { Latitude = Convert.ToDouble(allStops.Last().Y.Replace(',', '.'), CultureInfo.InvariantCulture), Longitude = Convert.ToDouble(allStops.Last().X.Replace(',', '.'), CultureInfo.InvariantCulture) } }; int counter = 1; foreach (var stop in stops) { busTripDto.TravelParts.Add(counter, new TravelPart { ArrivalName = stop.N2, DepartureName = stop.N, Type = stop.Tn == "Buss" ? TransportationType.Bus : TransportationType.Walk, Duration = Convert.ToDateTime(stop.A) - Convert.ToDateTime(stop.D) }); counter++; } return(busTripDto); }
public async Task <BusTripDto> FindBusTrip(Coordinates from, Coordinates to, DateTime travelTime, TripSchedule schedule) { var nearestStops = this.NearestStops(from); var destinationStop = this.NearestStops(to); var destinationStopCoordinates = new Coordinates { Latitude = Convert.ToDouble(destinationStop.Group.First().Y.Replace(',', '.'), CultureInfo.InvariantCulture), Longitude = Convert.ToDouble(destinationStop.Group.First().X.Replace(',', '.'), CultureInfo.InvariantCulture) }; var walkToDestination = await this.WalkService.WalkInfo(destinationStopCoordinates, to); var walkToDestinationtDuration = walkToDestination.rows.First()?.elements.First()?.duration?.value ?? 0; var busArrivalTime = travelTime.AddSeconds((-1) * walkToDestinationtDuration); BusTripDto busTrip = null; foreach (var departureStop in nearestStops.Group) { try { busTrip = this.GetBusTrip(departureStop, destinationStop.Group.First(), busArrivalTime, schedule); if (busTrip != null) { break; } } catch (Exception ex) { throw ex; } } if (busTrip == null || busTrip.TravelParts.Count == 0) { return(busTrip); } var walkToStart = await this.WalkService.WalkInfo(from, busTrip.StartCoordinates); var walkToStartDuration = walkToStart.rows.FirstOrDefault()?.elements.FirstOrDefault()?.duration?.value ?? 0; if (walkToStartDuration > 0) { busTrip.TravelParts.Add(0, new TravelPart { DepartureName = "Current Location", ArrivalName = busTrip.TravelParts[1].DepartureName, Duration = TimeSpan.FromSeconds(walkToStartDuration), Type = TransportationType.Walk }); busTrip.Duration += walkToStartDuration; busTrip.Start = busTrip.Start.AddSeconds(-1 * walkToStartDuration); } if (walkToDestinationtDuration > 0) { busTrip.TravelParts.Add(busTrip.TravelParts.Count + 1, new TravelPart { DepartureName = busTrip.TravelParts[busTrip.TravelParts.Count - 1].ArrivalName, ArrivalName = "Destination", Duration = TimeSpan.FromSeconds(walkToDestinationtDuration), Type = TransportationType.Walk }); busTrip.Duration += walkToDestinationtDuration; busTrip.Stop = busTrip.Stop.AddSeconds(walkToDestinationtDuration); } busTrip.DurationString = ""; if (busTrip.Duration >= 60) { busTrip.DurationString += busTrip.Duration / 60; busTrip.DurationString += busTrip.Duration / 60 == 1 ? " Hour" : " Hours"; busTrip.DurationString += " and "; } busTrip.DurationString += busTrip.Duration % 60; busTrip.DurationString += busTrip.Duration % 6 == 1 ? " Minute" : " Minutes"; return(busTrip); }