657da7df5c70b487c0c267a9b8dac5e0e733e28a
[kraftakt.git] / gfit / gfit.go
1 package gfit
2
3 import (
4         "context"
5         "fmt"
6         "net/http"
7         "strings"
8         "time"
9
10         "github.com/octo/gfitsync/app"
11         "github.com/octo/gfitsync/fitbit"
12         "golang.org/x/oauth2"
13         oauth2google "golang.org/x/oauth2/google"
14         fitness "google.golang.org/api/fitness/v1"
15         "google.golang.org/api/googleapi"
16         "google.golang.org/appengine"
17         "google.golang.org/appengine/log"
18 )
19
20 const (
21         csrfToken = "@CSRFTOKEN@"
22         userID    = "me"
23
24         dataTypeNameCalories        = "com.google.calories.expended"
25         dataTypeNameDistance        = "com.google.distance.delta"
26         dataTypeNameSteps           = "com.google.step_count.delta"
27         dataTypeNameHeartrate       = "com.google.heart_rate.summary"
28         dataTypeNameActivitySegment = "com.google.activity.segment"
29 )
30
31 var oauthConfig = &oauth2.Config{
32         ClientID:     "@GOOGLE_CLIENT_ID@",
33         ClientSecret: "@GOOGLE_CLIENT_SECRET@",
34         Endpoint:     oauth2google.Endpoint,
35         RedirectURL:  "https://kraftakt.octo.it/google/grant",
36         Scopes: []string{
37                 fitness.FitnessActivityWriteScope,
38                 fitness.FitnessBodyWriteScope,
39                 fitness.FitnessLocationWriteScope,
40         },
41 }
42
43 func Application(ctx context.Context) *fitness.Application {
44         return &fitness.Application{
45                 Name:       "Fitbit to Google Fit sync",
46                 Version:    appengine.VersionID(ctx),
47                 DetailsUrl: "", // optional
48         }
49 }
50
51 func AuthURL() string {
52         return oauthConfig.AuthCodeURL(csrfToken, oauth2.AccessTypeOffline)
53 }
54
55 func ParseToken(ctx context.Context, r *http.Request, u *app.User) error {
56         if state := r.FormValue("state"); state != csrfToken {
57                 return fmt.Errorf("invalid state parameter: %q", state)
58         }
59
60         tok, err := oauthConfig.Exchange(ctx, r.FormValue("code"))
61         if err != nil {
62                 return err
63         }
64
65         return u.SetToken(ctx, "Google", tok)
66 }
67
68 type Client struct {
69         *fitness.Service
70 }
71
72 func NewClient(ctx context.Context, u *app.User) (*Client, error) {
73         c, err := u.OAuthClient(ctx, "Google", oauthConfig)
74         if err != nil {
75                 return nil, err
76         }
77
78         service, err := fitness.New(c)
79         if err != nil {
80                 return nil, err
81         }
82
83         return &Client{
84                 Service: service,
85         }, nil
86 }
87
88 func DataStreamID(dataSource *fitness.DataSource) string {
89         fields := []string{
90                 dataSource.Type,
91                 dataSource.DataType.Name,
92                 "@PROJECT_NUMBER@", // FIXME
93         }
94
95         if dev := dataSource.Device; dev != nil {
96                 if dev.Manufacturer != "" {
97                         fields = append(fields, dev.Manufacturer)
98                 }
99                 if dev.Model != "" {
100                         fields = append(fields, dev.Model)
101                 }
102                 if dev.Uid != "" {
103                         fields = append(fields, dev.Uid)
104                 }
105         }
106
107         if dataSource.DataStreamName != "" {
108                 fields = append(fields, dataSource.DataStreamName)
109         }
110
111         return strings.Join(fields, ":")
112 }
113
114 func (c *Client) DataSourceCreate(ctx context.Context, dataSource *fitness.DataSource) (string, error) {
115         res, err := c.Service.Users.DataSources.Create(userID, dataSource).Context(ctx).Do()
116         if err != nil {
117                 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusConflict {
118                         if dataSource.DataStreamId != "" {
119                                 return dataSource.DataStreamId, nil
120                         }
121                         return DataStreamID(dataSource), nil
122                 }
123                 log.Errorf(ctx, "c.Service.Users.DataSources.Create(%q) = (%+v, %v)", dataSource, res, err)
124                 return "", err
125         }
126         return res.DataStreamId, nil
127 }
128
129 func (c *Client) DataSetPatch(ctx context.Context, dataSourceID string, points []*fitness.DataPoint) error {
130         startTimeNanos, endTimeNanos := int64(-1), int64(-1)
131         for _, p := range points {
132                 if startTimeNanos == -1 || startTimeNanos > p.StartTimeNanos {
133                         startTimeNanos = p.StartTimeNanos
134                 }
135                 if endTimeNanos == -1 || endTimeNanos < p.EndTimeNanos {
136                         endTimeNanos = p.EndTimeNanos
137                 }
138         }
139         datasetID := fmt.Sprintf("%d-%d", startTimeNanos, endTimeNanos)
140
141         dataset := &fitness.Dataset{
142                 DataSourceId:   dataSourceID,
143                 MinStartTimeNs: startTimeNanos,
144                 MaxEndTimeNs:   endTimeNanos,
145                 Point:          points,
146         }
147
148         _, err := c.Service.Users.DataSources.Datasets.Patch(userID, dataSourceID, datasetID, dataset).Context(ctx).Do()
149         if err != nil {
150                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Patch() = %v", err)
151                 return err
152         }
153         return nil
154 }
155
156 func (c *Client) SetDistance(ctx context.Context, meters float64, startOfDay time.Time) error {
157         return c.updateCumulative(ctx,
158                 &fitness.DataSource{
159                         Application: Application(ctx),
160                         DataType: &fitness.DataType{
161                                 Field: []*fitness.DataTypeField{
162                                         &fitness.DataTypeField{
163                                                 Name:   "distance",
164                                                 Format: "floatPoint",
165                                         },
166                                 },
167                                 Name: dataTypeNameDistance,
168                         },
169                         Name: "Distance covered",
170                         Type: "raw",
171                 },
172                 &fitness.Value{
173                         FpVal: meters,
174                 },
175                 startOfDay)
176 }
177
178 func (c *Client) SetSteps(ctx context.Context, totalSteps int, startOfDay time.Time) error {
179         return c.updateCumulative(ctx,
180                 &fitness.DataSource{
181                         Application: Application(ctx),
182                         DataType: &fitness.DataType{
183                                 Field: []*fitness.DataTypeField{
184                                         &fitness.DataTypeField{
185                                                 Name:   "steps",
186                                                 Format: "integer",
187                                         },
188                                 },
189                                 Name: dataTypeNameSteps,
190                         },
191                         Name: "Step Count",
192                         Type: "raw",
193                 },
194                 &fitness.Value{
195                         IntVal: int64(totalSteps),
196                 },
197                 startOfDay)
198 }
199
200 func (c *Client) SetCalories(ctx context.Context, totalCalories float64, startOfDay time.Time) error {
201         return c.updateCumulative(ctx,
202                 &fitness.DataSource{
203                         Application: Application(ctx),
204                         DataType: &fitness.DataType{
205                                 Field: []*fitness.DataTypeField{
206                                         &fitness.DataTypeField{
207                                                 Name:   "calories",
208                                                 Format: "floatPoint",
209                                         },
210                                 },
211                                 Name: dataTypeNameCalories,
212                         },
213                         Name: "Calories expended",
214                         Type: "raw",
215                 },
216                 &fitness.Value{
217                         FpVal: totalCalories,
218                 },
219                 startOfDay)
220 }
221
222 type Activity struct {
223         Start time.Time
224         End   time.Time
225         Type  int64
226 }
227
228 func (a Activity) String() string {
229         return fmt.Sprintf("%s-%s %d", a.Start.Format("15:04:05"), a.End.Format("15:04:05"), a.Type)
230 }
231
232 func (c *Client) SetActivities(ctx context.Context, activities []Activity, startOfDay time.Time) error {
233         if len(activities) == 0 {
234                 return nil
235         }
236
237         dataStreamID, err := c.DataSourceCreate(ctx, &fitness.DataSource{
238                 Application: Application(ctx),
239                 DataType: &fitness.DataType{
240                         Field: []*fitness.DataTypeField{
241                                 &fitness.DataTypeField{
242                                         Name:   "activity",
243                                         Format: "integer",
244                                 },
245                         },
246                         Name: dataTypeNameActivitySegment,
247                 },
248                 Type: "raw",
249         })
250         if err != nil {
251                 return err
252         }
253
254         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
255
256         datasetID := fmt.Sprintf("%d-%d", startOfDay.UnixNano(), endOfDay.UnixNano())
257         res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataStreamID, datasetID).Context(ctx).Do()
258         if err != nil {
259                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataStreamID, datasetID, err)
260                 return err
261         }
262
263         var dataPoints []*fitness.DataPoint
264 Next:
265         for _, a := range activities {
266                 startTimeNanos := a.Start.UnixNano()
267                 endTimeNanos := a.End.UnixNano()
268
269                 for _, p := range res.Point {
270                         if p.StartTimeNanos == startTimeNanos && p.EndTimeNanos == endTimeNanos && p.Value[0].IntVal == a.Type {
271                                 log.Debugf(ctx, "activity %s already stored in Google Fit", a)
272                                 continue Next
273                         }
274                 }
275
276                 log.Debugf(ctx, "activity %s will be added to Google Fit", a)
277                 dataPoints = append(dataPoints, &fitness.DataPoint{
278                         DataTypeName:   dataTypeNameActivitySegment,
279                         StartTimeNanos: startTimeNanos,
280                         EndTimeNanos:   endTimeNanos,
281                         Value: []*fitness.Value{
282                                 &fitness.Value{IntVal: a.Type},
283                         },
284                 })
285         }
286
287         if len(dataPoints) == 0 {
288                 return nil
289         }
290
291         return c.DataSetPatch(ctx, dataStreamID, dataPoints)
292 }
293
294 func (c *Client) updateCumulative(ctx context.Context, dataSource *fitness.DataSource, rawValue *fitness.Value, startOfDay time.Time) error {
295         switch f := dataSource.DataType.Field[0].Format; f {
296         case "integer":
297                 if rawValue.IntVal == 0 {
298                         return nil
299                 }
300         case "floatPoint":
301                 if rawValue.FpVal == 0 {
302                         return nil
303                 }
304         default:
305                 return fmt.Errorf("unexpected data type field format %q", f)
306         }
307
308         dataSourceID, err := c.DataSourceCreate(ctx, dataSource)
309         if err != nil {
310                 return err
311         }
312         dataSource.DataStreamId = dataSourceID
313
314         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
315         currValue, startTime, err := c.readCumulative(ctx, dataSource, startOfDay, endOfDay)
316         if err != nil {
317                 return err
318         }
319
320         var diffValue fitness.Value
321         if dataSource.DataType.Field[0].Format == "integer" {
322                 if rawValue.IntVal == currValue.IntVal {
323                         return nil
324                 }
325                 diffValue.IntVal = rawValue.IntVal - currValue.IntVal
326                 if diffValue.IntVal < 0 {
327                         log.Warningf(ctx, "stored value (%d) is larger than new value (%d); assuming count was reset", currValue.IntVal, rawValue.IntVal)
328                         diffValue.IntVal = rawValue.IntVal
329                 }
330         } else { // if dataSource.DataType.Field[0].Format == "floatPoint"
331                 if rawValue.FpVal == currValue.FpVal {
332                         return nil
333                 }
334                 diffValue.FpVal = rawValue.FpVal - currValue.FpVal
335                 if diffValue.FpVal < 0 {
336                         log.Warningf(ctx, "stored value (%g) is larger than new value (%g); assuming count was reset", currValue.FpVal, rawValue.FpVal)
337                         diffValue.FpVal = rawValue.FpVal
338                 }
339         }
340
341         endTime := endOfDay
342         if now := time.Now().In(startOfDay.Location()); now.Before(endOfDay) {
343                 endTime = now
344         }
345         log.Debugf(ctx, "adding cumulative data point: %v-%v %+v", startTime, endTime, diffValue)
346
347         return c.DataSetPatch(ctx, dataSource.DataStreamId, []*fitness.DataPoint{
348                 &fitness.DataPoint{
349                         DataTypeName:   dataSource.DataType.Name,
350                         StartTimeNanos: startTime.UnixNano(),
351                         EndTimeNanos:   endTime.UnixNano(),
352                         Value:          []*fitness.Value{&diffValue},
353                 },
354         })
355 }
356
357 func (c *Client) readCumulative(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (*fitness.Value, time.Time, error) {
358         datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano())
359
360         res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSource.DataStreamId, datasetID).Context(ctx).Do()
361         if err != nil {
362                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataSource.DataStreamId, datasetID, err)
363                 return nil, time.Time{}, err
364         }
365
366         if len(res.Point) == 0 {
367                 return &fitness.Value{}, startTime, nil
368         }
369
370         var sum fitness.Value
371         maxEndTime := startTime
372         for _, p := range res.Point {
373                 switch f := dataSource.DataType.Field[0].Format; f {
374                 case "integer":
375                         sum.IntVal += p.Value[0].IntVal
376                 case "floatPoint":
377                         sum.FpVal += p.Value[0].FpVal
378                 default:
379                         return nil, time.Time{}, fmt.Errorf("unexpected data type field format %q", f)
380                 }
381
382                 pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location())
383                 if maxEndTime.Before(pointEndTime) {
384                         maxEndTime = pointEndTime
385                 }
386         }
387
388         log.Debugf(ctx, "read cumulative data %s until %v: %+v", dataSource.DataStreamId, maxEndTime, sum)
389         return &sum, maxEndTime, nil
390 }
391
392 type heartRateDuration struct {
393         Min      int
394         Max      int
395         Duration time.Duration
396 }
397
398 type heartRateDurations []*heartRateDuration
399
400 func (res heartRateDurations) find(min, max int) (*heartRateDuration, bool) {
401         for _, d := range res {
402                 if d.Min != min || d.Max != max {
403                         continue
404                 }
405                 return d, true
406         }
407
408         return nil, false
409 }
410
411 func (c *Client) heartRate(ctx context.Context, dataSource *fitness.DataSource, startTime, endTime time.Time) (heartRateDurations, time.Time, error) {
412         datasetID := fmt.Sprintf("%d-%d", startTime.UnixNano(), endTime.UnixNano())
413
414         res, err := c.Service.Users.DataSources.Datasets.Get(userID, dataSource.DataStreamId, datasetID).Context(ctx).Do()
415         if err != nil {
416                 log.Errorf(ctx, "c.Service.Users.DataSources.Datasets.Get(%q, %q) = %v", dataSource.DataStreamId, datasetID, err)
417                 return nil, time.Time{}, err
418         }
419
420         if len(res.Point) == 0 {
421                 return nil, startTime, nil
422         }
423
424         var results heartRateDurations
425         maxEndTime := startTime
426         for _, p := range res.Point {
427                 max := int(p.Value[1].FpVal)
428                 min := int(p.Value[2].FpVal)
429                 duration := time.Unix(0, p.EndTimeNanos).Sub(time.Unix(0, p.StartTimeNanos))
430
431                 if d, ok := results.find(min, max); ok {
432                         d.Duration += duration
433                 } else {
434                         results = append(results, &heartRateDuration{
435                                 Min:      min,
436                                 Max:      max,
437                                 Duration: duration,
438                         })
439                 }
440
441                 pointEndTime := time.Unix(0, p.EndTimeNanos).In(startTime.Location())
442                 if maxEndTime.Before(pointEndTime) {
443                         maxEndTime = pointEndTime
444                 }
445         }
446
447         return results, maxEndTime, nil
448 }
449
450 func (c *Client) SetHeartRate(ctx context.Context, totalDurations []fitbit.HeartRateZone, restingHeartRate int, startOfDay time.Time) error {
451         dataSource := &fitness.DataSource{
452                 Application: Application(ctx),
453                 DataType: &fitness.DataType{
454                         Field: []*fitness.DataTypeField{
455                                 &fitness.DataTypeField{
456                                         Name:   "average",
457                                         Format: "floatPoint",
458                                 },
459                                 &fitness.DataTypeField{
460                                         Name:   "max",
461                                         Format: "floatPoint",
462                                 },
463                                 &fitness.DataTypeField{
464                                         Name:   "min",
465                                         Format: "floatPoint",
466                                 },
467                         },
468                         Name: dataTypeNameHeartrate,
469                 },
470                 Name: "Heart rate summary",
471                 Type: "raw",
472         }
473
474         dataSourceID, err := c.DataSourceCreate(ctx, dataSource)
475         if err != nil {
476                 return err
477         }
478         dataSource.DataStreamId = dataSourceID
479
480         endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Nanosecond)
481         prevDurations, startTime, err := c.heartRate(ctx, dataSource, startOfDay, endOfDay)
482
483         // calculate the difference between the durations mentioned in
484         // totalDurations and prevDurations and store it in diffDurations.
485         var diffDurations heartRateDurations
486         for _, d := range totalDurations {
487                 total := time.Duration(d.Minutes) * time.Minute
488
489                 var prev time.Duration
490                 if res, ok := prevDurations.find(d.Min, d.Max); ok {
491                         prev = res.Duration
492                 }
493
494                 diff := total - prev
495                 if diff < 0 {
496                         diff = total
497                 }
498
499                 if res, ok := diffDurations.find(d.Min, d.Max); ok {
500                         res.Duration += diff
501                 } else {
502                         diffDurations = append(diffDurations, &heartRateDuration{
503                                 Min:      d.Min,
504                                 Max:      d.Max,
505                                 Duration: diff,
506                         })
507                 }
508         }
509
510         // create a fitness.DataPoint for each non-zero duration difference.
511         var dataPoints []*fitness.DataPoint
512         for _, d := range diffDurations {
513                 if d.Duration < time.Nanosecond {
514                         continue
515                 }
516
517                 endTime := startTime.Add(d.Duration)
518                 if endTime.After(endOfDay) {
519                         log.Warningf(ctx, "heart rate durations exceed one day (current end time: %v)", endTime)
520                         break
521                 }
522
523                 average := float64(d.Min+d.Max) / 2.0
524                 if d.Min <= restingHeartRate && restingHeartRate <= d.Max {
525                         average = float64(restingHeartRate)
526                 }
527
528                 dataPoints = append(dataPoints, &fitness.DataPoint{
529                         DataTypeName:   dataSource.DataType.Name,
530                         StartTimeNanos: startTime.UnixNano(),
531                         EndTimeNanos:   endTime.UnixNano(),
532                         Value: []*fitness.Value{
533                                 &fitness.Value{
534                                         FpVal: average,
535                                 },
536                                 &fitness.Value{
537                                         FpVal: float64(d.Max),
538                                 },
539                                 &fitness.Value{
540                                         FpVal: float64(d.Min),
541                                 },
542                         },
543                 })
544
545                 startTime = endTime
546         }
547
548         if len(dataPoints) == 0 {
549                 return nil
550         }
551         return c.DataSetPatch(ctx, dataSource.DataStreamId, dataPoints)
552 }