Skip to content

Commit

Permalink
feat(cli): update on fitactivity conceal logic (#17)
Browse files Browse the repository at this point in the history
- now start and end positions in all lap messages are removed when conceal option is specified
- improve conceal record positions logic
- update README.md
  • Loading branch information
muktihari authored Oct 15, 2023
1 parent 8bfd3e6 commit bdc603c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ We provides some CLI programs to interact with FIT files that can be found in `c
2. `fitconv`: Converts FIT file to a CSV file, allowing us to read the FIT file in a human-readable format. [README.md](/cmd/fitconv/README.md)
3. `fitprint`: Print FIT file into stdout in human-readable format. [README.md](/cmd/fitprint/README.md)

> Now, the `fitactivity` and `fitconv` programs are automatically built during release for Linux, Windows, and macOS platforms. They are available for download in [Release's Assets](https://github.com/muktihari/fit/releases).
Now, the `fitactivity` and `fitconv` programs are automatically built during release; for Linux, Windows, and macOS platforms. They are available for download in [Release's Assets](https://github.com/muktihari/fit/releases).

## Generate Custom FIT SDK

Expand Down
11 changes: 7 additions & 4 deletions cmd/fitactivity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ First, we will order the files by `FileId.TimeCreated`.
The first file will be the base for the resulting file and we will combine these following messages from the next FIT files into the resulting file:

- Session: combine session by calculating some fields (list fields will be shown after this)
- Record: field distance will be calculated before append, the rest will be appended as it is
- Record: field `distance` will be calculated before append, the rest will be appended as it is
- Event: append as it is
- Lap: append as it is
- Lap: field `start_position_lat`, `start_position_long`, `end_position_lat`, and `end_position_long` will be removed only if conceal option is specified, the rest will be appended as it is.

Why lap positions must be removed? GPS Positions saved in lap messages can be vary, user may set new lap every 500m or new lap every 1 hour for example, we don't know the exact distance for each lap. If user want to conceal 1km, we need to find all laps within the conceal distance and decide whether to remove it or change it with new positions, this will add complexity. So, let's just remove it for now, if our upload target is Strava, they don't specify positions in lap message anyway.

Other messages from the next FIT files will not be combined.

Expand Down Expand Up @@ -77,7 +79,7 @@ _NOTE: Combining FIT activity files is NOT the same as merging multiple files in

## Build or Install

> Prerequisite: Install golang: [https://go.dev/doc/install](https://go.dev/doc/install)
_Prerequisite: Install golang: [https://go.dev/doc/install](https://go.dev/doc/install)_

### Build

Expand Down Expand Up @@ -106,6 +108,8 @@ Or you can install the program instead of manual build, this will build base on
go install .
```

**Alternatively, now you can just download the the binary from [Release's Assets](https://github.com/muktihari/fit/releases)**
## Usage Examples
After the program has been built or installed, you can call the fitactivity executable.
Expand All @@ -130,7 +134,6 @@ Note: conceal value is in meters
```sh
fitactivity --conceal-start 1000 part1.fit part2.fit
# ls output
# part1.fit part1_concealed_1000_0.fit part2.fit part2_concealed_1000_0.fit
```
Expand Down
28 changes: 23 additions & 5 deletions cmd/fitactivity/concealer/concealer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package concealer

import (
"github.com/muktihari/fit/cmd/fitactivity/finder"
"github.com/muktihari/fit/profile/untyped/fieldnum"
"github.com/muktihari/fit/profile/untyped/mesgnum"
"github.com/muktihari/fit/proto"
)

Expand All @@ -26,8 +26,11 @@ func ConcealPositionStart(fit *proto.Fit, concealDistance uint32) error {
return nil
}

sessionInfo := finder.FindFirstSessionInfo(fit)
for i := sessionInfo.RecordFirstIndex; i <= sessionInfo.RecordLastIndex; i++ {
for i := range fit.Messages {
if fit.Messages[i].Num != mesgnum.Record {
continue
}

if fit.Messages[i].FieldValueByNum(fieldnum.RecordPositionLat) == nil ||
fit.Messages[i].FieldValueByNum(fieldnum.RecordPositionLong) == nil {
continue
Expand Down Expand Up @@ -55,9 +58,12 @@ func ConcealPositionEnd(fit *proto.Fit, concealDistance uint32) error {
return nil
}

sessionInfo := finder.FindLastSessionInfo(fit)
var lastDistance uint32
for i := sessionInfo.RecordLastIndex; i >= sessionInfo.RecordFirstIndex; i-- {
for i := len(fit.Messages) - 1; i >= 0; i-- {
if fit.Messages[i].Num != mesgnum.Record {
continue
}

if fit.Messages[i].FieldValueByNum(fieldnum.RecordPositionLat) == nil ||
fit.Messages[i].FieldValueByNum(fieldnum.RecordPositionLong) == nil {
continue
Expand All @@ -81,3 +87,15 @@ func ConcealPositionEnd(fit *proto.Fit, concealDistance uint32) error {

return nil
}

// ConcealLapStartAndEndPosition removes StartPositionLat, StartPositionLong, EndPositionLat and EndPositionLong from any lap messages.
func ConcealLapStartAndEndPosition(fit *proto.Fit) {
for i := range fit.Messages {
if fit.Messages[i].Num == mesgnum.Lap {
fit.Messages[i].RemoveFieldByNum(fieldnum.LapStartPositionLat)
fit.Messages[i].RemoveFieldByNum(fieldnum.LapStartPositionLong)
fit.Messages[i].RemoveFieldByNum(fieldnum.LapEndPositionLat)
fit.Messages[i].RemoveFieldByNum(fieldnum.LapEndPositionLong)
}
}
}
3 changes: 3 additions & 0 deletions cmd/fitactivity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func combineAndConcealPosition(paths []string, out string, concealStart, conceal
return fmt.Errorf("could not conceal: %v", err)
}

concealer.ConcealLapStartAndEndPosition(fit)

var fout = os.Stdout // default output to stdout if not specified.
if out != "" {
fout, err = os.OpenFile(out, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o666)
Expand Down Expand Up @@ -133,6 +135,7 @@ func openAndConcealPosition(path string, concealStart, concealEnd uint32) error
if err := concealer.ConcealPosition(fits[i], concealStart, concealEnd); err != nil {
return fmt.Errorf("could not conceal fits[%d of %d]: %v", i+1, len(fits), err)
}
concealer.ConcealLapStartAndEndPosition(fits[i])
}

ext := filepath.Ext(path)
Expand Down
6 changes: 5 additions & 1 deletion cmd/fitactivity/opener/opener.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func worker(ctx context.Context, path string, resultc chan<- result, wg *sync.Wa
decoder.WithNoComponentExpansion(),
)

for dec.Next() {
for {
fileId, err := dec.PeekFileId()
if err != nil {
resultc <- result{err: err}
Expand All @@ -83,5 +83,9 @@ func worker(ctx context.Context, path string, resultc chan<- result, wg *sync.Wa
}

resultc <- result{fit: fit}

if !dec.Next() {
break
}
}
}

0 comments on commit bdc603c

Please sign in to comment.