class Fit4Ruby::GlobalFitMessage::Field

The Field objects describe the name, type and optional attributes of a FitMessage definition field. It also provides methods to convert field values into various formats.

Attributes

name[R]
opts[R]
type[R]

Public Class Methods

new(type, name, opts = {}) click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 36
def initialize(type, name, opts = {})
  @type = type
  @name = name
  @opts = opts
end

Public Instance Methods

fit_to_native(value) click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 109
def fit_to_native(value)
  return nil if value == FitDefinitionFieldBase.undefined_value(@type)

  if @opts.include?(:dict) && (dict = GlobalFitDictionaries[@opts[:dict]])
    return dict.name(value) || "Undocumented value #{value}"
  end

  value /= @opts[:scale].to_f if @opts[:scale]
  value -= @opts[:offset] if @opts[:offset]

  case @opts[:type]
  when 'coordinate'
    value *= 180.0 / 2147483648
  when 'date_time'
    value = fit_time_to_time(value)
  end

  value
end
is_array?() click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 50
def is_array?
  @opts[:array] == true
end
is_string?() click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 46
def is_string?
  @type == 'string'
end
is_undefined?(value) click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 42
def is_undefined?(value)
  value == FitDefinitionFieldBase.undefined_value(@type)
end
native_to_fit(value) click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 129
def native_to_fit(value)
  return FitDefinitionFieldBase.undefined_value(@type) if value.nil?

  if @opts.include?(:dict) && (dict = GlobalFitDictionaries[@opts[:dict]])
    unless (dv = dict.value_by_name(value))
      Log.error "Unknown value '#{value}' assigned to field #{@name}"
      return FitDefinitionFieldBase.undefined_value(@type)
    else
      return dv
    end
  end

  value += @opts[:offset] if @opts[:offset]
  value = (value * @opts[:scale].to_f).to_i if @opts[:scale]

  case @opts[:type]
  when 'coordinate'
    value = (value * 2147483648.0 / 180.0).to_i
  when 'date_time'
    value = time_to_fit_time(value)
  end
  if @type != 'float32' && value.is_a?(Float) && @opts[:scale].nil?
    Log.error "Field #{@name} must not be a Float value"
  end

  value
end
to_human(value) click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 81
def to_human(value)
  return nil if value.nil?

  if @opts.include?(:dict) &&
     (dict = GlobalFitDictionaries[@opts[:dict]])
    return [ dict.name(value) || "Undocumented value #{value}", nil ]
  end

  value /= @opts[:scale].to_f if @opts[:scale]
  value -= @opts[:offset] if @opts[:offset]

  case @opts[:type]
  when 'coordinate'
    value *= 180.0 / 2147483648
  when 'date_time'
    value = fit_time_to_time(value).strftime("%Y-%m-%d %H:%M:%S")
  when 'duration'
    value = secsToDHMS(value)
  when 'activity_intensity'
    # Activity monitoring data contains a byte value that consists of 5
    # bit for the activity type and 3 bit for the intensity. Activty 0x8
    # is resting. Instead if value + unit we return activity type +
    # intensity here.
    return [ value & 0x1F, (value >> 5) & 0x7 ]
  end
  [ value, @opts[:unit] ]
end
to_machine(value) click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 54
def to_machine(value)
  return nil if value.nil? ||
    value == FitDefinitionFieldBase.undefined_value(@type)

  if @opts.include?(:dict) &&
     (dict = GlobalFitDictionaries[@opts[:dict]])
    return dict.name(value) || "Undocumented value #{value}"
  end

  case @opts[:type]
  when 'coordinate'
    value *= 180.0 / 2147483648
  when 'date_time'
    value = fit_time_to_time(value)
  when 'float'
    if value >= 4294967295.0
      return nil
    end
  end
  if value.is_a?(Float) && value >= 4294967295.0
    return nil
  end
  value /= @opts[:scale].to_f if @opts[:scale]
  value -= @opts[:offset] if @opts[:offset]
  value
end
to_s(value = nil) click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 157
def to_s(value = nil)
  return "[no value]" if value.nil?

  human_readable = to_human(value)
  "#{human_readable[0]}" +
    "#{ human_readable[1] ? " #{human_readable[1]}" : ''}"
end