X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=gfit%2Fgfit.go;h=1599799e5689cf47a0adb562876b94ebdf5daa8f;hb=efc1f271711fbbbf06543c1d4883f377019f8c84;hp=fb668624e6b2e734b473333cb018cb685cae536d;hpb=a2b86ab96b046d7cccd96b066121ffa71e0eaf46;p=kraftakt.git diff --git a/gfit/gfit.go b/gfit/gfit.go index fb66862..1599799 100644 --- a/gfit/gfit.go +++ b/gfit/gfit.go @@ -7,8 +7,8 @@ import ( "strings" "time" - "github.com/octo/gfitsync/app" - "github.com/octo/gfitsync/fitbit" + "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" @@ -29,8 +29,8 @@ const ( ) 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://kraftakt.octo.it/google/grant", Scopes: []string{ @@ -42,7 +42,7 @@ var oauthConfig = &oauth2.Config{ func Application(ctx context.Context) *fitness.Application { return &fitness.Application{ - Name: "Fitbit to Google Fit sync", + Name: "Kraftakt", Version: appengine.VersionID(ctx), DetailsUrl: "", // optional } @@ -89,7 +89,7 @@ func DataStreamID(dataSource *fitness.DataSource) string { fields := []string{ dataSource.Type, dataSource.DataType.Name, - "@PROJECT_NUMBER@", // FIXME + app.Config.ProjectNumber, } if dev := dataSource.Device; dev != nil { @@ -120,7 +120,7 @@ func (c *Client) DataSourceCreate(ctx context.Context, dataSource *fitness.DataS } return DataStreamID(dataSource), nil } - log.Errorf(ctx, "c.Service.Users.DataSources.Create() = (%+v, %v)", res, err) + log.Errorf(ctx, "c.Service.Users.DataSources.Create(%q) = (%+v, %v)", dataSource, res, err) return "", err } return res.DataStreamId, nil @@ -230,12 +230,26 @@ func (a Activity) String() string { } func (c *Client) SetActivities(ctx context.Context, activities []Activity, startOfDay time.Time) error { - dataStreamID := DataStreamID(&fitness.DataSource{ + 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: dataTypeNameActivitySegment, }, Type: "raw", }) + if err != nil { + return err + } endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond) @@ -328,7 +342,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, "adding cumulative data point: %v-%v %+v", startTime, endTime, diffValue) + log.Debugf(ctx, "add cumulative data %s until %v: %+v", dataSource.DataStreamId, endTime, diffValue) return c.DataSetPatch(ctx, dataSource.DataStreamId, []*fitness.DataPoint{ &fitness.DataPoint{ @@ -350,6 +364,7 @@ func (c *Client) readCumulative(ctx context.Context, dataSource *fitness.DataSou } if len(res.Point) == 0 { + log.Debugf(ctx, "read cumulative data %s until %v: []", dataSource.DataStreamId, endTime) return &fitness.Value{}, startTime, nil }