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.



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

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#fit_to_native(value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 50

def is_array?
  @opts[:array] == true
end

#is_string?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 46

def is_string?
  @type == 'string'
end

#is_undefined?(value) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 42

def is_undefined?(value)
  value == FitDefinitionFieldBase.undefined_value(@type)
end

#native_to_fit(value) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object



157
158
159
160
161
162
163
# 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