/// <summary>
        /// process a new coordinates value for a car, work out the speed and race positions
        /// </summary>
        /// <param name="coords">new car coordinates</param>
        internal void HandleCarDataEvent(JCarCoords coords)
        {
            // TODO check whether it is computationally expensive to provide the new value
            // as this will only be needed each time data about a new car is received
            Car car = Cars.GetOrAdd(coords.CarIndex,
                                    new Car(coords, RaceMonitor_SpeedEvent, RaceMonitor_PositionEvent, RaceMonitor_LapEvent));

            // store the new coordinates
            Cars[coords.CarIndex].UpdateCoordinates(coords);
            currentTimeData.Timestamp = coords.Timestamp;
        }
Example #2
0
        /// <summary>
        /// handles a new MQTT Car Coordinates message
        /// </summary>
        /// <param name="sender">event source</param>
        /// <param name="e">event arguments</param>
        /// TODO - consider implementing the "IPublishDataConverter" to do conversion
        private void Client_MessageAvailable(object sender, MqttMessageEventArgs e)
        {
            // only process messages of interest
            if (CarCoordinatesTopic == e.Topic)
            {
                string ReceivedMessage = Encoding.UTF8.GetString((byte[])e.Message);

                // convert the message into a JCarCoords message
                JCarCoords coords = JsonConvert.DeserializeObject <JCarCoords>(ReceivedMessage);
                RaceDataHandler.ProcessRaceData(coords);
            }
        }
        /// <summary>
        /// implementation of the ICarCoordinates interface
        /// </summary>
        /// <param name="coords">car coordinate data</param>
        public void ProcessRaceData(JCarCoords coords)
        {
            if (currentTimeData == null)
            {
                // set up the first timestamp object
                currentTimeData = new TimestampData(coords.Timestamp, this);
            }
            else if (currentTimeData.Timestamp != coords.Timestamp)
            {
                // data has arrived with a new timestamp, work out the positions
                // of the cars for the previous timestamp
                RecalculatePositions(currentTimeData);
            }

            HandleCarDataEvent(coords);
        }
Example #4
0
        /// <summary>
        /// implementation of the ICarCoordinates interface
        /// </summary>
        /// <param name="coords">car coordinate data</param>
        public void ProcessRaceData(JCarCoords coords)
        {
            // TODO replace with a more meaningful race model

            // received new car coordinates
            // store the information - Car array
            // calculate speed & publish
            speed++;
            speed %= 300;
            client.SendSpeed(coords.Timestamp, coords.CarIndex, speed);
            // calculate posision & publish
            position = random.Next(1, 6);
            client.SendPosition(coords.Timestamp, coords.CarIndex, position);
            // identify interesting events & publish
            if (speed == 1)
            {
                client.SendRaceEvent(coords.Timestamp, $"event {position}");
            }
        }
 /// <summary>
 /// process new car coordinate message
 /// </summary>
 /// <param name="coords"></param>
 public void PerformTask(JCarCoords coords)
 {
     ProcessRaceData(coords);
 }