public async Task <IActionResult> UpdateVehiclePopulationPositions([FromBody] VehiclePopulation population)
        {
            try
            {
                if (TryValidateModel(population))
                {
                    this.Log.Info($"Receiving vehicle data from simulation: {population}");

                    await this.statisticsService.UpdatePersonalityStats(population);

                    await this.statisticsService.UpdateVehiclePopulation(population);

                    await this.statisticsService.UpdateMomentarySpeeds(population);

                    return(Ok());
                }
                else
                {
                    return(ValidationProblem());
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
    private IEnumerator GatherVehiclePopulationData(CancellationToken cancellationToken)
    {
        while (!cancellationToken.IsCancellationRequested)
        {
            var vehiclePopulation = new VehiclePopulation();
            foreach (Vehicle vehicle in vehicleManager.vehicles)
            {
                vehiclePopulation.VehicleStatuses.Add(
                    new VehicleStatus
                {
                    Id           = vehicle.id,
                    Latitude     = vehicle.transform.position.y,
                    Longitude    = vehicle.transform.position.x,
                    CurrentSpeed = vehicle.velocity,
                    Personality  = vehicle.driver.Personality
                });

                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
            }

            if (vehiclePopulation.VehicleCount != 0)
            {
                communicationModule.AddMessageToQueue(vehiclePopulation);
            }

            yield return(new WaitForSeconds(this.updateIntervalInSeconds));
        }
    }
 public async Task SignalForVehiclePopulation(VehiclePopulation population)
 {
     try
     {
         Log.Debug(population);
         await Clients.All.SendAsync(SignalMethods.SignalForVehiclePopulation.Method, population);
     }
     catch (Exception ex)
     {
         Log.Error($"Exception occured while sending statistics: {ex.Message}");
         throw new SignalHubException($"Error in SignalForVehiclePopulation method", ex);
     }
 }
        public async Task UpdateMomentarySpeeds(VehiclePopulation population)
        {
            foreach (Personality personality in Enum.GetValues(typeof(Personality)))
            {
                this.avgSpeedByPersonality[personality] = 0;
            }

            var groupped = population.VehicleStatuses.GroupBy(x => x.Personality);

            foreach (var group in groupped)
            {
                this.avgSpeedByPersonality[group.Key] = group.Select(x => x.CurrentSpeed).Average();
            }

            await this.hubContext.Clients.All.SendAsync(SignalMethods.SignalForAvgSpeedByPersonality.Method, this.avgSpeedByPersonality);
        }
        public async Task UpdatePersonalityStats(VehiclePopulation population)
        {
            foreach (Personality personality in Enum.GetValues(typeof(Personality)))
            {
                this.personalityStats[personality] = 0;
            }

            var groupped = population.VehicleStatuses.GroupBy(x => x.Personality);

            foreach (var group in groupped)
            {
                this.personalityStats[group.Key] = group.Count();
            }

            await this.hubContext.Clients.All.SendAsync(SignalMethods.SignalForPersonalityStats.Method, this.personalityStats);
        }
        public async Task UpdateVehiclePopulation(VehiclePopulation population)
        {
            foreach (var geoPosition in population.VehicleStatuses)
            {
                var offset = CalcDecimalDegreesFromMeters(
                    latitude: sceneData.latitude,
                    longitude: sceneData.longitude,
                    x: geoPosition.Latitude,
                    y: geoPosition.Longitude);

                geoPosition.Latitude  = offset.Item1;
                geoPosition.Longitude = offset.Item2;
            }

            this.vehiclePopulationList.Add(population);
            await this.hubContext.Clients.All.SendAsync(SignalMethods.SignalForVehiclePopulation.Method, population);
        }
 public async Task Update(VehiclePopulation population)
 {
     population.VehicleStatuses.ForEach(x => x.CurrentSpeed *= 3.6f); // change to km/h
     await this.ApiClient.SendAsync(population, Endpoints.VehiclePopulation);
 }