GPSD3.pm

filter: Statementstmt 50.7% 64 / 126 Branchbran 8.9% 5 / 56 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 70.8% 17 / 24 Podpod 100% 13 / 13 totaltotal 44.0% 100 / 227 CC 33 SCAR 32.6
CC
33
Cov
37%
default_handler
132.0
poll
20.0
watch
20.0
CRAP 26.1
↑ Summary GPSD3/Return/Unknown/Timestamp.pm »
1 package Net::GPSD3;
2 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use base qw{Net::GPSD3::Base};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use JSON::XS qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use IO::Socket::INET6 qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Net::GPSD3::Return::Unknown;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Net::GPSD3::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 12 our $VERSION='0.19';
12 12 our $PACKAGE=__PACKAGE__;
13
14 =head1 NAME
15
16 Net::GPSD3 - Interface to the gpsd server daemon protocol versions 3 (JSON).
17
18 =head1 SYNOPSIS
19
20 =head2 Watch Interface
21
22 use Net::GPSD3;
23 my $gpsd=Net::GPSD3->new;
24 $gpsd->watch;
25
26 One Liner
27
28 perl -MNet::GPSD3 -e 'Net::GPSD3->new->watch'
29
30 =head2 Poll Interface
31
32 use Net::GPSD3;
33 use Data::Dumper qw{Dumper};
34 my $gpsd=Net::GPSD3->new;
35 my $poll=$gpsd->poll;
36 print Dumper($poll);
37
38 One Liner
39
40 perl -MNet::GPSD3 -e 'printf "Protocol: %s\n", Net::GPSD3->new->poll->parent->cache->VERSION->protocol;'
41
42 Protocol: 3.4
43
44 =head2 POE Interface
45
46 See L<Net::GPSD3::POE>
47
48 =head1 DESCRIPTION
49
50 Net::GPSD3 provides an object client interface to the gpsd server daemon utilizing the version 3 protocol. gpsd is an open source GPS daemon from http://www.catb.org/gpsd/ Support for Version 3 of the protocol (JSON) was added to the daemon in version 2.90. If your daemon is before 2.90 (protocol 2.X), please use the L<Net::GPSD> package.
51
52 =head1 CONSTRUCTOR
53
54 =head2 new
55
56 Returns a new Net::GPSD3 object.
57
58 my $gpsd=Net::GPSD3->new;
59 my $gpsd=Net::GPSD3->new(host=>"127.0.0.1", port=>2947); #defaults
60
61 =head1 METHODS
62
63 =head2 host
64
65 Sets or returns the current gpsd host.
66
67 my $host=$obj->host;
68
69 =cut
70
71 3 ▶sub host {
Subroutine host: called CC=3 SCAR=12.7
Pod documented
72 3 my $self=shift;
73 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
74 3 $self->{'host'}=shift;
75 3 undef($self->{'socket'});
76 }
77 3 ▶ $self->{'host'}="127.0.0.1" unless defined $self->{'host'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"host"} 0 0 0
78 3 return $self->{'host'};
79 }
80
81 =head2 port
82
83 Sets or returns the current gpsd TCP port.
84
85 my $port=$obj->port;
86
87 =cut
88
89 3 ▶sub port {
Subroutine port: called CC=3 SCAR=12.7
Pod documented
90 3 my $self=shift;
91 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
92 3 $self->{'port'}=shift;
93 3 undef($self->{'socket'});
94 }
95 3 ▶ $self->{'port'}='2947' unless defined $self->{'port'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"port"} 0 0 0
96 3 return $self->{'port'};
97 }
98
99 =head2 poll
100
101 Sends a Poll request to the gpsd server and returns a L<Net::GPSD3::Return::POLL> object. The method also populates the cache object with the L<Net::GPSD3::Return::VERISON> and L<Net::GPSD3::Return::DEVICES> objects.
102
103 my $poll=$gpsd->poll; #isa Net::GPSD3::Return::POLL object
104
105 Note: In order to use the poll method consistently you should run the GPSD daemon as a service. You may also need to run the daemon with the "-n" option.
106
107 =cut
108
109 3 ▶sub poll {
Subroutine poll: called CC=4 SCAR=30.0
Pod documented
110 0 my $self=shift;
111 0 ▶ $self->socket->send(qq(?DEVICES;\n)) unless $self->cache->DEVICES;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cache->DEVICES 0 0 0
112 0 $self->socket->send(qq(?POLL;\n));
113 0 my $object;
114 0 ▶ do { #Reads and caches VERSION and DEVICES
Branch: 1 branch
BranchTrueFalseTotal
$object->class eq "POLL" or do { do { local $/ = "\r\n"; my $line = $self->socket->getline; chomp $line; $object = $self->constructor($self->decode($line), "string", $line); $self->cache->add($object) unless $object->class eq "POLL" } } 0 0 0
115 0 local $/="\r\n";
116 0 my $line=$self->socket->getline;
117 0 chomp $line;
118 0 $object=$self->constructor($self->decode($line), string=>$line);
119 0 ▶ $self->cache->add($object) unless $object->class eq "POLL";
Branch: 1 branch
BranchTrueFalseTotal
unless $object->class eq "POLL" 0 0 0
120 } until $object->class eq "POLL"; #this needs more logic
121 0 return $object;
122 }
123
124 =head2 watch
125
126 Calls all handlers that are registered in the handler method.
127
128 $gpsd->watch; #will not return unless something goes wrong.
129
130 =cut
131
132 3 ▶sub watch {
Subroutine watch: called CC=4 SCAR=30.0
Pod documented
133 0 my $self=shift;
134 0 my @handler=$self->handlers;
135 0 ▶ push @handler, \&default_handler unless scalar(@handler);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @handler 0 0 0
136 #$self->socket->send(qq(?DEVICES;\n)); #appears this is now done in the daemon
137 0 $self->socket->send($self->_watch_string_on. "\n");
138 0 my $object;
139 #man 8 gpsd - Each request returns a line of response text ended by a CR/LF.
140 0 local $/="\r\n";
141 0 my $line;
142 0 ▶ while (defined($line=$self->socket->getline)) { #Reads VERSION and DEVICES object too.
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $self->socket->getline)) 0 0 0
143 #print "$line\n";
144 0 chomp $line;
145 0 my $object=$self->constructor($self->decode($line), string=>$line);
146 0 $_->($object) foreach @handler;
147 0 $self->cache($object); #cache after handler so that the last point is available to the handler.
148 }
149 0 return $self;
150 }
151
152 0 ▶sub _watch_string_on {
Subroutine _watch_string_on: not called CC=1 SCAR=6.9
153 0 return q(?WATCH={"enable":true,"json":true};);
154 }
155
156 0 ▶sub _watch_string_off {
Subroutine _watch_string_off: not called CC=1 SCAR=6.9
157 0 return q(?WATCH={"enable":false,"json":true};);
158 }
159
160 =head2 addHandler
161
162 Adds handlers to the handler list.
163
164 $gpsd->addHandler(\&myHandler);
165 $gpsd->addHandler(\&myHandler1, \&myHandler2);
166
167 A handler is a sub reference where the first argument is a Net::GPSD3::Return::* object.
168
169 =cut
170
171 0 ▶sub addHandler {
Subroutine addHandler: not called CC=2 SCAR=17.9
Pod documented
172 0 my $self=shift;
173 0 my $array=$self->handlers;
174 0 ▶ push @$array, @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
175 0 return $self;
176 }
177
178 =head2 handlers
179
180 List of handlers that are called in order to process objects from the gpsd wathcer stream.
181
182 my @handler=$gpsd->handlers; #()
183 my $handler=$gpsd->handlers; #[]
184
185 =cut
186
187 0 ▶sub handlers {
Subroutine handlers: not called CC=3 SCAR=24.8
Pod documented
188 0 my $self=shift;
189 0 ▶ $self->{'handler'}=[] unless ref($self->{'handler'});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"handler"} 0 0 0
190 0 ▶ return wantarray ? @{$self->{'handler'}} : $self->{'handler'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
191 }
192
193 =head2 cache
194
195 Returns the L<Net::GPSD3::Cache> caching object.
196
197 =cut
198
199 0 ▶sub cache {
Subroutine cache: not called CC=2 SCAR=17.9
Pod documented
200 0 my $self=shift;
201 $self->{"cache"}=Net::GPSD3::Cache->new(parent=>$self)
202 0 ▶ unless defined $self->{"cache"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"cache"} 0 0 0
203 0 return $self->{"cache"};
204 }
205
206 =head1 METHODS Internal
207
208 =head2 default_handler
209
210 =cut
211
212 0 ▶sub default_handler {
Subroutine default_handler: not called CC=11 SCAR=48.8
Pod documented
213 0 my $object=shift;
214 #use Data::Dumper qw{Dumper};
215 #print Dumper($object);
216 0 ▶ if ($object->class eq "TPV") {
Branch: 8 branches
BranchTrueFalseTotal
if ($object->class eq 'TPV') { } 0 0 0
elsif ($object->class eq 'SKY') { } 0 0 0
elsif ($object->class eq 'SUBFRAME') { } 0 0 0
elsif ($object->class eq 'VERSION') { } 0 0 0
elsif ($object->class eq 'WATCH') { } 0 0 0
elsif ($object->class eq 'DEVICES') { } 0 0 0
elsif ($object->class eq 'DEVICE') { } 0 0 0
elsif ($object->class eq 'ERROR') { } 0 0 0
217 0 printf "%s: %s, Time: %s, Lat: %s, Lon: %s, Speed: %s, Heading: %s\n",
218 DateTime->now,
219 $object->class,
220 $object->timestamp,
221 $object->lat,
222 $object->lon,
223 $object->speed,
224 $object->track;
225 } elsif ($object->class eq "SKY") {
226 printf "%s: %s, Satellites: %s, Used: %s, PRNs: %s\n",
227 DateTime->now,
228 $object->class,
229 $object->reported,
230 $object->used,
231 0 join(",", map {$_->prn} grep {$_->used} $object->Satellites),
232 } elsif ($object->class eq "SUBFRAME") {
233 0 printf qq{%s: %s, Device: %s\n},
234 DateTime->now,
235 $object->class,
236 $object->device;
237 } elsif ($object->class eq "VERSION") {
238 0 printf "%s: %s, GPSD: %s (%s), %s: %s\n",
239 DateTime->now,
240 $object->class,
241 $object->release,
242 $object->revision,
243 ref($object->parent),
244 $object->parent->VERSION;
245 } elsif ($object->class eq "WATCH") {
246 0 printf "%s: %s, Enabled: %s\n",
247 DateTime->now,
248 $object->class,
249 $object->enabled;
250 } elsif ($object->class eq "DEVICES") {
251 0 my @device=$object->Devices;
252 0 foreach my $device (@device) {
253 0 ▶ if ($device->activated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device->activated) { } 0 0 0
254 0 $device=sprintf("%s (%s bps %s-%s)", $device->path, $device->bps, $device->driver, $device->subtype);
255 } else {
256 0 $device=$device->path;
257 }
258 }
259 0 printf "%s: %s, Devices: %s\n",
260 DateTime->now,
261 $object->class,
262 join(", ", @device);
263 } elsif ($object->class eq "DEVICE") {
264 0 printf qq{%s: %s, Device: %s (%s bps %s-%s)\n},
265 DateTime->now,
266 $object->class,
267 $object->path,
268 $object->bps,
269 $object->driver,
270 $object->subtype;
271 } elsif ($object->class eq "ERROR") {
272 0 printf qq{%s: %s, Message: "%s"\n},
273 DateTime->now,
274 $object->class,
275 $object->message;
276 } else {
277 0 warn(sprintf(qq{Warning: Unknown class "%s" for object "%s".}, $object->class, ref($object)));
278 #print Dumper($object);
279 }
280 #print Dumper($object);
281 }
282
283 =head2 socket
284
285 Returns the cached L<IO::Socket::INET6> object
286
287 my $socket=$gpsd->socket; #try to reconnect on failure
288
289 =cut
290
291 0 ▶sub socket {
Subroutine socket: not called CC=4 SCAR=30.0
Pod documented
292 0 my $self=shift;
293 0 ▶ unless (defined($self->{'socket'}) and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"socket"} and defined $self->{"socket"}->connected) 0 0 0
Condition0%0/3
defined $self->{"socket"} and defined $self->{"socket"}->connected
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"socket"} and defined $self->{"socket"}->connected
defined $self->{"socket"} defined $self->{"socket"}->connected
Truth table0%0/3
A and B
A= defined $self->{"socket"}B= defined $self->{"socket"}->connected
ABresult
0X0
100
111
294 defined($self->{'socket'}->connected)) {
295 0 $self->{"socket"}=IO::Socket::INET6->new(
296 PeerAddr => $self->host,
297 PeerPort => $self->port,
298 );
299 die(sprintf("Error: Cannot connect to gpsd://%s:%s/.\n",
300 0 ▶ $self->host, $self->port)) unless defined($self->{"socket"});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"socket"} 0 0 0
301 }
302 0 return $self->{'socket'};
303 }
304
305 =head2 json
306
307 Returns the cached L<JSON::XS> object
308
309 =cut
310
311 52 ▶sub json {
Subroutine json: called CC=2 SCAR=6.9
Pod documented
312 52 my $self=shift;
313 #Do I need to support JSON::PP?
314 52 ▶ $self->{"json"}=JSON::XS->new unless ref($self->{"json"}) eq "JSON::XS";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"json"} eq "JSON::XS" 11 41 52
315 52 return $self->{"json"};
316 }
317
318 =head2 decode
319
320 Returns a perl data structure given a JSON formated string.
321
322 my %data=$gpsd->decode($string); #()
323 my $data=$gpsd->decode($string); #{}
324
325 =cut
326
327 11 ▶sub decode {
Subroutine decode: called CC=3 SCAR=11.6
Pod documented
328 11 my $self=shift;
329 11 my $string=shift;
330 11 my $data=eval {$self->json->decode($string)};
331 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
332 0 $data={class=>"ERROR", message=>"Invalid JSON"};
333 }
334 11 ▶ return wantarray ? %$data : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 11 0 11
335 }
336
337 =head2 encode
338
339 Returns a JSON string from a perl data structure
340
341 =cut
342
343 41 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
344 41 my $self=shift;
345 41 my $data=shift;
346 41 my $string=$self->json->encode($data);
347 41 return $string;
348 }
349
350 =head2 constructor
351
352 Constructs a class object by lazy loading the classes.
353
354 my $obj=$gpsd->constructor(%$data);
355 my $obj=$gpsd->constructor(class=>"DEVICE",
356 string=>'{...}',
357 ...);
358
359 Returns and object in the Net::GPSD3::Return::* namespace.
360
361 =cut
362
363 52 ▶sub constructor {
Subroutine constructor: called CC=3 SCAR=11.1
Pod documented
364 52 my $self=shift;
365 52 my %data=@_;
366 52 ▶ $data{"class"}||="undef";
Condition50%1/2
$data{"class"} ||= "undef"
l!l
520
MC/DC0%0/1
$data{"class"} ||= "undef"
$data{"class"}
Truth table50%1/2
A
A= $data{"class"}
Aresult
11
00
367 52 my $class=join("::", $PACKAGE, "Return", $data{"class"});
368 52 my $object;
369 52 ▶ eval("use $class");
Subroutine BEGIN: called CC=1 SCAR=0.0
370 52 ▶ if ($@) { #Failed to load class
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 52 52
371 0 $object=Net::GPSD3::Return::Unknown->new(parent=>$self, %data);
372 } else {
373 52 $object=$class->new(parent=>$self, %data);
374 }
375 52 return $object;
376 }
377
378 =head1 BUGS
379
380 Log on RT and Send to gpsd-dev email list
381
382 There are no two GPS devices that are alike. Each GPS device has a different GPSD signature as well. If your GPS device does not work out of the box with this package, please send me a log of your devices JSON sentences.
383
384 echo '?POLL;' | nc 127.0.0.1 2947
385
386 echo '?WATCH={"enable":true,"json":true};' | socat -t10 stdin stdout | nc 127.0.0.1 2947
387
388 =head1 SUPPORT
389
390 DavisNetworks.com supports all Perl applications including this package.
391
392 Try gpsd-dev email list
393
394 =head1 AUTHOR
395
396 Michael R. Davis
397 CPAN ID: MRDVT
398 STOP, LLC
399 domain=>michaelrdavis,tld=>com,account=>perl
400 http://www.stopllc.com/
401
402 =head1 COPYRIGHT
403
404 This program is free software licensed under the...
405
406 The BSD License
407
408 The full text of the license can be found in the LICENSE file included with this module.
409
410 =head1 SEE ALSO
411
412 L<Net::GPSD>, L<Net::GPSD3::POE>, L<GPS::Point>, L<JSON::XS>, L<IO::Socket::INET6>, L<DateTime>
413
414 =cut
415
416 12 1;
↑ Summary GPSD3/Return/Unknown/Timestamp.pm »