예제 #1
0
        public virtual async Task <RouteDetails> getRouteDurationAndOverallDistance(PickUpAddress depot, List <Delivery> deliveries)
        {
            string waypointsUriString = "";

            foreach (Delivery delivery in deliveries)
            {
                waypointsUriString += DirectionsService.getStringFromAddressInLatLngFormat(delivery.Client.Address);
                waypointsUriString += "|";
            }
            waypointsUriString = waypointsUriString.TrimEnd('|');
            string depotUriString        = DirectionsService.getStringFromAddressInLatLngFormat(depot);
            string uri                   = "https://maps.googleapis.com/maps/api/directions/json?origin=" + depotUriString + "&destination=" + depotUriString + "&waypoints=optimize:true|" + waypointsUriString;
            HttpResponseMessage response = await googleMaps.performGoogleMapsRequestAsync(uri);

            var jsonString = response.Content.ReadAsStringAsync().Result;

            JObject json = JObject.Parse(jsonString);

            var distanceObjects     = json["routes"].Children()["legs"].Children()["distance"];
            var distanceValues      = distanceObjects.Select(ds => ds["value"]).Values <double>();
            var overallDistanceInKm = Math.Round(distanceValues.Sum() / 1000, 2);

            var durationObjects        = json["routes"].Children()["legs"].Children()["duration"];
            var durationValues         = durationObjects.Select(ds => ds["value"]).Values <double>();
            var overallDurationInHours = Math.Round(durationValues.Sum() / 3600, 2);

            return(new RouteDetails(overallDistanceInKm, overallDurationInHours));
        }
예제 #2
0
 public async Task SendStatusUpdateEmailToAdminAsync(string statusString, Delivery delivery, List <EmployeeUser> employees)
 {
     foreach (EmployeeUser user in employees)
     {
         string emailMessage = "Hi " + user.NormalizedUserName;
         emailMessage += "The status of delivery for the address " + DirectionsService.getStringFromAddress(delivery.Client.Address) + " dated " + delivery.DeliverBy.ToString() + " has been changed to " + statusString;
         string emailAddress = user.Email;
         string emailSubject = "Delivery status change to: " + statusString;
         await emailSender.SendEmailAsync(emailAddress, emailSubject, emailMessage);
     }
 }
예제 #3
0
        public async Task SendStatusUpdateEmailToClientAsync(Status status, Delivery delivery, Client client)
        {
            string emailMessage = "Hi " + client.FirstName;

            if (!status.Equals(Status.FailedDelivery))
            {
                emailMessage += "The status of delivery for the address " + DirectionsService.getStringFromAddress(delivery.Client.Address) + " dated " + delivery.DeliverBy.ToString() + " has been changed to " + status.DisplayName();
                string emailAddress = client.Email;
                string emailSubject = "The status of your order has changed to: " + status.DisplayName();
                await emailSender.SendEmailAsync(emailAddress, emailSubject, emailMessage);
            }
        }
예제 #4
0
        public virtual async Task addLocationDataToAddress(Address address)
        {
            string addressString         = DirectionsService.getStringFromAddress(address);
            string uri                   = "https://maps.googleapis.com/maps/api/geocode/json?address=" + addressString;
            HttpResponseMessage response = await googleMaps.performGoogleMapsRequestAsync(uri);

            var     jsonString = response.Content.ReadAsStringAsync().Result;
            JObject json       = JObject.Parse(jsonString);
            var     latValue   = (string)json.SelectToken("results[0].geometry.location.lat");
            var     lat        = Convert.ToDouble(latValue);

            var lngValue = (string)json.SelectToken("results[0].geometry.location.lng");
            var lng      = Convert.ToDouble(lngValue);

            address.Lat = lat;
            address.Lng = lng;
        }
예제 #5
0
        public virtual async Task <PickUpAddress> FindClosestDepotLocationForRoute(ICollection <PickUpAddress> pickUpLocations, Center center)
        {
            var distances = new double[pickUpLocations.Count()];

            for (int i = 0; i < pickUpLocations.Count(); i++)
            {
                string addressString         = DirectionsService.getStringFromAddress(pickUpLocations.ElementAt(i));
                string uri                   = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=" + center.lat + "," + center.lng + "&destinations=" + addressString;
                HttpResponseMessage response = await googleMaps.performGoogleMapsRequestAsync(uri);

                var     jsonString = response.Content.ReadAsStringAsync().Result;
                JObject json       = JObject.Parse(jsonString);
                var     distance   = Convert.ToDouble((string)json.SelectToken("rows[0].elements[0].distance.value"));
                distances[i] = distance;
            }
            var minDistanceIndex = distances.ToList().IndexOf(distances.Min());

            return(pickUpLocations.ElementAt(minDistanceIndex));
        }
예제 #6
0
        public virtual async Task <double> getDistanceBetweenTwoAddresses(Address addressOne, Address addressTwo)
        {
            string addressOneString = DirectionsService.getStringFromAddress(addressOne);
            string addressTwoString = DirectionsService.getStringFromAddress(addressTwo);

            string uri = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=" + addressOneString + "&destinations=" + addressTwoString;
            HttpResponseMessage response = await googleMaps.performGoogleMapsRequestAsync(uri);

            var     jsonString    = response.Content.ReadAsStringAsync().Result;
            JObject json          = JObject.Parse(jsonString);
            var     distanceValue = (string)json.SelectToken("rows[0].elements[0].distance.text");

            if (distanceValue == null) // can be NOT FOUND status
            {
                return(1000);
            }
            var valueInDouble = Convert.ToDouble(distanceValue.Replace("mi", ""));

            return(valueInDouble);
        }
        private async Task SelectDeliveriesBasedOnDistance(double DistanceWithin, IEnumerable <Address> addresses, HttpClient httpClient, string currentLocationString, IList <Delivery> deliveries)
        {
            foreach (Address address in addresses)
            {
                string addressLocationString = DirectionsService.getStringFromAddress(address, true);
                HttpResponseMessage response = await googleMaps.createDistanceUriAndGetResponse(currentLocationString, addressLocationString, httpClient);

                if (response.IsSuccessStatusCode)
                {
                    double distanceToAddress = GetDistanceFromResponse(response);
                    if (distanceToAddress != DEFAULT_NON_FOUND_ADDRESS_VALUE && distanceToAddress < DistanceWithin)
                    {
                        Delivery delivery = null;
                        Type     type     = address.GetType();
                        if (type == typeof(PickUpAddress))
                        {
                            delivery = context.Deliveries
                                       .Include(d => d.DeliveryStatus)
                                       .SingleOrDefault();
                        }
                        else
                        {
                            delivery = context.Deliveries
                                       .Include(d => d.DeliveryStatus)
                                       .Include(d => d.Client)
                                       .Include(d => d.Client.Address)
                                       .Where(d => d.Client.Address.ID == address.ID)
                                       .FirstOrDefault();
                        }
                        if (delivery != null)
                        {
                            DeliveryStatus status = delivery.DeliveryStatus;
                            if (status != null && status.Status == Status.New)
                            {
                                deliveries.Add(delivery);
                            }
                        }
                    }
                }
            }
        }