protected void Convert(string format) { if (!_fileHandler.DirExists(_config.App.DownloadDirectory)) { Log.Information("No download directory found. Nothing to do. {@File}", _config.App.DownloadDirectory); return; } var files = _fileHandler.GetFiles(_config.App.DownloadDirectory); if (files.Length == 0) { Log.Information("No files to convert in download directory. Nothing to do."); return; } if (_config.Garmin.Upload) { _fileHandler.MkDirIfNotExists(_config.App.UploadDirectory); } // Foreach file in directory foreach (var file in files) { using var workoutTimer = WorkoutsConverted.WithLabels(format).NewTimer(); // load file and deserialize P2GWorkout workoutData = null; try { workoutData = _fileHandler.DeserializeJson <P2GWorkout>(file); } catch (Exception e) { Log.Error(e, "Failed to load and parse workout data {@File}", file); _fileHandler.MoveFailedFile(file, _config.App.FailedDirectory); continue; } using var tracing = Tracing.Trace("Convert") .WithWorkoutId(workoutData.Workout.Id) .WithTag(TagKey.Format, format); // call internal convert method T converted = default; var workoutTitle = WorkoutHelper.GetUniqueTitle(workoutData.Workout); try { converted = Convert(workoutData.Workout, workoutData.WorkoutSamples); } catch (Exception e) { Log.Error(e, "Failed to convert workout data {@Workout} {@File}", workoutTitle, file); } if (converted is null) { _fileHandler.MoveFailedFile(file, _config.App.FailedDirectory); continue; } // write to output dir var path = Path.Join(_config.App.WorkingDirectory, $"{workoutTitle}.{format}"); try { Save(converted, path); } catch (Exception e) { Log.Error(e, "Failed to write {@Format} file for {@Workout}", format, workoutTitle); continue; } // copy to local save if (_config.Format.SaveLocalCopy) { try { _fileHandler.MkDirIfNotExists(_config.App.TcxDirectory); _fileHandler.MkDirIfNotExists(_config.App.FitDirectory); var dir = format == "fit" ? _config.App.FitDirectory : _config.App.TcxDirectory; var backupDest = Path.Join(dir, $"{workoutTitle}.{format}"); _fileHandler.Copy(path, backupDest, overwrite: true); Log.Information("Backed up file {@File}", backupDest); } catch (Exception e) { Log.Error(e, "Failed to backup {@Format} file for {@Workout}", format, workoutTitle); continue; } } // copy to upload dir if (_config.Garmin.Upload && _config.Garmin.FormatToUpload == format) { try { var uploadDest = Path.Join(_config.App.UploadDirectory, $"{workoutTitle}.{format}"); _fileHandler.Copy(path, uploadDest, overwrite: true); Log.Debug("Prepped {@Format} file {@Path} for upload.", format, uploadDest); } catch (Exception e) { Log.Error(e, "Failed to copy {@Format} file for {@Workout}", format, workoutTitle); continue; } } // update db item with conversion date SyncHistoryItem syncRecord = _dbClient.Get(workoutData.Workout.Id); if (syncRecord is null) { syncRecord = new SyncHistoryItem(workoutData.Workout) { DownloadDate = System.DateTime.Now, }; } syncRecord.ConvertedToFit = syncRecord.ConvertedToFit || format == "fit"; syncRecord.ConvertedToTcx = syncRecord.ConvertedToTcx || format == "tcx"; _dbClient.Upsert(syncRecord); } }
protected ConvertStatus Convert(FileFormat format, P2GWorkout workoutData) { using var tracing = Tracing.Trace($"{nameof(IConverter)}.{nameof(Convert)}.Workout")? .WithWorkoutId(workoutData.Workout.Id) .WithTag(TagKey.Format, format.ToString()); var status = new ConvertStatus(); // call internal convert method T converted = default; var workoutTitle = WorkoutHelper.GetUniqueTitle(workoutData.Workout); try { converted = Convert(workoutData.Workout, workoutData.WorkoutSamples); } catch (Exception e) { _logger.Error(e, "Failed to convert workout data to format {@Format} {@Workout}", format, workoutTitle); status.Success = false; status.ErrorMessage = "Failed to convert workout data."; tracing?.AddTag("excetpion.message", e.Message); tracing?.AddTag("exception.stacktrace", e.StackTrace); tracing?.AddTag("convert.success", false); tracing?.AddTag("convert.errormessage", status.ErrorMessage); return(status); } // write to output dir var path = Path.Join(_config.App.WorkingDirectory, $"{workoutTitle}.{format}"); try { _fileHandler.MkDirIfNotExists(_config.App.WorkingDirectory); Save(converted, path); status.Success = true; } catch (Exception e) { status.Success = false; status.ErrorMessage = "Failed to save converted workout for upload."; _logger.Error(e, "Failed to write {@Format} file for {@Workout}", format, workoutTitle); tracing?.AddTag("excetpion.message", e.Message); tracing?.AddTag("exception.stacktrace", e.StackTrace); tracing?.AddTag("convert.success", false); tracing?.AddTag("convert.errormessage", status.ErrorMessage); return(status); } // copy to local save try { SaveLocalCopy(path, workoutTitle); } catch (Exception e) { _logger.Error(e, "Failed to backup {@Format} file for {@Workout}", format, workoutTitle); } // copy to upload dir if (_config.Garmin.Upload && _config.Garmin.FormatToUpload == format) { try { var uploadDest = Path.Join(_config.App.UploadDirectory, $"{workoutTitle}.{format}"); _fileHandler.MkDirIfNotExists(_config.App.UploadDirectory); _fileHandler.Copy(path, uploadDest, overwrite: true); _logger.Debug("Prepped {@Format} for upload: {@Path}", format, uploadDest); } catch (Exception e) { _logger.Error(e, "Failed to copy {@Format} file for {@Workout}", format, workoutTitle); status.Success = false; status.ErrorMessage = $"Failed to save file for {@format} and workout {workoutTitle} to Upload directory"; tracing?.AddTag("excetpion.message", e.Message); tracing?.AddTag("exception.stacktrace", e.StackTrace); tracing?.AddTag("convert.success", false); tracing?.AddTag("convert.errormessage", status.ErrorMessage); return(status); } } return(status); }