Class: Gps::Nmea::Sentence

Inherits:
Object
  • Object
show all
Defined in:
lib/gps/nmea/sentence.rb

Direct Known Subclasses

Gga, Gll, Gsa, Gsv, Rmc, Vtg, Zda

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lineObject

Returns the value of attribute line.



4
5
6
# File 'lib/gps/nmea/sentence.rb', line 4

def line
  @line
end

Class Method Details

.date_part_to_str(date_part) ⇒ Object



29
30
31
32
# File 'lib/gps/nmea/sentence.rb', line 29

def self.date_part_to_str(date_part)
  century = date_part[4..5].to_i > 70 ? 1900 : 2000
  "#{century + date_part[4..5].to_i}-#{date_part[2..3]}-#{date_part[0..1]}"
end

.parse(line) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gps/nmea/sentence.rb', line 6

def self.parse(line)
  case
  when line.start_with?('$GPGGA')
    Gga.new line
  when line.start_with?('$GPGLL')
    Gll.new line
  when line.start_with?('$GPGSV')
    Gsv.new line
  when line.start_with?('$GPGSA')
    Gsa.new line
  when line.start_with?('$GPVTG')
    Vtg.new line
  when line.start_with?('$GPRMC')
    Rmc.new line
  else
    nil
  end
end

.time_part_to_str(time_part) ⇒ Object



25
26
27
# File 'lib/gps/nmea/sentence.rb', line 25

def self.time_part_to_str(time_part)
  "#{time_part[0..1]}:#{time_part[2..3]}:#{time_part[4..5]}"
end

Instance Method Details

#checksum_valid?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/gps/nmea/sentence.rb', line 34

def checksum_valid?
  get_checksum == generate_checksum
end

#generate_checksumObject



42
43
44
45
46
47
48
49
# File 'lib/gps/nmea/sentence.rb', line 42

def generate_checksum
  data = line.gsub('$', '').split('*')[0]
  res = 0
  data.split('').each do |c|
    res ^= c.ord
  end
  res
end

#get_checksumObject



38
39
40
# File 'lib/gps/nmea/sentence.rb', line 38

def get_checksum
  line.split(',')[-1].split('*')[1].to_i 16
end

#has_coordinates?Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'lib/gps/nmea/sentence.rb', line 94

def has_coordinates?
  !(@latitude.nil? || @longitude.nil? ||
      @latitude_direction.nil? || @longitude_direction.nil?)
end

#has_datetime?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/gps/nmea/sentence.rb', line 99

def has_datetime?
  respond_to? :datetime
end

#lat_decObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/gps/nmea/sentence.rb', line 51

def lat_dec
  return nil if @latitude.nil?
  return nil if @latitude_direction.nil?

  lat, lat_dec = @latitude.to_s.split '.'
  lat = lat.rjust 4, '0'
  res = lat[0..1].to_i + ("#{lat[2..3]}.#{lat_dec}".to_f / 60.0)
  res *= -1 if @latitude_direction == 'S'
  res
end

#lat_long_decObject



73
74
75
76
# File 'lib/gps/nmea/sentence.rb', line 73

def lat_long_dec
  return nil unless has_coordinates?
  "#{lat_dec} #{long_dec}"
end

#long_decObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/gps/nmea/sentence.rb', line 62

def long_dec
  return nil if @longitude.nil?
  return nil if @longitude_direction.nil?

  long, long_dec = @longitude.to_s.split '.'
  long = long.rjust 5, '0'
  res = long[0..2].to_i + ("#{long[3..4]}.#{long_dec}".to_f / 60.0)
  res *= -1 if @longitude_direction == 'W'
  res
end

#to_hObject



78
79
80
81
82
83
# File 'lib/gps/nmea/sentence.rb', line 78

def to_h
  self.class.instance_methods(false)
    .reject { |a| a.to_s.end_with? '=' }
    .map { |a| [a.to_s, self.send(a)] }
    .to_h.merge({ type: self.class.to_s.split('::').last.upcase })
end

#to_json(pretty = false) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/gps/nmea/sentence.rb', line 85

def to_json(pretty=false)
  require 'json'
  if pretty
    JSON.pretty_generate to_h
  else
    to_h.to_json
  end
end