Implement storing of calories expended.
[kraftakt.git] / gfitsync.go
index 22f4c56..beb44d7 100644 (file)
@@ -219,12 +219,18 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error {
                return err
        }
 
-       tm, err := time.Parse("2006-01-02", s.Date)
+       fitbitClient, err := fitbit.NewClient(ctx, s.OwnerID, u)
        if err != nil {
                return err
        }
 
-       fitbitClient, err := fitbit.NewClient(ctx, s.OwnerID, u)
+       profile, err := fitbitClient.Profile(ctx)
+       if err != nil {
+               return err
+       }
+       log.Debugf(ctx, "profile = %+v", profile)
+
+       tm, err := time.ParseInLocation("2006-01-02", s.Date, profile.Timezone)
        if err != nil {
                return err
        }
@@ -233,7 +239,8 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error {
        if err != nil {
                return err
        }
-       log.Debugf(ctx, "ActivitySummary for %s = %+v", u.Email, summary)
+       log.Debugf(ctx, "%s (%s) took %d steps on %s",
+               profile.Name, u.Email, summary.Summary.Steps, s.Date)
 
        gfitClient, err := gfit.NewClient(ctx, u)
        if err != nil {
@@ -244,5 +251,9 @@ func handleNotification(ctx context.Context, s *fitbit.Subscription) error {
                return fmt.Errorf("gfitClient.SetSteps(%d) = %v", summary.Summary.Steps, err)
        }
 
+       if err := gfitClient.SetCalories(ctx, summary.Summary.CaloriesOut, tm); err != nil {
+               return fmt.Errorf("gfitClient.SetCalories(%d) = %v", summary.Summary.CaloriesOut, err)
+       }
+
        return nil
 }