Class: Px4LogReader::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/px4_log_reader/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options) ⇒ Reader

Returns a new instance of Reader.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



120
121
122
# File 'lib/px4_log_reader/reader.rb', line 120

def context
  @context
end

#progressObject (readonly)

Returns the value of attribute progress.



119
120
121
# File 'lib/px4_log_reader/reader.rb', line 119

def progress
  @progress
end

Instance Method Details

#descriptors(&block) ⇒ Object

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.

Parameters:

  • optional (block)

    block is passed each descriptor as it is read



147
148
149
150
151
152
153
154
155
156
157
158
159
# 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 = {}, &block) ⇒ Object

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.

Parameters:

  • options (Hash) (defaults to: {})

    options

  • block (Block)

    block takes message as argument



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# 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) ⇒ Object

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

Parameters:

  • offset (Fixnum)

    File offset in bytes



235
236
237
238
239
240
241
# 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