From: Florian Forster Date: Thu, 1 Feb 2018 09:06:32 +0000 (+0100) Subject: Package gfit: Change handling of non-incremental data points. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=024f18e56decd3608f0412eb3f613c99140d77b1;p=kraftakt.git Package gfit: Change handling of non-incremental data points. --- diff --git a/gfit/gfit.go b/gfit/gfit.go index ec88b19..4993725 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -204,7 +204,7 @@ func (c *Client) DatasetPatch(ctx context.Context, dataSourceID string, points [ } 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{ @@ -226,7 +226,7 @@ func (c *Client) SetDistance(ctx context.Context, meters float64, startOfDay tim } 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{ @@ -248,7 +248,7 @@ func (c *Client) SetSteps(ctx context.Context, totalSteps int, startOfDay time.T } 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{ @@ -339,7 +339,7 @@ Next: 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 { @@ -360,30 +360,30 @@ func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataS 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 @@ -402,7 +402,7 @@ func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataS }) } -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