public async Task <RouteAM> GetRoute(AddressAM rootAddress, WaypointsAM waypoints) { var rootCoordinate = rootAddress.ToCoordinate(); var waypointsCoordinate = waypoints.Points.Select(p => p.ToCoordinate()); var externalRoute = await DirectionService.GetRoute(rootCoordinate, rootCoordinate, waypointsCoordinate); var result = await FromExternalRoute(rootAddress, rootAddress, waypoints, externalRoute); return(result); }
public async Task <ICollection <RouteAM> > GetPossibleRoutes(ICollection <AddressAM> rootAddresses, WaypointsAM waypoints) { var exceptions = new ConcurrentQueue <Exception>(); var result = new List <RouteAM>(); await rootAddresses.ParallelForEachAsync( async rootAddress => { try { var route = await GetRoute(rootAddress, waypoints); if (route.Legs.Any()) { result.Add(route); } } catch (Exception e) { exceptions.Enqueue(e); } } ); if (exceptions.Any()) { throw new AggregateException(exceptions); } return(result); }
public async Task <RouteAM> FromExternalRoute(AddressAM origin, AddressAM destination, WaypointsAM waypoints, RouteEM externalRoute) { var result = new RouteAM { Comment = waypoints.Comment }; var routePoints = new List <AddressAM> { origin, destination }; routePoints.AddRange(waypoints.Points); routePoints = routePoints.Distinct().ToList(); foreach (var externalLeg in externalRoute.Legs) { var routeLegKind = RouteLegKind.Transportation; if (externalLeg.Equals(externalRoute.Legs.First())) { routeLegKind = RouteLegKind.Feed; } if (externalLeg.Equals(externalRoute.Legs.Last())) { routeLegKind = RouteLegKind.WayBack; } var startAddress = await AddressService.GetNearestAddress(externalLeg.StartCoordinate, routePoints); startAddress.AdjustedLatitude = externalLeg.StartCoordinate.Latitude; startAddress.AdjustedLongitude = externalLeg.StartCoordinate.Longitude; var endAddress = await AddressService.GetNearestAddress(externalLeg.EndCoordinate, routePoints); endAddress.AdjustedLatitude = externalLeg.EndCoordinate.Latitude; endAddress.AdjustedLongitude = externalLeg.EndCoordinate.Longitude; var leg = new RouteLegAM { Kind = routeLegKind, StartAddress = startAddress, EndAddress = endAddress, Distance = externalLeg.Distance, Duration = externalLeg.Duration }; result.Legs.Add(leg); } return(result); }
public async Task FromExternalRoute() { var rootAddress = new AddressAM { Latitude = 55.55555, Longitude = 66.66666 }; var firstWaypointAddress = new AddressAM { Latitude = 11.11111, Longitude = 22.22222 }; var secondWaypointAddress = new AddressAM { Latitude = 33.33333, Longitude = 44.44444 }; var waypoints = new WaypointsAM { Points = new List <AddressAM> { firstWaypointAddress, secondWaypointAddress }, Comment = "Машина на охраняемой парковке. Пароль:\"Нраииттьься\"" }; var rootCoordinate = new Coordinate { Latitude = 55.55000, Longitude = 66.66000 }; var firstWaypointCoordinate = new Coordinate { Latitude = 11.11000, Longitude = 22.22000 }; var secondWaypointCoordinate = new Coordinate { Latitude = 33.33000, Longitude = 44.44000 }; var externalRoute = new RouteEM { Status = External.Models.Enums.Status.Ok, Legs = new List <LegEM> { new LegEM { StartCoordinate = rootCoordinate, EndCoordinate = firstWaypointCoordinate, Distance = 3000, Duration = 1600 }, new LegEM { StartCoordinate = firstWaypointCoordinate, EndCoordinate = secondWaypointCoordinate, Distance = 35000, Duration = 18000 }, new LegEM { StartCoordinate = secondWaypointCoordinate, EndCoordinate = rootCoordinate, Distance = 38000, Duration = 19600 } } }; Suite.AddressServiceMock .Setup(m => m.GetNearestAddress(rootCoordinate, It.IsAny <IEnumerable <AddressAM> >())) .ReturnsAsync(rootAddress); Suite.AddressServiceMock .Setup(m => m.GetNearestAddress(firstWaypointCoordinate, It.IsAny <IEnumerable <AddressAM> >())) .ReturnsAsync(firstWaypointAddress); Suite.AddressServiceMock .Setup(m => m.GetNearestAddress(secondWaypointCoordinate, It.IsAny <IEnumerable <AddressAM> >())) .ReturnsAsync(secondWaypointAddress); var result = await Suite.RouteService.FromExternalRoute(rootAddress, rootAddress, waypoints, externalRoute); Assert.Equal(3, result.Legs.Count); // Check feeding leg Assert.Equal(rootAddress, result.Legs[0].StartAddress); Assert.Equal(firstWaypointAddress, result.Legs[0].EndAddress); Assert.Equal(externalRoute.Legs[0].Distance, result.Legs[0].Distance); Assert.Equal(externalRoute.Legs[0].Duration, result.Legs[0].Duration); Assert.Equal(RouteLegKind.Feed, result.Legs[0].Kind); // Check transportation leg Assert.Equal(firstWaypointAddress, result.Legs[1].StartAddress); Assert.Equal(secondWaypointAddress, result.Legs[1].EndAddress); Assert.Equal(externalRoute.Legs[1].Distance, result.Legs[1].Distance); Assert.Equal(externalRoute.Legs[1].Duration, result.Legs[1].Duration); Assert.Equal(RouteLegKind.Transportation, result.Legs[1].Kind); // Check wayBacking leg Assert.Equal(secondWaypointAddress, result.Legs[2].StartAddress); Assert.Equal(rootAddress, result.Legs[2].EndAddress); Assert.Equal(externalRoute.Legs[2].Distance, result.Legs[2].Distance); Assert.Equal(externalRoute.Legs[2].Duration, result.Legs[2].Duration); Assert.Equal(RouteLegKind.WayBack, result.Legs[2].Kind); Assert.Equal(rootCoordinate.Latitude, result.Legs[0].StartAddress.AdjustedLatitude); Assert.Equal(rootCoordinate.Longitude, result.Legs[0].StartAddress.AdjustedLongitude); Assert.Equal(firstWaypointCoordinate.Latitude, result.Legs[1].StartAddress.AdjustedLatitude); Assert.Equal(firstWaypointCoordinate.Longitude, result.Legs[1].StartAddress.AdjustedLongitude); Assert.Equal(secondWaypointCoordinate.Latitude, result.Legs[2].StartAddress.AdjustedLatitude); Assert.Equal(secondWaypointCoordinate.Longitude, result.Legs[2].StartAddress.AdjustedLongitude); }