Class: SGS::GPS

Inherits:
RedisBase show all
Defined in:
lib/sgs/gps.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RedisBase

#count, #count_name, #load, load, #make_redis_name, #publish, redis, redis_handle, #redis_read_var, #save, #save_and_publish, setup, subscribe, to_redis, var_init

Constructor Details

#initialize(lat = nil, long = nil) ⇒ GPS

Create a new GPS record. Lat/Long are in radians.



43
44
45
46
47
48
49
50
51
# File 'lib/sgs/gps.rb', line 43

def initialize(lat = nil, long = nil)
  @time = Time.new(2000, 1, 1)
  @location = Location.new(lat, long)
  @sog = 0.0
  @cmg = 0.0
  @magvar = nil
  @valid = false
  super()
end

Instance Attribute Details

#cmgObject

Returns the value of attribute cmg.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def cmg
  @cmg
end

#locationObject

Returns the value of attribute location.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def location
  @location
end

#magvarObject

Returns the value of attribute magvar.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def magvar
  @magvar
end

#sogObject

Returns the value of attribute sog.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def sog
  @sog
end

#timeObject

Returns the value of attribute time.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def time
  @time
end

#valObject

Returns the value of attribute val.



39
40
41
# File 'lib/sgs/gps.rb', line 39

def val
  @val
end

Class Method Details

.daemonObject

Main daemon function (called from executable)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sgs/gps.rb', line 55

def self.daemon
  puts "GPS reader starting up..."
  config = Config.load

  sp = SerialPort.new config.gps_device, config.gps_speed
  sp.read_timeout = 10000

  loop do
    nmea = NMEA.parse sp.readline
    if nmea.is_gprmc?
      gps = nmea.parse_gprmc
      p gps
      gps.save_and_publish if gps and gps.valid?
    end
  end
end

Instance Method Details

#force(lat, long, time = nil) ⇒ Object

Hard-code a GPS value (usually for debugging purposes)



74
75
76
77
78
# File 'lib/sgs/gps.rb', line 74

def force(lat, long, time = nil)
  @time = time || Time.now
  @location = Location.new(lat, long)
  @valid = true
end

#is_validObject

Set the validity



82
83
84
# File 'lib/sgs/gps.rb', line 82

def is_valid
  @valid = true
end

#to_sObject

Display the GPS data as a useful string (in degrees)



94
95
96
97
98
99
100
# File 'lib/sgs/gps.rb', line 94

def to_s
  if valid?
    "@#{@time.strftime('%Y%m%d-%T')}, #{@location}, SOG:#{@sog}, CMG:#{@cmg}"
  else
    "GPS error"
  end
end

#valid?Boolean

Is the GPS data valid?

Returns:

  • (Boolean)


88
89
90
# File 'lib/sgs/gps.rb', line 88

def valid?
  @valid == true
end