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"
+ 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{
startOfDay)
}
+type Activity struct {
+ Start time.Time
+ End time.Time
+ Type int64
+}
+
+func (c *Client) SetActivities(ctx context.Context, activities []Activity, startOfDay time.Time) error {
+ dataStreamID := DataStreamID(&fitness.DataSource{
+ DataType: &fitness.DataType{
+ Name: dataTypeNameActivitySegment,
+ },
+ Type: "raw",
+ })
+
+ 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.Datasets.Get(%q, %q) = %v", dataStreamID, datasetID, err)
+ return err
+ }
+
+ 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 {
+ continue Next
+ }
+ }
+
+ 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":