public static int CalculateLane(TrafficSegmentConfiguration segmentConfiguration, TrafficSegmentSituation segmentSituation, SimulatedCar car)
 {
     if (segmentConfiguration.NumberOfLanes > 2)
     {
         // do complexity
         if (car.Speeding && segmentSituation.IsRushHour(SimulatedClock.Time, out var rushHour))
         {
             // Only take the two most left lanes
             return(new Random().Next(1, 3));
         }
     }
     return(new Random().Next(1, segmentConfiguration.NumberOfLanes + 1));
 }
Exemple #2
0
        private async Task MakeOneCarDrive(Random random, TrafficSegmentConfiguration trafficConfiguration, TrafficSegmentSituation segmentSituation, IEventTransmitter startCameraEventTransmitter, IEventTransmitter endCameraEventTransmitter, CancellationToken cancellationToken)
        {
            // Wait random short interval to add randomness
            await Task.Delay(TimeSpan.FromMilliseconds(random.Next(2000)), cancellationToken);

            var car = SimulatedCar.Randomize
                      (
                random, segmentSituation
                      );

            try
            {
                //regenerate new license plate for every run
                var carTimespan = car.CalculateTime(trafficConfiguration.CameraDistance, _simulationSettings.TimeSimulationAccelerator);
                await startCameraEventTransmitter.Transmit(
                    new CameraEvent
                {
                    TrajectId = trafficConfiguration.SegmentId,
                    CameraId  = CameraType.Camera1.ToString(),
                    EventTime = SimulatedClock.Time,
                    Car       = car,
                    Lane      = LaneCalculator.CalculateLane(trafficConfiguration, segmentSituation, car)
                }, cancellationToken);

                _logger.Trace($"{car.Color} {car.Make} with license plate {car.LicensePlate} detected by camera 01 (limit {segmentSituation.SpeedLimit})");
                await Task.Delay(carTimespan, cancellationToken);

                await endCameraEventTransmitter.Transmit(
                    new CameraEvent
                {
                    TrajectId = trafficConfiguration.SegmentId,
                    CameraId  = CameraType.Camera2.ToString(),
                    EventTime = SimulatedClock.Time,
                    Car       = car,
                    Lane      = LaneCalculator.CalculateLane(trafficConfiguration, segmentSituation, car)
                }, cancellationToken);

                _logger.Trace($"{car.Color} {car.Make} with license plate {car.LicensePlate} detected by camera 02 (limit {segmentSituation.SpeedLimit})");
            }
            catch (TaskCanceledException)
            {
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error happened in one of the simulation threads");
            }
        }