/// <summary>
        /// Find driving directions associated with the given trail ID and address.
        /// </summary>
        /// <param name="trailId">The ID of the trail in the data store.</param>
        /// <param name="address">The address to look up.</param>
        /// <returns>The associated driving directions, or null if none exist.</returns>
        private DrivingDirections FindDrivingDirections(int trailId, Address address)
        {
            DbGeographyPointComparer comparer = new DbGeographyPointComparer();

            DrivingDirections directions;
            using (MyTrailsContext context = new MyTrailsContext())
            {
                Address innerAddress = context.Addresses
                    .ToList()
                    .Where(a => comparer.Equals(a.Coordinate, address.Coordinate))
                    .FirstOrDefault();

                directions = innerAddress == null ?
                    null :
                    innerAddress.Directions
                        .Where(d => d.TrailId == trailId)
                        .FirstOrDefault();
            }

            return directions;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add driving directions for the given trail / address pair.
        /// </summary>
        /// <param name="address">The starting address.</param>
        /// <param name="trail">The trail ending address.</param>
        /// <returns>Task for asynchronous completion.</returns>
        private async Task AddDrivingDirections(Address address, Trail trail)
        {
            RouteRequest request = new RouteRequest
            {
                Credentials = new Credentials
                {
                    ApplicationId = this.Configuration.ApplicationId,
                },
                Waypoints = new[]
                {
                    new Waypoint
                    {
                        Description = "Start",
                        Location = new Location
                        {
                            Latitude = address.Coordinate.Latitude.Value,
                            Longitude = address.Coordinate.Longitude.Value,
                        },
                    },
                    new Waypoint
                    {
                        Description = "End",
                        Location = new Location
                        {
                            Latitude = trail.Location.Latitude.Value,
                            Longitude = trail.Location.Longitude.Value,
                        },
                    },
                },
            };

            ResponseSummary summary;
            RouteResult result;
            RetryPolicy policy = this.BuildRetryPolicy();
            IRouteService routeService = this.RouteServiceFactory.CreateRouteService();
            try
            {
                RouteResponse response = await policy.ExecuteAsync(() => routeService.CalculateRouteAsync(request));
                summary = response.ResponseSummary;
                result = response.Result;
            }
            catch (FaultException<ResponseSummary> ex)
            {
                summary = ex.Detail;
                result = null;
            }
            finally
            {
                IDisposable disposable = routeService as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }

            if (summary.StatusCode != ResponseStatusCode.Success)
            {
                if (summary.StatusCode == ResponseStatusCode.BadRequest && 
                    summary.FaultReason.Contains("No route was found for the waypoints provided."))
                {
                    this.Logger.WarnFormat("No route found between trail add address: '{0}', '{1}'", trail, address);
                }
                else
                {
                    throw new ApplicationException(string.Format("Routing service call failed. {0}: {1}", summary.StatusCode, summary.FaultReason));
                }
            }
            else
            {
                address.Directions.Add(new DrivingDirections
                {
                    Address = address,
                    Trail = trail,
                    DrivingTimeSeconds = (int)result.Summary.TimeInSeconds,
                });
            }
        }
        public void TestInitialize()
        {
            this.ClearDatabase();

            this._anyAddress = new Address
            {
                Location = "any location string",
                Coordinate = DbGeographyExt.PointFromCoordinates(23.45, -67.89),
            };
            this._anyTrail = new Trail
            {
                WtaId = "any-wta-trail-id",
                Name = "Any Trail Name",
                Url = new Uri("http://any/trail/uri"),
                Location = DbGeographyExt.PointFromCoordinates(12.34, -56.78),
            };

            this._routeServiceMock = new Mock<IRouteService>(MockBehavior.Strict);
            this._routeServiceMock
                .Setup(rs => rs.CalculateRouteAsync(It.IsAny<RouteRequest>()))
                .Returns(TaskExt.WrapInTask(() => this.CreateRouteResponse(123456)));

            Mock<IRouteServiceFactory> routeServiceFactoryMock = new Mock<IRouteServiceFactory>(MockBehavior.Strict);
            routeServiceFactoryMock
                .Setup(rsf => rsf.CreateRouteService())
                .Returns(this._routeServiceMock.Object);

            this._configurationMock = new Mock<IBingMapsConfiguration>(MockBehavior.Strict);
            this._configurationMock
                .SetupGet(c => c.ApplicationId)
                .Returns("anyApplicationId");
            this._configurationMock
                .SetupGet(c => c.RetryCount)
                .Returns(0);
            this._configurationMock
                .SetupGet(c => c.RetryMinBackOff)
                .Returns(TimeSpan.FromMilliseconds(1234));
            this._configurationMock
                .SetupGet(c => c.RetryMaxBackOff)
                .Returns(TimeSpan.FromMilliseconds(2345));
            this._configurationMock
                .SetupGet(c => c.RetryDeltaBackOff)
                .Returns(TimeSpan.FromMilliseconds(456));

            this._extender = new DrivingDistanceExtender
            {
                RouteServiceFactory = routeServiceFactoryMock.Object,
                Configuration = this._configurationMock.Object,
                Logger = new StubLog(),
            };
        }