示例#1
0
        private async Task SendDeliveryUpdates(object source, ElapsedEventArgs e, System.Timers.Timer currentTimer)
        {
            int iColor = 0;

            try
            {
                if (_timer != null)
                {
                    _timer.Stop();
                }                                      // stop the timer while sending multiple position updates to avoid overlaps

                Console.WriteLine(string.Empty);
                Console.WriteLine($"SENDING UPDATES FOR {_runningDeliveries.Count} DELIVERIES.");
                foreach (RunningDelivery rd in _runningDeliveries)
                {
                    _runningDelivery = rd;

                    Console.ForegroundColor = _consoleColors[iColor % _consoleColors.Length];
                    Console.WriteLine($"Sending update for {rd._delivery.DeliveryId}");

                    await rd.SendDeliveryUpdate();

                    await Task.Delay(_config.CarUpdateSpan);

                    iColor++;
                }
                Console.ResetColor();

                Console.WriteLine(string.Empty);
                Console.WriteLine("All Updates Complete");
                var activeDeliveries = _runningDeliveries.Where(p => p._iLocation < p._locations.Count);

                if (_timer != null)
                {
                    _timer.Start();
                }                                       // restart to wait _config.TimerSpan before sending the next batch

                if (activeDeliveries.Count() > 0)
                {
                    ListSimulationOptions();
                }
                else
                {
                    Console.WriteLine("All Deliveries Exhaused.  Press [q] to quit simulation.");
                }
            }
            catch (Exception ex)
            {
                Console.ResetColor();
                throw ex;
            }
        }
示例#2
0
        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);
            }
        }