fitreader 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fitreader.gemspec +38 -0
- data/lib/fitreader/definition.rb +34 -0
- data/lib/fitreader/degraded_record.rb +38 -0
- data/lib/fitreader/errors.rb +18 -0
- data/lib/fitreader/field_data.rb +58 -0
- data/lib/fitreader/field_definition.rb +15 -0
- data/lib/fitreader/file_header.rb +17 -0
- data/lib/fitreader/fitfile.rb +61 -0
- data/lib/fitreader/record.rb +170 -0
- data/lib/fitreader/record_header.rb +31 -0
- data/lib/fitreader/sdk/constants.yml +734 -0
- data/lib/fitreader/sdk/types.yml +2551 -0
- data/lib/fitreader/static.rb +25 -0
- data/lib/fitreader/version.rb +3 -0
- data/lib/fitreader.rb +49 -0
- data/schema +37 -0
- data/spec/2016-04-09-13-19-18.fit +0 -0
- data/spec/fitreader_spec.rb +55 -0
- data/test/fitreader_test.rb +11 -0
- data/test/test_helper.rb +4 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8e70e256d1754b9d603ebd7c0c0a3e312b14337f
|
4
|
+
data.tar.gz: 6d972f23def9ffb2bce1738bbc892a06a203506d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7c416df560642d9e80b0cab03ff5fc78f74679a7bcb6a778417c2df80a058836534493435e92a2409a645c05bfba23a5c3d44cf19581b3ddd36c3fa73e43558
|
7
|
+
data.tar.gz: 5164ac137fcf5d36abe42520a9c38d6776e609a5294c04262792b0c6758d680da8b27018b95ca1339aae59f740227df0d75f552674704b031e6d2c9b7ba58017
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Richard Brodie
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Richard Brodie
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Fitreader
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fitreader`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'fitreader'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install fitreader
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fitreader.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "fitreader"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/fitreader.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fitreader/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fitreader"
|
8
|
+
spec.version = Fitreader::VERSION
|
9
|
+
spec.authors = ["Richard Brodie"]
|
10
|
+
spec.email = ["richard@samsari.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Library for reading FIT files generated by Garmin devices.}
|
13
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://github.com/samsari/fitreader"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0")
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
# spec.add_development_dependency "minitest", "~> 5.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.5"
|
35
|
+
spec.add_development_dependency "pry"
|
36
|
+
|
37
|
+
spec.add_dependency('bindata')
|
38
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'fitreader/field_definition'
|
2
|
+
|
3
|
+
module Fitreader
|
4
|
+
class Definition
|
5
|
+
attr_accessor :name, :global_num, :local_num, :fit_msg, :field_definitions, :num_fields
|
6
|
+
def initialize(msg_num, bytes)
|
7
|
+
@local_num = msg_num
|
8
|
+
@architecture = bytes[1].unpack('C').first
|
9
|
+
@global_num = bytes[2..3].unpack('v').first
|
10
|
+
@fit_msg = Static.message[@global_num]
|
11
|
+
@name = Static.enums[:enum_mesg_num][@global_num]
|
12
|
+
@num_fields = bytes[4].unpack('C').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def field(id)
|
16
|
+
@field_definitions.find { |x| x.def_num == id }
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_fields(bytes)
|
20
|
+
bytes.chars.each_slice(3) do |x|
|
21
|
+
fd = FieldDefinition.new(@global_num, x)
|
22
|
+
(@field_definitions ||= []).push(fd)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def content_length
|
27
|
+
@field_definitions.inject(0){|sum,x| sum + x.size}
|
28
|
+
end
|
29
|
+
|
30
|
+
def message_type
|
31
|
+
Static.message[@global_num]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'fitreader/field_data'
|
2
|
+
require 'fitreader/errors'
|
3
|
+
|
4
|
+
module Fitreader
|
5
|
+
class DegradedRecord
|
6
|
+
attr_reader :definition, :fields
|
7
|
+
def initialize(definition, bytes)
|
8
|
+
@definition = definition
|
9
|
+
@fields = {}
|
10
|
+
|
11
|
+
start = 0
|
12
|
+
@definition.field_definitions.each do |f|
|
13
|
+
raw = bytes[start...start+=f.size]
|
14
|
+
b = Static.base[f.base_num]
|
15
|
+
data = unpack_data(f, b, raw)
|
16
|
+
@fields[f.def_num] = data
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def unpack_data(f, b, raw)
|
21
|
+
if !b[:unpack_type].nil?
|
22
|
+
if b[:size] != f.size && f.base_num != 7
|
23
|
+
data = []
|
24
|
+
s = 0
|
25
|
+
(f.size/b[:size]).times do |_|
|
26
|
+
data.push(raw[s..s+=b[:size]].unpack(b[:unpack_type]).first)
|
27
|
+
end
|
28
|
+
else
|
29
|
+
data = raw.unpack(b[:unpack_type]).first
|
30
|
+
end
|
31
|
+
elsif f.base_num.zero?
|
32
|
+
data = raw.unpack('C').first
|
33
|
+
else
|
34
|
+
data = raw.split(/\0/)[0]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Fitreader
|
2
|
+
class UnknownFieldTypeError < RuntimeError
|
3
|
+
attr :field, :definition, :data, :reason
|
4
|
+
def initialize(definition, field, data, reason)
|
5
|
+
@field = field
|
6
|
+
@definition = definition
|
7
|
+
@data = data
|
8
|
+
@reason = reason
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class UnknownMessageTypeError < RuntimeError
|
13
|
+
attr :definition
|
14
|
+
def initialize(definition)
|
15
|
+
@definition = definition
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Fitreader
|
2
|
+
class FieldData
|
3
|
+
attr_accessor :id, :raw_value, :value, :name, :valid
|
4
|
+
|
5
|
+
def initialize(id, raw_value, type)
|
6
|
+
@id = id
|
7
|
+
@raw_value = raw_value
|
8
|
+
unless type.nil?
|
9
|
+
process_type(raw_value, type)
|
10
|
+
else
|
11
|
+
puts "nil type #{id} - #{raw_value}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def process_type(data, type)
|
17
|
+
@name = type[:name]
|
18
|
+
if type[:type][0..3].to_sym == :enum
|
19
|
+
data = Static.enums[type[:type]][data]
|
20
|
+
elsif type[:type] == :date_time
|
21
|
+
data = process_datetime(data)
|
22
|
+
elsif type[:type] == :local_date_time
|
23
|
+
data = process_local_datetime(data)
|
24
|
+
elsif type[:type] == :coordinates
|
25
|
+
data = process_coord(data)
|
26
|
+
end
|
27
|
+
|
28
|
+
if type[:scale] != 0
|
29
|
+
if data.class == Array
|
30
|
+
data = data.collect{|x| (x*1.0)/type[:scale]}
|
31
|
+
else
|
32
|
+
data = (data*1.0)/type[:scale]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
if type[:offset] != 0
|
37
|
+
if data.class == Array
|
38
|
+
data = data.collect{|x| x - type[:offset]}
|
39
|
+
else
|
40
|
+
data = data - type[:offset]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
@value = data
|
44
|
+
end
|
45
|
+
|
46
|
+
def process_local_datetime(data)
|
47
|
+
t = Time.new(1989, 12, 31, 0, 0, 0, '+02:00').utc.to_i
|
48
|
+
Time.at(data+t)
|
49
|
+
end
|
50
|
+
def process_datetime(data)
|
51
|
+
t = Time.new(1989, 12, 31, 0, 0, 0, '+00:00').utc.to_i
|
52
|
+
Time.at(data+t).utc
|
53
|
+
end
|
54
|
+
def process_coord(data)
|
55
|
+
data * ( 180.0 / 2**31 )
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Fitreader
|
2
|
+
class FieldDefinition
|
3
|
+
attr_accessor :def_num, :size, :base_num
|
4
|
+
|
5
|
+
def initialize(msg_num, bytes)
|
6
|
+
@def_num = bytes[0].unpack('C').first
|
7
|
+
@size = bytes[1].unpack('C').first
|
8
|
+
# @single_byte = bytes[2].unpack('C').first & ENDIAN_ABILITY == 0
|
9
|
+
@base_num = bytes[2].unpack('C').first & BASE_TYPE_NUM
|
10
|
+
end
|
11
|
+
|
12
|
+
ENDIAN_ABILITY = 128
|
13
|
+
BASE_TYPE_NUM = 31
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class FileHeader
|
2
|
+
attr_accessor :header_size, :protocol_version,
|
3
|
+
:profile_version, :num_records, :valid_file, :crc
|
4
|
+
|
5
|
+
def initialize(bytes)
|
6
|
+
@header_size = bytes[0].unpack('C').first
|
7
|
+
@protocol_version = bytes[1].unpack('C').first
|
8
|
+
@profile_version = bytes[2..3].unpack('v').first
|
9
|
+
@num_records = bytes[4..7].unpack('V').first
|
10
|
+
@valid_file = bytes[8..11] == ".FIT"
|
11
|
+
@crc = bytes[12..13].unpack('v').first
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
"size: #{header_size}, proto_v: #{protocol_version}, prof_v: #{profile_version}, records: #{num_records}"
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'fitreader/file_header'
|
2
|
+
require 'fitreader/record_header'
|
3
|
+
require 'fitreader/definition'
|
4
|
+
require 'fitreader/record'
|
5
|
+
require 'fitreader/degraded_record'
|
6
|
+
require 'fitreader/static'
|
7
|
+
|
8
|
+
module Fitreader
|
9
|
+
class FitFile
|
10
|
+
attr_reader :header, :records, :defs, :file, :error_records
|
11
|
+
|
12
|
+
def initialize(path)
|
13
|
+
@file = File.open(path, 'rb')
|
14
|
+
@header = FileHeader.new(@file.read(14))
|
15
|
+
if valid?
|
16
|
+
@defs = {}
|
17
|
+
@records = []
|
18
|
+
@error_records = []
|
19
|
+
while @file.pos < @header.num_records do
|
20
|
+
process_next_record
|
21
|
+
end
|
22
|
+
puts "number of bad records found: #{@error_records.length}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def valid?
|
28
|
+
@header.valid_file
|
29
|
+
end
|
30
|
+
|
31
|
+
def process_next_record
|
32
|
+
h = RecordHeader.new(@file.read(1).unpack("C").first)
|
33
|
+
dr = @defs[h.local_num]
|
34
|
+
if h.header_type == :definition
|
35
|
+
dr = Definition.new(h.local_num, @file.read(5))
|
36
|
+
dr.add_fields(@file.read(dr.num_fields*3))
|
37
|
+
@defs[h.local_num] = dr
|
38
|
+
elsif h.header_type == :data
|
39
|
+
unless dr.nil?
|
40
|
+
begin
|
41
|
+
data = @file.read(dr.content_length)
|
42
|
+
datr = Record.new(dr, data)
|
43
|
+
@records.push(datr)
|
44
|
+
rescue UnknownMessageTypeError => error
|
45
|
+
# @error_records.push(error.definition)
|
46
|
+
puts error
|
47
|
+
degraded = DegradedRecord.new(dr, data)
|
48
|
+
@error_records.push degraded
|
49
|
+
end
|
50
|
+
else
|
51
|
+
msg = "no record def found! #{h.local_msg_num}"
|
52
|
+
raise msg
|
53
|
+
end
|
54
|
+
elsif h.header_type == :timestamp
|
55
|
+
raise "timestamp :: msg_num: #{h.local_msg_num}, offset: #{h.timestamp_offset}"
|
56
|
+
else
|
57
|
+
raise "not a valid record"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'fitreader/field_data'
|
2
|
+
require 'fitreader/errors'
|
3
|
+
|
4
|
+
module Fitreader
|
5
|
+
class Record
|
6
|
+
attr_reader :definition, :fields, :error_fields
|
7
|
+
def initialize(definition, bytes)
|
8
|
+
@definition = definition
|
9
|
+
@fields = {}
|
10
|
+
@error_fields = {}
|
11
|
+
|
12
|
+
unless @definition.fit_msg.nil?
|
13
|
+
start = 0
|
14
|
+
@definition.field_definitions.each do |f|
|
15
|
+
raw = bytes[start...start+=f.size]
|
16
|
+
b = Static.base[f.base_num]
|
17
|
+
data = unpack_data(f, b, raw)
|
18
|
+
begin
|
19
|
+
process_data(f.def_num, b[:invalid], data)
|
20
|
+
rescue UnknownFieldTypeError => error
|
21
|
+
push_error error.reason, error.field, error.data
|
22
|
+
# puts error unless error.reason == :invalid
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
if @definition.global_num == 21
|
27
|
+
process_event
|
28
|
+
elsif @definition.global_num == 23
|
29
|
+
process_deviceinfo
|
30
|
+
end
|
31
|
+
else
|
32
|
+
msg = "no known message type: #{@definition.global_num}"
|
33
|
+
raise UnknownMessageTypeError.new(definition), msg, caller
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def unpack_data(f, b, raw)
|
38
|
+
if !b[:unpack_type].nil?
|
39
|
+
if b[:size] != f.size && f.base_num != 7
|
40
|
+
data = []
|
41
|
+
s = 0
|
42
|
+
(f.size/b[:size]).times do |_|
|
43
|
+
data.push(raw[s..s+=b[:size]].unpack(b[:unpack_type]).first)
|
44
|
+
end
|
45
|
+
else
|
46
|
+
data = raw.unpack(b[:unpack_type]).first
|
47
|
+
end
|
48
|
+
elsif f.base_num.zero?
|
49
|
+
data = raw.unpack('C').first
|
50
|
+
else
|
51
|
+
data = raw.split(/\0/)[0]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def process_data(fieldDefNum, invalid, data)
|
56
|
+
field_def = @definition.fit_msg[fieldDefNum]
|
57
|
+
unless field_def.nil?
|
58
|
+
# populate invalid
|
59
|
+
if data.is_a?(Array)
|
60
|
+
data = data.select{ |x| x != invalid } if data.is_a?(Array)
|
61
|
+
invalid = data.empty?
|
62
|
+
else
|
63
|
+
invalid = data == invalid
|
64
|
+
end
|
65
|
+
|
66
|
+
unless invalid
|
67
|
+
@fields[fieldDefNum] = FieldData.new(fieldDefNum, data, field_def)
|
68
|
+
else
|
69
|
+
msg = "invalid field data (#{data}) processed for field number [#{fieldDefNum}::#{field_def[:name]}] in message (#{@definition.global_num}::#{@definition.name})"
|
70
|
+
raise UnknownFieldTypeError.new(@definition, fieldDefNum, data, :invalid), msg, caller
|
71
|
+
end
|
72
|
+
else
|
73
|
+
msg = "invalid field [#{fieldDefNum}] encountered for message [#{@definition.global_num}::#{@definition.name}] with data [#{data}]"
|
74
|
+
raise UnknownFieldTypeError.new(@definition, fieldDefNum, data, :unknown), msg, caller
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def temporal?
|
79
|
+
fields.key?(253)
|
80
|
+
end
|
81
|
+
|
82
|
+
def push_error(reason, field, data)
|
83
|
+
(@error_fields[reason] ||= {})[field] = data
|
84
|
+
end
|
85
|
+
|
86
|
+
# :file_id 1
|
87
|
+
# :user_profile 1
|
88
|
+
# :zones_target 1
|
89
|
+
# :session 1
|
90
|
+
# :lap 1
|
91
|
+
# :record 2899
|
92
|
+
# :event 73
|
93
|
+
# :source 58
|
94
|
+
# :device_info 14
|
95
|
+
# :activity 1
|
96
|
+
# :file_creator 1
|
97
|
+
# :training_file 1
|
98
|
+
# :battery_info 40
|
99
|
+
# :sensor_info 3
|
100
|
+
def type
|
101
|
+
case @definition.name
|
102
|
+
when :file_id, :user_profile, :zones_target, :session
|
103
|
+
:ride
|
104
|
+
when condition
|
105
|
+
:lap
|
106
|
+
else
|
107
|
+
:time
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def get_val(key)
|
112
|
+
if key.class == String
|
113
|
+
f = @fields.values.detect{|v| v.name == key}
|
114
|
+
elsif key.class == Integer
|
115
|
+
f = @fields[key]
|
116
|
+
end
|
117
|
+
return f != nil ? f.value : nil
|
118
|
+
end
|
119
|
+
|
120
|
+
def get_raw(key)
|
121
|
+
if key.class == String
|
122
|
+
f = @fields.values.detect{|v| v.name == key}
|
123
|
+
elsif key.class == Integer
|
124
|
+
f = @fields[key]
|
125
|
+
end
|
126
|
+
return !f.nil? ? f.raw_value : nil
|
127
|
+
end
|
128
|
+
|
129
|
+
def to_s
|
130
|
+
s = "#{name} (#{definition.global_num})\n"
|
131
|
+
@fields.each do |_, v|
|
132
|
+
s += v.to_s
|
133
|
+
end
|
134
|
+
"#{s}\n"
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
def process_event
|
139
|
+
t = @fields[0]
|
140
|
+
d = @fields[3]
|
141
|
+
d.value = case t.value
|
142
|
+
when :rear_gear_change, :front_gear_change
|
143
|
+
process_gear_change(d.raw_value)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def process_deviceinfo
|
148
|
+
d = @fields[1]
|
149
|
+
t = @fields[25]
|
150
|
+
if !d.nil? && !t.nil?
|
151
|
+
d.value = case t.value
|
152
|
+
when :antplus
|
153
|
+
Static.enums[:antplus_device_type][d.raw_value]
|
154
|
+
end
|
155
|
+
end
|
156
|
+
d = @fields[4]
|
157
|
+
t = @fields[2]
|
158
|
+
if !d.nil? && !t.nil?
|
159
|
+
d.value = case t.value
|
160
|
+
when :garmin, :dynastream, :dynastream_oem
|
161
|
+
Static.enums[:enum_garmin_product][d.raw_value]
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def process_gear_change(data)
|
167
|
+
[data].pack('V*').unpack('C*')
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Fitreader
|
2
|
+
class RecordHeader
|
3
|
+
attr_accessor :header_type, :local_num,
|
4
|
+
:timestamp_offset, :developer
|
5
|
+
|
6
|
+
def initialize(byte)
|
7
|
+
timstamp_header = byte & TIMESTAMP_RECORD > 0
|
8
|
+
unless timstamp_header
|
9
|
+
@header_type = byte & DEFINITION_MESSAGE > 0 ? :definition : :data
|
10
|
+
@local_num = byte & NORMAL_TYPE
|
11
|
+
if byte & DEVELOPER_DATA == DEVELOPER_DATA
|
12
|
+
@developer = true
|
13
|
+
puts "developer"
|
14
|
+
end
|
15
|
+
else
|
16
|
+
@header_type = :timestamp
|
17
|
+
@local_num = byte & TIMESTAMP_TYPE
|
18
|
+
@timestamp_offset = byte & TIMESTAMP_OFFSET
|
19
|
+
puts "timestamp header: #{@local_msg_num} :: #{@timestamp_offset}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
TIMESTAMP_RECORD = 0b10000000
|
25
|
+
DEFINITION_MESSAGE = 0b01000000
|
26
|
+
DEVELOPER_DATA = 0b00100000
|
27
|
+
NORMAL_TYPE = 0b00001111
|
28
|
+
TIMESTAMP_TYPE = 0b01100000
|
29
|
+
TIMESTAMP_OFFSET = 0b00011111
|
30
|
+
end
|
31
|
+
end
|