Class: Spektrum::Log::File

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ File

Returns a new instance of File.



16
17
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spektrum/log/file.rb', line 16

def initialize(uri)
  headers = []
  headers_complete = false
  records = []
  @flights = []

  first_word = true

  file = BufferedFile.new(uri, 'rb')
  loop do
    first4 = file.read(4)

    # quick check to see if this could even be a Spektrum TLM file
    if first_word
      if first4.nil? || (0xFFFFFFFF != first4.unpack('V')[0])
        raise ArgumentError, 'File does not appear to be a Spektrum log'
      end
      first_word = false
    end

    if first4.nil?
      if headers_complete || !records.empty?
        # we have records, this is a new entry
        @flights << Flight.new(headers, records)
        headers = []
        records = []
      end
      break
    end

    first = first4.unpack('V')[0]
    if 0xFFFFFFFF == first
      if headers_complete || !records.empty?
        # we have records, this is a new entry
        @flights << Flight.new(headers, records)
        headers = []
        records = []
        headers_complete = false
      end

      rest = file.read(32)
      headers << Headers.create(rest)

      headers_complete = rest.unpack('S')[0] == 0x1717
    else
      data = file.read(16)
      type = data[0].unpack('C')[0]
      records << Records.create(type, first, data)
    end
  end
rescue => e
  raise ArgumentError, "File does not appear to be a Spektrum log (#{e})"
ensure
  file.close
end

Instance Attribute Details

#flightsObject (readonly)

Returns the value of attribute flights.



6
7
8
# File 'lib/spektrum/log/file.rb', line 6

def flights
  @flights
end

#recordsObject (readonly)

Returns the value of attribute records.



6
7
8
# File 'lib/spektrum/log/file.rb', line 6

def records
  @records
end

Class Method Details

.spektrum?(uri) ⇒ Spektrum::Log::File

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

Parameters:

  • uri

    URI to file to read

Returns:



12
13
14
# File 'lib/spektrum/log/file.rb', line 12

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

Instance Method Details

#durationFloat

Gets the total duration of all flights contained within.

Returns:

  • (Float)

    total duration of all flights, in seconds



75
76
77
# File 'lib/spektrum/log/file.rb', line 75

def duration
  @flights.map(&:duration).reduce(&:+)
end

#to_kml(options = {}) ⇒ String

Converts the file into a KML document containing placemarks for each flight containing GPS data.

Parameters:

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

    hash containing options for file

Returns:

  • (String)

    KML document for all applicable flights in the file

Raises:

  • (RuntimeError)

See Also:



92
93
94
95
# File 'lib/spektrum/log/file.rb', line 92

def to_kml(options = {})
  raise RuntimeError, 'No coordinates available for KML generation' unless to_kml?
  to_kml_file(options).render
end

#to_kml?Boolean

Determines if KML methods can be called for this file.

Returns:

  • (Boolean)

    true if KML can be generated for this file, false otherwise



82
83
84
# File 'lib/spektrum/log/file.rb', line 82

def to_kml?
  @flights.any?(&:to_kml?)
end

#to_kml_file(options = {}) ⇒ KMLFile

Converts the file into a KMLFile containing placemarks for each flight containing GPS data.

Parameters:

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

    hash containing options for file

Options Hash (options):

  • :name (String)

    name option of KML::Document

  • :description (String)

    name option of KML::Document

Returns:

  • (KMLFile)

    file for the flight

Raises:

  • (RuntimeError)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/spektrum/log/file.rb', line 104

def to_kml_file(options = {})
  raise RuntimeError, 'No coordinates available for KML generation' unless to_kml?
  options = apply_default_file_options(options)

  style = 'kmlfile-style-id'
  kml_flights = @flights.select(&:to_kml?)
  marks = kml_flights.each_with_object({ :style_url => "##{style}" }).map(&:to_kml_placemark)

  kml = KMLFile.new
  kml.objects << KML::Document.new(
    :name => options[:name],
    :description => options[:description],
    :styles => [
      KML::Style.new(
        :id => style,
        :line_style => KML::LineStyle.new(:color => '7F00FFFF', :width => 4),
        :poly_style => KML::PolyStyle.new(:color => '7F00FF00')
      )
    ],
    :features => marks
  )
  kml
end