+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{
+ Application: Application(ctx),
+ DataType: &fitness.DataType{
+ Field: []*fitness.DataTypeField{
+ &fitness.DataTypeField{
+ Name: "steps",
+ Format: "integer",
+ },
+ },
+ Name: dataTypeNameSteps,
+ },
+ Name: "Step Count",
+ Type: "raw",
+ },
+ &fitness.Value{
+ IntVal: int64(totalSteps),
+ },
+ startOfDay)
+}
+
+func (c *Client) SetCalories(ctx context.Context, totalCalories float64, startOfDay time.Time) error {
+ return c.updateCumulative(ctx,
+ &fitness.DataSource{
+ Application: Application(ctx),
+ DataType: &fitness.DataType{
+ Field: []*fitness.DataTypeField{
+ &fitness.DataTypeField{
+ Name: "calories",
+ Format: "floatPoint",
+ },
+ },
+ Name: dataTypeNameCalories,
+ },
+ Name: "Calories expended",
+ Type: "raw",
+ },
+ &fitness.Value{
+ FpVal: totalCalories,
+ },
+ startOfDay)
+}
+
+func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataSource, rawValue *fitness.Value, startOfDay time.Time) error {
+ switch f := dataSource.DataType.Field[0].Format; f {
+ case "integer":
+ if rawValue.IntVal == 0 {
+ return nil
+ }
+ case "floatPoint":
+ if rawValue.FpVal == 0 {
+ return nil
+ }
+ default:
+ return fmt.Errorf("unexpected data type field format %q", f)
+ }
+
+ dataSourceID, err := c.DataSourceCreate(ctx, dataSource)
+ if err != nil {
+ return err
+ }
+ dataSource.DataStreamId = dataSourceID
+
+ endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
+ currValue, startTime, err := c.readCumulative(ctx, dataSource, startOfDay, endOfDay)
+ if err != nil {
+ return err
+ }
+
+ var diffValue fitness.Value
+ if dataSource.DataType.Field[0].Format == "integer" {
+ if rawValue.IntVal == currValue.IntVal {
+ return nil
+ }
+ diffValue.IntVal = rawValue.IntVal - currValue.IntVal
+ if diffValue.IntVal < 0 {
+ log.Warningf(ctx, "stored value (%d) is larger than new value (%d); assuming count was reset", currValue.IntVal, rawValue.IntVal)
+ diffValue.IntVal = rawValue.IntVal
+ }
+ } else { // if dataSource.DataType.Field[0].Format == "floatPoint"
+ if rawValue.FpVal == currValue.FpVal {
+ return nil
+ }
+ diffValue.FpVal = rawValue.FpVal - currValue.FpVal
+ if diffValue.FpVal < 0 {
+ log.Warningf(ctx, "stored value (%g) is larger than new value (%g); assuming count was reset", currValue.FpVal, rawValue.FpVal)
+ diffValue.FpVal = rawValue.FpVal
+ }
+ }
+
+ endTime := endOfDay
+ if now := time.Now().In(startOfDay.Location()); now.Before(endOfDay) {
+ endTime = now
+ }
+ log.Debugf(ctx, "adding cumulative data point: %v-%v %+v", startTime, endTime, diffValue)
+
+ return c.DataSetPatch(ctx, dataSource.DataStreamId, []*fitness.DataPoint{
+ &fitness.DataPoint{
+ DataTypeName: dataSource.DataType.Name,
+ StartTimeNanos: startTime.UnixNano(),
+ EndTimeNanos: endTime.UnixNano(),
+ Value: []*fitness.Value{&diffValue},