Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filedef activity file add sports and update its sorting #464

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions profile/filedef/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Activity struct {
TimeInZones []*mesgdef.TimeInZone
Splits []*mesgdef.Split
SplitSummaries []*mesgdef.SplitSummary // entries must be unique within each split_type
Sports []*mesgdef.Sport

// Messages not related to Activity
UnrelatedMessages []proto.Message
Expand Down Expand Up @@ -105,6 +106,8 @@ func (f *Activity) Add(mesg proto.Message) {
f.Splits = append(f.Splits, mesgdef.NewSplit(&mesg))
case mesgnum.SplitSummary:
f.SplitSummaries = append(f.SplitSummaries, mesgdef.NewSplitSummary(&mesg))
case mesgnum.Sport:
f.Sports = append(f.Sports, mesgdef.NewSport(&mesg))
default:
mesg.Fields = append(mesg.Fields[:0:0], mesg.Fields...)
f.UnrelatedMessages = append(f.UnrelatedMessages, mesg)
Expand All @@ -119,7 +122,8 @@ func (f *Activity) ToFIT(options *mesgdef.Options) proto.FIT {
len(f.Laps) + len(f.Records) + len(f.DeviceInfos) + len(f.Events) +
len(f.Lengths) + len(f.SegmentLaps) + len(f.ZonesTargets) + len(f.Workouts) +
len(f.WorkoutSteps) + len(f.HRs) + len(f.HRVs) + len(f.GpsMetadatas) +
len(f.TimeInZones) + len(f.Splits) + len(f.SplitSummaries) + len(f.UnrelatedMessages)
len(f.TimeInZones) + len(f.Splits) + len(f.SplitSummaries) + len(f.Sports) +
len(f.UnrelatedMessages)

fit := proto.FIT{
Messages: make([]proto.Message, 0, size),
Expand All @@ -135,12 +139,45 @@ func (f *Activity) ToFIT(options *mesgdef.Options) proto.FIT {
for i := range f.FieldDescriptions {
fit.Messages = append(fit.Messages, f.FieldDescriptions[i].ToMesg(options))
}
for i := range f.DeviceInfos {
fit.Messages = append(fit.Messages, f.DeviceInfos[i].ToMesg(options))
}

// Let's put non-timestamp messages at the beginning, we don't need to sort it anyway.
if f.UserProfile != nil {
sortStartPos += 1
fit.Messages = append(fit.Messages, f.UserProfile.ToMesg(options))
}
sortStartPos += len(f.ZonesTargets)
for i := range f.ZonesTargets {
fit.Messages = append(fit.Messages, f.ZonesTargets[i].ToMesg(options))
}
sortStartPos += len(f.Workouts)
for i := range f.Workouts {
fit.Messages = append(fit.Messages, f.Workouts[i].ToMesg(options))
}
sortStartPos += len(f.WorkoutSteps)
for i := range f.WorkoutSteps {
fit.Messages = append(fit.Messages, f.WorkoutSteps[i].ToMesg(options))
}
sortStartPos += len(f.HRVs)
for i := range f.HRVs {
fit.Messages = append(fit.Messages, f.HRVs[i].ToMesg(options))
}
sortStartPos += len(f.Splits)
for i := range f.Splits {
fit.Messages = append(fit.Messages, f.Splits[i].ToMesg(options))
}
sortStartPos += len(f.SplitSummaries)
for i := range f.SplitSummaries {
fit.Messages = append(fit.Messages, f.SplitSummaries[i].ToMesg(options))
}
sortStartPos += len(f.Sports)
for i := range f.Sports {
fit.Messages = append(fit.Messages, f.Sports[i].ToMesg(options))
}

// Below are messages with timestamp, should be sorted.
for i := range f.DeviceInfos {
fit.Messages = append(fit.Messages, f.DeviceInfos[i].ToMesg(options))
}
if f.Activity != nil {
fit.Messages = append(fit.Messages, f.Activity.ToMesg(options))
}
Expand All @@ -162,33 +199,15 @@ func (f *Activity) ToFIT(options *mesgdef.Options) proto.FIT {
for i := range f.SegmentLaps {
fit.Messages = append(fit.Messages, f.SegmentLaps[i].ToMesg(options))
}
for i := range f.ZonesTargets {
fit.Messages = append(fit.Messages, f.ZonesTargets[i].ToMesg(options))
}
for i := range f.Workouts {
fit.Messages = append(fit.Messages, f.Workouts[i].ToMesg(options))
}
for i := range f.WorkoutSteps {
fit.Messages = append(fit.Messages, f.WorkoutSteps[i].ToMesg(options))
}
for i := range f.HRs {
fit.Messages = append(fit.Messages, f.HRs[i].ToMesg(options))
}
for i := range f.HRVs {
fit.Messages = append(fit.Messages, f.HRVs[i].ToMesg(options))
}
for i := range f.GpsMetadatas {
fit.Messages = append(fit.Messages, f.GpsMetadatas[i].ToMesg(options))
}
for i := range f.TimeInZones {
fit.Messages = append(fit.Messages, f.TimeInZones[i].ToMesg(options))
}
for i := range f.Splits {
fit.Messages = append(fit.Messages, f.Splits[i].ToMesg(options))
}
for i := range f.SplitSummaries {
fit.Messages = append(fit.Messages, f.SplitSummaries[i].ToMesg(options))
}

fit.Messages = append(fit.Messages, f.UnrelatedMessages...)

Expand Down
139 changes: 76 additions & 63 deletions profile/filedef/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func newActivityMessageForTest(now time.Time) []proto.Message {

func newActivityMessagesWithExpectedOrder(now time.Time) (mesgs []proto.Message, ordered []proto.Message) {
mesgs = []proto.Message{
// Messages have no timestamp field:
0: {Num: mesgnum.FileId, Fields: []proto.Field{
factory.CreateField(mesgnum.FileId, fieldnum.FileIdType).WithValue(uint8(typedef.FileActivity)),
factory.CreateField(mesgnum.FileId, fieldnum.FileIdTimeCreated).WithValue(datetime.ToUint32(now)),
Expand All @@ -76,90 +77,100 @@ func newActivityMessagesWithExpectedOrder(now time.Time) (mesgs []proto.Message,
factory.CreateField(mesgnum.FieldDescription, fieldnum.FieldDescriptionNativeFieldNum).WithValue(uint8(fieldnum.RecordHeartRate)),
factory.CreateField(mesgnum.FieldDescription, fieldnum.FieldDescriptionFitBaseTypeId).WithValue(uint8(basetype.Uint8)),
}},
3: {Num: mesgnum.DeviceInfo, Fields: []proto.Field{
factory.CreateField(mesgnum.DeviceInfo, fieldnum.DeviceInfoManufacturer).WithValue(uint16(typedef.ManufacturerGarmin)),
}},
4: {Num: mesgnum.UserProfile, Fields: []proto.Field{
3: {Num: mesgnum.UserProfile, Fields: []proto.Field{
factory.CreateField(mesgnum.UserProfile, fieldnum.UserProfileFriendlyName).WithValue("Mary Jane"),
factory.CreateField(mesgnum.UserProfile, fieldnum.UserProfileAge).WithValue(uint8(21)),
}},
5: {Num: mesgnum.Event, Fields: []proto.Field{
4: {Num: mesgnum.ZonesTarget, Fields: []proto.Field{
factory.CreateField(mesgnum.ZonesTarget, fieldnum.ZonesTargetMaxHeartRate).WithValue(uint8(190)),
}},
5: {Num: mesgnum.Workout, Fields: []proto.Field{
factory.CreateField(mesgnum.Workout, fieldnum.WorkoutSport).WithValue(uint8(typedef.SportCycling)),
}},
6: {Num: mesgnum.WorkoutStep, Fields: []proto.Field{
factory.CreateField(mesgnum.WorkoutStep, fieldnum.WorkoutStepIntensity).WithValue(uint8(typedef.IntensityActive)),
}},
7: {Num: mesgnum.Hrv, Fields: []proto.Field{
factory.CreateField(mesgnum.Hrv, fieldnum.HrvTime).WithValue([]uint16{uint16(1000)}),
}},
8: {Num: mesgnum.Split, Fields: []proto.Field{
factory.CreateField(mesgnum.Split, fieldnum.SplitTotalDistance).WithValue(uint32(10000)),
}},
9: {Num: mesgnum.SplitSummary, Fields: []proto.Field{
factory.CreateField(mesgnum.SplitSummary, fieldnum.SplitSummarySplitType).WithValue(typedef.SplitTypeAscentSplit.Byte()),
}},
10: {Num: mesgnum.Sport, Fields: []proto.Field{
factory.CreateField(mesgnum.Sport, fieldnum.SportName).WithValue("cycling"),
}},
// Messages have timestamp field:
11: {Num: mesgnum.DeviceInfo, Fields: []proto.Field{
factory.CreateField(mesgnum.DeviceInfo, fieldnum.DeviceInfoTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
factory.CreateField(mesgnum.DeviceInfo, fieldnum.DeviceInfoManufacturer).WithValue(uint16(typedef.ManufacturerGarmin)),
}},
12: {Num: mesgnum.Event, Fields: []proto.Field{
factory.CreateField(mesgnum.Event, fieldnum.EventTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
factory.CreateField(mesgnum.Event, fieldnum.EventEvent).WithValue(uint8(typedef.EventActivity)),
factory.CreateField(mesgnum.Event, fieldnum.EventEventType).WithValue(uint8(typedef.EventTypeStart)),
}},
6: {Num: mesgnum.Record, Fields: []proto.Field{
13: {Num: mesgnum.Record, Fields: []proto.Field{
factory.CreateField(mesgnum.Record, fieldnum.RecordTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
7: {Num: mesgnum.Record, Fields: []proto.Field{
14: {Num: mesgnum.Record, Fields: []proto.Field{
factory.CreateField(mesgnum.Record, fieldnum.RecordTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
8: {Num: mesgnum.Event, Fields: []proto.Field{
15: {Num: mesgnum.Event, Fields: []proto.Field{
factory.CreateField(mesgnum.Event, fieldnum.EventTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
9: {Num: mesgnum.Record, Fields: []proto.Field{
16: {Num: mesgnum.Record, Fields: []proto.Field{
factory.CreateField(mesgnum.Record, fieldnum.RecordTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
10: {Num: mesgnum.Event, Fields: []proto.Field{
17: {Num: mesgnum.Event, Fields: []proto.Field{
// Intentionally using same timestamp as last message.
factory.CreateField(mesgnum.Event, fieldnum.EventTimestamp).WithValue(datetime.ToUint32(now)),
}},
11: {Num: mesgnum.Lap, Fields: []proto.Field{
18: {Num: mesgnum.Lap, Fields: []proto.Field{
factory.CreateField(mesgnum.Lap, fieldnum.LapTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
12: {Num: mesgnum.Session, Fields: []proto.Field{
19: {Num: mesgnum.Session, Fields: []proto.Field{
factory.CreateField(mesgnum.Session, fieldnum.SessionTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
13: {Num: mesgnum.Activity, Fields: []proto.Field{
20: {Num: mesgnum.Activity, Fields: []proto.Field{
factory.CreateField(mesgnum.Activity, fieldnum.ActivityTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
// Unordered optional Messages
14: {Num: mesgnum.Length, Fields: []proto.Field{
21: {Num: mesgnum.Length, Fields: []proto.Field{
factory.CreateField(mesgnum.Length, fieldnum.LengthTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
factory.CreateField(mesgnum.Length, fieldnum.LengthAvgSpeed).WithValue(uint16(1000)),
}},
15: {Num: mesgnum.SegmentLap, Fields: []proto.Field{
22: {Num: mesgnum.SegmentLap, Fields: []proto.Field{
factory.CreateField(mesgnum.SegmentLap, fieldnum.SegmentLapTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
factory.CreateField(mesgnum.SegmentLap, fieldnum.SegmentLapAvgCadence).WithValue(uint8(100)),
}},
16: {Num: mesgnum.ZonesTarget, Fields: []proto.Field{
factory.CreateField(mesgnum.ZonesTarget, fieldnum.ZonesTargetMaxHeartRate).WithValue(uint8(190)),
}},
17: {Num: mesgnum.Workout, Fields: []proto.Field{
factory.CreateField(mesgnum.Workout, fieldnum.WorkoutSport).WithValue(uint8(typedef.SportCycling)),
}},
18: {Num: mesgnum.WorkoutStep, Fields: []proto.Field{
factory.CreateField(mesgnum.WorkoutStep, fieldnum.WorkoutStepIntensity).WithValue(uint8(typedef.IntensityActive)),
}},
19: {Num: mesgnum.Hr, Fields: []proto.Field{
23: {Num: mesgnum.Hr, Fields: []proto.Field{
factory.CreateField(mesgnum.Hr, fieldnum.HrTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
20: {Num: mesgnum.Hrv, Fields: []proto.Field{
factory.CreateField(mesgnum.Hrv, fieldnum.HrvTime).WithValue([]uint16{uint16(1000)}),
}},
// Unrelated messages
21: {Num: mesgnum.BarometerData, Fields: []proto.Field{
24: {Num: mesgnum.BarometerData, Fields: []proto.Field{
factory.CreateField(mesgnum.BarometerData, fieldnum.BarometerDataTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
25: {Num: mesgnum.BikeProfile, Fields: []proto.Field{ // have no timestamp, should be sorted at top.
factory.CreateField(mesgnum.BikeProfile, fieldnum.BikeProfileBikeWeight).WithValue(uint16(100)),
}},
// Special case:
// 1. CoursePoint's Timestamp Num is 1
// 2. Set's Timestamp Num is 254
22: {Num: mesgnum.CoursePoint, Fields: []proto.Field{
26: {Num: mesgnum.CoursePoint, Fields: []proto.Field{
factory.CreateField(mesgnum.CoursePoint, fieldnum.CoursePointTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
23: {Num: mesgnum.Set, Fields: []proto.Field{
27: {Num: mesgnum.Set, Fields: []proto.Field{
factory.CreateField(mesgnum.Set, fieldnum.SetTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
24: {Num: mesgnum.GpsMetadata, Fields: []proto.Field{
// Optional messages:
28: {Num: mesgnum.GpsMetadata, Fields: []proto.Field{
factory.CreateField(mesgnum.GpsMetadata, fieldnum.GpsMetadataTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
25: {Num: mesgnum.TimeInZone, Fields: []proto.Field{
29: {Num: mesgnum.TimeInZone, Fields: []proto.Field{
factory.CreateField(mesgnum.TimeInZone, fieldnum.TimeInZoneTimestamp).WithValue(datetime.ToUint32(incrementSecond(&now))),
}},
26: {Num: mesgnum.Split, Fields: []proto.Field{
factory.CreateField(mesgnum.Split, fieldnum.SplitTotalDistance).WithValue(uint32(10000)),
}},
27: {Num: mesgnum.SplitSummary, Fields: []proto.Field{
factory.CreateField(mesgnum.SplitSummary, fieldnum.SplitSummarySplitType).WithValue(typedef.SplitTypeAscentSplit.Byte()),
}},
}

ordered = []proto.Message{
Expand All @@ -168,29 +179,31 @@ func newActivityMessagesWithExpectedOrder(now time.Time) (mesgs []proto.Message,
2: mesgs[2],
3: mesgs[3],
4: mesgs[4],
5: mesgs[14],
6: mesgs[15],
7: mesgs[16],
8: mesgs[17],
9: mesgs[18],
10: mesgs[20],
11: mesgs[26],
12: mesgs[27],
13: mesgs[5],
14: mesgs[6],
15: mesgs[7],
16: mesgs[8],
17: mesgs[9],
18: mesgs[10],
19: mesgs[11],
20: mesgs[12],
21: mesgs[13],
22: mesgs[19],
23: mesgs[21],
24: mesgs[22],
25: mesgs[23],
26: mesgs[24],
27: mesgs[25],
5: mesgs[5],
6: mesgs[6],
7: mesgs[7],
8: mesgs[8],
9: mesgs[9],
10: mesgs[10],
11: mesgs[25],
12: mesgs[11],
13: mesgs[12],
14: mesgs[13],
15: mesgs[14],
16: mesgs[15],
17: mesgs[16],
18: mesgs[17],
19: mesgs[18],
20: mesgs[19],
21: mesgs[20],
22: mesgs[21],
23: mesgs[22],
24: mesgs[23],
25: mesgs[24],
26: mesgs[26],
27: mesgs[27],
28: mesgs[28],
29: mesgs[29],
}
return
}
Expand Down