Class: Fit4Ruby::FitMessageRecord

Inherits:
Object
  • Object
show all
Includes:
BDFieldNameTranslator
Defined in:
lib/fit4ruby/FitMessageRecord.rb

Overview

The FitMessageRecord models a part of the FIT file that contains the FIT message records. Each message record has a number, a local type and a set of data fields. The content of a FitMessageRecord is defined by the FitDefinition. This class is only used for reading data from a FIT file. For writing FIT message records, the class FitDataRecord and its decendents are used.

Constant Summary

Constants included from BDFieldNameTranslator

BDFieldNameTranslator::BD_DICT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BDFieldNameTranslator

#to_bd_field_name

Constructor Details

#initialize(definition) ⇒ FitMessageRecord

Returns a new instance of FitMessageRecord.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fit4ruby/FitMessageRecord.rb', line 35

def initialize(definition)
  @definition = definition
  @global_message_number = definition.global_message_number.snapshot

  if (@gfm = GlobalFitMessages[@global_message_number])
    @name = @gfm.name
  else
    @name = "message#{@global_message_number}"
    Log.debug { "Unknown global message number #{@global_message_number}" }
  end
  @message_record = produce(definition)
end

Instance Attribute Details

#global_message_numberObject (readonly)

Returns the value of attribute global_message_number.



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

def global_message_number
  @global_message_number
end

#message_recordObject (readonly)

Returns the value of attribute message_record.



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

def message_record
  @message_record
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#read(io, entity, filter = nil, fields_dump = nil, fit_entity) ⇒ Object



48
49
50
51
52
53
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
80
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
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
# File 'lib/fit4ruby/FitMessageRecord.rb', line 48

def read(io, entity, filter = nil, fields_dump = nil, fit_entity)
  @message_record.read(io)

  if @name == 'file_id'
    # Caveat: 'type' is used as '_type' in BinData fields!
    unless (entity_type = @message_record['_type'].snapshot)
      Log.fatal "Corrupted FIT file: file_id record has no type definition"
    end
    entity.set_type(entity_type)
  end
  obj = entity.new_fit_data_record(@name)

  # It's important to ensure that alternative fields are processed after
  # the regular fields so that the decision field has already been set.
  sorted_fields = @definition.data_fields.sort do |f1, f2|
    f1alt = is_alt_field?(f1)
    f2alt = is_alt_field?(f2)
    f1alt == f2alt ?
      f1.field_definition_number.snapshot <=>
      f2.field_definition_number.snapshot :
      f1alt ? 1 : -1
  end

  sorted_fields.each do |field|
    value = @message_record[to_bd_field_name(field.name)].snapshot
    # Strings are null byte terminated. There may be more bytes in the
    # file, but we have to discard all bytes from the first null byte
    # onwards.
    if value.is_a?(String)
      if (null_byte = value.index("\0"))
        value = null_byte == 0 ? '' : value[0..(null_byte - 1)]
      end
      if value.empty?
        value = nil
      end
    end

    field_name, field_def = get_field_name_and_global_def(field, obj)
    obj.set(field_name, v = (field_def || field).to_machine(value)) if obj

    if filter && fields_dump &&
       (filter.field_names.nil? ||
        filter.field_names.include?(field_name)) &&
       !(((value.is_a?(String) &&
           (value.count(field.undefined_value.chr) == value.length)) ||
          value == field.undefined_value) && filter.ignore_undef)
      fields_dump << DumpedField.new(
        @global_message_number,
        field.field_definition_number.snapshot,
        field_name,
        field.type(true),
        (field_def ? field_def : field).to_s(value))
    end
  end

  @definition.developer_fields.each do |field|
    # Find the corresponding field description for the given developer and
    # field number.
    field_number = field.field_number.snapshot

    unless (field_description = field.find_field_definition)
      Log.error "There is no field definition for developer " +
        "#{field.developer_data_index} field #{field_number}"
      next
    end

    field_name = field_description.full_field_name(fit_entity)
    units = field_description.units
    type = field.type
    native_message_number = field_description.native_mesg_num
    native_field_number = field_description.native_field_num

    value = @message_record[field.name].snapshot
    value = nil if value == field.undefined_value

    obj.set(field_name, value) if obj

    if filter && fields_dump &&
       (filter.field_names.nil? ||
        filter.field_names.include?(field_name)) &&
       !(((value.respond_to?('count') &&
           (value.count(field.undefined_value) == value.length)) ||
          value == field.undefined_value) && filter.ignore_undef)
      fields_dump << DumpedField.new(
        native_message_number,
        256 * (1 + field.developer_data_index) + field_number,
        field_name, type, field.to_s(value))
    end
  end

  if @name == 'field_description'
    obj.create_global_definition(fit_entity)
  end
end