Exemple #1
0
        /// <inheritdoc />
        public async Task <(double MinLat, double MaxLat, double MinLon, double MaxLon)> UpdateRoutesForAgencyAsync(
            string agencyTag,
            CancellationToken cancellationToken = default
            )
        {
            var nextbusResponse = await GetNextBusPolicy()
                                  .ExecuteAsync(_ => _nextbusClient.GetRoutesForAgency(agencyTag), cancellationToken)
                                  .ConfigureAwait(false);

            var nextbusRoutes = nextbusResponse
//                .Where(r => new[] { "6", "110", "85", "985", }.Contains(r.Tag))
                                .ToArray();

            _logger.LogInformation("{0} routes for {1} are loaded from NextBus", nextbusRoutes.Length, agencyTag);

            // setting the max/min default values helps with the comparisons in the for loop below
            (double MinLat, double MaxLat, double MinLon, double MaxLon)boundaries =
                (double.MaxValue, Double.MinValue, double.MaxValue, Double.MinValue);

            for (int i = 0; i < nextbusRoutes.Length; i++)
            {
                var nextbusRoute = nextbusRoutes[i];

                var nextbusRouteConfig = await GetNextBusPolicy()
                                         .ExecuteAsync(_ => _nextbusClient.GetRouteConfig(agencyTag, nextbusRoute.Tag), cancellationToken)
                                         .ConfigureAwait(false);

                var mongoRoute = Converter.FromNextBusRoute(nextbusRoute, nextbusRouteConfig);
                mongoRoute.AgencyTag = agencyTag;

                _logger.LogInformation("Inserting route {0} ({1})", mongoRoute.Title, mongoRoute.Tag);
                Error error = await _routeRepo.AddAsync(mongoRoute, cancellationToken)
                              .ConfigureAwait(false);

                if (error != null)
                {
                    _logger.LogError(
                        "Failed to persist the route {0}: {1}", mongoRoute.Title ?? mongoRoute.Tag, error.Message
                        );
                }

                await UpdateDirectionsForRouteAsync(agencyTag, mongoRoute.Tag, cancellationToken)
                .ConfigureAwait(false);

                if ((double)nextbusRouteConfig.LatMin < boundaries.MinLat)
                {
                    boundaries.MinLat = (double)nextbusRouteConfig.LatMin;
                }
                if ((double)nextbusRouteConfig.LonMin < boundaries.MinLon)
                {
                    boundaries.MinLon = (double)nextbusRouteConfig.LonMin;
                }
                if (boundaries.MaxLat < (double)nextbusRouteConfig.LatMax)
                {
                    boundaries.MaxLat = (double)nextbusRouteConfig.LatMax;
                }
                if (boundaries.MaxLon < (double)nextbusRouteConfig.LonMax)
                {
                    boundaries.MaxLon = (double)nextbusRouteConfig.LonMax;
                }
            }

            return(boundaries);
        }
Exemple #2
0
        private async Task SeedAgencyDataAsync(NextbusAgency nxtbAgency)
        {
            var nxtbRoutes = (await _nextBusClient.GetRoutesForAgency(nxtbAgency.Tag).ConfigureAwait(false)).ToArray();

            Agency agency = (Agency)nxtbAgency;

            {
                // Populate first agency coords
                var routeConfig = await _nextBusClient.GetRouteConfig(nxtbAgency.Tag, nxtbRoutes[0].Tag)
                                  .ConfigureAwait(false);

                agency.MinLatitude  = (double)routeConfig.LatMin;
                agency.MaxLatitude  = (double)routeConfig.LatMax;
                agency.MinLongitude = (double)routeConfig.LonMin;
                agency.MaxLongitude = (double)routeConfig.LonMax;
            }

            List <AgencyRoute> routes = new List <AgencyRoute>(90);

            foreach (var nxtbRoute in nxtbRoutes)
            {
                // for testing
                //if (
                //    (agency.Tag == "ttc" && !new[] { "57", "85", "190" }.Contains(nxtbRoute.Tag))
                //    || (agency.Tag == "jtafla" && !new[] { "53" }.Contains(nxtbRoute.Tag))
                //    )
                //{
                //    continue;
                //}

                var routeConfig = await _nextBusClient.GetRouteConfig(nxtbAgency.Tag, nxtbRoute.Tag)
                                  .ConfigureAwait(false);

                AgencyRoute route = (AgencyRoute)routeConfig;

                UpdateAgencyMinMaxCoordinates(ref agency,
                                              route.MaxLatitude,
                                              route.MinLatitude,
                                              route.MaxLongitude,
                                              route.MinLongitude);

                BusStop[] busStops = GetAllBusStopsPersistNew(routeConfig.Stops);

                List <RouteDirection> directions = new List <RouteDirection>(routeConfig.Directions.Length);

                foreach (NextbusDirection nextbusDir in routeConfig.Directions)
                {
                    RouteDirection dir = (RouteDirection)nextbusDir;
                    dir.RouteDirectionBusStops.Capacity = nextbusDir.StopTags.Length;

                    foreach (string stopTag in nextbusDir.StopTags)
                    {
//                        try
//                        {
                        BusStop stop = busStops.Single(s => s.Tag == stopTag);
                        dir.RouteDirectionBusStops.Add(new RouteDirectionBusStop(dir, stop));
//                        }
//                        catch (Exception e)
//                        {
//                            Console.WriteLine(e);
//                            throw;
//                        }
                    }
                    directions.Add(dir);
                }

                route.Directions = directions;
                routes.Add(route);
            }

            agency.Routes = routes;

            if (agency.Tag.Equals(TestAgencyTag, StringComparison.OrdinalIgnoreCase))
            {
                agency.Country = TestAgencyTag;
            }

            _dbContext.Agencies.Add(agency);

            await _dbContext.SaveChangesAsync().ConfigureAwait(false);
        }