Class: GPS::Receiver
- Inherits:
-
Object
- Object
- GPS::Receiver
- 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
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #close ⇒ Object
- #command(command, options = {}) ⇒ Object
- #connect(options = nil) ⇒ Object
-
#initialize(options = {}) ⇒ Receiver
constructor
A new instance of Receiver.
- #poll ⇒ Object
- #read_response(blocking = true) ⇒ Object
- #send_command(cmd, options = {}) ⇒ Object
- #socket ⇒ Object
- #watch(options = {}) ⇒ Object
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 = # 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 [:autoconnect] == false end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
23 24 25 |
# File 'lib/gpsd.rb', line 23 def filename @filename end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
23 24 25 |
# File 'lib/gpsd.rb', line 23 def hostname @hostname end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
23 24 25 |
# File 'lib/gpsd.rb', line 23 def port @port end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
23 24 25 |
# File 'lib/gpsd.rb', line 23 def version @version end |
Instance Method Details
#close ⇒ Object
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, ={}) block = .delete(:block) send_command(command, ) # 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(=nil) attempts = 0 = @_options.merge( || {}) Timeout.timeout([:timeout]) do # connect to a UNIX socket if [:socket] raise GPS::Error.new("File #{[:socket]} is not a socket") unless File.socket?([:socket]) @filename = [:socket] @socket = UNIXSocket.new([:socket]) else begin attempts += 1 @hostname = [:hostname] @port = [:port] @socket = TCPSocket.new([:hostname], [:port].to_i) rescue Errno::ECONNREFUSED raise GPS::ConnectionError.new("Could not connect to #{[:hostname]}:#{[:port]} after #{attempts} attempts, connection is closed") if attempts >= [:attempts] sleep [: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 |
#poll ⇒ Object
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, ={}) cmdline = "?#{cmd.to_s.upcase}#{.empty? ? '' : '='+MultiJson.dump()};" begin socket().puts(cmdline) rescue Errno::EPIPE connect() retry end end |
#socket ⇒ Object
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(={}) = ({ :enable => true, :json => true }) if .empty? return command(:watch, ) end |