public static void AddRecord(FitRecord record, DateTime?time = null) { Trace.TraceInformation("AddRecord()"); if (isPaused) { return; } var now = time ?? DateTime.UtcNow; if (now - lastRecordTime < TimeSpan.FromSeconds(1)) { return; // do not record twice with same timestamp } totalDistance += record.Speed / 3.6f * (float)(now - lastEventTime).TotalSeconds; csvFile.WriteLine($"{(int)(now - startTime).TotalSeconds,7},{record}," + totalDistance.ToString("0.0", CultureInfo.InvariantCulture)); totalTimerTime += (now - lastEventTime); try { var newRecord = new RecordMesg(); var hr = record.Heartrate > 0 ? (byte?)record.Heartrate : null; var cad = record.Cadence > 0 ? (byte?)record.Cadence : null; newRecord.SetTimestamp(new Dynastream.Fit.DateTime(now)); newRecord.SetHeartRate(hr); newRecord.SetCadence(cad); newRecord.SetPower((ushort)record.Power); // newRecord.SetGrade(State.BikeIncline); newRecord.SetDistance(totalDistance); newRecord.SetSpeed(record.Speed / 3.6f); newRecord.SetEnhancedSpeed(record.Speed / 3.6f); if (record.Lat != 0f && record.Lon != 0f) { var semiLat = record.Lat * Math.Pow(2, 31) / 180.0; // convert degrees to semicircles var semiLon = record.Lon * Math.Pow(2, 31) / 180.0; newRecord.SetPositionLong((int)semiLon); newRecord.SetPositionLat((int)semiLat); } newRecord.SetAltitude(record.Alt); newRecord.SetEnhancedAltitude(record.Alt); encoder.Write(newRecord); lastRecordTime = now; lastEventTime = now; } catch (Exception e) { Trace.TraceError("Error while writing FIT record: " + e.Message); } }
async Task ConsumeClientDataAsync() { await Task.Run(() => { while (Thread.CurrentThread.IsAlive) { var message = InputMessages.Take(); Trace.TraceInformation("Processing message : " + message); var command = message.Split(':')[0]; var value = message.Split(':')[1]; switch (command) { case "SLOPE": _ = SendSlopeAsync(value); break; case "TARGET_POWER": _ = SendTargetPowerAsync(value); break; case "START_RECORDING": _ = FitWriter.StartAsync(); break; case "WRITE_RECORD": _ = FitWriter.AddRecordAsync(FitRecord.FromString(value)); break; case "STOP_RECORDING": _ = FitWriter.StopAsync(); break; case "PAUSE_RECORDING": _ = FitWriter.PauseAsync(); break; case "RESUME_RECORDING": _ = FitWriter.ResumeAsync(); break; default: break; } } }); }
static void FITFromCSV() { var started = false; DateTime segmentStart = DateTime.Now; int timeOffset = 0; foreach (var f in Directory.GetFiles(@"C:\Users\Damien\Documents\BeamNG.drive\VeloActivities\test")) { var filename = f.Split('\\').Last().Split('.').First(); var fields = filename.Split('_'); var year = int.Parse(fields[0]); var month = int.Parse(fields[1]); var day = int.Parse(fields[2]); var hour = int.Parse(fields[3]); var min = int.Parse(fields[4]); var sec = int.Parse(fields[5]); segmentStart = new DateTime(year, month, day, hour, min, sec); if (!started) { FitWriter.Start(segmentStart); started = true; } using (var s = new StreamReader(f)) { while (!s.EndOfStream) { var ligne = s.ReadLine(); timeOffset = int.Parse(ligne.Split(',')[0]); string fitString = string.Join(",", ligne.Split(',').Skip(1).ToArray()); FitRecord record = FitRecord.FromString(fitString); FitWriter.AddRecord(record, segmentStart + TimeSpan.FromSeconds(timeOffset)); } } } FitWriter.Stop(segmentStart + TimeSpan.FromSeconds(timeOffset)); }
public static async Task AddRecordAsync(FitRecord record, DateTime?time = null) { await Task.Run(() => { lock (lockObj) AddRecord(record, time); }); }