Class: Spektrum::Log::File
- Inherits:
-
Object
- Object
- Spektrum::Log::File
- Defined in:
- lib/spektrum/log/file.rb
Instance Attribute Summary collapse
-
#flights ⇒ Object
readonly
Returns the value of attribute flights.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Class Method Summary collapse
-
.spektrum?(uri) ⇒ Spektrum::Log::File
Determines if the file at the given URI is a Spektrum telemetry log file.
Instance Method Summary collapse
-
#duration ⇒ Float
Gets the total duration of all flights contained within.
-
#initialize(uri) ⇒ File
constructor
A new instance of File.
-
#to_kml(options = {}) ⇒ String
Converts the file into a KML document containing placemarks for each flight containing GPS data.
-
#to_kml? ⇒ Boolean
Determines if KML methods can be called for this file.
-
#to_kml_file(options = {}) ⇒ KMLFile
Converts the file into a KMLFile containing placemarks for each flight containing GPS data.
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
#flights ⇒ Object (readonly)
Returns the value of attribute flights.
6 7 8 |
# File 'lib/spektrum/log/file.rb', line 6 def flights @flights end |
#records ⇒ Object (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.
12 13 14 |
# File 'lib/spektrum/log/file.rb', line 12 def self.spektrum?(uri) File.new(uri) rescue nil end |
Instance Method Details
#duration ⇒ Float
Gets the total duration of all flights contained within.
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.
92 93 94 95 |
# File 'lib/spektrum/log/file.rb', line 92 def to_kml( = {}) raise RuntimeError, 'No coordinates available for KML generation' unless to_kml? to_kml_file().render end |
#to_kml? ⇒ Boolean
Determines if KML methods can be called for this file.
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.
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( = {}) raise RuntimeError, 'No coordinates available for KML generation' unless to_kml? = () 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 => [:name], :description => [: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 |