Skip to content

Commit

Permalink
cli: fitactivity auto determine protocol version (#380)
Browse files Browse the repository at this point in the history
* cli: fitactivity combine fix error format

* cli: fitactivity auto determine protocol version
  • Loading branch information
muktihari authored Aug 30, 2024
1 parent b5f7b1a commit 5b12b0d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
10 changes: 5 additions & 5 deletions cmd/fitactivity/combiner/combiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ const (
ErrSportMismatch = errorString("sport mismatch")
)

func Combine(fits ...proto.FIT) (*proto.FIT, error) {
func Combine(fits ...*proto.FIT) (*proto.FIT, error) {
if len(fits) < 2 {
return nil, fmt.Errorf("provide at least 2 fits to combine: %w", ErrMinimalCombine)
}

slices.SortStableFunc(fits, func(f1, f2 proto.FIT) int {
slices.SortStableFunc(fits, func(f1, f2 *proto.FIT) int {
if len(f1.Messages) == 0 || len(f2.Messages) == 0 {
return 0
}
Expand All @@ -53,7 +53,7 @@ func Combine(fits ...proto.FIT) (*proto.FIT, error) {

sessionsByFIT := make([][]proto.Message, len(fits))
for i := range fits {
fit := &fits[i]
fit := fits[i]
for j := 0; j < len(fit.Messages); j++ {
mesg := fit.Messages[j]
switch mesg.Num {
Expand Down Expand Up @@ -84,11 +84,11 @@ func Combine(fits ...proto.FIT) (*proto.FIT, error) {
}
}

fitResult := &fits[0]
fitResult := fits[0]

for i := range sessionsByFIT {
if len(sessionsByFIT[i]) == 0 {
return nil, fmt.Errorf("fits[i]: %w", ErrNoSessionFound)
return nil, fmt.Errorf("fits[%d]: %w", i, ErrNoSessionFound)
}
}

Expand Down
16 changes: 14 additions & 2 deletions cmd/fitactivity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func combineAndConcealPosition(paths []string, opts *options) error {
return fmt.Errorf("could not combine: %v", err)
}

protocolVersion := latestProtocolVersion(fits)

if err := concealer.ConcealPosition(fit, opts.concealStart, opts.concealEnd); err != nil {
return fmt.Errorf("could not conceal: %v", err)
}
Expand All @@ -150,7 +152,7 @@ func combineAndConcealPosition(paths []string, opts *options) error {
}

enc := encoder.New(fout,
encoder.WithProtocolVersion(proto.V2),
encoder.WithProtocolVersion(protocolVersion),
opts.encoderHeaderOption,
)

Expand All @@ -161,6 +163,16 @@ func combineAndConcealPosition(paths []string, opts *options) error {
return nil
}

func latestProtocolVersion(fits []*proto.FIT) proto.Version {
var version = byte(proto.V1)
for i := range fits {
if fits[i].FileHeader.ProtocolVersion > version {
version = fits[i].FileHeader.ProtocolVersion
}
}
return proto.Version(version)
}

func openAndConcealPosition(path string, opts *options) error {
f, err := os.Open(path)
if err != nil {
Expand Down Expand Up @@ -196,7 +208,7 @@ func openAndConcealPosition(path string, opts *options) error {
defer fout.Close()

enc := encoder.New(fout,
encoder.WithProtocolVersion(proto.V2),
encoder.WithProtocolVersion(latestProtocolVersion(fits)),
opts.encoderHeaderOption,
)

Expand Down
6 changes: 3 additions & 3 deletions cmd/fitactivity/opener/opener.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// Open opens all given paths concurrently.
func Open(paths ...string) ([]proto.FIT, error) {
func Open(paths ...string) ([]*proto.FIT, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand All @@ -33,12 +33,12 @@ func Open(paths ...string) ([]proto.FIT, error) {
close(resultc)
}()

fits := make([]proto.FIT, 0, len(paths))
fits := make([]*proto.FIT, 0, len(paths))
for res := range resultc {
if res.err != nil {
return nil, res.err
}
fits = append(fits, *res.fit)
fits = append(fits, res.fit)
}

return fits, nil
Expand Down
Binary file added cmd/fitactivity/test.fit
Binary file not shown.

0 comments on commit 5b12b0d

Please sign in to comment.