}
func (c *Client) SetDistance(ctx context.Context, meters float64, startOfDay time.Time) error {
- return c.updateCumulative(ctx,
+ return c.updateIncremental(ctx,
&fitness.DataSource{
Application: Application(ctx),
DataType: &fitness.DataType{
}
func (c *Client) SetSteps(ctx context.Context, totalSteps int, startOfDay time.Time) error {
- return c.updateCumulative(ctx,
+ return c.updateIncremental(ctx,
&fitness.DataSource{
Application: Application(ctx),
DataType: &fitness.DataType{
}
func (c *Client) SetCalories(ctx context.Context, totalCalories float64, startOfDay time.Time) error {
- return c.updateCumulative(ctx,
+ return c.updateIncremental(ctx,
&fitness.DataSource{
Application: Application(ctx),
DataType: &fitness.DataType{
return c.DatasetPatch(ctx, dataStreamID, dataPoints)
}
-func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataSource, rawValue *fitness.Value, startOfDay time.Time) error {
+func (c *Client) updateIncremental(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 {
dataSource.DataStreamId = dataSourceID
endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
- currValue, startTime, err := c.readCumulative(ctx, dataSource, startOfDay, endOfDay)
+ storedValue, startTime, err := c.readIncremental(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 {
+ if storedValue.IntVal > rawValue.IntVal {
+ log.Warningf(ctx, "stored value (%d) is larger than new value (%d)", storedValue.IntVal, rawValue.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
+ if rawValue.IntVal == storedValue.IntVal {
+ return nil
}
+ diffValue.IntVal = rawValue.IntVal - storedValue.IntVal
} else { // if dataSource.DataType.Field[0].Format == "floatPoint"
- if rawValue.FpVal == currValue.FpVal {
+ if storedValue.FpVal > rawValue.FpVal {
+ log.Warningf(ctx, "stored value (%g) is larger than new value (%g)", storedValue.FpVal, rawValue.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
+ if rawValue.FpVal == storedValue.FpVal {
+ return nil
}
+ diffValue.FpVal = rawValue.FpVal - storedValue.FpVal
}
endTime := endOfDay
})
}
-func (c *Client) readCumulative(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (*fitness.Value, time.Time, error) {
+func (c *Client) readIncremental(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (*fitness.Value, time.Time, error) {
dataset, err := c.DatasetGet(ctx, dataSource.DataStreamId, startTime, endTime)
if err != nil {
return nil, time.Time{}, err