Пример #1
0
        public void Test_Calcolo_Distanze()
        {
            var punti = Points.Select(p => p.Destination.Location).Distinct(new Location_Comparer()).ToList();

            punti.Clear();
            for (int i = 0; i < 100; i++)
                punti.Add(new Location { Latitude = 44 + 0.0001 * i, Longitude = 12 + 0.0001 * i });

            DateTime? start = null;
            var stop = start;
            var distanze = new List<DistanzaStimata>();
            var mre = new ManualResetEvent(false);
            var jobs = new Queue<DistanzaStimata>();

            var route = maps.ServiceHelper.GetRouteService();
            //route.CalculateRouteCompleted += (sender, e) =>
            //{
            //    var d = e.UserState as DistanzaStimata;
            //    if (start == null)
            //        start = DateTime.Now;

            //    if (e.Error == null && d != null)
            //    {
            //        d.Km = e.Result.Result.Summary.Distance;
            //        d.TimeInSeconds = e.Result.Result.Summary.TimeInSeconds;
            //    }
            //    lock (d)
            //        Monitor.Pulse(d);

            //    d.Done();
            //    if (distanze.All(a => a.Completed))
            //    {
            //        stop = DateTime.Now;
            //        mre.Set();
            //    }
            //};

            var combos = new Utilities.Combinatorics.Combinations<Location>(punti, 2);
            foreach (var combinazione in combos)
            {
                var da = combinazione[0];
                var a = combinazione[1];
                var distanza = new DistanzaStimata { From = da, To = a };
                distanze.Add(distanza);
                jobs.Enqueue(distanza);
            }

            //for (int i = 0; i < 10; i++)
            //{
            //    Task.Factory.StartNew(() =>
            //    {
            //        while (jobs.Count > 0)
            //        {
            //            var distanza = jobs.Dequeue();

            //            var points = new[] { distanza.From, distanza.To }.Select(l => new maps.RouteService.Waypoint
            //            {
            //                Location = new maps.RouteService.Location { Latitude = l.Latitude, Longitude = l.Longitude }
            //            }).To_ObservableCollection();

            //            var request = new maps.RouteService.RouteRequest
            //            {
            //                Credentials = new maps.RouteService.Credentials() { Token = maps.ServiceHelper.GeocodeServiceCredentials },
            //                Culture = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(),
            //                Options = new maps.RouteService.RouteOptions { Optimization = maps.RouteService.RouteOptimization.MinimizeDistance },
            //                Waypoints = points
            //            };

            //            route.CalculateRouteAsync(request, distanza);

            //            lock (distanza)
            //                Monitor.Wait(distanza);

            //        }
            //    });
            //}

            var builder = new Route_Request_Builder(distanze);
            var requests = builder.Get_Requests().To_Queue();
            var test = builder.Get_Requests();
            var q1 = test.SelectMany(r => r.Get_Routes()).Where(r => !r.Completed);

            route.CalculateRouteCompleted += (sender, e) =>
            {
                var d = e.UserState as Request;
                if (start == null)
                    start = DateTime.Now;

                if (e.Error == null && d != null)
                {
                    for (int i = 0; i < e.Result.Result.Legs.Count; i++)
                    {
                        var leg = e.Result.Result.Legs[i];
                        var r = d.Get_Routes().ElementAt(i);
                        r.Km = leg.Summary.Distance;
                        r.TimeInSeconds = leg.Summary.TimeInSeconds;
                        r.Done();
                    }
                    d.Done();
                }
                lock (d)
                    Monitor.Pulse(d);

                if (test.All(a => a.Completed))
                {
                    stop = DateTime.Now;
                    mre.Set();
                }
            };

            for (int i = 0; i < 10; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    while (requests.Count > 0)
                    {
                        var request = requests.Dequeue();

                        var points = request.Get_Locations().Select(l => new maps.RouteService.Waypoint
                        {
                            Location = new maps.RouteService.Location { Latitude = l.Latitude, Longitude = l.Longitude }
                        }).To_ObservableCollection();

                        var routeRequest = new maps.RouteService.RouteRequest
                        {
                            Credentials = new maps.RouteService.Credentials() { Token = maps.ServiceHelper.GeocodeServiceCredentials },
                            Culture = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(),
                            Options = new maps.RouteService.RouteOptions { Optimization = maps.RouteService.RouteOptimization.MinimizeDistance },
                            Waypoints = points
                        };

                        route.CalculateRouteAsync(routeRequest, request);

                        lock (request)
                            Monitor.Wait(request);

                    }
                });
            }
        }
Пример #2
0
        public Task<DistanzaStimata> Calcola_Distanza(Location da, Location a)
        {
            var points = new[] { da, a }.Select(l => new maps.RouteService.Waypoint
            {
                Location = new maps.RouteService.Location { Latitude = l.Latitude, Longitude = l.Longitude }
            }).To_ObservableCollection();

            var tcs = new TaskCompletionSource<DistanzaStimata>();

            var route = maps.ServiceHelper.GetRouteService();

            route.CalculateRouteCompleted += (sender, e) =>
            {
                var d = e.UserState as DistanzaStimata;
                d.Done();

                if (e.Error != null || d == null)
                {
                    tcs.TrySetException(e.Error);
                    return;
                }
                d.Km = e.Result.Result.Summary.Distance;
                d.TimeInSeconds = e.Result.Result.Summary.TimeInSeconds;

                tcs.TrySetResult(d);
            };

            var request = new maps.RouteService.RouteRequest
            {
                Credentials = new maps.RouteService.Credentials() { Token = maps.ServiceHelper.GeocodeServiceCredentials },
                Culture = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(),
                Options = new maps.RouteService.RouteOptions { Optimization = maps.RouteService.RouteOptimization.MinimizeDistance },
                Waypoints = points
            };

            route.CalculateRouteAsync(request, new DistanzaStimata { From= da, To = a });

            return tcs.Task;
        }
Пример #3
0
        protected void Refresh_Routes_Old()
        {
            if (SelectedSimulation == null)
            {
                SelectedSimulation = new SimulationDto {  Voyages = new ObservableCollection<VoyageDto>() };
                SelectedSimulation.Starting_Address = "Home";
                SelectedSimulation.Returning_Address = "Home";

                SelectedSimulation.Starting_Latitude =   44.1383781433105;
                SelectedSimulation.Starting_Longitude =   12.2387800216675;
                SelectedSimulation.Returning_Latitude =  44.1383781433105;
                SelectedSimulation.Returning_Longitude = 12.2387800216675;

                for (int i = 0; i < 1; i++)
                {
                    var v = new VoyageDto() { Orders = new ObservableCollection<int>() };
                    foreach(var order in Scenario.Orders)
                        v.Orders.Add(order.Number);

                    v.Departing = DateTime.Today.AddDays(1);
                    v.Exitmated_Time = TimeSpan.FromDays(1);
                    v.Extimated_Lenght_Km = 12;

                    SelectedSimulation.Voyages.Add(v);
                }
                Simulations.Add(SelectedSimulation);
            }

            var service = maps.ServiceHelper.GetRouteService();
            service.CalculateRouteCompleted += (sender, e) =>
            {
                var voyage = e.UserState as VoyageDto;

                Routes.Add(new RouteViewModel(voyage, e.Result.Result.RoutePath.Points));
            };

            foreach (var route in SelectedSimulation.Voyages)
            {
                var wp = route.Orders.Select(v => new maps.RouteService.Waypoint
                    {
                        Description = v + "",
                        Location = new maps.RouteService.Location { Latitude = GetOrder(v).Latitude, Longitude = GetOrder(v).Longitude }
                    }).To_ObservableCollection();

                wp.Add(new maps.RouteService.Waypoint { Description = SelectedSimulation.Starting_Address, Location = new maps.RouteService.Location { Latitude = SelectedSimulation.Starting_Latitude, Longitude= SelectedSimulation.Starting_Longitude } });
                //wp.Add(new maps.RouteService.Waypoint { Description = SelectedSimulation.Returning_Address, Location = new maps.RouteService.Location { Latitude = SelectedSimulation.Returning_Latitude, Longitude =SelectedSimulation.Returning_Longitude } });

                var request = new maps.RouteService.RouteRequest
                {
                    Credentials = new maps.RouteService.Credentials() { Token = maps.ServiceHelper.GeocodeServiceCredentials },
                    Culture = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(),
                    Waypoints = wp

                };
                request.Options = new maps.RouteService.RouteOptions();
                request.Options.RoutePathType = maps.RouteService.RoutePathType.Points;

                service.CalculateRouteAsync(request, route);
            }
        }