public async Task <GeocodeModel> CalculateRouteDestination(string address) { RestRequest request = new RestRequest($"Locations", Method.GET); request.AddParameter("q", address, ParameterType.QueryString); request.AddParameter("o", "json", ParameterType.QueryString); request.AddParameter("key", _bingConfig.Key, ParameterType.QueryString); var result = await _bingClient.ExecuteTaskAsync <string>(request); if (!result.IsSuccessful) { throw new Exception($"Failed to find address with error {result.StatusCode}"); } string json = result.Data; JObject jobj = JsonConvert.DeserializeObject <JObject>(json); BingGeocodeResult obj = jobj.ToObject <BingGeocodeResult>(); BingGeocodeResource geo = obj.resourceSets[0].resources[0]; return(new GeocodeModel() { location = new LocationModel() { latitude = geo.point.coordinates[0], longitude = geo.point.coordinates[1] }, confidence = geo.confidence, formattedAddress = geo.address.formattedAddress }); }
private async Task <BingGeocodeResource> Geocode(string address) { RestRequest request = new RestRequest($"Locations", Method.GET); request.AddParameter("q", address, ParameterType.QueryString); request.AddParameter("o", "json", ParameterType.QueryString); request.AddParameter("key", _config.BingKey, ParameterType.QueryString); var result = await _bingClient.ExecuteTaskAsync <string>(request); if (!result.IsSuccessful) { throw new Exception($"Failed to find address with error {result.StatusCode}"); } string json = result.Data; JObject jobj = JsonConvert.DeserializeObject <JObject>(json); BingGeocodeResult obj = jobj.ToObject <BingGeocodeResult>(); BingGeocodeResource geo = obj.resourceSets[0].resources[0]; return(geo); }
private async Task RunDelivery() { ClearTimer(); try { Console.WriteLine("Enter the Event Id"); string eventId = Console.ReadLine(); Console.WriteLine("Enter the Delivery Id. Use comma-separated list for multiple deliveries."); string deliveryCSV = Console.ReadLine(); string[] deliveries = deliveryCSV.Split(','); if (deliveries.Count() == 0) { throw new Exception("Could not understand input string"); } _runningDeliveries = new List <RunningDelivery>(); foreach (string d in deliveries) { string deliveryId = d; DeliveryModel delivery = await GetDelivery(eventId, deliveryId); if (delivery == null) { throw new Exception("Invalid event or delivery id. Please try again."); } BingGeocodeResource destination = await Geocode(delivery.Destination); if (destination == null) { throw new Exception("Could not geocode destination. Please try again."); } string city = destination.address.locality; string state = destination.address.adminDistrict; BingGeocodeResource cityCenter = await Geocode($"{city}, {state}"); if (cityCenter == null) { throw new Exception("Could not find a suitable delivery start location"); } var bingout = await ComputeRoute(cityCenter.point, destination.point); var distance = bingout.routeLegs[0].travelDistance; var startLat = bingout.routePath.line.coordinates[0][0]; var startLong = bingout.routePath.line.coordinates[0][1]; var coords = ReduceCoordinateResolution(bingout.routePath.line.coordinates, bingout.routePath.generalizations[0].pathIndices); var numPoints = coords.Count; RunningDelivery runningDelivery = new RunningDelivery() { _deviceClient = _deviceClient, _delivery = delivery, _tripId = Guid.NewGuid().ToString(), _vehicleId = delivery.VehicleId, _driverId = delivery.DriverId, _distanceIncrement = distance / (decimal)numPoints, _locations = coords.ConvertAll(x => new MobiliyaLocation { Latitude = x[0].ToString(), Longitude = x[1].ToString() }) }; _runningDeliveries.Add(runningDelivery); } Console.WriteLine("** Ready To Send Simulated Route Points **"); ListSimulationOptions(); while (true) { char keychar = Console.ReadKey().KeyChar; if (keychar == ' ') { if (_timer != null) { ClearTimer(); Console.WriteLine("Auto Play Terminated."); } await SendDeliveryUpdates(null, null, null); } else if (keychar == 'a') { System.Timers.Timer newTimer = new System.Timers.Timer(); newTimer.Elapsed += async(sender, e) => await SendDeliveryUpdates(sender, e, newTimer); newTimer.Interval = _config.FleetUpdateSpan; newTimer.Start(); _timer = newTimer; ListSimulationOptions(); } else if (keychar == 'q') { Console.WriteLine("Simulation Terminated"); break; } else { Console.WriteLine("Not a valid option."); ListSimulationOptions(); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }