Beispiel #1
0
        public IHttpActionResult StartDelivery(StartDeliveryModel model)
        {
            Courier courier = CourierHelper.GetCourier(context, model.Identifier, model.IdentifierType);

            if (courier == null)
            {
                return(BadRequest());
            }
            var order = context.GetTable <Order>().Where(x => x.Id == model.OrderId).FirstOrDefault();

            order.State = OrderStateEnum.InDelivery;
            order.StateChangeTimestamp = DateTime.Now;
            order.CourierId            = courier.Id;
            context.SubmitChanges();
            HereApi api  = new HereApi();
            var     time = api.GetTimeTravel(order.StartAddressCoords, order.EndAddressCoords);

            return(Json(time));
        }
Beispiel #2
0
        public JsonResult StartDeliverySummary(StartDeliveryModel Start)
        {
            string   error = "";
            Delivery delivery;

            try
            {
                delivery = _context.Deliveries.Where(d => d.Id == Start.DeliveryId)
                           .Include(d => d.Vehicle).First();
            }
            catch (Exception)
            {
                error = "Delivery Not Found";
                return(new JsonResult(error));
            }

            if (delivery == null)
            {
                error = "Delivery Not Found";
                return(new JsonResult(error));
            }
            delivery.Started = true;
            try
            {
                using (var webClient = new WebClient())
                {
                    string url = "https://matrix.route.api.here.com/routing/7.2/calculatematrix.json"
                                 + "?app_id=ORWs1MBbnXAyzlgdPGpw"
                                 + "&app_code=ftEQwIdOxSdxiRv6pd1Rvw";
                    url += "&start0" + "=" + delivery.SourceLatitude + "," + delivery.SourceLongtitude;
                    url += "&destination0=" + delivery.DestinationLatitude + "," + delivery.DestinationLongtitude;
                    url += "&summaryAttributes=distance,traveltime&mode=fastest;car;traffic:disabled";
                    var     rawJSON = webClient.DownloadStringTaskAsync(url).Result;
                    JObject rss     = JObject.Parse(rawJSON);
                    delivery.OptimalDistance = (float)rss["response"]["matrixEntry"][0]["summary"]["distance"] / 1000;
                    delivery.OptimalTime     = (int)rss["response"]["matrixEntry"][0]["summary"]["travelTime"] / 60;
                }
            }
            catch (Exception)
            {
                //  error = "Optimal Distance Error";
                //return new JsonResult(error);
            }

            delivery.OptimalFuelConsumption = delivery.OptimalDistance * delivery.Vehicle.FuelConsumption;


            DeliverySummary summary = new DeliverySummary()
            {
                Delivery          = delivery,
                StartTime         = Start.StartTime,
                EndTime           = Start.StartTime,
                StartFuelLevel    = Start.StartFuelLevel,
                EndFuelLevel      = Start.StartFuelLevel,
                StartOdometer     = Start.StartOdometer,
                EndOdometer       = Start.StartOdometer,
                HardCorneringRate = 5,
                HarshAccelerationAndDeceleration = 5,
                HarshBreakingsRate  = 5,
                FuelConsumptionRate = 5,
                OnTimeDeliveryRate  = 5,
                Idling        = 5,
                OverRevving   = 5,
                SpeedingsRate = 5,
                SeatBeltRate  = 5
            };
            Vehicle V = delivery.Vehicle;

            if (V != null)
            {
                V.Latitude          = Start.Latitude;
                V.Longtitude        = Start.Longtitude;
                V.isCurrentlyActive = true;
            }
            else
            {
                error = "Vehicle Error";
                return(new JsonResult(error));
            }
            _context.Deliveries.Update(delivery);
            _context.DeliverySummaries.Add(summary);
            _context.Vehicles.Update(V);
            _context.SaveChanges();

            long DeliverySummaryId = summary.Id;

            return(Json(new { DeliverySummaryId }));
        }