private async Task HandleNewSensorMeasurementAsync(int sensorId, DateTime time)
        {
            var run = await DaoProvider.RunDao.GetCurrentRun();

            if (run == null)
            {
                return;
            }

            var sensorMeasurement = new SensorMeasurement
            {
                SensorId  = sensorId,
                Run       = run,
                Timestamp = (time - UnixEpoch).TotalMilliseconds / 1000
            };

            var sensorMeasurements = (await DaoProvider.SensorMeasurementDao.GetMeasurementsForRun(run)).ToArray();

            if (sensorId > 0 && sensorMeasurements.Length == 0)
            {
                return;
            }

            if (
                // First measurement
                sensorId == 0 && sensorMeasurements.Length == 0 ||

                // Sequential measurement
                sensorMeasurements.Length > 0 && sensorId == sensorMeasurements.Last().SensorId + 1
                )
            {
                await DaoProvider.SensorMeasurementDao.Insert(sensorMeasurement);

                if (sensorId > 0)
                {
                    var interimTime = sensorMeasurement.Timestamp - sensorMeasurements.Last().Timestamp;
                    var timeSpan    = TimeSpan.FromMilliseconds(interimTime * 1000);
                    SensorMeasurementAdded?.Invoke(run.Race, run.RunNumber, run.Skier, timeSpan);
                }
                else if (sensorId == 0)
                {
                    RunStarted?.Invoke(run.Race, run.RunNumber, run.Skier);
                }
            }

            // Last measurement
            if (sensorId == run.Race.NumberOfSensors - 1)
            {
                run.TotalTime = run.RunNumber == 2
                    ? run.TotalTime + sensorMeasurement.Timestamp - sensorMeasurements[0].Timestamp
                    : sensorMeasurement.Timestamp - sensorMeasurements[0].Timestamp;
                run.Status = RunStatus.Completed;
                await DaoProvider.RunDao.Update(run);

                var newLeaderBoard = await GetLeaderBoard(run.Race, run.RunNumber);

                RunStatusChanged?.Invoke(run.Race, run.RunNumber, run.Skier, run.Status);
                LeaderBoardUpdated?.Invoke(run.Race, run.RunNumber, newLeaderBoard);
            }
        }
예제 #2
0
        private async Task <Speedtest> RunSpeedtest(DateTimeOffset timestamp)
        {
            await _semaphore.WaitAsync();

            IsRunning = true;
            RunStarted?.Invoke();

            var test = new Speedtest {
                Timestamp = timestamp
            };

            try
            {
                var settings = await _client.GetSettingsAsync();

                var server = settings.Servers
                             .OrderBy(s => s.Distance)
                             .FirstOrDefault();

                test.ServerLatency = await _client.TestServerLatencyAsync(server);

                test.DownloadSpeed = await _client.TestDownloadSpeedAsync(server);

                test.UploadSpeed = await _client.TestUploadSpeedAsync(server);

                _logger.LogInformation($"Test ran successfully - Time: {timestamp}, Latency: {test.ServerLatency} ms, " +
                                       $"Download: {test.DownloadSpeed / 1000} Mbps, Upload: {test.UploadSpeed / 1000} Mbps");

                using var scope = _services.CreateScope();
                var context = scope.ServiceProvider.GetRequiredService <AppDbContext>();
                context.Speedtests.Add(test);
                await context.SaveChangesAsync();
            }
            finally
            {
                IsRunning = false;
                RunCompleted?.Invoke(test);

                _semaphore.Release();
            }

            return(test);
        }
예제 #3
0
 protected void OnRunStarted()
 {
     RunStarted?.Invoke(this, EventArgs.Empty);
 }
예제 #4
0
 protected void SetStatusToRunning()
 {
     RunStarted?.Invoke();
 }