-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit_explorer.go
135 lines (117 loc) · 2.65 KB
/
fit_explorer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bytes"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"strconv"
"time"
"github.com/tormoder/fit"
)
// print the contents of the obj
func PrettyPrint(data interface{}) {
var p []byte
p, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s \n", p)
}
func convertToMinutes(m float64) string {
minutes, secondsdecimal := math.Modf(m)
seconds := secondsdecimal * 60
return fmt.Sprintf("%02.0f:%02.0f", minutes, seconds)
}
func cInt(i int) string {
v := strconv.Itoa(i)
return v
}
func cInt8(i int8) string {
v := strconv.Itoa(int(i))
return v
}
func cUint8(i uint8) string {
v := strconv.Itoa(int(i))
return v
}
func cDecimal(f float64) string {
v := strconv.FormatFloat(f, 'f', -1, 64)
return v
}
func cTime(t time.Time) string {
v := t.String()
return v
}
func cDuration(d time.Duration) string {
v := d.String()
return v
}
func main() {
// Read our FIT test file data
testFilePtr := flag.String("file", "file.fit", "the FIT file name")
flag.Parse()
fmt.Println("I will open FIT file:", *testFilePtr)
testData, err := ioutil.ReadFile(*testFilePtr)
if err != nil {
log.Fatalln("error reading fit file:", err)
return
}
// Decode the FIT file data
fit, err := fit.Decode(bytes.NewReader(testData))
if err != nil {
log.Fatalln("error decoding fir file:", err)
return
}
w := csv.NewWriter(os.Stdout)
// Inspect the TimeCreated field in the FileId message
log.Println("TIME CREATED:", fit.FileId.TimeCreated)
// Inspect the dynamic Product field in the FileId message
log.Println("PRODUCT:", fit.FileId.GetProduct())
// Inspect the FIT file type
log.Println("FIT TYPE:", fit.Type())
// Get the actual activity
activity, err := fit.Activity()
if err != nil {
log.Fatalln("error writing record to csv:", err)
return
}
idx := 0
t0 := time.Now()
for _, record := range activity.Records {
idx++
if idx == 1 {
t0 = record.Timestamp
}
x := []string{
cInt(idx),
cDecimal(record.PositionLat.Degrees()),
cDecimal(record.PositionLong.Degrees()),
cInt8(record.Temperature),
cTime(record.Timestamp),
cDuration(record.Timestamp.Sub(t0)),
cUint8(record.Cadence),
cDecimal(record.GetDistanceScaled() / 1000),
"km",
cDecimal(record.GetAltitudeScaled()),
"m",
cUint8(record.HeartRate),
"HR bpm",
convertToMinutes(16.666666666667 / record.GetSpeedScaled()),
"min/km",
cDecimal(record.GetSpeedScaled() * 3.6),
"km/h",
}
w.Write(x)
}
w.Flush()
// Print the sport of the first Session message
for _, session := range activity.Sessions {
log.Println("SESSION SPORT:", session.Sport)
}
}