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); } }
public async Task UpdateRunStatus(Race race, int runNumber, Skier skier, RunStatus status) { var run = await DaoProvider.RunDao.GetBySkierAndRace(race, runNumber, skier); run.Status = status; await DaoProvider.RunDao.Update(run); RunStatusChanged?.Invoke(race, runNumber, skier, status); }