class Message

Attributes

data[RW]
global_num[RW]
name[RW]

Public Class Methods

new(definitions) click to toggle source
# File lib/fitreader/message.rb, line 4
def initialize(definitions)
  @global_num = definitions[0]
  @name = Sdk.message(@global_num)
  return unless @name

  fd = Sdk.fields(@global_num)
  @data = definitions[1].map { |x| make_message(x, fd) }.flatten
end

Private Instance Methods

make_message(definition, fields) click to toggle source
# File lib/fitreader/message.rb, line 15
def make_message(definition, fields)
  return if definition.valid.nil?
  definition.valid.map do |d|
    sdk_fields = d.select { |k, v| fields.has_key?(k) }
    h = Hash[sdk_fields.map { |k, v| process_value(fields[k], v.raw) }]
    case @global_num
    when 21
      h = process_event(h)
    when 0, 23
      h = process_deviceinfo(h)
    end
    dev_fields = Hash[d.select { |k, v| k.is_a?(Symbol) }.map {|k,v| [k, v.raw]}]
    h.merge(dev_fields)
  end
end
process_deviceinfo(h) click to toggle source
# File lib/fitreader/message.rb, line 72
def process_deviceinfo(h)
  case h[:source_type]
  when :antplus
    h[:device_type] = Sdk.enum(:antplus_device_type)[h[:value]]
  end

  case h[:manufacturer]
  when :garmin, :dynastream, :dynastream_oem
    h[:garmin_product] = Sdk.enum(:enum_garmin_product)[h[:garmin_product]]
  end
  h
end
process_event(h) click to toggle source
# File lib/fitreader/message.rb, line 64
def process_event(h)
  case h[:event]
  when :rear_gear_change, :front_gear_change
    h[:data] = Array(h[:data]).pack('V*').unpack('C*')
  end
  h
end
process_value(type, val) click to toggle source
# File lib/fitreader/message.rb, line 31
def process_value(type, val)
  if type[:type][0..3].to_sym == :enum
    val = Sdk.enum(type[:type])[val]
  elsif type[:type] == :date_time
    t = Time.new(1989, 12, 31, 0, 0, 0, '+00:00').utc.to_i
    Time.at(val + t).utc
  elsif type[:type] == :local_date_time
    t = Time.new(1989, 12, 31, 0, 0, 0, '+02:00').utc.to_i
    val = Time.at(val + t)
  elsif type[:type] == :coordinates
    val *= (180.0 / 2**31)
  end

  unless type[:scale].zero?
    if val.is_a? Array
      val = val.map { |x| (x * 1.0) / type[:scale] }
    else
      val = (val * 1.0) / type[:scale]
    end
  end

  unless type[:offset].zero?
    if val.is_a? Array
      val.map { |x| x - type[:offset] }
    else
      val - type[:offset]
    end
  end
  [type[:name], val]
rescue => e
  puts e
end