-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathfile.go
326 lines (292 loc) · 9.29 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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
// Developer data fields.
fieldDescriptionMsgs []*FieldDescriptionMsg
developerDataIdMsgs []*DeveloperDataIdMsg
// 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)
}
// NewFile creates a new File of the given type.
func NewFile(t FileType, h Header) (*File, error) {
f := new(File)
f.FileId.Type = t
f.Header = h
if err := f.init(); err != nil {
return nil, fmt.Errorf("error creating file: %w", err)
}
return f, nil
}
func (f *File) add(msg reflect.Value) {
x := msg.Interface()
switch tmp := x.(type) {
case FileIdMsg:
f.FileId = tmp
case FileCreatorMsg:
f.FileCreator = &tmp
case TimestampCorrelationMsg:
f.TimestampCorrelation = &tmp
case FieldDescriptionMsg:
f.fieldDescriptionMsgs = append(f.fieldDescriptionMsgs, &tmp)
case DeveloperDataIdMsg:
f.developerDataIdMsgs = append(f.developerDataIdMsgs, &tmp)
default:
f.msgAdder.add(msg)
}
}
func (f *File) init() error {
t := f.Type()
switch t {
case FileTypeActivity:
f.activity = new(ActivityFile)
f.msgAdder = f.activity
case FileTypeDevice:
f.device = new(DeviceFile)
f.msgAdder = f.device
case FileTypeSettings:
f.settings = new(SettingsFile)
f.msgAdder = f.settings
case FileTypeSport:
f.sport = new(SportFile)
f.msgAdder = f.sport
case FileTypeWorkout:
f.workout = new(WorkoutFile)
f.msgAdder = f.workout
case FileTypeCourse:
f.course = new(CourseFile)
f.msgAdder = f.course
case FileTypeSchedules:
f.schedules = new(SchedulesFile)
f.msgAdder = f.schedules
case FileTypeWeight:
f.weight = new(WeightFile)
f.msgAdder = f.weight
case FileTypeTotals:
f.totals = new(TotalsFile)
f.msgAdder = f.totals
case FileTypeGoals:
f.goals = new(GoalsFile)
f.msgAdder = f.goals
case FileTypeBloodPressure:
f.bloodPressure = new(BloodPressureFile)
f.msgAdder = f.bloodPressure
case FileTypeMonitoringA:
f.monitoringA = new(MonitoringAFile)
f.msgAdder = f.monitoringA
case FileTypeActivitySummary:
f.activitySummary = new(ActivitySummaryFile)
f.msgAdder = f.activitySummary
case FileTypeMonitoringDaily:
f.monitoringDaily = new(MonitoringDailyFile)
f.msgAdder = f.monitoringDaily
case FileTypeMonitoringB:
f.monitoringB = new(MonitoringBFile)
f.msgAdder = f.monitoringB
case FileTypeSegment:
f.segment = new(SegmentFile)
f.msgAdder = f.segment
case FileTypeSegmentList:
f.segmentList = new(SegmentListFile)
f.msgAdder = f.segmentList
case FileTypeInvalid:
return FormatError("file type was set invalid")
default:
switch {
case t > FileTypeMonitoringB && t < FileTypeMfgRangeMin:
return FormatError(
fmt.Sprintf("unknown file type: %v", t),
)
case t >= FileTypeMfgRangeMin && t <= FileTypeMfgRangeMax:
return NotSupportedError("manufacturer specific file types")
default:
return FormatError(
fmt.Sprintf("unknown file type: %v", t),
)
}
}
return nil
}
// 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
}