class Fit4Ruby::FitMessageIdMapper

The FIT file maps GlobalFitMessage numbers to local numbers. Due to restrictions in the format, only 16 local messages can be active at any point in the file. If a GlobalFitMessage is needed that is currently not mapped, a new entry is generated and the least recently used message is evicted. The FitMessageIdMapper is the objects that stores those 16 active entries and can map global to local message numbers.

Public Class Methods

new() click to toggle source
# File lib/fit4ruby/FitMessageIdMapper.rb, line 27
def initialize
  @entries = Array.new(16, nil)
end

Public Instance Methods

add_global(message) click to toggle source

Add a new GlobalFitMessage to the mapper and return the local message number.

# File lib/fit4ruby/FitMessageIdMapper.rb, line 33
def add_global(message)
  unless (slot = @entries.index { |e| e.nil? })
    # No more free slots. We have to find the least recently used one.
    slot = 0
    0.upto(15) do |i|
      if i != slot && @entries[slot].last_use > @entries[i].last_use
        slot = i
      end
    end
  end
  @entries[slot] = Entry.new(message, Time.now)

  slot
end
get_local(message) click to toggle source

Get the local message number for a given GlobalFitMessage. If there is no message number, nil is returned.

# File lib/fit4ruby/FitMessageIdMapper.rb, line 50
def get_local(message)
  0.upto(15) do |i|
    if (entry = @entries[i]) && entry.global_message == message
      entry.last_use = Time.now
      return i
    end
  end
  nil
end