Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 69 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ The FIT JavaScript SDK uses ECMAScript module syntax and requires Node.js v14.0
```sh
npm install @garmin/fitsdk
```
## Usage
## Decoder
### Usage
````js
import { Decoder, Stream, Profile, Utils } from '@garmin/fitsdk';

Expand All @@ -27,8 +28,6 @@ const { messages, errors } = decoder.read();
console.log(errors);
console.log(messages);
````
## Decoder

### Constructor

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.
Expand All @@ -37,24 +36,24 @@ Once a Decoder object is created it can be used to check that the Stream is a FI

### isFIT Method

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.
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.

```
````bash
Offset: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000: 0E 10 43 08 78 06 09 00 2E 46 49 54 96 85 40 00 ..C.x....FIT..@.
00000010: 00 00 00 07 03 04 8C 04 04 86 07 04 86 01 02 84 ................
00000020: 02 02 84 05 02 84 00 01 00 00 19 28 7E C5 95 B0 ...........(~E.0
```
````

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.
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.

The Decoder class includes a static and instance version of the isFIT method.

### Check Integrity Method

The checkIntegrity method performs three checks on a FIT file:

1. Checks that bytes 8–11 of the header contain the ASCII values ".FIT.
1. Checks that bytes 8–11 of the header contain the ASCII values ".FIT".
2. Checks that the total file size is equal to Header Size + Data Size + CRC Size.
3. Reads the contents of the file, computes the CRC, and then checks that the computed CRC matches the file CRC.

Expand Down Expand Up @@ -83,7 +82,6 @@ const { messages, errors } = decoder.read({
Optional callback function that 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.

Example mesgListener callback that tracks the field names across all Record messages.

````js
const recordFields = new Set();

Expand Down Expand Up @@ -221,3 +219,65 @@ A convince method for converting FIT Epoch values to JavaScript Date objects.
````js
const jsDate = Utils.convertDateTimeToDate(fitDateTime);
````
## Encoder
### Usage
````js
// Import the SDK
import { Encoder, Profile} from "@garmin/fitsdk";

// Create an Encoder
const encoder = new Encoder();

//
// Write messages to the output-stream
//
// The message data should match the format returned by
// the Decoder. Field names should be camelCase. The fields
// definitions can be found in the Profile.
//

// Pass the MesgNum and message data as separate parameters to the onMesg() method
encoder.onMesg(Profile.MesgNum.FILE_ID, {
manufacturer: "development",
product: 1,
timeCreated: new Date(),
type: "activity",
});

// The writeMesg() method expects the mesgNum to be included in the message data
// Internally, writeMesg() calls onMesg()
encoder.writeMesg({
mesgNum: Profile.MesgNum.FILE_ID,
manufacturer: "development",
product: 1,
timeCreated: new Date(),
type: "activity",
});

// Unknown values in the message will be ignored by the Encoder
encoder.onMesg(Profile.MesgNum.FILE_ID, {
manufacturer: "development",
product: 1,
timeCreated: new Date(),
type: "activity",
customField: 12345, // This value will be ignored by the Encoder
});

// Subfield values in the message will be ignored by the Encoder
encoder.onMesg(Profile.MesgNum.FILE_ID, {
manufacturer: "development",
product: 4440, // This is the main product field, which is a uint16
garminProduct: "edge1050", // This value will be ignored by the Encoder, use the main field value instead
timeCreated: new Date(),
type: "activity",
});

// Closing the encoder returns the file as an UInt8 Array
const uint8Array = encoder.close();

// Write the file to disk,
import * as fs from "fs";
fs.writeFileSync("example.fit", uint8Array);

````
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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@garmin/fitsdk",
"version": "21.170.0",
"version": "21.171.0",
"description": "FIT JavaScript SDK",
"main": "src/index.js",
"type": "module",
Expand Down
4 changes: 2 additions & 2 deletions src/accumulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/bit-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/crc-calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/fit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/mesg-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/output-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
10 changes: 7 additions & 3 deletions src/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


const Profile = {
version: {
major: 21,
minor: 170,
minor: 171,
patch: 0,
type: "Release"
},
Expand Down Expand Up @@ -24716,6 +24716,8 @@ types: {
4586: "instinct3Amoled45mm",
4587: "instinct3Amoled50mm",
4588: "descentG2",
4606: "hrm200",
4625: "vivoactive6",
4647: "approachS44",
4656: "approachS50",
4666: "fenixE",
Expand Down Expand Up @@ -24843,6 +24845,8 @@ types: {
4: "drill",
5: "mixed",
6: "im", // IM is a mixed interval containing the same number of lengths for each of: Butterfly, Backstroke, Breaststroke, Freestyle, swam in that order.
7: "imByRound", // For repeated workout steps, a new individual medly stroke is used for each round.
8: "rimo", // Reverse IM Order
},
activityType: {
0: "generic",
Expand Down
4 changes: 2 additions & 2 deletions src/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/utils-hr-mesg.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/utils-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
4 changes: 2 additions & 2 deletions src/utils-memo-glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////

import Profile from "./profile.js";
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.170.0Release
// Tag = production/release/21.170.0-0-g5991e72
// Profile Version = 21.171.0Release
// Tag = production/release/21.171.0-0-g57fed75
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
2 changes: 1 addition & 1 deletion test/encode-activity-recipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ test("Can encode a FIT Activity file", () => {
timestamp: timestamp,
numSessions: 1,
localTimestamp: timestamp + localTimestampOffset,
totalTimerTime: timestamp = startTime,
totalTimerTime: timestamp - startTime,
});

try {
Expand Down