-
Notifications
You must be signed in to change notification settings - Fork 1
/
fit.go
1398 lines (1305 loc) · 47 KB
/
fit.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package fit
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/jezard/fit/maps"
"log"
"math"
"os"
"time"
)
type File_id struct { //message number: 0
Serial_number uint32
Time_created int64
Manufacturer string
Product string
Number uint16
File_type byte
}
type File_creator struct { //message number: 1
Software_version float64
Hardware_version uint8
}
type Device_info struct { //message number: 23
Timestamp int64
Serial_number uint32
Cum_operating_time uint32
Manufacturer string
Product string
Software_version float64
Battery_voltage float64
Device_index uint8
Device_type string
Hardware_version uint8
Battery_status uint8
Source_type string
Ant_network string
}
type Event struct { //message number: 21
Timestamp int64
Time_trigger string
Event string
Event_type string
}
type Record struct { //message number: 20
Timestamp int64
Position_lat float64
Position_long float64
Distance float64
Time_from_course int32
Compressed_speed_distance uint8
Heart_rate uint8
Altitude float64
Speed float64
Power uint16
Grade int16
Cadence uint8
Registance uint8
Cycle_length uint8
Temperature int8
}
type Lap struct { //message number: 19
Timestamp int64
Start_time int64
Start_position_lat float64
Start_position_long float64
End_position_lat float64
End_position_long float64
Total_elapsed_time float64
Total_timer_time float64
Total_distance float64
Total_cycles uint32
Total_work uint32
Message_index uint16
Total_calories uint16
Total_fat_calories uint16
Avg_speed float64
Max_speed float64
Avg_power uint16
Max_power uint16
Norm_power uint16
Left_right_balance_100 float64
Total_ascent uint16
Total_descent uint16
Avg_heart_rate uint8
Max_heart_rate uint8
Avg_cadence uint8
Max_cadence uint8
Event_group uint8
Event string
Event_type string
Intensity uint8
}
type Session struct { //message number 18
Timestamp int64
Start_time int64
Start_position_lat float64
Start_position_long float64
Total_elapsed_time float64
Total_timer_time float64
Total_distance float64
Total_work uint32
Total_cycles uint32
Nec_lat float64
Nec_long float64
Swc_lat float64
Swc_long float64
Message_index uint16
Total_calories uint16
Total_fat_calories uint16
Avg_speed float64
Max_speed float64
Avg_power uint16
Max_power uint16
Total_ascent uint16
Total_descent uint16
Avg_heart_rate uint8
Max_heart_rate uint8
Avg_cadence uint8
Max_cadence uint8
Event string
Event_type string
Sport string
Sub_sport string
First_lap_index uint16
Num_laps uint16
Session_trigger string
Norm_power uint16
Training_stress_score float64
Intensity_factor float64
Left_right_balance_100 float64
Threshold_power uint16
Avg_left_torque_effectiveness float32
Avg_right_torque_effectiveness float32
Avg_left_pedal_smoothness float32
Avg_right_pedal_smoothness float32
Avg_combined_pedal_smoothness float32
Time_standing float64
Stand_count uint16
Total_training_effect float64
/* That's all for now folks */
}
type Activity struct { //message number 34
Timestamp int64
Num_sessions uint16
Total_timer_time int64
Local_timestamp int64
Event string
Event_type string
Event_group uint8
Type string
}
type FitFile struct {
FileId File_id
FileCreator File_creator
DeviceInfo []Device_info
Events []Event
Records []Record
Laps []Lap
Sessions []Session
Activity Activity
}
var crc uint16
var count int
var section string //verbose_mode - flag for record type
var verbose_mode bool
func Parse(filename string, show_verbose_mode bool) FitFile {
verbose_mode = show_verbose_mode
if verbose_mode {
fmt.Printf("FUNCTION Parse() called: %v\n", time.Now())
} //verbose_mode
const FIT_HDR_TYPE_MASK uint8 = 0x0F
crc = 0 //reset CRC
//data structures
var fitFile FitFile
f, err := os.Open(filename)
check(err)
fi, err := f.Stat() //file info
check(err)
b1 := make([]byte, 14) //for header (14 may need to be made a dynamic value)
n1, err := f.Read(b1)
check(err)
fl := fi.Size()
b2 := make([]byte, 2) //for crc
n2, err := f.ReadAt(b2, fl-2) //last 2 bytes of file
if verbose_mode {
fmt.Printf("%d (Header) %d (crc) %d (file size) bytes\n", n1, n2, fl)
} //verbose_mode
/*****
*
* Get the Fit file header information
*
*****/
headerSize, _ := binary.Uvarint(b1[0:1]) //convert 1 byte to uint64
//Indicates the length of this file header including header size. Minimum size is 12. This may be increased in future to add additional optional information.
if verbose_mode {
fmt.Printf("Header Size: %v\n", headerSize)
} //verbose_mode
//Protocol version number as provided in SDK
if verbose_mode {
fmt.Printf("protocol v.: %v\n", b1[1:2])
} //verbose_mode
//Profile version number as provided in SDK
pv := binary.LittleEndian.Uint16(b1[2:4])
if verbose_mode {
fmt.Printf("profile v. : %v\n", pv)
} //verbose_mode
//Length of the data records section in bytes (not including Header or CRC)
dataSize := binary.LittleEndian.Uint32(b1[4:8])
if verbose_mode {
fmt.Printf("Data Size : %v bytes\n", dataSize)
} //verbose_mode
//ASCII values for “.FIT”. A FIT binary file opened with a text editor will contain a readable “.FIT” in the first line.
if verbose_mode {
fmt.Printf("Ascii : %s%s%s%s\n", b1[8:9], b1[9:10], b1[10:11], b1[11:12])
} //verbose_mode
/*****
*
* Get the next CRCs
*
*****/
if headerSize > 12 {
_crc := binary.LittleEndian.Uint16(b1[12:14]) //try to get the CRC from the header
if _crc == 0x0000 {
_crc = binary.LittleEndian.Uint16(b2[0:2]) //otherwise calcuate it from the final 2 bytes
}
if verbose_mode {
fmt.Printf("Expected CRC: %x\n", _crc)
} //verbose_mode
} else {
_crc := binary.LittleEndian.Uint16(b2[0:2])
if verbose_mode {
fmt.Printf("Expected CRC: %x\n", _crc)
} //verbose_mode
}
//close the file
f.Close()
//RE-READ the file to get the correct calculated CRC (the contents of the file excluding the CRCs final 2 bytes)
nf, err := os.Open(filename)
check(err)
b3 := make([]byte, fl-2) //file length - 2 byte final crc
nf.Read(b3)
for i := 0; i < len(b3); i++ {
calc_crc(b3[i])
}
nf.Close()
if verbose_mode {
fmt.Printf("Calculated CRC: %x \n", crc)
} //verbose_mode
/*****
*
* Get the next bits (file records!!!)
*
*****/
r, err := os.Open(filename)
check(err)
defer r.Close()
//test to read first record header
rHead := make([]byte, 1)
type Field_def struct {
field_definition_number int
size int
base_type int
offset uint64 //length of data (calculated using field def size) within data record preceding the data field
}
type Def_message struct {
arch int
global_message_number uint16
number_of_fields uint64
field_defs []Field_def
}
//map definition info to a local message type and global message number !important
definition := make(map[uint64]Def_message)
var def_message Def_message
var localMsgType byte
var glob_msge_num_0_read bool //flag when first global message num = 0 read as any that follow are probably errors
var k uint64
for k = 0; k < uint64(dataSize); k++ { //loop through file byte by byte
var rc_length uint64
r.ReadAt(rHead, int64(headerSize+k))
//see section 4.3 of SDK referring to definition header/record content,
//4.21 describes fixed content of first 5 bytes, followed by variable number of field definitions @ 3 bytes/field
//Is definition message? message type is bit 6
if rHead[0]>>6 == 1 { //01000000 -> 01
section = "DEFINITION"
localMsgType = rHead[0] & 0x1f
//get record content 4.2.1 of fit SDK
//skip arch for now!
arch := make([]byte, 1)
r.ReadAt(arch, int64(headerSize+k+1))
def_message.arch = int(arch[0])
//Global Message Number
gmn := make([]byte, 2)
r.ReadAt(gmn, int64(headerSize+k+3))
def_message.global_message_number = binary.LittleEndian.Uint16(gmn[0:2])
//number of fields
nof := make([]byte, 1)
r.ReadAt(nof, int64(headerSize+k+5))
def_message.number_of_fields = uint64(nof[0])
//THIS verbose_mode INFO SEEMS PRETTY ACCURATE - I'M GETTING CONFUSED WITH GLOBAL AND LOCAL MESSAGE TYPES...
if verbose_mode {
fmt.Printf("\n[POS: %8d] ", uint64(dataSize)-k-1) //verbose_mode
fmt.Print("DEFINITION MESSAGE HEADER, ") //verbose_mode
fmt.Printf("VAL: %b", rHead[0]) //verbose_mode
fmt.Printf(" LOCAL MESSAGE TYPE: %d (%s)", localMsgType, maps.Global_message_type(def_message.global_message_number)) //verbose_mode
fmt.Printf(" GLOB MESSAGE NUM: %d", def_message.global_message_number) //verbose_mode
fmt.Printf(" FIELDS: %d", def_message.number_of_fields) //verbose_mode
}
//field definitions
var f uint64
const DEF_MSG_RECORD_HEADER_SIZE = 1 //bytes
const DEF_MSG_FIXED_CONTENT_SIZE = 5 //bytes
const DEF_MSG_FIELD_DEF_SIZE = 3 //bytes
var cumulative_size uint64
//loop through each of the fields defs
for f = 0; f < def_message.number_of_fields; f++ {
var def_contents Field_def
var r_offset uint64 //byte offset for file reader
// 1 byte field definition number. 4.2.1.4.1 in FIT SDK
fdn := make([]byte, 1)
r_offset = 0
r.ReadAt(fdn, int64(headerSize+k+DEF_MSG_RECORD_HEADER_SIZE+DEF_MSG_FIXED_CONTENT_SIZE+(DEF_MSG_FIELD_DEF_SIZE*f)+r_offset))
//...size
size := make([]byte, 1)
r_offset = 1
r.ReadAt(size, int64(headerSize+k+DEF_MSG_RECORD_HEADER_SIZE+DEF_MSG_FIXED_CONTENT_SIZE+(DEF_MSG_FIELD_DEF_SIZE*f)+r_offset))
//...base type
baseType := make([]byte, 1)
r_offset = 2
r.ReadAt(baseType, int64(headerSize+k+DEF_MSG_RECORD_HEADER_SIZE+DEF_MSG_FIXED_CONTENT_SIZE+(DEF_MSG_FIELD_DEF_SIZE*f)+r_offset))
//cumulative size for calulating data field offset
cumulative_size += uint64(size[0])
//def_contents.field_definition_number - for an activity file, see 9.1 FIT Messages in FIT File Types Desription
def_contents.field_definition_number = int(fdn[0])
def_contents.size = int(size[0])
def_contents.base_type = int(baseType[0])
def_contents.offset = cumulative_size - uint64(size[0]) //start, not end of field data
if verbose_mode {
fmt.Printf("\n\tFIELD DEF NUMBER: %v\n\tSIZE: %v\n\tBASE_TYPE: %v\n\tOFFSET %v\n",
def_contents.field_definition_number,
def_contents.size,
def_contents.base_type,
def_contents.offset)
} //verbose_mode
//we will need a means of temporarily storing the all the fields definition data so that it can be used to retrieve the record data later
def_message.field_defs = append(def_message.field_defs, def_contents)
}
//store field definitions against local message type
definition[uint64(localMsgType)] = def_message //of course this gets overwritten if localMsgType has been used before
if verbose_mode {
fmt.Printf("\t---------------\n\t%d BYTES \n", cumulative_size)
} //verbose_mode
rc_length = uint64(5 + uint64(nof[0])*3) //combined length of fixed and varible record content field
k = skip(k, rc_length) //move the pointer to the end of the field definition
continue
} else { //10000000 (Data Message)
section = "DATA"
var compHeader bool
def_message.field_defs = nil
//check for compressed header
if rHead[0]>>7 == 1 { //is compressed header
compHeader = true
}
//set vars dependant on header type
if compHeader {
//timeOffset := rHead[0]&0x1F, //time offset 0-4
localMsgType = rHead[0] & 0x60 >> 5 //LMT is bits 5-6
} else {
localMsgType = rHead[0] & 0x1f //LMT is bits 0-3
}
if verbose_mode {
fmt.Printf("\n[POS: %8d] DATA MESSAGE HEADER, VAL: %8b LOCAL MESSAGE TYPE: %d GLOB MESSAGE NUMBER: %d", uint64(dataSize)-k-1, rHead[0], localMsgType, definition[uint64(localMsgType)].global_message_number)
//process data
fmt.Println("\n")
} //verbose_mode
global_message_number := definition[uint64(localMsgType)].global_message_number
//Here's where we extract the data from the .fit activity file and add it to our FitFile data structure
switch global_message_number { //look up the global message number using the local message type
case 0: //file_id
var sumRecordsDataSize int
for _, val := range definition[uint64(localMsgType)].field_defs {
sumRecordsDataSize += val.size
if !glob_msge_num_0_read {
v := make([]byte, val.size)
r.ReadAt(v, int64(headerSize+k+val.offset+1))
switch val.field_definition_number {
case 0:
fitFile.FileId.File_type = v[0]
if fitFile.FileId.File_type != 4 {
log.Fatal("ERROR: This is not an activity type file")
}
if verbose_mode {
fmt.Printf("\tFILE TYPE: %d\n", v[0])
} //verbose_mode
break
case 1:
fitFile.FileId.Manufacturer = maps.Manufacturer(uint64(binary.LittleEndian.Uint16(v[0:val.size])))
if verbose_mode {
fmt.Printf("\tMANUFACTURER: %s\n", fitFile.FileId.Manufacturer)
} //verbose_mode
break
case 2:
fitFile.FileId.Product = maps.Product(uint64(binary.LittleEndian.Uint16(v[0:val.size])))
if verbose_mode {
fmt.Printf("\tPRODUCT: %s\n", fitFile.FileId.Product)
} //verbose_mode
break
case 3:
fitFile.FileId.Serial_number = binary.LittleEndian.Uint32(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSERIAL NUMBER: %d\n", binary.LittleEndian.Uint32(v[0:val.size]))
} //verbose_mode
break
case 4:
fitFile.FileId.Time_created = int64(binary.LittleEndian.Uint32(v[0:val.size])) + 631065600 //need to add on unix timestamp for 31/12/1989 to get up to correct date (We can still get up to 2038)
t := time.Unix(fitFile.FileId.Time_created, 0)
if verbose_mode {
fmt.Printf("\tTIME CREATED: %d (rectified) %v\n", fitFile.FileId.Time_created, t)
} //verbose_mode
break
case 5:
fitFile.FileId.Number = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tNUMBER: %d\n", binary.LittleEndian.Uint16(v[0:val.size]))
} //verbose_mode
}
}
}
glob_msge_num_0_read = true
//def_message.field_defs = nil
k = skip(k, uint64(sumRecordsDataSize)) //move the reader to the end of the record data
break
case 34: //Activity
var activity Activity
var sumRecordsDataSize int
for _, val := range definition[uint64(localMsgType)].field_defs {
sumRecordsDataSize += val.size
v := make([]byte, val.size)
r.ReadAt(v, int64(headerSize+k+val.offset+1))
switch val.field_definition_number {
case 253:
activity.Timestamp = int64(binary.LittleEndian.Uint32(v[0:val.size])) + 631065600 //need to add on unix timestamp for 31/12/1989 to get up to correct date (We can still get up to 2038)
t := time.Unix(activity.Timestamp, 0)
if verbose_mode {
fmt.Printf("\tACTIVITY TIMESTAMP: %d (rectified) %v\n", activity.Timestamp, t)
} //verbose_mode
break
case 1:
activity.Num_sessions = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tACTIVITY NUM SESSIONS: %d\n", activity.Num_sessions)
} //verbose_mode
case 2:
temp, _ := binary.Uvarint(v[0:1])
activity.Type = maps.Activity(uint64(temp))
if verbose_mode {
fmt.Printf("\tACTIVITY TYPE: %s\n", activity.Type)
} //verbose_mode
break
case 3:
temp, _ := binary.Uvarint(v[0:1])
activity.Event = maps.Event(uint64(temp))
if verbose_mode {
fmt.Printf("\tACTIVITY EVENT: %s\n", activity.Event)
} //verbose_mode
break
case 4:
temp, _ := binary.Uvarint(v[0:1])
activity.Event_type = maps.Event_type(uint64(temp))
if verbose_mode {
fmt.Printf("\tACTIVITY EVENT TYPE: %s\n", activity.Event_type)
} //verbose_mode
break
case 5:
activity.Local_timestamp = int64(binary.LittleEndian.Uint32(v[0:val.size])) + 631065600 //need to add on unix timestamp for 31/12/1989 to get up to correct date (We can still get up to 2038)
t := time.Unix(activity.Local_timestamp, 0)
if verbose_mode {
fmt.Printf("\tACTIVITY LOCAL TIMESTAMP: %d (rectified) %v\n", activity.Local_timestamp, t)
} //verbose_mode
break
}
}
//def_message.field_defs = nil
k = skip(k, uint64(sumRecordsDataSize))
break
case 18: //session
var session Session
var sumRecordsDataSize int
for _, val := range definition[uint64(localMsgType)].field_defs {
sumRecordsDataSize += val.size
v := make([]byte, val.size)
r.ReadAt(v, int64(headerSize+k+val.offset+1))
switch val.field_definition_number {
case 253:
session.Timestamp = int64(binary.LittleEndian.Uint32(v[0:val.size])) + 631065600 //need to add on unix timestamp for 31/12/1989 to get up to correct date (We can still get up to 2038)
t := time.Unix(session.Timestamp, 0)
if verbose_mode {
fmt.Printf("\tSESSION TIMESTAMP: %d (rectified) %v\n", session.Timestamp, t)
} //verbose_mode
break
case 0:
temp, _ := binary.Uvarint(v[0:1])
session.Event = maps.Event(uint64(temp))
if verbose_mode {
fmt.Printf("\tSESSION EVENT: %s\n", session.Event)
} //verbose_mode
break
case 1:
temp, _ := binary.Uvarint(v[0:1])
session.Event_type = maps.Event_type(uint64(temp))
if verbose_mode {
fmt.Printf("\tSESSION EVENT TYPE: %s\n", session.Event_type)
} //verbose_mode
break
case 2:
session.Start_time = int64(binary.LittleEndian.Uint32(v[0:val.size])) + 631065600 //need to add on unix timestamp for 31/12/1989 to get up to correct date (We can still get up to 2038)
t := time.Unix(session.Start_time, 0)
if verbose_mode {
fmt.Printf("\tSESSION START TIME: %d (rectified) %v\n", session.Start_time, t)
} //verbose_mode
break
case 3:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
session.Start_position_lat = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tSESSION START LAT: %f°\n", session.Start_position_lat)
} //verbose_mode
break
case 4:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
session.Start_position_long = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tSESSION START LON: %f°\n", session.Start_position_long)
} //verbose_mode
break
case 5:
temp, _ := binary.Uvarint(v[0:1])
session.Sport = maps.Sport(uint64(temp))
if verbose_mode {
fmt.Printf("\tSESSION SPORT: %s\n", session.Sport)
} //verbose_mode
break
case 6:
temp, _ := binary.Uvarint(v[0:1])
session.Sub_sport = maps.Sub_sport(uint64(temp))
if verbose_mode {
fmt.Printf("\tSESSION SUB SPORT: %s\n", session.Sub_sport)
} //verbose_mode
break
case 7:
session.Total_elapsed_time = float64(binary.LittleEndian.Uint32(v[0:val.size])) / 1000
if verbose_mode {
fmt.Printf("\tSESSION TOTAL ELAPSED TIME: %fs\n", session.Total_elapsed_time)
} //verbose_mode
break
case 8:
session.Total_timer_time = float64(binary.LittleEndian.Uint32(v[0:val.size])) / 1000
if verbose_mode {
fmt.Printf("\tSESSION TOTAL TIMER TIME: %fs\n", session.Total_timer_time)
} //verbose_mode
break
case 9:
session.Total_distance = float64(binary.LittleEndian.Uint32(v[0:val.size])) / 100
if verbose_mode {
fmt.Printf("\tSESSION TOTAL DISTANCE: %f M\n", session.Total_distance)
} //verbose_mode
break
case 10:
session.Total_cycles = binary.LittleEndian.Uint32(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION TOTAL CYCLES: %d Cycles\n", session.Total_cycles)
} //verbose_mode
break
case 11:
session.Total_calories = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION TOTAL CALORIES: %d Kcal\n", session.Total_calories)
} //verbose_mode
break
case 13:
session.Total_fat_calories = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION TOTAL FAT CALORIES: %d Kcal\n", session.Total_fat_calories)
} //verbose_mode
break
case 14:
session.Avg_speed = float64(binary.LittleEndian.Uint16(v[0:val.size])) / 1000
if verbose_mode {
fmt.Printf("\tSESSION AVERAGE SPEED: %f M/S\n", session.Avg_speed)
} //verbose_mode
break
case 15:
session.Max_speed = float64(binary.LittleEndian.Uint16(v[0:val.size])) / 1000
if verbose_mode {
fmt.Printf("\tSESSION MAX SPEED: %f M/S\n", session.Max_speed)
} //verbose_mode
break
case 16:
temp, _ := binary.Uvarint(v[0:1])
session.Avg_heart_rate = uint8(temp)
if verbose_mode {
fmt.Printf("\tSESSION AVERAGE HEART RATE: %d BPM\n", session.Avg_heart_rate)
} //verbose_mode
break
case 17:
temp, _ := binary.Uvarint(v[0:1])
session.Max_heart_rate = uint8(temp)
if verbose_mode {
fmt.Printf("\tSESSION MAX HEART RATE: %d BPM\n", session.Max_heart_rate)
} //verbose_mode
break
case 18:
temp, _ := binary.Uvarint(v[0:1])
session.Avg_cadence = uint8(temp)
if verbose_mode {
fmt.Printf("\tSESSION AVERAGE CADENCE: %d RPM\n", session.Avg_cadence)
} //verbose_mode
break
case 19:
temp, _ := binary.Uvarint(v[0:1])
session.Max_cadence = uint8(temp)
if verbose_mode {
fmt.Printf("\tSESSION MAX CADENCE: %d RPM\n", session.Max_cadence)
} //verbose_mode
break
case 20:
session.Avg_power = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION AVERAGE POWER %d W\n", session.Avg_power)
} //verbose_mode
break
case 21:
session.Max_power = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION MAX POWER %d W\n", session.Max_power)
} //verbose_mode
break
case 22:
session.Total_ascent = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION TOTAL ASCENT %d M\n", session.Total_ascent)
} //verbose_mode
break
case 23:
session.Total_descent = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION TOTAL DESCENT %d M\n", session.Total_descent)
} //verbose_mode
break
case 24:
temp, _ := binary.Uvarint(v[0:1])
session.Total_training_effect = float64(temp)
if verbose_mode {
fmt.Printf("\tSESSION TOTAL TRAINING EFFECT %1.2f\n", session.Total_training_effect)
} //verbose_mode
break
case 48:
session.Total_work = binary.LittleEndian.Uint32(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION TOTAL WORK: %d J\n", session.Total_work)
} //verbose_mode
break
case 254:
session.Message_index = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION MESSAGE INDEX: %d\n", session.Message_index)
} //verbose_mode
break
case 25:
session.First_lap_index = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION FIRST LAP INDEX: %d\n", session.First_lap_index)
} //verbose_mode
break
case 26:
session.Num_laps = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION NUM LAPS: %d\n", session.Num_laps)
} //verbose_mode
break
case 27:
break
case 28:
temp, _ := binary.Uvarint(v[0:1])
session.Session_trigger = maps.Session_trigger(uint64(temp))
if verbose_mode {
fmt.Printf("\tSESSION TRIGGER: %s \n", session.Session_trigger)
} //verbose_mode
break
case 29:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
session.Nec_lat = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tSESSION NEC LAT: %f°\n", session.Nec_lat)
} //verbose_mode
break
case 30:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
session.Nec_long = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tSESSION NEC LON: %f°\n", session.Nec_long)
} //verbose_mode
break
case 31:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
session.Swc_lat = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tSESSION SWC LAT: %f°\n", session.Swc_lat)
} //verbose_mode
break
case 32:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
session.Swc_long = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tSESSION SWC LON: %f°\n", session.Swc_long)
} //verbose_mode
break
case 34:
session.Norm_power = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION NORMALIZED POWER %d W\n", session.Norm_power)
} //verbose_mode
break
case 35:
session.Training_stress_score = float64(binary.LittleEndian.Uint16(v[0:val.size])) / 10
if verbose_mode {
fmt.Printf("\tSESSION TRAINING STRESS SCORE %1.2f TSS\n", session.Training_stress_score)
} //verbose_mode
break
case 36:
session.Intensity_factor = float64(binary.LittleEndian.Uint16(v[0:val.size])) / 1000
if verbose_mode {
fmt.Printf("\tSESSION INTENSITY FACTOR %1.2f IF\n", session.Intensity_factor)
} //verbose_mode
break
case 37:
session.Left_right_balance_100 = (float64(binary.LittleEndian.Uint16(v[0:val.size])) / 65535) * 100
if verbose_mode {
fmt.Printf("\tSESSION LEFT RIGHT BALANCE %1.2f Percent (0 = left, 50 = center, 100 = right) \n", session.Left_right_balance_100) //needs verifing!
} //verbose_mode
break
//37 - 44 are for swimmers
case 45:
session.Threshold_power = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION THRESHOLD POWER: %d W\n", session.Threshold_power)
} //verbose_mode
break
case 101:
temp, _ := binary.Uvarint(v[0:1])
session.Avg_left_torque_effectiveness = float32(temp) / 2
if verbose_mode {
fmt.Printf("\tSESSION AVG LEFT TORQUE EFFECTIVENESS: %1.2f percent\n", session.Avg_left_torque_effectiveness)
} //verbose_mode
break
case 102:
temp, _ := binary.Uvarint(v[0:1])
session.Avg_right_torque_effectiveness = float32(temp) / 2
if verbose_mode {
fmt.Printf("\tSESSION AVG RIGHT TORQUE EFFECTIVENESS: %1.2f percent\n", session.Avg_right_torque_effectiveness)
} //verbose_mode
break
case 103:
temp, _ := binary.Uvarint(v[0:1])
session.Avg_left_pedal_smoothness = float32(temp) / 2
if verbose_mode {
fmt.Printf("\tSESSION AVG LEFT PEDAL SMOOTHNESS: %1.2f percent\n", session.Avg_left_pedal_smoothness)
} //verbose_mode
break
case 104:
temp, _ := binary.Uvarint(v[0:1])
session.Avg_right_pedal_smoothness = float32(temp) / 2
if verbose_mode {
fmt.Printf("\tSESSION AVG RIGHT PEDAL SMOOTHNESS: %1.2f percent\n", session.Avg_right_pedal_smoothness)
} //verbose_mode
break
case 105:
temp, _ := binary.Uvarint(v[0:1])
session.Avg_combined_pedal_smoothness = float32(temp) / 2
if verbose_mode {
fmt.Printf("\tSESSION AVG COMBINED PEDAL SMOOTHNESS: %1.2f percent\n", session.Avg_combined_pedal_smoothness)
} //verbose_mode
break
case 111:
break
case 112:
session.Time_standing = float64(binary.LittleEndian.Uint32(v[0:val.size])) / 1000
if verbose_mode {
fmt.Printf("\tSESSION TIME STANDING: %1.2f s\n", session.Time_standing)
} //verbose_mode
break
case 113:
session.Stand_count = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tSESSION STAND COUNT: %d\n", session.Stand_count)
} //verbose_mode
break
case 114:
break
case 115:
break
case 116:
break
case 117:
break
case 118:
break
case 119:
break
case 120:
break
case 121:
break
case 122:
break
case 123:
break
}
/* That's all for now folks */
}
fitFile.Sessions = append(fitFile.Sessions, session)
k = skip(k, uint64(sumRecordsDataSize))
break
case 19: //lap
var lap Lap
var sumRecordsDataSize int
for _, val := range definition[uint64(localMsgType)].field_defs {
sumRecordsDataSize += val.size
v := make([]byte, val.size)
r.ReadAt(v, int64(headerSize+k+val.offset+1))
switch val.field_definition_number {
case 253:
lap.Timestamp = int64(binary.LittleEndian.Uint32(v[0:val.size])) + 631065600 //need to add on unix timestamp for 31/12/1989 to get up to correct date (We can still get up to 2038)
t := time.Unix(lap.Timestamp, 0)
if verbose_mode {
fmt.Printf("\tLAP TIMESTAMP: %d (rectified) %v\n", lap.Timestamp, t)
} //verbose_mode
break
case 2:
lap.Start_time = int64(binary.LittleEndian.Uint32(v[0:val.size])) + 631065600 //need to add on unix timestamp for 31/12/1989 to get up to correct date (We can still get up to 2038)
t := time.Unix(lap.Start_time, 0)
if verbose_mode {
fmt.Printf("\tLAP START TIME: %d (rectified) %v\n", lap.Start_time, t)
} //verbose_mode
break
case 3:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
lap.Start_position_lat = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tLAP START LAT: %f°\n", lap.Start_position_lat)
} //verbose_mode
break
case 4:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
lap.Start_position_long = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tLAP START LON: %f°\n", lap.Start_position_long)
} //verbose_mode
break
case 5:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
lap.End_position_lat = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tLAP END LAT: %f°\n", lap.End_position_lat)
} //verbose_mode
break
case 6:
semicircles := float64(read_int32(v[0:val.size])) //convert from semicircles to degrees
lap.End_position_long = semicircles_to_degrees(semicircles)
if verbose_mode {
fmt.Printf("\tLAP END LON: %f°\n", lap.End_position_long)
} //verbose_mode
break
case 7:
lap.Total_elapsed_time = float64(binary.LittleEndian.Uint32(v[0:val.size])) / 1000
if verbose_mode {
fmt.Printf("\tLAP TOTAL ELAPSED TIME: %fs\n", lap.Total_elapsed_time)
} //verbose_mode
break
case 8:
lap.Total_timer_time = float64(binary.LittleEndian.Uint32(v[0:val.size])) / 1000
if verbose_mode {
fmt.Printf("\tLAP TOTAL TIMER TIME: %fs\n", lap.Total_timer_time)
} //verbose_mode
break
case 9:
lap.Total_distance = float64(binary.LittleEndian.Uint32(v[0:val.size])) / 100
if verbose_mode {
fmt.Printf("\tLAP TOTAL DISTANCE: %f M\n", lap.Total_distance)
} //verbose_mode
break
case 10:
lap.Total_cycles = binary.LittleEndian.Uint32(v[0:val.size])
if verbose_mode {
fmt.Printf("\tLAP TOTAL CYCLES: %d Cycles\n", lap.Total_cycles)
} //verbose_mode
break
case 41:
lap.Total_work = binary.LittleEndian.Uint32(v[0:val.size])
if verbose_mode {
fmt.Printf("\tLAP TOTAL WORK: %d J\n", lap.Total_work)
} //verbose_mode
break
case 254:
lap.Message_index = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tLAP MESSAGE INDEX: %d\n", lap.Message_index)
} //verbose_mode
break
case 11:
lap.Total_calories = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tLAP TOTAL CALORIES: %d Kcal\n", lap.Total_calories)
} //verbose_mode
break
case 12:
lap.Total_fat_calories = binary.LittleEndian.Uint16(v[0:val.size])
if verbose_mode {
fmt.Printf("\tLAP TOTAL FAT CALORIES: %d Kcal\n", lap.Total_fat_calories)
} //verbose_mode
break
case 13:
lap.Avg_speed = float64(binary.LittleEndian.Uint16(v[0:val.size])) / 1000
if verbose_mode {