Exemplo n.º 1
0
        public void Properly_Update_Current_And_Previous_Car_Coordinates()
        {
            Location startingLocation = new Location {
                Latitude = 52.069342, Longitude = -1.022140
            };
            Location updatedLocation = new Location {
                Latitude = 52.070342, Longitude = -1.022140
            };

            #region Setup Car Coordinates

            CarCoordinates starting = new CarCoordinates
            {
                CarIndex  = 0,
                Location  = startingLocation,
                TimeStamp = 1
            };

            CarCoordinates updated = new CarCoordinates
            {
                CarIndex  = 0,
                Location  = updatedLocation,
                TimeStamp = 1
            };

            #endregion

            CarDetails testData = new CarDetails
            {
                CarLocationData = starting
            };

            Assert.IsNull(testData.PreviousCarCoordinates);
            Assert.AreEqual(startingLocation.Latitude, testData.CarLocationData.Location.Latitude);
            Assert.AreEqual(startingLocation.Longitude, testData.CarLocationData.Location.Longitude);

            testData.UpdateCarCoordinates(updated);

            Assert.IsNotNull(testData.PreviousCarCoordinates);
            Assert.AreEqual(updatedLocation.Latitude, testData.CarLocationData.Location.Latitude);
            Assert.AreEqual(updatedLocation.Longitude, testData.CarLocationData.Location.Longitude);
            Assert.AreEqual(startingLocation.Latitude, testData.PreviousCarCoordinates.Location.Latitude);
            Assert.AreEqual(startingLocation.Longitude, testData.PreviousCarCoordinates.Location.Longitude);
        }
Exemplo n.º 2
0
        public void Calculate_Car_Speed_And_Distance_Traveled()
        {
            #region Setup Car Coordinates

            CarCoordinates starting = new CarCoordinates
            {
                CarIndex = 0,
                Location = new Location {
                    Latitude = 52.06792886633373, Longitude = -1.0238781510887842
                },
                TimeStamp = 1581977379142
            };

            CarCoordinates updated = new CarCoordinates
            {
                CarIndex = 0,
                Location = new Location {
                    Latitude = 52.06786406260594, Longitude = -1.0239574561135762
                },
                TimeStamp = 1581977378941
            };

            #endregion

            CarDetails testData = new CarDetails
            {
                CarLocationData        = starting,
                PreviousCarCoordinates = updated,
                AverageSpeedPerLap     = new Dictionary <int, double>(),
                LapNumber = 1
            };

            testData.UpdateCarSpeed();

            Assert.AreEqual(100, (int)testData.CurrentSpeed);
            Assert.IsTrue(testData.AverageSpeedPerLap.ContainsKey(1));
        }
 /// <summary>
 /// Updates the Current and Previous Coordinates of the car
 /// </summary>
 /// <param name="carDetails"></param>
 /// <param name="currentCarCoordinates"></param>
 public static void UpdateCarCoordinates(this CarDetails carDetails, CarCoordinates currentCarCoordinates)
 {
     carDetails.PreviousCarCoordinates = carDetails.CarLocationData;
     carDetails.CarLocationData        = currentCarCoordinates;
 }
        /// <summary>
        /// Process the Car Coordinates and update cache and publish relevant Status and Events
        /// </summary>
        /// <param name="carCoordinatesPayload"></param>
        public void Process(string carCoordinatesPayload)
        {
            CarCoordinates currentCarDetails = JsonConvert.DeserializeObject <CarCoordinates>(carCoordinatesPayload);

            if (_allCarDetailsCache.ContainsKey(currentCarDetails.CarIndex))
            {
                #region Update Car Details

                CarDetails currentCarInformation = _allCarDetailsCache[currentCarDetails.CarIndex];
                currentCarInformation.UpdateCarCoordinates(currentCarDetails);
                currentCarInformation.UpdateCarSpeed();
                currentCarInformation.UpdateRank(ReorderAllCarsAndGetCurrentCarsPosition(currentCarInformation));

                #endregion

                #region Publish Car Speed

                PublisherClient.PublishMessage("carStatus", currentCarInformation.GenerateCarStatusPayload(StatusPayloadType.Speed));

                #endregion

                #region Update Position Of Car if Changed

                if (currentCarInformation.Rank != currentCarInformation.PreviousRank)
                {
                    PublisherClient.PublishMessage("carStatus", currentCarInformation.GenerateCarStatusPayload(StatusPayloadType.Position));
                }

                #endregion

                #region Update Lap

                if (CarLocationProcessor.FinishedLap(currentCarInformation))
                {
                    PublisherClient.PublishMessage("events", currentCarInformation.GenerateCarEvent(EventType.LapComplete));

                    currentCarInformation.PreviousLapTime = currentCarDetails.TimeStamp;
                    currentCarInformation.LapNumber++;
                }

                #endregion
            }
            else
            {
                CarDetails carDetails = new CarDetails
                {
                    CarLocationData    = currentCarDetails,
                    Rank               = -1,
                    CurrentSpeed       = 0,
                    CarIndex           = currentCarDetails.CarIndex,
                    DistancedTraveled  = 0,
                    PreviousRank       = 0,
                    AverageSpeedPerLap = new Dictionary <int, double>()
                    {
                        { 1, 0.0 }
                    },
                    LapNumber       = 1,
                    PreviousLapTime = currentCarDetails.TimeStamp
                };

                _allCarDetailsCache.Add(currentCarDetails.CarIndex, carDetails);
            }
        }