// Finds the closest stop for the given route name and gets arrival data for that stop // Returns a list of DateTimes for the timezone of the given lat/lon public async Task <List <double> > GetArrivalTimesForRouteName(string routeShortName, string lat, string lon, DateTime time) { // validate lat,lon inputs GeocodeHelpers.ValidateLatLon(lat, lon); // find the route object for the given name and the closest stop for that route // demo tuples (Route, Stop)info = await GetRouteAndStopForLocation(routeShortName, lat, lon); List <ArrivalsAndDeparture> arrivalData = await GetArrivalsAndDepartures(info.Item2.Id, info.Item1.ShortName); var universalTime = time.ToUniversalTime(); IEnumerable <DateTime> query = from a in arrivalData select BusHelpers.ConvertMillisecondsToUTC(a.PredictedArrivalTime); //demo sourcelink var busTimes = arrivalData.Select(a => BusHelpers.ConvertMillisecondsToUTC(a.PredictedArrivalTime)).Take(3); var timeUntil = new List <double>(); foreach (var t in busTimes) { var delta = t - universalTime; //demo had to add in the Round bc was off in decimals var min = Math.Round(delta.TotalMinutes, 1); timeUntil.Add(min); //timeUntil.Add(delta.TotalMinutes); } return(timeUntil); }
// Finds the bus route that matches the route short name and finds the closest // bus stop that contains the route. // Returns a tuple of the user's Route and the nearest Stop in a 1800-meter radius public async Task <(Route route, Stop stop)> GetRouteAndStopForLocation(string routeShortName, string lat, string lon) { (Route, List <Stop> stops)routeAndStops = await GetStopsForRoute(routeShortName, lat, lon); if (routeAndStops.Item1 == null || routeAndStops.Item2 == null) { throw new ArgumentException("No stops were found within a mile of your location for your bus route."); } //Stop minDistStop = routeAndStops.Item2.First(); // demo pythia in claculate distance // demo linq query to foreach (want to step into method, so to set easier breakpoint convert to foreach) //var minDistance = routeAndStops.stops.Min(s => GeocodeHelpers.CalculateDistance(lat, lon, s.Lat, s.Lon)); var min = (from stop in routeAndStops.stops select GeocodeHelpers.CalculateDistance(lat, lon, stop.Lat, stop.Lon)).Min(); Stop minDistStop = routeAndStops.stops.Where(x => GeocodeHelpers.CalculateDistance(lat, lon, x.Lat, x.Lon) == min).FirstOrDefault(); return(routeAndStops.Item1, minDistStop); }