Refactor writing of step values via the generic methods.
authorFlorian Forster <ff@octo.it>
Mon, 15 Jan 2018 09:02:19 +0000 (10:02 +0100)
committerFlorian Forster <ff@octo.it>
Mon, 15 Jan 2018 09:02:19 +0000 (10:02 +0100)
gfit/gfit.go

index ac4815e..7540bf4 100644 (file)
@@ -144,96 +144,26 @@ func (c *Client) DataSetPatch(ctx context.Context, dataSourceID string, points [
        return nil
 }
 
-func (c *Client) Steps(ctx context.Context, startTime, endTime time.Time) (int, time.Time, error) {
-       dataSourceID := DataStreamID(&fitness.DataSource{
-               Type: "raw",
-               DataType: &fitness.DataType{
-                       Name: dataTypeNameSteps,
-               },
-       })
-       datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano())
-
-       res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSourceID, datasetID).Context(ctx).Do()
-       if err != nil {
-               log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v",
-                       dataSourceID, datasetID, err)
-               return 0, time.Time{}, err
-       }
-
-       if len(res.Point) == 0 {
-               return 0, startTime, nil
-       }
-
-       steps := 0
-       maxEndTime := startTime
-       for _, p := range res.Point {
-               pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location())
-               value := p.Value[0].IntVal
-
-               steps += int(value)
-               if maxEndTime.Before(pointEndTime) {
-                       maxEndTime = pointEndTime
-               }
-       }
-
-       log.Debugf(ctx, "Google Fit has data points until %v: %d steps", maxEndTime, steps)
-       return steps, maxEndTime, nil
-}
-
 func (c *Client) SetSteps(ctx context.Context, totalSteps int, startOfDay time.Time) error {
-       if totalSteps == 0 {
-               return nil
-       }
-
-       dataSourceID, err := c.DataSourceCreate(ctx, &fitness.DataSource{
-               Application:    Application(ctx),
-               DataStreamName: "", // "daily summary"?
-               DataType: &fitness.DataType{
-                       Field: []*fitness.DataTypeField{
-                               &fitness.DataTypeField{
-                                       Format: "integer",
-                                       Name:   "steps",
+       return c.updateCumulative(ctx,
+               &fitness.DataSource{
+                       Application: Application(ctx),
+                       DataType: &fitness.DataType{
+                               Field: []*fitness.DataTypeField{
+                                       &fitness.DataTypeField{
+                                               Name:   "steps",
+                                               Format: "integer",
+                                       },
                                },
+                               Name: dataTypeNameSteps,
                        },
-                       Name: dataTypeNameSteps,
+                       Name: "Step Count",
+                       Type: "raw",
                },
-               Name: "Step Count",
-               Type: "raw",
-               // Type: "derived",
-       })
-       if err != nil {
-               return err
-       }
-
-       endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
-       prevSteps, startTime, err := c.Steps(ctx, startOfDay, endOfDay)
-       if totalSteps == prevSteps {
-               return nil
-       }
-       diffSteps := totalSteps - prevSteps
-       if diffSteps < 0 {
-               log.Warningf(ctx, "c.Steps returned %d steps, but current count is %d", prevSteps, totalSteps)
-               diffSteps = totalSteps
-       }
-       endTime := endOfDay
-       if now := time.Now().In(startOfDay.Location()); now.Before(endOfDay) {
-               endTime = now
-       }
-       log.Debugf(ctx, "new data point: %v-%v %d steps", startTime, endTime, diffSteps)
-
-       return c.DataSetPatch(ctx, dataSourceID, []*fitness.DataPoint{
-               &fitness.DataPoint{
-                       ComputationTimeMillis: time.Now().UnixNano() / 1000000,
-                       DataTypeName:          dataTypeNameSteps,
-                       StartTimeNanos:        startTime.UnixNano(),
-                       EndTimeNanos:          endTime.UnixNano(),
-                       Value: []*fitness.Value{
-                               &fitness.Value{
-                                       IntVal: int64(diffSteps),
-                               },
-                       },
+               &fitness.Value{
+                       IntVal: int64(totalSteps),
                },
-       })
+               startOfDay)
 }
 
 func (c *Client) SetCalories(ctx context.Context, totalCalories float64, startOfDay time.Time) error {
@@ -306,7 +236,7 @@ func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataS
        if now := time.Now().In(startOfDay.Location()); now.Before(endOfDay) {
                endTime = now
        }
-       log.Debugf(ctx, "new cumulative data point: [%v--%v] %+v", startTime, endTime, diffValue)
+       log.Debugf(ctx, "adding cumulative data point: %v-%v %+v", startTime, endTime, diffValue)
 
        return c.DataSetPatch(ctx, dataSource.DataStreamId, []*fitness.DataPoint{
                &fitness.DataPoint{
@@ -349,6 +279,6 @@ func (c *Client) readCumulative(ctx context.Context, dataSource *fitness.DataSou
                }
        }
 
-       log.Debugf(ctx, "Google Fit has data points for %s until %v: %+v", dataSource.DataStreamId, maxEndTime, sum)
+       log.Debugf(ctx, "read cumulative data %s until %v: %+v", dataSource.DataStreamId, maxEndTime, sum)
        return &sum, maxEndTime, nil
 }