Class: Px4LogReader::Reader
- Inherits:
-
Object
- Object
- Px4LogReader::Reader
- Defined in:
- lib/px4_log_reader/reader.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#progress ⇒ Object
readonly
Returns the value of attribute progress.
Instance Method Summary collapse
-
#descriptors(&block) ⇒ Object
Get the list of descriptors associated with the open PX4 log file.
-
#each_message(options = {}, &block) ⇒ Object
Iterate over all log messages.
-
#initialize(file, options) ⇒ Reader
constructor
A new instance of Reader.
-
#seek(offset) ⇒ Object
Seek to the specified file offset.
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, ) opts = { cache_filename: '', }.merge( ) @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
#context ⇒ Object (readonly)
Returns the value of attribute context.
120 121 122 |
# File 'lib/px4_log_reader/reader.rb', line 120 def context @context end |
#progress ⇒ Object (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.
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.
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 ( = {}, &block ) opts ={ with: [], # white list - empty means all minus those in without list without: ['FMT'] # black list - includes types or names }.merge( || {} ) 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 , offset = LogFile::( @log_file, @message_descriptors ) break if .nil? # Add message to the set of latest messages. @context.set( ) if opts[:with].empty? if !opts[:without].include?( .descriptor.type ) yield end else if opts[:with].include?( .descriptor.type ) yield 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.
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 |