From: Florian Forster Date: Tue, 16 Jan 2018 21:37:42 +0000 (+0100) Subject: Implement distance conversion. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=059b0aebeffa117c539a98678950c10c0f69a73d;p=kraftakt.git Implement distance conversion. --- diff --git a/gfit/gfit.go b/gfit/gfit.go index ea41b95..37d30b4 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -22,6 +22,7 @@ const ( userID = "me" dataTypeNameCalories = "com.google.calories.expended" + dataTypeNameDistance = "com.google.distance.delta" dataTypeNameSteps = "com.google.step_count.delta" dataTypeNameHeartrate = "com.google.heart_rate.summary" ) @@ -34,6 +35,7 @@ var oauthConfig = &oauth2.Config{ Scopes: []string{ fitness.FitnessActivityWriteScope, fitness.FitnessBodyWriteScope, + fitness.FitnessLocationWriteScope, }, } @@ -146,6 +148,28 @@ func (c *Client) DataSetPatch(ctx context.Context, dataSourceID string, points [ return nil } +func (c *Client) SetDistance(ctx context.Context, meters float64, startOfDay time.Time) error { + return c.updateCumulative(ctx, + &fitness.DataSource{ + Application: Application(ctx), + DataType: &fitness.DataType{ + Field: []*fitness.DataTypeField{ + &fitness.DataTypeField{ + Name: "distance", + Format: "floatPoint", + }, + }, + Name: dataTypeNameDistance, + }, + Name: "Distance covered", + Type: "raw", + }, + &fitness.Value{ + FpVal: meters, + }, + startOfDay) +} + func (c *Client) SetSteps(ctx context.Context, totalSteps int, startOfDay time.Time) error { return c.updateCumulative(ctx, &fitness.DataSource{ diff --git a/gfitsync.go b/gfitsync.go index c5b4b62..28c0685 100644 --- a/gfitsync.go +++ b/gfitsync.go @@ -259,6 +259,18 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error { return fmt.Errorf("gfitClient.SetCalories(%d) = %v", summary.Summary.CaloriesOut, err) } + var distanceMeters float64 + for _, d := range summary.Summary.Distances { + if d.Activity != "total" { + continue + } + distanceMeters = 1000.0 * d.Distance + break + } + if err := gfitClient.SetDistance(ctx, distanceMeters, tm); err != nil { + return fmt.Errorf("gfitClient.SetDistance(%d) = %v", distanceMeters, err) + } + if err := gfitClient.SetHeartRate(ctx, summary.Summary.HeartRateZones, summary.Summary.RestingHeartRate, tm); err != nil { return fmt.Errorf("gfitClient.SetHeartRate() = %v", err) }