Class: MAVLink::Log::File

Inherits:
Object
  • Object
show all
Defined in:
lib/mavlink/log/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ File



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mavlink/log/file.rb', line 18

def initialize(uri)
  @entries = []
  @messages = []
  open(uri, 'rb') do |file|
    loop do
      raw_time = file.read(8)
      break if raw_time.nil?

      header = Header.new(file.read(6))
      raise "Unexpected magic number (#{header.magic})" unless header.magic == 0xFE

      payload = file.read(header.length)
      raw_crc = file.read(2)
      @entries << Entry.new(raw_time, header, payload, raw_crc)
      @messages << Messages::Factory.build(@entries.last)
    end
  end

  if @entries.empty?
    raise 'No entries found in file'
  end
rescue => e
  unless @entries.length >= 2
    # bad ending message, give the file benefit of the doubt...
    raise ArgumentError, "File does not appear to be an MAVLink log (#{e})"
  end
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



8
9
10
# File 'lib/mavlink/log/file.rb', line 8

def entries
  @entries
end

#messagesObject (readonly)

Returns the value of attribute messages.



8
9
10
# File 'lib/mavlink/log/file.rb', line 8

def messages
  @messages
end

Class Method Details

.mavlink?(uri) ⇒ MAVLink::Log::File

Determines if the file at the given URI is a MAVLink telemetry log file.



14
15
16
# File 'lib/mavlink/log/file.rb', line 14

def self.mavlink?(uri)
  File.new(uri) rescue nil
end

Instance Method Details

#durationFloat

Gets the duration of the session, in seconds.



49
50
51
52
# File 'lib/mavlink/log/file.rb', line 49

def duration
  return 0 if @entries.empty?
  ended_at - started_at
end

#ended_atFloat

Gets the ending time as a Unix Epoch time stamp in seconds.



64
65
66
# File 'lib/mavlink/log/file.rb', line 64

def ended_at
  time_in_seconds(@entries.last.time)
end

#started_atFloat

Gets the starting time as a Unix Epoch time stamp in seconds.



57
58
59
# File 'lib/mavlink/log/file.rb', line 57

def started_at
  time_in_seconds(@entries.first.time)
end