You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Decoder objects are created from Streams representing the binary FIT file data to be decoded. See [Creating Streams](#creatingstreams) for more information on constructing Stream objects.
@@ -37,24 +36,24 @@ Once a Decoder object is created it can be used to check that the Stream is a FI
37
36
38
37
### isFIT Method
39
38
40
-
All valid FIT files should include a 12 or 14 byte file header. The 14 byte header is the preferred header size and the most common size used. Bytes 8–11 of the header contain the ASCII values ".FIT”. This string can easily be spotted when opening a binary FIT file in a text or hex editor.
39
+
All valid FIT files should include a 12 or 14 byte file header. The 14 byte header is the preferred header size and the most common size used. Bytes 8–11 of the header contain the ASCII values ".FIT". This string can easily be spotted when opening a binary FIT file in a text or hex editor.
The isFIT method reads the file header and returns true if bytes 8–11 are equal to the ACSII values ".FIT”. isFIT provides a quick way to check that the file is a FIT file before attempting to decode the file.
48
+
The isFIT method reads the file header and returns trueif bytes 8–11 are equal to the ACSII values ".FIT". isFIT provides a quick way to check that the file is a FIT file before attempting to decode the file.
50
49
51
50
The Decoder class includes a static and instance version of the isFIT method.
52
51
53
52
### Check Integrity Method
54
53
55
54
The checkIntegrity method performs three checks on a FIT file:
56
55
57
-
1. Checks that bytes 8–11 of the header contain the ASCII values ".FIT”.
56
+
1. Checks that bytes 8–11 of the header contain the ASCII values ".FIT".
58
57
2. Checks that the total file size is equal to Header Size + Data Size + CRC Size.
59
58
3. Reads the contents of the file, computes the CRC, and then checks that the computed CRC matches the file CRC.
Optional callback functionthat can be used to inspect or manipulate messages after they are fully decoded and all the options have been applied. The message is mutable and we be returned from the Read method in the messages dictionary.
84
83
85
84
Example mesgListener callback that tracks the field names across all Record messages.
86
-
87
85
````js
88
86
const recordFields = new Set();
89
87
@@ -221,3 +219,65 @@ A convince method for converting FIT Epoch values to JavaScript Date objects.
// The message data should match the format returned by
235
+
// the Decoder. Field names should be camelCase. The fields
236
+
// definitions can be found in the Profile.
237
+
//
238
+
239
+
// Pass the MesgNum and message data as separate parameters to the onMesg() method
240
+
encoder.onMesg(Profile.MesgNum.FILE_ID, {
241
+
manufacturer: "development",
242
+
product: 1,
243
+
timeCreated: new Date(),
244
+
type: "activity",
245
+
});
246
+
247
+
// The writeMesg() method expects the mesgNum to be included in the message data
248
+
// Internally, writeMesg() calls onMesg()
249
+
encoder.writeMesg({
250
+
mesgNum: Profile.MesgNum.FILE_ID,
251
+
manufacturer: "development",
252
+
product: 1,
253
+
timeCreated: new Date(),
254
+
type: "activity",
255
+
});
256
+
257
+
// Unknown values in the message will be ignored by the Encoder
258
+
encoder.onMesg(Profile.MesgNum.FILE_ID, {
259
+
manufacturer: "development",
260
+
product: 1,
261
+
timeCreated: new Date(),
262
+
type: "activity",
263
+
customField: 12345, // This value will be ignored by the Encoder
264
+
});
265
+
266
+
// Subfield values in the message will be ignored by the Encoder
267
+
encoder.onMesg(Profile.MesgNum.FILE_ID, {
268
+
manufacturer: "development",
269
+
product: 4440, // This is the main product field, which is a uint16
270
+
garminProduct: "edge1050", // This value will be ignored by the Encoder, use the main field value instead
271
+
timeCreated: new Date(),
272
+
type: "activity",
273
+
});
274
+
275
+
// Closing the encoder returns the file as an UInt8 Array
276
+
const uint8Array = encoder.close();
277
+
278
+
// Write the file to disk,
279
+
import * as fs from "fs";
280
+
fs.writeFileSync("example.fit", uint8Array);
281
+
282
+
````
283
+
See the [Encode Activity Recipe](https://github.com/garmin/fit-javascript-sdk/blob/main/test/encode-activity-recipe.test.js) for a complete example of encoding a FIT Activity file usine the FIT JavaScript SDK.
0 commit comments