gpsd_client 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -42,7 +42,6 @@ end
42
42
 
43
43
  # To stop polling the daemon
44
44
  gpsd.stop()
45
- # Actually Gpsd.stop() doesn't close the connection socket (to be fixed)
46
45
  ```
47
46
 
48
47
  ## Development
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "bundler", "~> 1.8"
28
28
  spec.add_development_dependency "rake", "~> 10.0"
29
29
  spec.add_development_dependency "rspec-core", "~> 3.2.1"
30
+ spec.add_development_dependency "rspec-expectations", "~> 3.2.1"
30
31
  end
@@ -4,6 +4,10 @@ require "socket"
4
4
  require "json"
5
5
 
6
6
  module GpsdClient
7
+
8
+ if ! ::IO.const_defined?(:EAGAINWaitReadable)
9
+ class ::IO::EAGAINWaitReadable; end
10
+ end
7
11
 
8
12
  class Gpsd
9
13
  attr_reader :host, :port
@@ -11,7 +15,7 @@ module GpsdClient
11
15
  @started = false
12
16
 
13
17
  def initialize(options = {})
14
- @host = options[:host] ||= 'localhost'
18
+ @host = options[:host] ||= '127.0.0.1'
15
19
  @port = options[:port] ||= 2947
16
20
  end
17
21
 
@@ -22,12 +26,12 @@ module GpsdClient
22
26
  @socket.puts 'w+'
23
27
  line = JSON.parse @socket.gets rescue ''
24
28
  if line.is_a? Hash and line['class'] == 'VERSION'
25
- #@socket.puts '?WATCH={"enable":true,"json":true}' # disabled reporting, instead we are polling
26
29
  @socket.puts '?WATCH={"enable":true};'
27
30
  @started = true
31
+ flush_socket
28
32
  end
29
33
 
30
- rescue Exception => ex
34
+ rescue => ex
31
35
  puts 'Some error happen starting socket connection:'
32
36
  puts ex.message
33
37
  self.stop
@@ -35,11 +39,11 @@ module GpsdClient
35
39
  end
36
40
  return @started
37
41
  end
38
-
42
+
39
43
  def started?
40
44
  @started
41
45
  end
42
-
46
+
43
47
  def stop
44
48
  return not_started_msg("Gpsd.stop") if not self.started?
45
49
  @socket.puts '?WATCH={"enable":false};'
@@ -47,25 +51,28 @@ module GpsdClient
47
51
  @started = false if @socket.closed?
48
52
  !self.started?
49
53
  end
50
-
54
+
51
55
  def get_position
52
- reads = 0
53
56
  empty_hash = {lat: nil, lon: nil, time: nil, speed: nil, altitude: nil }
54
57
  return empty_hash if not self.started?
55
-
56
- while reads < 10 do # Skip VERSION SKY WATCH or DEVICES response
57
- line = ""
58
- begin
59
- @socket.puts '?WATCH={"enable":true};'
60
- sleep 0.1
61
- @socket.puts "?POLL;"
62
- line = @socket.gets
63
- rescue Exception => ex
64
- puts "Error while reading Socket: #{ex.message}"
58
+ begin
59
+ @socket.puts "?POLL;"
60
+ retries = 0
61
+ until retries == 10 do
62
+ begin
63
+ lines = @socket.read_nonblock(4096).split("\r\n")
64
+ rescue IO::WaitReadable, IO::EAGAINWaitReadable
65
+ retries += 1
66
+ sleep 0.1*retries
67
+ end
65
68
  end
66
-
67
- # Parse line, return empty string on fail
68
- # if parsed, extract ptv Hash from the JSON report polled
69
+ rescue => ex
70
+ puts "Error while reading Socket: #{ex.message}"
71
+ end
72
+
73
+ # Parse line, return empty string on fail
74
+ # if parsed, extract ptv Hash from the JSON report polled
75
+ lines.each do |line|
69
76
  line = JSON.parse(line) rescue ''
70
77
  if line.is_a? Hash and line['tpv'].is_a? Array
71
78
  #puts "debug >> #{line.to_json.to_s}"
@@ -76,22 +83,32 @@ module GpsdClient
76
83
  # http://www.catb.org/gpsd/client-howto.html
77
84
  # mode 1 means no valid data
78
85
  # return "Lat: #{line['lat'].to_s}, Lon: #{line['lon'].to_s}" unless line['mode'] == 1
86
+ flush_socket
79
87
  return {lat: line['lat'], lon: line['lon'], time: line['time'], speed: line['speed'], altitude: line['alt']} unless line['mode'] == 1
80
88
  end
81
-
82
- reads = reads + 1
83
-
89
+ #puts "debug >> TPV not found polling on GPSd"
84
90
  end
85
- #puts "debug >> TPV not found polling on GPSd"
86
91
  return empty_hash
87
92
  end
88
-
93
+
89
94
  private
90
-
95
+
91
96
  def not_started_msg( method = 'Gpsd' )
92
97
  puts "#{method}: No socket connection started"
93
98
  return nil
94
99
  end
95
-
100
+
101
+ # Reads from socket until no more data is returned, the read data will be thrown away.
102
+ # Params:
103
+ # +socket+:: the socket to read from
104
+ def flush_socket
105
+ begin
106
+ loop do
107
+ @socket.read_nonblock(1024)
108
+ end
109
+ rescue IO::WaitReadable, IO::EAGAINWaitReadable
110
+ true
111
+ end
112
+ end
96
113
  end
97
114
  end
@@ -1,3 +1,3 @@
1
1
  module GpsdClient
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gpsd_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-07-11 00:00:00.000000000 Z
12
+ date: 2016-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.2.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-expectations
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 3.2.1
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 3.2.1
62
78
  description: A simple GPSd client intended for use on the Raspberry Pi.
63
79
  email:
64
80
  - rccursach@gmail.com
@@ -93,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
109
  version: '0'
94
110
  segments:
95
111
  - 0
96
- hash: -491074447
112
+ hash: -295433375
97
113
  required_rubygems_version: !ruby/object:Gem::Requirement
98
114
  none: false
99
115
  requirements:
@@ -102,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
118
  version: '0'
103
119
  segments:
104
120
  - 0
105
- hash: -491074447
121
+ hash: -295433375
106
122
  requirements: []
107
123
  rubyforge_project:
108
124
  rubygems_version: 1.8.23