class Px4LogReader::Reader

Attributes

context[R]
progress[R]

Public Class Methods

new( file, options ) click to toggle source
# File lib/px4_log_reader/reader.rb, line 122
def initialize( file, options )

        opts = {
                cache_filename: '',
        }.merge( options )

        @message_descriptors = {}
        @descriptor_cache = nil
        @context = Context.new

        @log_file = file
        @progress = Progress.new( @log_file )

        @descriptor_cache = MessageDescriptorCache.new( opts[:cache_filename] )
end

Public Instance Methods

descriptors( &block ) click to toggle source

Get the list of descriptors associated with the open PX4 log file. If a valid descriptor cache was specified at startup, the descriptors are loaded from the cache. Otherwise, the descriptors are parsed from the open log.

@param [block] optional block is passed each descriptor as it is read @return descriptors [Array] Array of descriptors

# File lib/px4_log_reader/reader.rb, line 147
def descriptors( &block )
        if @log_file && @message_descriptors.empty?
                if @descriptor_cache && @descriptor_cache.exist?
                        @message_descriptors = @descriptor_cache.read_descriptors
                else
                        @message_descriptors = LogFile::read_descriptors( @log_file, @descriptor_cache, &block )
                end

                @message_descriptors[ FORMAT_MESSAGE.type ] = FORMAT_MESSAGE
        end

        return @message_descriptors
end
each_message( options = {} ) { |message| ... } click to toggle source

Iterate over all log messages. Embedded message descriptors are skipped. If a “with” list is supplied, only messages in the list are passed to the caller-supplied block. If a “without” list supplied, all messages except those in the list are passed to the caller-supplied block. The caller must supply a block.

@param options [Hash] options @param block [Block] block takes message as argument

# File lib/px4_log_reader/reader.rb, line 171
def each_message( options = {}, &block )

        opts ={
                with: [],        # white list - empty means all minus those in without list
                without: ['FMT'] # black list - includes types or names
        }.merge( options || {} )

        opts[:with].map! do |val|
                if val.class == String
                        descriptor = descriptors.values.find { |desc| desc.name == val }
                        if descriptor
                                val = descriptor.type
                        else
                                puts "Failed to find descriptor with name '#{val}'"
                        end
                end
        end

        opts[:without].map! do |val|
                if val.class == String
                        descriptor = descriptors.values.find { |desc| desc.name == val }

                        if descriptor
                                val = descriptor.type
                        else
                                raise "Failed to find descriptor with name '#{val}'"
                        end
                end
        end

        if block_given?

                loop do

                        message, offset = LogFile::read_message( @log_file, @message_descriptors )
                        break if message.nil?

                        # Add message to the set of latest messages.
                        @context.set( message )

                        if opts[:with].empty?
                                if !opts[:without].include?( message.descriptor.type )
                                        yield message
                                end
                        else
                                if opts[:with].include?( message.descriptor.type )
                                        yield message
                                end
                        end

                end

        else
                raise BlockRequiredError.new
        end

end
seek( offset ) click to toggle source

Seek to the specified file offset. If the offset is greater than the file size, seeks to the end of the file.

@param offset [Fixnum] File offset in bytes

# File lib/px4_log_reader/reader.rb, line 235
def seek( offset )
        if @log_file
                seek_offset = offset
                seek_offset = @progress.file_size if offset > @progress.file_size
                @log_file.seek( seek_offset, IO::SEEK_SET )
        end
end