Class: Fit4Ruby::FitDataRecord

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

Instance Method Summary collapse

Methods included from Converters

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

Constructor Details

#initialize(record_id) ⇒ FitDataRecord



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fit4ruby/FitDataRecord.rb', line 22

def initialize(record_id)
  @message = GlobalFitMessages.find_by_name(record_id)

  # Create instance variables that correspond to every field of the
  # corresponding FIT data record.
  @message.fields_by_name.each do |name, field|
    create_instance_variable(name)
  end
  # Meta fields are additional fields that are not part of the FIT
  # specification but are convenient to have. These are typcially
  # aggregated or converted values of regular fields.
  @meta_field_units = {}
  @timestamp = Time.now
end

Instance Method Details

#<=>(fdr) ⇒ Object



93
94
95
# File 'lib/fit4ruby/FitDataRecord.rb', line 93

def <=>(fdr)
  @timestamp <=> fdr.timestamp
end

#==(fdr) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fit4ruby/FitDataRecord.rb', line 76

def ==(fdr)
  @message.fields_by_name.each do |name, field|
    ivar_name = '@' + name
    v1 = field.fit_to_native(field.native_to_fit(
      instance_variable_get(ivar_name)))
    v2 = field.fit_to_native(field.native_to_fit(
      fdr.instance_variable_get(ivar_name)))

    unless v1 == v2
      Log.error "#{name}: #{v1} != #{v2}"
      return false
    end
  end

  true
end

#get(name) ⇒ Object



53
54
55
56
57
58
# File 'lib/fit4ruby/FitDataRecord.rb', line 53

def get(name)
  ivar_name = '@' + name
  return nil unless instance_variable_defined?(ivar_name)

  instance_variable_get(ivar_name)
end

#get_as(name, to_unit) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fit4ruby/FitDataRecord.rb', line 60

def get_as(name, to_unit)
  value = respond_to?(name) ? send(name) : get(name)
  return nil if value.nil?

  if @meta_field_units.include?(name)
    unit = @meta_field_units[name]
  else
    field = @message.fields_by_name[name]
    unless (unit = field.opts[:unit])
      Log.fatal "Field #{name} has no unit"
    end
  end

  value * conversion_factor(unit, to_unit)
end

#inspectObject



150
151
152
153
154
155
156
157
# File 'lib/fit4ruby/FitDataRecord.rb', line 150

def inspect
  fields = {}
  @message.fields_by_name.each do |name, field|
    ivar_name = '@' + field.name
    fields[field.name] = instance_variable_get(ivar_name)
  end
  fields.inspect
end

#set(name, value) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/fit4ruby/FitDataRecord.rb', line 43

def set(name, value)
  ivar_name = '@' + name
  unless instance_variable_defined?(ivar_name)
    Log.warn("Unknown FIT record field '#{name}' in global message " +
             "#{@message.name} (#{@message.number}).")
    return
  end
  instance_variable_set(ivar_name, value)
end

#set_field_values(field_values) ⇒ Object



37
38
39
40
41
# File 'lib/fit4ruby/FitDataRecord.rb', line 37

def set_field_values(field_values)
  field_values.each do |field, value|
    set(field.to_s, value)
  end
end

#write(io, id_mapper) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fit4ruby/FitDataRecord.rb', line 97

def write(io, id_mapper)
  # Construct a GlobalFitMessage object that matches exactly the provided
  # set of fields. It does not contain any AltField objects.
  fields = {}
  @message.fields_by_name.each_key do |name|
    fields[name] = instance_variable_get('@' + name)
  end
  global_fit_message = @message.construct(fields)

  # Map the global message number to the current local message number.
  unless (local_message_number = id_mapper.get_local(global_fit_message))
    # If the current dictionary does not contain the global message
    # number, we need to create a new entry for it. The index in the
    # dictionary is the local message number.
    local_message_number = id_mapper.add_global(global_fit_message)
    # Write the definition of the global message number to the file.
    global_fit_message.write(io, local_message_number)
  end

  # Write data record header.
  header = FitRecordHeader.new
  header.normal = 0
  header.message_type = 0
  header.local_message_type = local_message_number
  header.write(io)

  # Create a BinData::Struct object to store the data record.
  fields = []
  global_fit_message.fields_by_number.each do |field_number, field|
    bin_data_type = FitDefinitionField.fit_type_to_bin_data(field.type)
    fields << [ bin_data_type, field.name ]
  end
  bd = BinData::Struct.new(:endian => :little, :fields => fields)

  # Fill the BinData::Struct object with the values from the corresponding
  # instance variables.
  global_fit_message.fields_by_number.each do |field_number, field|
    iv = "@#{field.name}"
    if instance_variable_defined?(iv) &&
       !(iv_value = instance_variable_get(iv)).nil?
      value = field.native_to_fit(iv_value)
    else
      # If we don't have a corresponding variable or the variable is nil
      # we write the 'undefined' value instead.
      value = FitDefinitionField.undefined_value(field.type)
    end
    bd[field.name] = value
  end

  # Write the data record to the file.
  bd.write(io)
end