Class: Fit4Ruby::GlobalFitMessage::Field

Inherits:
Object
  • Object
show all
Includes:
Converters
Defined in:
lib/fit4ruby/GlobalFitMessage.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Converters

#conversion_factor, #fit_time_to_time, #secsToDHMS, #secsToHM, #secsToHMS, #speedToPace, #time_to_fit_time

Constructor Details

#initialize(type, name, opts = {}) ⇒ Field

Returns a new instance of Field.



35
36
37
38
39
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 35

def initialize(type, name, opts = {})
  @type = type
  @name = name
  @opts = opts
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 33

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



33
34
35
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 33

def opts
  @opts
end

#typeObject (readonly)

Returns the value of attribute type.



33
34
35
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 33

def type
  @type
end

Instance Method Details

#fit_to_native(value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 88

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

#native_to_fit(value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 108

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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 60

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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 41

def to_machine(value)
  return nil if value.nil?

  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)
  end
  value /= @opts[:scale].to_f if @opts[:scale]
  value -= @opts[:offset] if @opts[:offset]
  value
end

#to_s(value) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 136

def to_s(value)
  return "[no value]" if value.nil?

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