private static List <BusArrivalTime> InterleaveRouteScheduleAndEstimates(BusStopRouteSchedule routeSchedule,
                                                                                 Dictionary <string, List <int> > stopEstimates, DateTimeOffset currentTime)
        {
            var arrivalTimes = Enumerable.Empty <BusArrivalTime>();

            var daySchedule = routeSchedule.DaySchedules.FirstOrDefault(ds => DaysOfWeekUtils.IsToday(ds.Days, currentTime));

            if (daySchedule != null)
            {
                var relativeSchedule = MakeRelativeScheduleWithinCutoff(daySchedule, currentTime);
                arrivalTimes = relativeSchedule.Select(minutes => new BusArrivalTime(minutes, isEstimate: false));
            }

            if (stopEstimates.ContainsKey(routeSchedule.RouteNo))
            {
                var routeEstimates = stopEstimates[routeSchedule.RouteNo];
                arrivalTimes = arrivalTimes.Where(arrivalTime =>
                                                  !routeEstimates.Any(estimate =>
                                                                      Math.Abs(arrivalTime.MinutesFromNow - estimate) <= ESTIMATE_CORRELATION_TOLERANCE_MINUTES));
                arrivalTimes = arrivalTimes.Concat(
                    routeEstimates.Select(estimate => new BusArrivalTime(estimate, isEstimate: true)));
            }

            arrivalTimes = arrivalTimes.OrderBy(arrivalTime => arrivalTime);

            return(arrivalTimes.ToList());
        }
예제 #2
0
        private static List <int> InterleaveRouteScheduleAndEstimates(BusStopRouteSchedule routeSchedule,
                                                                      Dictionary <string, List <int> > stopEstimates, DateTimeOffset currentTime)
        {
            var result = new List <int>();

            var daySchedule = routeSchedule.DaySchedules.FirstOrDefault(ds => DaysOfWeekUtils.IsToday(ds.Days, currentTime));

            if (daySchedule != null)
            {
                result.AddRange(ToMinutesFromNow(daySchedule, currentTime));
            }

            if (stopEstimates.ContainsKey(routeSchedule.RouteNo))
            {
                var routeEstimates = stopEstimates[routeSchedule.RouteNo];
                foreach (int estimate in routeEstimates)
                {
                    // When an estimate time is close to a scheduled time, overwrite the schedule with the estimate.
                    var scheduleIndex = result.FindIndex(i => Math.Abs(i - estimate) <= 10);
                    if (scheduleIndex != -1)
                    {
                        result[scheduleIndex] = estimate;
                    }
                    else
                    {
                        result.Insert(0, estimate);
                    }
                }
            }

            result.Sort();

            return(result);
        }