X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=gfit%2Fgfit.go;h=1599799e5689cf47a0adb562876b94ebdf5daa8f;hb=efc1f271711fbbbf06543c1d4883f377019f8c84;hp=ed8f7cb840acdc7f7b23cb3a4a116984f65f884e;hpb=43223a02eecd68642404b09c3085f435722cb144;p=kraftakt.git diff --git a/gfit/gfit.go b/gfit/gfit.go index ed8f7cb..1599799 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -4,32 +4,45 @@ import ( "context" "fmt" "net/http" + "strings" "time" - "github.com/octo/gfitsync/app" + "github.com/octo/kraftakt/app" + "github.com/octo/kraftakt/fitbit" "golang.org/x/oauth2" oauth2google "golang.org/x/oauth2/google" fitness "google.golang.org/api/fitness/v1" + "google.golang.org/api/googleapi" "google.golang.org/appengine" "google.golang.org/appengine/log" ) +const ( + csrfToken = "@CSRFTOKEN@" + userID = "me" + + dataTypeNameCalories = "com.google.calories.expended" + dataTypeNameDistance = "com.google.distance.delta" + dataTypeNameSteps = "com.google.step_count.delta" + dataTypeNameHeartrate = "com.google.heart_rate.summary" + dataTypeNameActivitySegment = "com.google.activity.segment" +) + var oauthConfig = &oauth2.Config{ - ClientID: "@GOOGLE_CLIENT_ID@", - ClientSecret: "@GOOGLE_CLIENT_SECRET@", + ClientID: app.Config.GoogleClientID, + ClientSecret: app.Config.GoogleClientSecret, Endpoint: oauth2google.Endpoint, - RedirectURL: "https://fitbit-gfit-sync.appspot.com/google/grant", + RedirectURL: "https://kraftakt.octo.it/google/grant", Scopes: []string{ fitness.FitnessActivityWriteScope, fitness.FitnessBodyWriteScope, + fitness.FitnessLocationWriteScope, }, } -const csrfToken = "@CSRFTOKEN@" - func Application(ctx context.Context) *fitness.Application { return &fitness.Application{ - Name: "Fitbit to Google Fit sync", + Name: "Kraftakt", Version: appengine.VersionID(ctx), DetailsUrl: "", // optional } @@ -72,57 +85,469 @@ func NewClient(ctx context.Context, u *app.User) (*Client, error) { }, nil } -func (c *Client) SetSteps(ctx context.Context, steps int, date time.Time) error { - const userID = "me" - const dataTypeName = "com.google.step_count.delta" - dataSource := &fitness.DataSource{ - Application: Application(ctx), - DataStreamName: "", // "daily summary"? +func DataStreamID(dataSource *fitness.DataSource) string { + fields := []string{ + dataSource.Type, + dataSource.DataType.Name, + app.Config.ProjectNumber, + } + + if dev := dataSource.Device; dev != nil { + if dev.Manufacturer != "" { + fields = append(fields, dev.Manufacturer) + } + if dev.Model != "" { + fields = append(fields, dev.Model) + } + if dev.Uid != "" { + fields = append(fields, dev.Uid) + } + } + + if dataSource.DataStreamName != "" { + fields = append(fields, dataSource.DataStreamName) + } + + return strings.Join(fields, ":") +} + +func (c *Client) DataSourceCreate(ctx context.Context, dataSource *fitness.DataSource) (string, error) { + res, err := c.Service.Users.DataSources.Create(userID, dataSource).Context(ctx).Do() + if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusConflict { + if dataSource.DataStreamId != "" { + return dataSource.DataStreamId, nil + } + return DataStreamID(dataSource), nil + } + log.Errorf(ctx, "c.Service.Users.DataSources.Create(%q) = (%+v, %v)", dataSource, res, err) + return "", err + } + return res.DataStreamId, nil +} + +func (c *Client) DataSetPatch(ctx context.Context, dataSourceID string, points []*fitness.DataPoint) error { + startTimeNanos, endTimeNanos := int64(-1), int64(-1) + for _, p := range points { + if startTimeNanos == -1 || startTimeNanos > p.StartTimeNanos { + startTimeNanos = p.StartTimeNanos + } + if endTimeNanos == -1 || endTimeNanos < p.EndTimeNanos { + endTimeNanos = p.EndTimeNanos + } + } + datasetID := fmt.Sprintf("%d-%d", startTimeNanos, endTimeNanos) + + dataset := &fitness.Dataset{ + DataSourceId: dataSourceID, + MinStartTimeNs: startTimeNanos, + MaxEndTimeNs: endTimeNanos, + Point: points, + } + + _, err := c.Service.Users.DataSources.Datasets.Patch(userID, dataSourceID, datasetID, dataset).Context(ctx).Do() + if err != nil { + log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Patch() = %v", err) + return err + } + 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{ + 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) +} + +type Activity struct { + Start time.Time + End time.Time + Type int64 +} + +func (a Activity) String() string { + return fmt.Sprintf("%s-%s %d", a.Start.Format("15:04:05"), a.End.Format("15:04:05"), a.Type) +} + +func (c *Client) SetActivities(ctx context.Context, activities []Activity, startOfDay time.Time) error { + if len(activities) == 0 { + return nil + } + + dataStreamID, err := c.DataSourceCreate(ctx, &fitness.DataSource{ + Application: Application(ctx), DataType: &fitness.DataType{ Field: []*fitness.DataTypeField{ &fitness.DataTypeField{ + Name: "activity", Format: "integer", - Name: "steps", }, }, - Name: dataTypeName, + Name: dataTypeNameActivitySegment, }, - Name: "Step Count", Type: "raw", + }) + if err != nil { + return err } - dataSource, err := c.Service.Users.DataSources.Create(userID, dataSource).Context(ctx).Do() + endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond) + + datasetID := fmt.Sprintf("%d-%d", startOfDay.UnixNano(), endOfDay.UnixNano()) + res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataStreamID, datasetID).Context(ctx).Do() if err != nil { - log.Errorf(ctx, "c.Service.Users.DataSources.Create() = (%+v, %v)", dataSource, err) + log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataStreamID, datasetID, err) return err } - dataSourceID := dataSource.DataStreamId - startTimeNanos := date.UnixNano() - endTimeNanos := date.Add(86399999999999 * time.Nanosecond).UnixNano() - datasetID := fmt.Sprintf("%d-%d", startTimeNanos, endTimeNanos) - dataset := &fitness.Dataset{ - MinStartTimeNs: startTimeNanos, - MaxEndTimeNs: endTimeNanos, - Point: []*fitness.DataPoint{ - &fitness.DataPoint{ - ComputationTimeMillis: time.Now().UnixNano() / 1000000, - DataTypeName: dataTypeName, - StartTimeNanos: startTimeNanos, - EndTimeNanos: endTimeNanos, - Value: []*fitness.Value{ - &fitness.Value{ - IntVal: int64(steps), - }, + var dataPoints []*fitness.DataPoint +Next: + for _, a := range activities { + startTimeNanos := a.Start.UnixNano() + endTimeNanos := a.End.UnixNano() + + for _, p := range res.Point { + if p.StartTimeNanos == startTimeNanos && p.EndTimeNanos == endTimeNanos && p.Value[0].IntVal == a.Type { + log.Debugf(ctx, "activity %s already stored in Google Fit", a) + continue Next + } + } + + log.Debugf(ctx, "activity %s will be added to Google Fit", a) + dataPoints = append(dataPoints, &fitness.DataPoint{ + DataTypeName: dataTypeNameActivitySegment, + StartTimeNanos: startTimeNanos, + EndTimeNanos: endTimeNanos, + Value: []*fitness.Value{ + &fitness.Value{IntVal: a.Type}, + }, + }) + } + + if len(dataPoints) == 0 { + return nil + } + + return c.DataSetPatch(ctx, dataStreamID, dataPoints) +} + +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, "add cumulative data %s until %v: %+v", dataSource.DataStreamId, 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}, + }, + }) +} + +func (c *Client) readCumulative(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (*fitness.Value, time.Time, error) { + datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano()) + + res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSource.DataStreamId, datasetID).Context(ctx).Do() + if err != nil { + log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataSource.DataStreamId, datasetID, err) + return nil, time.Time{}, err + } + + if len(res.Point) == 0 { + log.Debugf(ctx, "read cumulative data %s until %v: []", dataSource.DataStreamId, endTime) + return &fitness.Value{}, startTime, nil + } + + var sum fitness.Value + maxEndTime := startTime + for _, p := range res.Point { + switch f := dataSource.DataType.Field[0].Format; f { + case "integer": + sum.IntVal += p.Value[0].IntVal + case "floatPoint": + sum.FpVal += p.Value[0].FpVal + default: + return nil, time.Time{}, fmt.Errorf("unexpected data type field format %q", f) + } + + pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location()) + if maxEndTime.Before(pointEndTime) { + maxEndTime = pointEndTime + } + } + + log.Debugf(ctx, "read cumulative data %s until %v: %+v", dataSource.DataStreamId, maxEndTime, sum) + return &sum, maxEndTime, nil +} + +type heartRateDuration struct { + Min int + Max int + Duration time.Duration +} + +type heartRateDurations []*heartRateDuration + +func (res heartRateDurations) find(min, max int) (*heartRateDuration, bool) { + for _, d := range res { + if d.Min != min || d.Max != max { + continue + } + return d, true + } + + return nil, false +} + +func (c *Client) heartRate(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (heartRateDurations, time.Time, error) { + datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano()) + + res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSource.DataStreamId, datasetID).Context(ctx).Do() + if err != nil { + log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataSource.DataStreamId, datasetID, err) + return nil, time.Time{}, err + } + + if len(res.Point) == 0 { + return nil, startTime, nil + } + + var results heartRateDurations + maxEndTime := startTime + for _, p := range res.Point { + max := int(p.Value[1].FpVal) + min := int(p.Value[2].FpVal) + duration := time.Unix(0, p.EndTimeNanos).Sub(time.Unix(0, p.StartTimeNanos)) + + if d, ok := results.find(min, max); ok { + d.Duration += duration + } else { + results = append(results, &heartRateDuration{ + Min: min, + Max: max, + Duration: duration, + }) + } + + pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location()) + if maxEndTime.Before(pointEndTime) { + maxEndTime = pointEndTime + } + } + + return results, maxEndTime, nil +} + +func (c *Client) SetHeartRate(ctx context.Context, totalDurations []fitbit.HeartRateZone, restingHeartRate int, startOfDay time.Time) error { + dataSource := &fitness.DataSource{ + Application: Application(ctx), + DataType: &fitness.DataType{ + Field: []*fitness.DataTypeField{ + &fitness.DataTypeField{ + Name: "average", + Format: "floatPoint", + }, + &fitness.DataTypeField{ + Name: "max", + Format: "floatPoint", + }, + &fitness.DataTypeField{ + Name: "min", + Format: "floatPoint", }, }, + Name: dataTypeNameHeartrate, }, + Name: "Heart rate summary", + Type: "raw", } - dataset, err = c.Service.Users.DataSources.Datasets.Patch(userID, dataSourceID, datasetID, dataset).Context(ctx).Do() + dataSourceID, err := c.DataSourceCreate(ctx, dataSource) if err != nil { - log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Patch() = (%+v, %v)", dataset, err) return err } - return nil + dataSource.DataStreamId = dataSourceID + + endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond) + prevDurations, startTime, err := c.heartRate(ctx, dataSource, startOfDay, endOfDay) + + // calculate the difference between the durations mentioned in + // totalDurations and prevDurations and store it in diffDurations. + var diffDurations heartRateDurations + for _, d := range totalDurations { + total := time.Duration(d.Minutes) * time.Minute + + var prev time.Duration + if res, ok := prevDurations.find(d.Min, d.Max); ok { + prev = res.Duration + } + + diff := total - prev + if diff < 0 { + diff = total + } + + if res, ok := diffDurations.find(d.Min, d.Max); ok { + res.Duration += diff + } else { + diffDurations = append(diffDurations, &heartRateDuration{ + Min: d.Min, + Max: d.Max, + Duration: diff, + }) + } + } + + // create a fitness.DataPoint for each non-zero duration difference. + var dataPoints []*fitness.DataPoint + for _, d := range diffDurations { + if d.Duration < time.Nanosecond { + continue + } + + endTime := startTime.Add(d.Duration) + if endTime.After(endOfDay) { + log.Warningf(ctx, "heart rate durations exceed one day (current end time: %v)", endTime) + break + } + + average := float64(d.Min+d.Max) / 2.0 + if d.Min <= restingHeartRate && restingHeartRate <= d.Max { + average = float64(restingHeartRate) + } + + dataPoints = append(dataPoints, &fitness.DataPoint{ + DataTypeName: dataSource.DataType.Name, + StartTimeNanos: startTime.UnixNano(), + EndTimeNanos: endTime.UnixNano(), + Value: []*fitness.Value{ + &fitness.Value{ + FpVal: average, + }, + &fitness.Value{ + FpVal: float64(d.Max), + }, + &fitness.Value{ + FpVal: float64(d.Min), + }, + }, + }) + + startTime = endTime + } + + if len(dataPoints) == 0 { + return nil + } + return c.DataSetPatch(ctx, dataSource.DataStreamId, dataPoints) }