Class: NMEAParser
- Inherits:
-
Object
- Object
- NMEAParser
- Defined in:
- lib/nmea_parser.rb
Instance Attribute Summary collapse
-
#latitude ⇒ Object
readonly
Returns the value of attribute latitude.
-
#longitude ⇒ Object
readonly
Returns the value of attribute longitude.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
-
#initialize(s = nil) ⇒ NMEAParser
constructor
A new instance of NMEAParser.
- #parse(raw_line) ⇒ Object
- #to_h ⇒ Object
- #to_struct ⇒ Object
Constructor Details
#initialize(s = nil) ⇒ NMEAParser
Returns a new instance of NMEAParser.
14 15 16 |
# File 'lib/nmea_parser.rb', line 14 def initialize(s=nil) parse(s) if s end |
Instance Attribute Details
#latitude ⇒ Object (readonly)
Returns the value of attribute latitude.
12 13 14 |
# File 'lib/nmea_parser.rb', line 12 def latitude @latitude end |
#longitude ⇒ Object (readonly)
Returns the value of attribute longitude.
12 13 14 |
# File 'lib/nmea_parser.rb', line 12 def longitude @longitude end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
12 13 14 |
# File 'lib/nmea_parser.rb', line 12 def time @time end |
Instance Method Details
#parse(raw_line) ⇒ Object
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 |
# File 'lib/nmea_parser.rb', line 18 def parse(raw_line) msgcode = raw_line[/^\$GP(\w+)/] return unless msgcode a = raw_line.split(',') case msgcode when /GGA/ _, raw_time, raw_lat, ns, raw_lon, ew = a @time, @latitude, @longitude = Time.parse(raw_time), decimalize(raw_lat, ns), decimalize(raw_lon, ew) # additional data captured # # Fix quality, Number of satellites being tracked, Horizontal dilution # of position, Altitude (Meters, above mean sea level), Height of # geoid, time in seconds since last DGPS update, DGPS station ID number @quality, @num_sat, @hdop, @altitude, @alt_unit, @height_geoid, @height_geoid_unit, @last_dgps, @dgps = a[6..-2] << a[-1][/^[^\*]+/] when /RMC/ _, raw_time, _, raw_lat, ns, raw_lon, ew = a @time, @latitude, @longitude = Time.parse(raw_time), decimalize(raw_lat, ns), decimalize(raw_lon, ew) # additional data captured # # Speed over the ground in knots, Track angle in degrees True, # Date, Magnetic Variation, @speed, @course = a[7..8] @date, @variation = Date.parse(a[9]), a[10] @var_direction = a[-1][/^[^\*]+/] when /VTG/ # data captured # True track made good (degrees), Magnetic track made good, # Ground speed (knots), Ground speed (Kilometers per hour) @track, @mag_track, @speed, @speed_kph = a.values_at(1,3,5,7) end end |
#to_h ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/nmea_parser.rb', line 70 def to_h() { time: @time, latitude: @latitude, longitude: @longitude, quality: @quality, num_sat: @num_sat, hdop: @hdop, altitude: @altitude, alt_unit: @alt_unit, height_geoid: @height_geoid, height_geoid_unit: @height_geoid_unit, last_dgps: @last_dgps, dgps: @dgps, speed: @speed, course: @course, date: @date, variation: @variation, var_direction: @var_direction, track: @track, mag_track: @mag_track, speed: @speed, speed_kph: @speed_kph } end |
#to_struct ⇒ Object
82 83 84 85 |
# File 'lib/nmea_parser.rb', line 82 def to_struct() h = self.to_h Struct.new(*h.keys.map(&:to_sym)).new(*h.values).freeze end |