Git Product home page Git Product logo

fit-php's Introduction

fit-php

A PHP class to encode and decode Garmin .FIT files, based on the FIT SDK. The FIT SDK can be found at: http://www.thisisant.com/resources/fit

Example

<?php
//Create some data, always set a message 'file_id'.
$time = time() - mktime(0,0,0,12,31,1989);
$data = new \Fit\Data;
$data->setFile(\Fit\FileType::activity);
$data
    ->add('file_id', array(
        'type'                  => \Fit\FileType::activity,
        'manufacturer'          => \Fit\Manufacturer::development,
        'product'               => 0,
        'serial_number'         => 0,
        'time_created'          => $time,
    ))
    ->add('activity', array(
        'timestamp'             => $time,
        'num_sessions'          => 1,
        'type'                  => \Fit\Activity::manual,
        'event'                 => \Fit\Event::workout,
        'event_type'            => \Fit\EventType::start,
    ))
    ->add('event', array(
        'timestamp'             => $time,
        'event_type'            => \Fit\EventType::start,
    ))
    ->add('session', array(
        'sport'                 => \Fit\Sport::cycling,
        'sub_sport'             => \Fit\SubSport::spin,
        'total_elapsed_time'    => 0,
        'total_timer_time'      => 0,
        'total_distance'        => 0,
        'total_ascent'          => 0,
    ))
    ->add('record', array(
        'timestamp'             => $time++, 
        'position_lat'          => 0, 
        'position_long'         => 0, 
        'altitude'              => 0, 
        'heart_rate'            => 65, 
        'cadence'               => 45, 
        'distance'              => 0, 
        'power'                 => 0, 
        'temperature'           => 19, 
    ))
    ->add('record', array(
        'timestamp'             => $time++, 
        'position_lat'          => 0, 
        'position_long'         => 0, 
        'altitude'              => 0, 
        'heart_rate'            => 70, 
        'cadence'               => 90, 
        'distance'              => 10, 
        'power'                 => 0, 
        'temperature'           => 19, 
    ))
    ->add('record', array(
        'timestamp'             => $time++, 
        'position_lat'          => 0, 
        'position_long'         => 0, 
        'altitude'              => 0, 
        'heart_rate'            => 73, 
        'cadence'               => 90, 
        'distance'              => 20, 
        'power'                 => 0, 
        'temperature'           => 19, 
    ))
    ->add('event', array(
        'timestamp'             => $time,
        'event_type'            => \Fit\EventType::stop,
    ))
;

$debug = true;

//Write the data
$fitwriter = new \Fit\Writer($debug);
$filepath = $fitwriter->writeData($data);

//Read the data that was just created
$fit = new \Fit\Reader(true);
$fit->parseFile($filepath, $debug);

//Delete the written data
unlink($filepath);

//output the resulting data
echo '<pre>';
var_dump($fit);
echo '</pre>';


fit-php's People

Contributors

krwes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fit-php's Issues

Generated FIT file not accepted

Hi guys - Is anyone using this code? I tried the posted example, and it generates a FIT file, but none of the applications used to read FIT files can read it. I'm having trouble diagnosing the issue, and wondering if anyone has gotten this code to work. Thanks.

Not compatible with latest format?

I'm trying to parse my day records exported from the Garmin Connect website. But the records show binary data:

            [3] => Array
                (
                    [no:0] => Array
                        (
                            [value] => �9���2��ڧ�:�5?1�T
                            [unit] => 
                        )

                )

What's the best way to bring this library up to date to be able to parse these files?

Incorrect header size

Hello there,
I'have an array "stepsData":

{"0":{"power":112.5,"hr":99.75,"cadence":90},"660":{"power":175,"hr":142.5,"cadence":90},"990":{"power":175,"hr":142.5,"cadence":90},"2550":{"power":225,"hr":161.5,"cadence":90},"3150":{"power":175,"hr":142.5,"cadence":90},"3750":{"power":350,"hr":185.25,"cadence":90},"3780":{"power":175,"hr":161.5,"cadence":90},"3810":{"power":350,"hr":185.25,"cadence":90},"3840":{"power":175,"hr":161.5,"cadence":90},"3870":{"power":350,"hr":185.25,"cadence":90},"3900":{"power":175,"hr":161.5,"cadence":90},"3930":{"power":350,"hr":185.25,"cadence":90},"3960":{"power":175,"hr":161.5,"cadence":90},"3990":{"power":350,"hr":185.25,"cadence":90},"4020":{"power":175,"hr":161.5,"cadence":90},"4050":{"power":350,"hr":185.25,"cadence":90},"4080":{"power":175,"hr":161.5,"cadence":90},"4110":{"power":175,"hr":142.5,"cadence":90},"4950":{"power":800,"hr":142.5,"cadence":125},"4960":{"power":112.5,"hr":142.5,"cadence":90},"5010":{"power":800,"hr":142.5,"cadence":125},"5020":{"power":112.5,"hr":142.5,"cadence":90},"5070":{"power":800,"hr":142.5,"cadence":125},"5080":{"power":112.5,"hr":142.5,"cadence":90},"5130":{"power":800,"hr":142.5,"cadence":125},"5140":{"power":112.5,"hr":142.5,"cadence":90},"5190":{"power":800,"hr":142.5,"cadence":125},"5200":{"power":112.5,"hr":142.5,"cadence":90},"5250":{"power":175,"hr":142.5,"cadence":90},"6810":
{"power":112.5,"hr":99.75,"cadence":90}}

And I use this fork https://github.com/johnnye/fit-php like this

public function generateWahooWorkoutFit($trainingpplansession,$stepsData,$fitpath){
        $time = time() - mktime(0,0,0,12,31,1989);
        $data = new \Fit\Data;
        $data->setFile(\Fit\FileType::workout);
        $data
            ->add('file_id', array(
                'type'                  => \Fit\FileType::workout,
                'manufacturer'          => \Fit\Manufacturer::development,
                'product'               => 0,
                'serial_number'         => 0,
                'time_created'          => $time,
            ))->add('event', array(
                'timestamp'             => $time,
                'event_type'            => \Fit\EventType::start,
                
            ))->add('session', array(
                'sport'                 => \Fit\Sport::cycling,
                'sub_sport'             => \Fit\SubSport::generic,
                'total_elapsed_time'    => $trainingpplansession->getDuration(),
                'total_timer_time'      => $trainingpplansession->getDuration(),
                'total_distance'        => 0,
                'total_ascent'          => 0,
        ));

        foreach ($stepsData as $timestamp=>$stepdata){
            $data->add('record', array(
                'timestamp'             => $timestamp+$time, 
                //'position_lat'          => 0, 
                //'position_long'         => 0, 
                //'altitude'              => 0, 
                'heart_rate'            => $stepdata["hr"],
                'cadence'               => $stepdata["cadence"], 
                //'distance'              => 0, 
                'power'                 => $stepdata["power"], 
                //'temperature'           => 0, 
             ));
        }
        $data->add('event', array(
            'timestamp'             => $trainingpplansession->getDuration()+$time,
            'event_type'            => \Fit\EventType::stop,
        ));


        $debug = false;
        
        //Write the data
        $fitwriter = new \Fit\Writer($debug);
        $fitwriter->writeData($data,$fitpath);
        
        
        //Read the data that was just created
        /*$fit = new \Fit\Reader(true);
        $fit->parseFile($filepath, $debug);
        
        //Delete the written data
        unlink($filepath);
        var_dump($fit);*/
    }

The generated fit (https://www.dropbox.com/s/adc1icycm893zd2/WAHOO-2021-07-13.fit?dl=0) is sent to wahoo we contacted the support who reply us:

It looks like your base64 encoding, data url, and request format are all correct. However, there is a problem with your FIT file that is preventing our cloud from parsing it. We are failing because of an “Incorrect header size.” I recommend using a different tool to generate your FIT file. Once you have a valid FIT file your request should work.

So I'm curious if I do something wrong with the library

Thanks in advance

Can I add subfields to workout -> workout_step

I see that there are subfields to workout_step like duration_step and duration_power and etc. Could you please hint how can I add it to protocol?

More precisely, I am in need to add something like

Duration = 3000
PowerHigh = 0.85
PowerLow = 0.65
Pace = 2
Units = 1
Message = ...

To workout step.

Or could you please add a full example for Workout type? Thanks.

Problems with interface

Using PHP8.1.2 I get the following warnings and errors. I'm not sure how to correct this as I don't use interfaces often.

PHP Warning:  Private methods cannot be final as they are never overridden by other classes in /fit-php/src/Zend_Io_Reader.php on line 449
PHP Stack trace:
PHP   1. {main}() /fit-php/test.php:0
PHP   2. require() /fit-php/test.php:2
PHP   3. ComposerAutoloaderInit61dea3865b8bf1b598e8596bd7cf1692::getLoader() /fit-php/vendor/autoload.php:7
PHP   4. composerRequire61dea3865b8bf1b598e8596bd7cf1692($fileIdentifier = '5965d4b9d7bfe08b8c92d40b7ef4f645', $file = '/fit-php/vendor/composer/../../src/Zend_Io_Reader.php')/fit-php/vendor/composer/autoload_real.php:59

PHP Warning:  Private methods cannot be final as they are never overridden by other classes in /fit-php/src/Zend_Io_Writer.php on line 287
PHP Stack trace:
PHP   1. {main}() /fit-php/test.php:0
PHP   2. require()/fit-php/test.php:2
PHP   3. ComposerAutoloaderInit61dea3865b8bf1b598e8596bd7cf1692::getLoader() /fit-php/vendor/autoload.php:7
PHP   4. composerRequire61dea3865b8bf1b598e8596bd7cf1692($fileIdentifier = 'f471493d81660f54409d5e62e382dce3', $file = '/fit-php/vendor/composer/../../src/Zend_Io_Writer.php') /fit-php/vendor/composer/autoload_real.php:59

PHP Fatal error:  Uncaught TypeError: Fit\Reader::__construct(): Argument #1 ($reader) must be of type Fit\IoReader, bool given, called in /fit-php/test.php on line 81 and defined in /fit-php/src/Reader.php:74
Stack trace:
#0 /fit-php/test.php(81): Fit\Reader->__construct()
#1 {main}
  thrown in /fit-php/src/Reader.php on line 74

using test.php

<?php
require __DIR__ . '/vendor/autoload.php';

//Create some data, always set a message 'file_id'.
$time = time() - mktime(0,0,0,12,31,1989);
$data = new \Fit\Data;
$data->setFile(\Fit\FileType::activity);
$data
    ->add('file_id', array(
        'type'                  => \Fit\FileType::activity,
        'manufacturer'          => \Fit\Manufacturer::development,
        'product'               => 0,
        'serial_number'         => 0,
        'time_created'          => $time,
    ))
    ->add('activity', array(
        'timestamp'             => $time,
        'num_sessions'          => 1,
        'type'                  => \Fit\Activity::manual,
        'event'                 => \Fit\Event::workout,
        'event_type'            => \Fit\EventType::start,
    ))
    ->add('event', array(
        'timestamp'             => $time,
        'event_type'            => \Fit\EventType::start,
    ))
    ->add('session', array(
        'sport'                 => \Fit\Sport::cycling,
        'sub_sport'             => \Fit\SubSport::spin,
        'total_elapsed_time'    => 0,
        'total_timer_time'      => 0,
        'total_distance'        => 0,
        'total_ascent'          => 0,
    ))
    ->add('record', array(
        'timestamp'             => $time++, 
        'position_lat'          => 0, 
        'position_long'         => 0, 
        'altitude'              => 0, 
        'heart_rate'            => 65, 
        'cadence'               => 45, 
        'distance'              => 0, 
        'power'                 => 0, 
        'temperature'           => 19, 
    ))
    ->add('record', array(
        'timestamp'             => $time++, 
        'position_lat'          => 0, 
        'position_long'         => 0, 
        'altitude'              => 0, 
        'heart_rate'            => 70, 
        'cadence'               => 90, 
        'distance'              => 10, 
        'power'                 => 0, 
        'temperature'           => 19, 
    ))
    ->add('record', array(
        'timestamp'             => $time++, 
        'position_lat'          => 0, 
        'position_long'         => 0, 
        'altitude'              => 0, 
        'heart_rate'            => 73, 
        'cadence'               => 90, 
        'distance'              => 20, 
        'power'                 => 0, 
        'temperature'           => 19, 
    ))
    ->add('event', array(
        'timestamp'             => $time,
        'event_type'            => \Fit\EventType::stop,
    ))
;

$debug = true;

//Write the data
$fitwriter = new \Fit\Writer($debug);
$filepath = $fitwriter->writeData($data);

//Read the data that was just created
$fit = new \Fit\Reader(true);
$fit->parseFile($filepath, $debug);

//Delete the written data
unlink($filepath);

//output the resulting data
echo '<pre>';
var_dump($fit);
echo '</pre>';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.