Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Commit

Permalink
Add tolerance command line parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Metalnem committed Dec 28, 2016
1 parent e2f7df1 commit dab9138
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 13 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
)

const (
// ToleranceKey is key used to get or set tolerance option in Context passed to GetActivity and GetActivities functions.
ToleranceKey = RuntasticContextKey("tolerance")

appKey = "com.runtastic.android"
appSecret = "T68bA6dHk2ayW1Y39BQdEnUmGqM8Zq1SFZ3kNas3KYDjp471dJNXLcoYWsDBd1mH"
appVersion = "6.9.2"
Expand Down Expand Up @@ -57,6 +60,9 @@ type UserID string
// ActivityID is unique activity identifier.
type ActivityID string

// RuntasticContextKey is the type of keys used in Context.
type RuntasticContextKey string

// Session contains session data for single authenticated user.
type Session struct {
UserID UserID `json:"userId"`
Expand Down Expand Up @@ -448,7 +454,7 @@ func parseHeartRateData(trace string) ([]heartRatePoint, error) {
return points, nil
}

func merge(gpsData []gpsPoint, heartRateData []heartRatePoint) []DataPoint {
func merge(ctx context.Context, gpsData []gpsPoint, heartRateData []heartRatePoint) []DataPoint {
var data []DataPoint

if len(gpsData) == 0 {
Expand All @@ -460,7 +466,11 @@ func merge(gpsData []gpsPoint, heartRateData []heartRatePoint) []DataPoint {
}

l := len(heartRateData)
diff := 5 * time.Second
diff := 15 * time.Second

if tolerance, ok := ctx.Value(ToleranceKey).(int); ok && tolerance > 0 {
diff = time.Duration(tolerance) * time.Second
}

for _, gps := range gpsData {
point := gps.DataPoint()
Expand Down Expand Up @@ -545,7 +555,7 @@ func GetActivity(ctx context.Context, session *Session, id ActivityID) (*Activit
ID: id,
StartTime: time.Time(data.RunSessions.StartTime),
EndTime: time.Time(data.RunSessions.EndTime),
Data: merge(gpsData, heartRateData),
Data: merge(ctx, gpsData, heartRateData),
}

return &activity, nil
Expand Down
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
)

var (
email = flag.String("email", "", "Email (required)")
password = flag.String("password", "", "Password (required)")
email = flag.String("email", "", "")
password = flag.String("password", "", "")
tolerance = flag.Int("tolerance", 15, "")

errMissingCredentials = errors.New("Missing email address or password")
errNoActivities = errors.New("There are no activities to backup")
Expand Down Expand Up @@ -170,7 +171,13 @@ func main() {
glog.Exit(err)
}

activities, err := api.GetActivities(context.Background(), user)
ctx := context.Background()

if *tolerance > 0 {
ctx = context.WithValue(ctx, api.ToleranceKey, *tolerance)
}

activities, err := api.GetActivities(ctx, user)

if err != nil {
glog.Exit(err)
Expand Down

0 comments on commit dab9138

Please sign in to comment.