Class: Broutes::Formats::FitFile

Inherits:
Object
  • Object
show all
Defined in:
lib/broutes/formats/fit_file.rb

Instance Method Summary collapse

Instance Method Details

#convert_position(value) ⇒ Object



30
31
32
# File 'lib/broutes/formats/fit_file.rb', line 30

def convert_position(value)
  (8.381903171539307e-08 * value).round(5)
end

#load(file, route) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/broutes/formats/fit_file.rb', line 6

def load(file, route)
  fit_file = Fit::File.read(file)
  Broutes.logger.info {"Started fit processing"}
  i = 0
  fit_file.records.select {|r| r.content && r.content.record_type == :record }.each do |r|
    begin
      pr = r.content
      data = { time: record_time(r) }
      data[:lat] = convert_position(pr.position_lat) if pr.respond_to?(:position_lat)
      data[:lon] = convert_position(pr.position_long) if pr.respond_to?(:position_long)
      data[:elevation] = pr.altitude if pr.respond_to?(:altitude)
      [:distance, :heart_rate, :power, :speed, :cadence, :temperature].each do |m| 
        data[m] = pr.send(m) if pr.respond_to?(m) 
      end

      route.add_point(data)
      i += 1
    rescue => e
      Broutes.logger.debug {"#{e.message} for #{r}"}
    end
  end
  Broutes.logger.info {"Loaded #{i} data points"}
end

#record_time(record) ⇒ Object



34
35
36
37
38
# File 'lib/broutes/formats/fit_file.rb', line 34

def record_time(record)
  utc_seconds = record.content.timestamp
  utc_seconds += record.header.time_offset if record.header.compressed_timestamp? 
  Time.new(1989, 12, 31) + utc_seconds
end