lib/fit4ruby/FitDefinitionField.rb in fit4ruby-0.0.4 vs lib/fit4ruby/FitDefinitionField.rb in fit4ruby-0.0.5
- old
+ new
@@ -15,10 +15,15 @@
require 'fit4ruby/Log'
require 'fit4ruby/GlobalFitMessage'
module Fit4Ruby
+ # The FitDefinitionField models the part of the FIT file that contains the
+ # template definition for a field of a FitMessageRecord. It should match the
+ # corresponding definition in GlobalFitMessages. In case we don't have a
+ # known entry in GlobalFitMessages we can still read the file since we know
+ # the exact size of the binary records.
class FitDefinitionField < BinData::Record
@@TypeDefs = [
# FIT Type, BinData type, undefined value, bytes
[ 'enum', 'uint8', 0xFF, 1 ],
@@ -26,11 +31,11 @@
[ 'uint8', 'uint8', 0xFF, 1 ],
[ 'sint16', 'int16', 0x7FFF, 2 ],
[ 'uint16', 'uint16', 0xFFFF, 2 ],
[ 'sint32', 'int32', 0x7FFFFFFF, 4 ],
[ 'uint32', 'uint32', 0xFFFFFFFF, 4 ],
- [ 'string', 'stringz', '', 0 ],
+ [ 'string', 'string', '', 0 ],
[ 'float32', 'float', 0xFFFFFFFF, 4 ],
[ 'float63', 'double', 0xFFFFFFFF, 4 ],
[ 'uint8z', 'uint8', 0, 1 ],
[ 'uint16z', 'uint16', 0, 2 ],
[ 'uint32z', 'uint32', 0, 4 ],
@@ -60,13 +65,14 @@
def init
@global_message_number = parent.parent.global_message_number.snapshot
@global_message_definition = GlobalFitMessages[@global_message_number]
field_number = field_definition_number.snapshot
if @global_message_definition &&
- (field = @global_message_definition.fields[field_number])
- @name = field.name
- @type = field.type
+ (field = @global_message_definition.fields_by_number[field_number])
+ @name = field.respond_to?('name') ? field.name :
+ "choice_#{field_number}"
+ @type = field.respond_to?('type') ? field.type : nil
if @type && (td = @@TypeDefs[base_type_number]) && td[0] != @type
Log.warn "#{@global_message_number}:#{@name} must be of type " +
"#{@type}, not #{td[0]}"
end
@@ -86,28 +92,42 @@
def to_machine(value)
init unless @global_message_number
value = nil if value == undefined_value
field_number = field_definition_number.snapshot
- if @global_message_definition &&
- (field = @global_message_definition.fields[field_number])
- field.to_machine(value)
+ if value.kind_of?(Array)
+ ary = []
+ value.each { |v| ary << to_machine(v) }
+ ary
else
- value
+ if @global_message_definition &&
+ (field = @global_message_definition.
+ fields_by_number[field_number]) &&
+ field.respond_to?('to_machine')
+ field.to_machine(value)
+ else
+ value
+ end
end
end
def to_s(value)
init unless @global_message_number
value = nil if value == undefined_value
- field_number = field_definition_number.snapshot
- if @global_message_definition &&
- (field = @global_message_definition.fields[field_number])
- field.to_s(value)
+ if value.kind_of?(Array)
+ s = '[ '
+ value.each { |v| s << to_s(v) + ' ' }
+ s + ']'
else
- "[#{value.to_s}]"
+ field_number = field_definition_number.snapshot
+ if @global_message_definition &&
+ (field = @global_message_definition.fields_by_number[field_number])
+ field.to_s(value)
+ else
+ "[#{value.to_s}]"
+ end
end
end
def set_type(fit_type)
idx = @@TypeDefs.index { |x| x[0] == fit_type }
@@ -115,22 +135,44 @@
self.base_type_number = idx
self.byte_count = @@TypeDefs[idx][3]
end
def type(fit_type = false)
- if @@TypeDefs.length <= base_type_number.snapshot
- Log.fatal "Unknown FIT Base type #{base_type_number.snapshot}"
+ check_fit_base_type
+ @@TypeDefs[base_type_number.snapshot][fit_type ? 0 : 1]
+ end
+
+ def is_array?
+ if total_bytes > base_type_bytes
+ if total_bytes % base_type_bytes != 0
+ Log.fatal "Total bytes (#{total_bytes}) must be multiple of " +
+ "base type bytes (#{base_type_bytes})."
+ end
+ return true
end
+ false
+ end
- @@TypeDefs[base_type_number.snapshot][fit_type ? 0 : 1]
+ def total_bytes
+ self.byte_count.snapshot
end
+ def base_type_bytes
+ check_fit_base_type
+ @@TypeDefs[base_type_number.snapshot][3]
+ end
+
def undefined_value
+ check_fit_base_type
+ @@TypeDefs[base_type_number.snapshot][2]
+ end
+
+ private
+
+ def check_fit_base_type
if @@TypeDefs.length <= base_type_number.snapshot
Log.fatal "Unknown FIT Base type #{base_type_number.snapshot}"
end
-
- @@TypeDefs[base_type_number.snapshot][2]
end
end
end