checkpoint 3

This commit is contained in:
2025-08-25 11:53:08 -07:00
parent 8f0325f766
commit 99e69be74a

View File

@@ -5,7 +5,8 @@ import (
"math" "math"
"time" "time"
"github.com/sstent/garminsync-go/internal/parser" "github.com/sstent/garminsync-go/internal/models"
"os"
) )
// GPX represents the root element of a GPX file // GPX represents the root element of a GPX file
@@ -36,7 +37,13 @@ type TrkPt struct {
// GPXParser implements the Parser interface for GPX files // GPXParser implements the Parser interface for GPX files
type GPXParser struct{} type GPXParser struct{}
func (p *GPXParser) Parse(data []byte) (*activity.Activity, error) { func (p *GPXParser) ParseFile(filename string) (*models.ActivityMetrics, error) {
// Read the file content
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var gpx GPX var gpx GPX
if err := xml.Unmarshal(data, &gpx); err != nil { if err := xml.Unmarshal(data, &gpx); err != nil {
return nil, err return nil, err
@@ -51,14 +58,13 @@ func (p *GPXParser) Parse(data []byte) (*activity.Activity, error) {
startTime, _ := time.Parse(time.RFC3339, points[0].Time) startTime, _ := time.Parse(time.RFC3339, points[0].Time)
endTime, _ := time.Parse(time.RFC3339, points[len(points)-1].Time) endTime, _ := time.Parse(time.RFC3339, points[len(points)-1].Time)
activity := &activity.Activity{ metrics := &models.ActivityMetrics{
ActivityType: "hiking", ActivityType: "hiking",
StartTime: startTime, StartTime: startTime,
Duration: int(endTime.Sub(startTime).Seconds()), Duration: time.Duration(endTime.Sub(startTime).Seconds()) * time.Second,
StartLatitude: points[0].Lat,
StartLongitude: points[0].Lon,
} }
// Calculate distance and elevation // Calculate distance and elevation
var totalDistance, elevationGain float64 var totalDistance, elevationGain float64
prev := points[0] prev := points[0]
@@ -73,12 +79,13 @@ func (p *GPXParser) Parse(data []byte) (*activity.Activity, error) {
prev = curr prev = curr
} }
activity.Distance = totalDistance metrics.Distance = totalDistance
activity.ElevationGain = elevationGain metrics.ElevationGain = elevationGain
return activity, nil return metrics, nil
} }
// haversine calculates the distance between two points on Earth // haversine calculates the distance between two points on Earth
func haversine(lat1, lon1, lat2, lon2 float64) float64 { func haversine(lat1, lon1, lat2, lon2 float64) float64 {
const R = 6371000 // Earth radius in meters const R = 6371000 // Earth radius in meters