jimmykane / fit-parser

Parse your FIT files easily, directly from JS (Garmin, Polar, Suunto)
Other
89 stars 33 forks source link

unable to parse downloaded fit file (via REST api) #15

Closed MichaelLiss closed 4 years ago

MichaelLiss commented 4 years ago

Hi,

I am trying to use your fit file parser and ran into a little issue I am hoping you will be able to shed some light on.

I am using the garmin health-api and downloading a FIT file directly through the REST api. Here is an example call

https://healthapi.garmin.com/wellness-api/rest/activityFile?id=86711045091

Of course there is a bunch of stuff (oAuth, headers and callbacks) that needs to occur before you can download the fit file, but rest assured, I have worked with Garmin to get all of the "rest of the stuff" hammered out and I am getting FIT file data from the REST api.

Here is what the fit file data looks like when I view it in my debugger:

FIT_File_download

Here is the code where I am using your object to directly parse the downloaded data from the REST call:

const internalParse = async (data) => {

    // Create a FitParser instance (options argument is optional)
    var fitParser = new FitParser({
        force: true,
        speedUnit: 'km/h',
        lengthUnit: 'km',
        temperatureUnit: 'kelvin',
        elapsedRecordField: true,
        mode: 'cascade',
        });

        var error = null;
        var data = null;
        // Parse your file
        try{
            fitParser.parse(data, function (error, data){

When I try to read this data using your object I get the following error:

Incorrect header size
Missing '.FIT' in header

My question to you: Is there a way I can simply pass that data to your object for parsing? or do I need to do something to it before your object will accept it?

MichaelLiss commented 4 years ago

Just so you know... I do have code that uses your object and successfully parses a file that is read from the hard drive - so I know your object works when reading files from hard drive. :-)

    var fName = path.join(__dirname, './4861335825.fit');

    fs.readFile(fName, function (err, content) {

        console.log('reading fit file - content',content);

        // Create a FitParser instance (options argument is optional)
        var fitParser = new FitParser({
          force: true,
          speedUnit: 'km/h',
          lengthUnit: 'km',
          temperatureUnit: 'kelvin',
          elapsedRecordField: true,
          mode: 'cascade',
        });

        // Parse your file
        fitParser.parse(content, function (error, data) {
MichaelLiss commented 4 years ago

I am going down the right path now;

    const buf1 = Buffer.from(binary, 'utf8');
    var data = await internalParse(buf1);

will get me into your parser... now I am getting this:

stack:"TypeError: Cannot read property 'globalMessageNumber' of undefined
    at readRecord (d:\Code\Udemy\instanttiming\node_modules\fit-file-parser\dist\binary.js:332:60)
MichaelLiss commented 4 years ago

I see this in the binary.js file:

// TODO: handle compressed header ((recordHeader & 128) == 128)

could a compressed header be the reason for the error ?

MichaelLiss commented 4 years ago

I thought it might be that utf8 was the wrong encoding... so I tried this:

    var encoding = 'utf8';
    encoding = 'utf16le';
    const buf1 = Buffer.from(binary, encoding);

But that makes the parser have a wrong fileTypeString:

utf16le

jimmykane commented 4 years ago

Hey. Could you try the option force ? That should allow that block of code to exacute.

Sorry I am ooo and might take a few to reply properly.

Thanks in advance for reporting those issues.

On Fri, 1 May 2020, 14:42 MrLister, notifications@github.com wrote:

I thought it might be that utf8 was the wrong encoding... so I tried this:

var encoding = 'utf8';
encoding = 'utf16le';
const buf1 = Buffer.from(binary, encoding);

But that makes the parser have a wrong fileTypeString:

[image: utf16le] https://user-images.githubusercontent.com/39492750/80806058-78f1d700-8b6e-11ea-835d-bca3d6d63d74.PNG

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jimmykane/fit-parser/issues/15#issuecomment-622372603, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJVX42JPLMMEA2SRDSDMELRPK7UJANCNFSM4MWWVJ7A .

MichaelLiss commented 4 years ago

I think I figured it out:

https://stackoverflow.com/questions/61543176/why-is-the-content-of-these-two-buffers-different?noredirect=1#comment108864787_61543176

jimmykane commented 4 years ago

I have the exact same code on other lib , that consumers this lib (fit parser) but also extends for gpx and tcx are you interested ?

On Fri, 1 May 2020, 16:12 MrLister, notifications@github.com wrote:

Closed #15 https://github.com/jimmykane/fit-parser/issues/15.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jimmykane/fit-parser/issues/15#event-3293295675, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJVX43EVOTNOBZMZMSUZ53RPLKENANCNFSM4MWWVJ7A .

MichaelLiss commented 4 years ago

I need the information that the activity file provides and the FIT file format is the only format that is available for activity file download.

Additionally, the fit file format is WAY smaller than a gpx or tcx,

Lastly, parsing it (much thanks to your parser) is super quick ... so I can just parse on demand when I need to get data

so the short answer is : thanks, but no. :-)