forked from tormoder/fit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
240 lines (209 loc) · 6.98 KB
/
file.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package fit
import (
"fmt"
"reflect"
)
// File represents a decoded FIT file.
type File struct {
// Header is the FIT file header.
Header Header
// CRC is the FIT file CRC.
CRC uint16
// FileId is a message required for all FIT files.
FileId FileIdMsg
// Common messages for all FIT file types.
FileCreator *FileCreatorMsg
TimestampCorrelation *TimestampCorrelationMsg
DeviceInfo *DeviceInfoMsg
// UnknownMessages is a slice of unknown messages encountered during
// decoding. It is sorted by message number.
UnknownMessages []UnknownMessage
// UnknownFields is a slice of unknown fields for known messages
// encountered during decoding. It is sorted by message number.
UnknownFields []UnknownField
msgAdder msgAdder
activity *ActivityFile
device *DeviceFile
settings *SettingsFile
sport *SportFile
workout *WorkoutFile
course *CourseFile
schedules *SchedulesFile
weight *WeightFile
totals *TotalsFile
goals *GoalsFile
bloodPressure *BloodPressureFile
monitoringA *MonitoringAFile
activitySummary *ActivitySummaryFile
monitoringDaily *MonitoringDailyFile
monitoringB *MonitoringBFile
segment *SegmentFile
segmentList *SegmentListFile
}
type msgAdder interface {
add(reflect.Value)
}
func (f *File) add(msg reflect.Value) {
x := msg.Interface()
switch x.(type) {
case FileIdMsg:
f.FileId = x.(FileIdMsg)
case FileCreatorMsg:
tmp := x.(FileCreatorMsg)
f.FileCreator = &tmp
case TimestampCorrelationMsg:
tmp := x.(TimestampCorrelationMsg)
f.TimestampCorrelation = &tmp
case DeviceInfoMsg:
tmp := x.(DeviceInfoMsg)
f.DeviceInfo = &tmp
default:
f.msgAdder.add(msg)
}
}
// Type returns the FIT file type.
func (f *File) Type() FileType {
return f.FileId.Type
}
type wrongFileTypeError struct {
actual, requested FileType
}
func (e wrongFileTypeError) Error() string {
return fmt.Sprintf("fit file type is %v, not %v", e.actual, e.requested)
}
// Activity returns f's Activity file. An error is returned if the FIT file is
// not of type activity.
func (f *File) Activity() (*ActivityFile, error) {
if !(f.FileId.Type == FileTypeActivity) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeActivity}
}
return f.activity, nil
}
// Device returns f's Device file. An error is returned if the FIT file is
// not of type device.
func (f *File) Device() (*DeviceFile, error) {
if !(f.FileId.Type == FileTypeDevice) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeDevice}
}
return f.device, nil
}
// Settings returns f's Settings file. An error is returned if the FIT file is
// not of type settings.
func (f *File) Settings() (*SettingsFile, error) {
if !(f.FileId.Type == FileTypeSettings) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeSettings}
}
return f.settings, nil
}
// Sport returns f's Sport file. An error is returned if the FIT file is
// not of type sport.
func (f *File) Sport() (*SportFile, error) {
if !(f.FileId.Type == FileTypeSport) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeSport}
}
return f.sport, nil
}
// Workout returns f's Workout file. An error is returned if the FIT file is
// not of type workout.
func (f *File) Workout() (*WorkoutFile, error) {
if !(f.FileId.Type == FileTypeWorkout) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeWorkout}
}
return f.workout, nil
}
// Course returns f's Course file. An error is returned if the FIT file is
// not of type course.
func (f *File) Course() (*CourseFile, error) {
if !(f.FileId.Type == FileTypeCourse) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeCourse}
}
return f.course, nil
}
// Schedules returns f's Schedules file. An error is returned if the FIT file is
// not of type schedules.
func (f *File) Schedules() (*SchedulesFile, error) {
if !(f.FileId.Type == FileTypeSchedules) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeSchedules}
}
return f.schedules, nil
}
// Weight returns f's Weight file. An error is returned if the FIT file is
// not of type weight.
func (f *File) Weight() (*WeightFile, error) {
if !(f.FileId.Type == FileTypeWeight) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeWeight}
}
return f.weight, nil
}
// Totals returns f's Totals file. An error is returned if the FIT file is
// not of type totals.
func (f *File) Totals() (*TotalsFile, error) {
if !(f.FileId.Type == FileTypeTotals) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeTotals}
}
return f.totals, nil
}
// Goals returns f's Goals file. An error is returned if the FIT file is
// not of type goals.
func (f *File) Goals() (*GoalsFile, error) {
if !(f.FileId.Type == FileTypeGoals) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeGoals}
}
return f.goals, nil
}
// BloodPressure returns f's BloodPressure file. An error is returned if the FIT file is
// not of type blood pressure.
func (f *File) BloodPressure() (*BloodPressureFile, error) {
if !(f.FileId.Type == FileTypeBloodPressure) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeBloodPressure}
}
return f.bloodPressure, nil
}
// MonitoringA returns f's MonitoringA file. An error is returned if the FIT file is
// not of type monitoring A.
func (f *File) MonitoringA() (*MonitoringAFile, error) {
if !(f.FileId.Type == FileTypeMonitoringA) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeMonitoringA}
}
return f.monitoringA, nil
}
// ActivitySummary returns f's ActivitySummary file. An error is returned if the FIT file is
// not of type activity summary.
func (f *File) ActivitySummary() (*ActivitySummaryFile, error) {
if !(f.FileId.Type == FileTypeActivitySummary) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeActivitySummary}
}
return f.activitySummary, nil
}
// MonitoringDaily returns f's MonitoringDaily file. An error is returned if the FIT file is
// not of type monitoring daily.
func (f *File) MonitoringDaily() (*MonitoringDailyFile, error) {
if !(f.FileId.Type == FileTypeMonitoringDaily) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeMonitoringDaily}
}
return f.monitoringDaily, nil
}
// MonitoringB returns f's MonitoringB file. An error is returned if the FIT file is
// not of type monitoring B.
func (f *File) MonitoringB() (*MonitoringBFile, error) {
if !(f.FileId.Type == FileTypeMonitoringB) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeMonitoringB}
}
return f.monitoringB, nil
}
// Segment returns f's Segment file. An error is returned if the FIT file is
// not of type segment.
func (f *File) Segment() (*SegmentFile, error) {
if !(f.FileId.Type == FileTypeSegment) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeSegment}
}
return f.segment, nil
}
// SegmentList returns f's SegmentList file. An error is returned if the FIT file is
// not of type segment list.
func (f *File) SegmentList() (*SegmentListFile, error) {
if !(f.FileId.Type == FileTypeSegmentList) {
return nil, wrongFileTypeError{f.FileId.Type, FileTypeSegmentList}
}
return f.segmentList, nil
}