Implement distance conversion.
authorFlorian Forster <ff@octo.it>
Tue, 16 Jan 2018 21:37:42 +0000 (22:37 +0100)
committerFlorian Forster <ff@octo.it>
Tue, 16 Jan 2018 21:42:35 +0000 (22:42 +0100)
gfit/gfit.go
gfitsync.go

index ea41b95..37d30b4 100644 (file)
@@ -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{
index c5b4b62..28c0685 100644 (file)
@@ -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)
        }