Class: GPS::Receiver

Inherits:
Object
  • Object
show all
Defined in:
lib/gpsd.rb

Constant Summary collapse

DEFAULT_HOSTNAME =
'localhost'
DEFAULT_PORT =
2947
DEFAULT_TIMEOUT =
5
DEFAULT_MAX_ATTEMPT =
3
DEFAULT_RETRY_DELAY =
10
DEFAULT_READ_TIMEOUT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Receiver

Returns a new instance of Receiver.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gpsd.rb', line 25

def initialize(options={})
  @_options = options

# set default options
  @_options[:timeout]      ||= DEFAULT_TIMEOUT
  @_options[:read_timeout] ||= DEFAULT_READ_TIMEOUT
  @_options[:hostname]     ||= DEFAULT_HOSTNAME
  @_options[:port]         ||= DEFAULT_PORT
  @_options[:retry_delay]  ||= DEFAULT_RETRY_DELAY
  @_options[:attempts]     ||= DEFAULT_MAX_ATTEMPT

  connect() unless options[:autoconnect] == false
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



23
24
25
# File 'lib/gpsd.rb', line 23

def filename
  @filename
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



23
24
25
# File 'lib/gpsd.rb', line 23

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



23
24
25
# File 'lib/gpsd.rb', line 23

def port
  @port
end

#versionObject (readonly)

Returns the value of attribute version.



23
24
25
# File 'lib/gpsd.rb', line 23

def version
  @version
end

Instance Method Details

#closeObject



77
78
79
# File 'lib/gpsd.rb', line 77

def close()
  socket().close() if not socket().closed?
end

#command(command, options = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/gpsd.rb', line 107

def command(command, options={})
  block = options.delete(:block)
  send_command(command, options)

# wait for reply
  socket().wait(@_options[:read_timeout])

  return read_response(block)
end

#connect(options = nil) ⇒ Object



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
# File 'lib/gpsd.rb', line 39

def connect(options=nil)
  attempts = 0
  options = @_options.merge(options || {})

  Timeout.timeout(options[:timeout]) do
  # connect to a UNIX socket
    if options[:socket]
      raise GPS::Error.new("File #{options[:socket]} is not a socket") unless File.socket?(options[:socket])
      @filename = options[:socket]
      @socket = UNIXSocket.new(options[:socket])
    else
      begin
        attempts += 1
        @hostname = options[:hostname]
        @port     = options[:port]
        @socket = TCPSocket.new(options[:hostname], options[:port].to_i)
      rescue Errno::ECONNREFUSED
        raise GPS::ConnectionError.new("Could not connect to #{options[:hostname]}:#{options[:port]} after #{attempts} attempts, connection is closed") if attempts >= options[:attempts]
        sleep options[:retry_delay]
        retry
      end
    end

    if (gps_ident = GPS::Response.new(socket().gets()))
      raise GPS::ProtocolError.new("Problem connecting to GPSd: invalid banner") if gps_ident.nil? or gps_ident.classname != :VERSION
      @version = gps_ident.release
    end
  end
end

#pollObject



127
128
129
130
131
132
133
# File 'lib/gpsd.rb', line 127

def poll()
  return command(:poll).select{|i|
    i.classname == :POLL
  }.sort{|a,b|
    a.time <=> b.time
  }
end

#read_response(blocking = true) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gpsd.rb', line 92

def read_response(blocking=true)
  rv = []

  if blocking == true
    return GPS::Response.new(socket().gets())
  else
    while socket().ready?
      v = socket().gets()
      rv << GPS::Response.new(v)
    end
  end

  return rv
end

#send_command(cmd, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/gpsd.rb', line 81

def send_command(cmd, options={})
  cmdline = "?#{cmd.to_s.upcase}#{options.empty? ? '' : '='+MultiJson.dump(options)};"

  begin
    socket().puts(cmdline)
  rescue Errno::EPIPE
    connect()
    retry
  end
end

#socketObject



69
70
71
72
73
74
75
# File 'lib/gpsd.rb', line 69

def socket()
  if @socket.closed?
    connect()
  end

  return @socket
end

#watch(options = {}) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/gpsd.rb', line 118

def watch(options={})
  options = ({
    :enable => true,
    :json   => true
  }) if options.empty?

  return command(:watch, options)
end