-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonfit.js
293 lines (250 loc) · 8.37 KB
/
jsonfit.js
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
/******
* jsonfit
* Author: Mikael Lofjärd
* License: MIT
******/
/* global define */
(function (root, factory) {
if (typeof exports === "object" && exports) {
factory(exports); // CommonJS
} else {
var jsonfit = {};
factory(jsonfit);
if (typeof define === "function" && define.amd) {
define(jsonfit); // AMD
} else {
root.jsonfit = jsonfit; // <script>
}
}
}(this, function (jsonfit) {
const scConst = (180/Math.pow(2,31)); // Used for converting to Long/Lat coordinates
/* Private methods */
function addEndian(littleEndian, bytes) {
let result = 0;
if (!littleEndian) bytes.reverse();
for(let i = 0; i < bytes.length; i++) {
result += (bytes[i] << (i << 3)) >>> 0;
}
return result;
}
/*
* blob: Binary FIT file as UInt8Array
*/
function calculateCRC(blob, start, end) {
const crc_table = [
0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
];
console.log('crcing byte ' + start + ' to ' + end);
let crc = 0;
for (let i = start; i < end; i++) {
var byte = blob[i];
var tmp = crc_table[crc & 0xF];
crc = (crc >> 4) & 0x0FFF;
crc = crc ^ tmp ^ crc_table[byte & 0xF];
tmp = crc_table[crc & 0xF];
crc = (crc >> 4) & 0x0FFF;
crc = crc ^ tmp ^ crc_table[(byte >> 4) & 0xF];
}
return crc;
}
/*
* blob: Binary FIT file as UInt8Array
*/
function readData(blob, fDef, startIndex) {
if (fDef.endianAbility === true) {
let temp = [];
for (let i = 0; i < fDef.size; i++) {
temp.push(blob[startIndex + i]);
}
let uint32Rep = addEndian(fDef.littleEndian, temp);
if (fDef.dataType == 'sint32') {
return (uint32Rep >> 0);
}
return uint32Rep;
}
return blob[startIndex];
}
function readRecord(blob, messageTypes, startIndex) {
const logMessageNo = 9999;
let recordHeader = blob[startIndex];
let localMessageType = (recordHeader & 15);
//console.log('Local Msg type: ' + localMessageType);
if ((recordHeader & 64) == 64) {
// is definition message
// startIndex + 1 is reserved
let lEnd = blob[startIndex + 2] === 0;
let mTypeDef = {
littleEndian: lEnd,
globalMessageNumber: addEndian(lEnd, [blob[startIndex+3], blob[startIndex+4]]),
numberOfFields: blob[startIndex + 5],
fieldDefs: []
};
if (mTypeDef.globalMessageNumber == logMessageNo) {
console.log('Found definition message');
console.log('GlobalMessageNumber: ' + mTypeDef.globalMessageNumber);
console.log('# fields: ' + mTypeDef.numberOfFields);
}
let message = fitMessage(mTypeDef.globalMessageNumber);
// loop over field definitions
for (let i = 0; i < mTypeDef.numberOfFields; i++) {
let fDefIndex = startIndex + 6 + (i * 3);
let baseType = blob[fDefIndex + 2];
let fDef = {
fDefNo: blob[fDefIndex],
size: blob[fDefIndex + 1],
endianAbility: (baseType & 128) == 128,
littleEndian: lEnd,
baseTypeNo: (baseType & 15),
name: message.fieldName(blob[fDefIndex]),
type: message.type(blob[fDefIndex]),
dataType: fitMessageBaseType(baseType & 15)
};
// if (mTypeDef.globalMessageNumber == logMessageNo) {
// console.log('--');
// console.log(' FDefNo: ' + fDef.fDefNo);
// console.log(' Name: ' + fDef.name);
// console.log(' Size: ' + fDef.size);
// console.log(' Base type: ' + baseType);
// console.log(' Endian ability: ' + fDef.endianAbility);
// console.log(' BT number: ' + fDef.baseTypeNo);
// console.log(' Data type: ' + fDef.dataType);
// }
mTypeDef.fieldDefs.push(fDef);
}
messageTypes[localMessageType] = mTypeDef;
return {
messageType: 'fieldDescription',
nextIndex: startIndex + 6 + (mTypeDef.numberOfFields * 3)
};
} else {
// is data message
let messageType = messageTypes[localMessageType];
// console.log('Found data message - ' + fitMessage(messageType.globalMessageNumber).name);
if ((recordHeader & 128) == 128) {
// compressed header
alert('Compressed');
return {
messageType: 'compressed',
nextIndex: 99999999999999999999999999999
};
} else {
// uncompressed header
let messageSize = 0;
let readDataFromIndex = startIndex + 1;
if (messageType.globalMessageNumber == logMessageNo) {
console.log('--');
}
let fields = {};
let message = fitMessage(messageType.globalMessageNumber);
for (let i = 0; i < messageType.fieldDefs.length; i++) {
let fDef = messageType.fieldDefs[i];
let data = readData(blob, fDef, readDataFromIndex);
if (fDef.type == 'position') data *= scConst;
if (fDef.type == 'time') data = new Date((data * 1000) + 631062000000);
console.log(message.name + ' - ' + message.fieldName(fDef.fDefNo) + ': ' + data);
let fieldName = message.fieldName(fDef.fDefNo);
if (fieldName !== 'unknown') {
fields[fieldName] = data;
}
readDataFromIndex += fDef.size;
messageSize += fDef.size;
}
let result = {
messageType: message.name,
nextIndex: startIndex + messageSize + 1
};
// if record
if (messageType.globalMessageNumber === FIT.MSGTYPE.RECORD) {
result.point = [fields.positionLong, fields.positionLat];
}
// if lap
if (messageType.globalMessageNumber === FIT.MSGTYPE.LAP) {
result.point = [fields.startPositionLong, fields.startPositionLat];
}
// if session
if (messageType.globalMessageNumber === FIT.MSGTYPE.SESSION) {
result.metadata = {
speed: fields.avgSpeed,
distance: fields.totalDistance,
time: fields.totalTimerTime,
sport: fields.sport
};
}
return result;
}
}
}
/* Public methods */
/*
* blob: Binary FIT file as UInt8Array
*/
jsonfit.parse = function(blob, cbSuccess, cbError) {
// check if file is big enough to hold a FIT header
if (blob.length < 12){
cbError('File to small to be a FIT file');
return;
}
// check for a correct header size (12 or 14 bytes)
let headerLength = blob[0];
if (headerLength != 14 && headerLength != 12) {
cbError('Incorrect header size');
return;
}
// check for '.FIT' string in header
let fileTypeString = '';
for(let i = 8; i < 11; i++) {
fileTypeString += ' - ' + String.fromCharCode(blob[i]);
}
if (fileTypeString !== '.FIT'){
cbError('Missing \'.FIT\' in header');
return;
}
// check CRCs
if (headerLength == 14) {
let crcHeader = blob[12] + (blob[13] << 8);
let crcHeaderCalc = calculateCRC(blob, 0, 12);
if (crcHeader !== crcHeaderCalc) {
cbError('Header CRC mismatch');
return;
}
}
let dataLength = blob[4] + (blob[5] << 8) + (blob[6] << 16) + (blob[7] << 24);
let crcStart = dataLength + headerLength;
let crcFile = blob[crcStart] + (blob[crcStart + 1] << 8);
let crcFileCalc = calculateCRC(blob, headerLength === 12 ? 0 : headerLength, crcStart);
if(crcFile !== crcFileCalc) {
cbError('File CRC mismatch');
return;
}
let fitAsJson = {};
let sessions = [];
let laps = [];
let records = [];
let events = [];
let loopIndex = headerLength;
let messageTypes = [];
while (loopIndex < crcStart) {
let {nextIndex, messageType, message} = readRecord(blob, messageTypes, loopIndex);
loopIndex = nextIndex;
if (messageType == 'record') {
records.push(message);
}
else if (messageType == 'lap') {
laps.push(message);
}
else if (messageType === 'session') {
sessions.push(message);
}
else if (messageType === 'event') {
events.push(message);
}
}
fitAsJson.sessions = sessions;
/* TODO: group by timestamp */
fitAsJson.laps = laps;
fitAsJson.records = records;
fitAsJson.events = events;
cbSuccess(fitAsJson);
};
}));