Example #1
0
        /// <summary>
        /// Dependency-injected application settings which are then passed on to other components.
        /// </summary>
        public TransitApiController(IApplicationEnvironment appEnv)
        {
            _repository = new MemoryTransitRepository(appEnv.ApplicationBasePath);
            _client     = new TransitClient();

            _getCurrentTime = () => TimeZoneInfo.ConvertTime(DateTimeOffset.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
        }
        /// <summary>
        /// Dependency-injected application settings which are then passed on to other components.
        /// </summary>
        public TransitApiController(IApplicationEnvironment appEnv)
        {
            _repository = new MemoryTransitRepository(appEnv.ApplicationBasePath);
            _client = new TransitClient();

            _getCurrentTime = () => TimeZoneInfo.ConvertTime(DateTimeOffset.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
        }
        /// <summary>
        /// Dependency-injected application settings which are then passed on to other components.
        /// </summary>
        public TransitApiController()
        {
            if (bool.Parse(ConfigurationManager.AppSettings["UseAzureStorage"]))
            {
                var appSettings = new AppSettings(ConfigurationManager.AppSettings);
                _repository = new AzureTransitRepository(appSettings);
            }
            else
            {
                var filePath = HostingEnvironment.MapPath("~");
                _repository = new MemoryTransitRepository(filePath);
            }
            _client = new TransitClient();

            _getCurrentTime = () => TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.Now, "Pacific Standard Time");
        }
        /// <summary>
        /// Dependency-injected application settings which are then passed on to other components.
        /// </summary>
        public TransitApiController()
        {
            if (bool.Parse(ConfigurationManager.AppSettings["UseAzureStorage"]))
            {
                var appSettings = new AppSettings(ConfigurationManager.AppSettings);
                _repository = new AzureTransitRepository(appSettings);
            }
            else
            {
                var filePath = HostingEnvironment.MapPath("~");
                _repository = new MemoryTransitRepository(filePath);
            }
            _client = new TransitClient();

            _getCurrentTime = () => TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.Now, "Pacific Standard Time");
        }
        /// <summary>
        /// Returns the bus schedule for the given stop IDs, incorporating the ETA from Connexionz.
        /// </summary>
        public static async Task<ClientBusSchedule> GetSchedule(ITransitRepository repository, ITransitClient client, DateTimeOffset currentTime, IEnumerable<int> stopIds)
        {
            var schedulesTask = repository.GetScheduleAsync();
            var estimatesTask = GetEtas(repository, client, stopIds);

            var schedule = await schedulesTask;
            var estimates = await estimatesTask;
            
            // Holy nested dictionaries batman
            var todaySchedule = stopIds.Where(schedule.ContainsKey)
                                       .ToDictionary(platformNo => platformNo,
                                                     platformNo => schedule[platformNo]
                .ToDictionary(routeSchedule => routeSchedule.RouteNo,
                              routeSchedule => InterleaveRouteScheduleAndEstimates(routeSchedule, estimates.ContainsKey(platformNo) ? estimates[platformNo] : new Dictionary<string, List<int>>(), currentTime)));

            return todaySchedule;
        }
        /// <summary>
        /// Gets a user friendly arrivals summary for the requested stops.
        /// Returns a dictionary which takes a stop ID and returns the list of route arrival summaries (used to populate a table).
        /// </summary>
        public static async Task<Dictionary<int, List<RouteArrivalsSummary>>> GetArrivalsSummary(ITransitRepository repository, ITransitClient client, DateTimeOffset currentTime, IEnumerable<int> stopIds)
        {
            var schedule = await GetSchedule(repository, client, currentTime, stopIds);

            return schedule.ToDictionary(stopSchedule => stopSchedule.Key,
                stopSchedule => ToRouteArrivalsSummaries(stopSchedule.Value, currentTime));
        }
        /// <summary>
        /// Gets the ETA info for a set of stop IDS.
        /// The outer dictionary takes a route number and gives a dictionary that takes a stop ID to an ETA.
        /// </summary>
        public static async Task<BusArrivalEstimates> GetEtas(ITransitRepository repository, ITransitClient client, IEnumerable<int> stopIds)
        {
            var toPlatformTag = await repository.GetPlatformTagsAsync();
            
            Func<int, Task<Tuple<int, ConnexionzPlatformET>>> getEtaIfTagExists =
                async id => Tuple.Create(id, toPlatformTag.ContainsKey(id) ? await client.GetEtaAsync(toPlatformTag[id]) : null);

            var tasks = stopIds.Select(getEtaIfTagExists);
            var results = await Task.WhenAll(tasks);

            return results.ToDictionary(eta => eta.Item1,
                eta => eta.Item2?.RouteEstimatedArrivals
                                ?.ToDictionary(routeEta => routeEta.RouteNo,
                                               routeEta => routeEta.EstimatedArrivalTime)
                       ?? new Dictionary<string, List<int>>());
        }
        public static async Task<List<FavoriteStopViewModel>> GetFavoritesViewModel(ITransitRepository repository,
            ITransitClient client, DateTimeOffset currentTime, IEnumerable<int> stopIds, LatLong? optionalUserLocation)
        {
            var staticData = JsonConvert.DeserializeObject<BusStaticData>(await repository.GetSerializedStaticDataAsync());

            var favoriteStops = GetFavoriteStops(staticData, stopIds, optionalUserLocation);

            var scheduleTask = GetSchedule(repository, client, currentTime, favoriteStops.Select(f => f.Id));

            var schedule = await scheduleTask;

            var result = favoriteStops.Select(favorite => ToViewModel(favorite, staticData, schedule, currentTime))
                                      .ToList();

            return result;
        }
Example #9
0
 public TransitApiController(IHostingEnvironment env)
 {
     _repository     = new MemoryTransitRepository(env.WebRootPath);
     _client         = new TransitClient();
     _getCurrentTime = () => TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.Now, _destinationTimeZoneId);
 }
        /// <summary>
        /// Gets a user friendly arrivals summary for the requested stops.
        /// Returns a dictionary which takes a stop ID and returns the list of route arrival summaries (used to populate a table).
        /// </summary>
        public static async Task<Dictionary<int, List<RouteArrivalsSummary>>> GetArrivalsSummary(ITransitRepository repository, ITransitClient client, DateTimeOffset currentTime, IEnumerable<int> stopIds)
        {
            var schedule = await GetSchedule(repository, client, currentTime, stopIds);
            var staticData = await repository.GetStaticDataAsync();

            var matchingStopIds = stopIds.Where(staticData.Stops.ContainsKey);
            var arrivalsSummaries = matchingStopIds.ToDictionary(stopId => stopId,
                stopId => ToRouteArrivalsSummaries(staticData.Stops[stopId].RouteNames, schedule[stopId], currentTime));
            return arrivalsSummaries;
        }
        /// <summary>
        /// Returns the bus schedule for the given stop IDs, incorporating the ETA from Connexionz.
        /// </summary>
        public static async Task <ClientBusSchedule> GetSchedule(ITransitRepository repository, ITransitClient client, DateTimeOffset currentTime, IEnumerable <int> stopIds)
        {
            var schedulesTask = repository.GetScheduleAsync();
            var estimatesTask = GetEtas(repository, client, stopIds);

            var schedule  = await schedulesTask;
            var estimates = await estimatesTask;

            Func <int, Dictionary <string, List <BusArrivalTime> > > makePlatformSchedule = platformNo =>
                                                                                            schedule[platformNo].ToDictionary(routeSchedule => routeSchedule.RouteNo,
                                                                                                                              routeSchedule => InterleaveRouteScheduleAndEstimates(
                                                                                                                                  routeSchedule,
                                                                                                                                  estimates.ContainsKey(platformNo)
                            ? estimates[platformNo]
                            : new Dictionary <string, List <int> >(),
                                                                                                                                  currentTime));

            var todaySchedule = stopIds.Where(schedule.ContainsKey)
                                .ToDictionary(platformNo => platformNo, makePlatformSchedule);

            return(todaySchedule);
        }
        /// <summary>
        /// Gets a user friendly arrivals summary for the requested stops.
        /// Returns a dictionary which takes a stop ID and returns the list of route arrival summaries (used to populate a table).
        /// </summary>
        public static async Task <Dictionary <int, List <RouteArrivalsSummary> > > GetArrivalsSummary(ITransitRepository repository, ITransitClient client, DateTimeOffset currentTime, IEnumerable <int> stopIds)
        {
            var schedule = await GetSchedule(repository, client, currentTime, stopIds);

            var staticData = await repository.GetStaticDataAsync();

            var matchingStopIds   = stopIds.Where(staticData.Stops.ContainsKey);
            var arrivalsSummaries = matchingStopIds.ToDictionary(stopId => stopId,
                                                                 stopId => ToRouteArrivalsSummaries(staticData.Stops[stopId].RouteNames, schedule[stopId], currentTime));

            return(arrivalsSummaries);
        }
        /// <summary>
        /// Gets the ETA info for a set of stop IDS.
        /// The outer dictionary takes a route number and gives a dictionary that takes a stop ID to an ETA.
        /// </summary>
        public static async Task <BusArrivalEstimates> GetEtas(ITransitRepository repository, ITransitClient client, IEnumerable <int> stopIds)
        {
            var toPlatformTag = await repository.GetPlatformTagsAsync();

            Func <int, Task <Tuple <int, ConnexionzPlatformET> > > getEtaIfTagExists =
                async id => Tuple.Create(id, toPlatformTag.ContainsKey(id) ? await client.GetEta(toPlatformTag[id]) : null);

            var tasks   = stopIds.Select(getEtaIfTagExists);
            var results = await Task.WhenAll(tasks);

            return(results.ToDictionary(eta => eta.Item1,
                                        eta => eta.Item2?.RouteEstimatedArrivals
                                        ?.ToDictionary(routeEta => routeEta.RouteNo,
                                                       routeEta => routeEta.EstimatedArrivalTime)
                                        ?? new Dictionary <string, List <int> >()));
        }
        public static async Task <List <FavoriteStopViewModel> > GetFavoritesViewModel(ITransitRepository repository,
                                                                                       ITransitClient client, DateTimeOffset currentTime, IEnumerable <int> stopIds, LatLong?optionalUserLocation)
        {
            var staticData = await repository.GetStaticDataAsync();

            var favoriteStops = GetFavoriteStops(staticData, stopIds, optionalUserLocation);

            var scheduleTask = GetSchedule(repository, client, currentTime, favoriteStops.Select(f => f.Id));

            var schedule = await scheduleTask;

            var result = favoriteStops.Select(favorite => ToViewModel(favorite, staticData, schedule, currentTime))
                         .ToList();

            return(result);
        }
Example #15
0
        /// <summary>
        /// Gets a user friendly arrivals summary for the requested stops.
        /// Returns a dictionary which takes a stop ID and returns the list of route arrival summaries (used to populate a table).
        /// </summary>
        public static async Task <Dictionary <int, List <RouteArrivalsSummary> > > GetArrivalsSummary(ITransitRepository repository, ITransitClient client, DateTimeOffset currentTime, IEnumerable <int> stopIds)
        {
            var schedule = await GetSchedule(repository, client, currentTime, stopIds);

            return(schedule.ToDictionary(stopSchedule => stopSchedule.Key,
                                         stopSchedule => ToRouteArrivalsSummaries(stopSchedule.Value, currentTime)));
        }
Example #16
0
        /// <summary>
        /// Gets the ETA info for a set of stop IDS.
        /// The outer dictionary takes a route number and gives a dictionary that takes a stop ID to an ETA.
        /// </summary>
        public static async Task <BusArrivalEstimates> GetEtas(ITransitRepository repository, ITransitClient client, IEnumerable <int> stopIds)
        {
            var toPlatformTag = await repository.GetPlatformTagsAsync();

            var tasks   = stopIds.Select(getEtaIfTagExists);
            var results = await Task.WhenAll(tasks);

            return(results.ToDictionary(eta => eta.stopId,
                                        eta => eta.platformET?.RouteEstimatedArrivals
                                        ?.ToDictionary(routeEta => routeEta.RouteNo,
                                                       routeEta => routeEta.EstimatedArrivalTime)
                                        ?? new Dictionary <string, List <int> >()));

            async Task <(int stopId, ConnexionzPlatformET?platformET)> getEtaIfTagExists(int id)
            => (id, toPlatformTag.TryGetValue(id, out int tag) ? await client.GetEta(tag) : null);
        }