Flux icon indicating copy to clipboard operation
Flux copied to clipboard

Splitting off the FIT parser

Open jastax opened this issue 3 years ago • 5 comments

Hey,

great work you're doing here.

I've been looking for a way to parse JSON to FIT in my project for quite some time and I'm not the only one. Most .fit parsers have github issues in which people wish & hope someone will implement it. As I see it in your project, you have done it.

Do you think it would be possible to split it off into a separate project or npm package for others to use?

Have a nice day!

jastax avatar Jan 27 '22 16:01 jastax

Hi, thanks, glad you liked the project.

Actually I did split the .FIT code to a separate project, but the goal was to make a demo for a specific application (fixing broken Zwift .FIT files). I wrote about the project in the ZwiftCoders fb group, but there was like zero interest so I abandoned it and focused on Flux and WebANT. It’s gonna need some restructuring in order to be publishable on npm, some docs, nicer public API, plus I wanna save the demo. Will start some planning on this, but probably won’t be able to do anything before next month.

This is the project.

dvmarinoff avatar Jan 27 '22 19:01 dvmarinoff

Hi,

cool, I thought that project was only for fixing .fit files!

Could you give me some info on how to properly use it?

Say I have a parsed fitFile (in json)

What I found was a way for which I would have to split it in laps and records, i.e. for a fit file with one session:

const laps = parse(realFitFile).activity.sessions[0].laps;
const records = laps.flatMap(parsed => parsed.records);
const fitjsActivity = activity.encode({ records: records, laps: laps });
const encoded = fit.activity.encode(fitjsActivity);

While this returns a .fit, there is of course a lot of information loss doing so and for me the laps and records (and some other data) are missing, when I upload it to Strava.

Am I missing something?

Perfect use would be a one method call taking JSON and returning .fit allowing a flow like this .fit file -> parse .fit to Json -> FluxFitEncoder.encode(parsedFit) -> .fit file

Again, thanks for your work!

jastax avatar Jan 28 '22 05:01 jastax

For the last few months I had my head all into trying to understand the ANT+ protocol, so it’s been some time since I last did any work on FitFIle and the details are a bit faded.

Now as I remember, since the whole .FIT protocol is ridiculously vast, I focused only on what I thought is relevant for indoor cycling, so it won't yet work with an arbitrary fit file, but only with the subset that Zwift and Flux are using. It’s not hard to expand support for bigger parts of the protocol. There are two areas that will require work. Let’s call the first one the computational part, the one major feature it’s lacking is support for custom developer fields, else it’s good to go. The second is the definitions part.

There is a profiles.xlsx document inside the FIT SDK it contains all standart definitions for fields, records, units etc. that can be found in a fit file and they are a lot. Those can be slowly over time extracted to ‘javascript’. I have the profiles.js file that contains the definitions for decoding/reading a .FIT file, and local-message-definitions.js which has the definitions for encoding/writing a file. If you need support for a particular message or a field, you can look it up in the ‘profiles.xlsx’ and add its definition to those files.

There is an intermediate format which I named FITjs. It’s basically the contents of a fit file translated to javascript objects. Those are suitable for direct encoding into a binary fit file, and suitable for a binary file to be decoded to. It maps closely to the FIT protocol, kinda low level. At the FitFile demo page if you upload a fit file and open the developer console you will see the FITjs printed.

On top of that I have application layer, which is well … application dependant. I have activity.js in Flux as one example, but it’s closely linked to the way data is recorded and represented inside the Flux app. Its purpose is to map the BLE and ANT+ sensor data to FITjs.

I’ll need more details about your use case. And maybe an example input fit file, or an example of the JSON that you need to encode to a fit file.

This is an example that reads a fit file, manipulates the data and encodes it back to binary. https://github.com/dvmarinoff/FitFile/blob/master/src/models/activity.js#L34

This project was very helpful for me when I studied the fit protocol and had to validate the results of my code. It's more convenient than uploading to Strava.

dvmarinoff avatar Jan 28 '22 09:01 dvmarinoff

Thank you! I will try it for myself with the information you provided and maybe come back later :)

Trying https://www.fitfileviewer.com/ I discovered that 0 laps are in my resulting fit files and all timestamps are fixed at "31.12.1989, 01:00:00", so that's where I will start and then go from there.

jastax avatar Jan 31 '22 15:01 jastax

I've been looking for a way to parse JSON to FIT in my project for quite some time and I'm not the only one. Most .fit parsers have github issues in which people wish & hope someone will implement it. As I see it in your project, you have done it.

Have you seen the parsing from GoldenCheetah? I'm pretty sure they store their data in JSON and then output to FIT for upload and such.

mjunkmyjunk avatar Feb 15 '22 09:02 mjunkmyjunk

The fit parser has been completely re-written, and is available as mostly standalone on here: https://github.com/dvmarinoff/FitFile

The Flux project will track closely the implementation at FitFile, and will probably get fixes and improvements first, before they are applied to the FitFile repo. Plans are that the FitFile repo will develop as a general Fit visualizer and editor.

dvmarinoff avatar Mar 18 '24 08:03 dvmarinoff