… | |
… | |
88 | |
88 | |
89 | C<snr> contains the signal strength in decibals (28+ is usually the |
89 | C<snr> contains the signal strength in decibals (28+ is usually the |
90 | minimum value for a good fix). |
90 | minimum value for a good fix). |
91 | |
91 | |
92 | C<fix> contains either C<1> to indicate that this satellite was used for |
92 | C<fix> contains either C<1> to indicate that this satellite was used for |
93 | the last position fix, C<0> otherwise. EGNOS/WAAS etc. satellites will |
93 | the last position fix, C<0> otherwise. EGNOS/WAAS etc. satellites will |
94 | always show as C<0>, even if their correction info was used. |
94 | always show as C<0>, even if their correction info was used. |
|
|
95 | |
|
|
96 | The passed hash references are read-only. |
95 | |
97 | |
96 | =item on_fix => $cb->({point}) |
98 | =item on_fix => $cb->({point}) |
97 | |
99 | |
98 | Called regularly (usually about once/second), even when there is no |
100 | Called regularly (usually about once/second), even when there is no |
99 | connection to the GPSD (so is useful to update your idea of the current |
101 | connection to the GPSD (so is useful to update your idea of the current |
… | |
… | |
151 | |
153 | |
152 | sub retry { |
154 | sub retry { |
153 | my ($self) = @_; |
155 | my ($self) = @_; |
154 | |
156 | |
155 | delete $self->{fh}; |
157 | delete $self->{fh}; |
|
|
158 | delete $self->{command}; |
156 | |
159 | |
157 | Scalar::Util::weaken $self; |
160 | Scalar::Util::weaken $self; |
158 | $self->{retry_w} = AnyEvent->timer (after => 1, cb => sub { |
161 | $self->{retry_w} = AnyEvent->timer (after => 1, cb => sub { |
159 | delete $self->{retry_w}; |
162 | delete $self->{retry_w}; |
160 | $self->connect; |
163 | $self->connect; |
… | |
… | |
204 | $self->retry; |
207 | $self->retry; |
205 | }, |
208 | }, |
206 | on_eof => sub { |
209 | on_eof => sub { |
207 | $! = &Errno::EPIPE; |
210 | $! = &Errno::EPIPE; |
208 | $self->event ("error"); |
211 | $self->event ("error"); |
|
|
212 | $self->log ("disconnect"); |
209 | $self->retry; |
213 | $self->retry; |
210 | }, |
214 | }, |
211 | on_read => sub { |
215 | on_read => sub { |
212 | $_[0]{rbuf} =~ s/^([^\015\012]*)\015\012// |
216 | $_[0]{rbuf} =~ s/^([^\015\012]*)\015\012// |
213 | or return; |
217 | or return; |
… | |
… | |
220 | $self->send ("o"); |
224 | $self->send ("o"); |
221 | $self->send ("y"); |
225 | $self->send ("y"); |
222 | $self->send ("c"); |
226 | $self->send ("c"); |
223 | |
227 | |
224 | $self->event ("connect"); |
228 | $self->event ("connect"); |
|
|
229 | $self->log ("connect"); |
225 | } else { |
230 | } else { |
226 | $self->event ("error"); |
231 | $self->event ("error"); |
227 | } |
232 | } |
228 | }; |
233 | }; |
229 | |
234 | |
… | |
… | |
249 | sub feed { |
254 | sub feed { |
250 | my ($self, $line) = @_; |
255 | my ($self, $line) = @_; |
251 | |
256 | |
252 | $self->{now} = AnyEvent->now; |
257 | $self->{now} = AnyEvent->now; |
253 | |
258 | |
|
|
259 | $self->log (raw => $line) |
|
|
260 | if $self->{logfh}; |
|
|
261 | |
254 | unless ($line =~ /^GPSD,(.)=(.*)$/) { |
262 | unless ($line =~ /^GPSD,(.)=(.*)$/) { |
255 | $! = &Errno::EBADMSG; |
263 | $! = &Errno::EBADMSG; |
256 | $self->event ("error"); |
264 | $self->event ("error"); |
257 | return $self->retry; |
265 | return $self->retry; |
258 | } |
266 | } |
259 | |
267 | |
260 | my ($type, $data) = ($1, $2); |
268 | my ($type, $data) = ($1, $2); |
|
|
269 | |
|
|
270 | #warn "$type=$data\n";#d# |
261 | |
271 | |
262 | $self->{state}{$type} = [$data => $self->{now}]; |
272 | $self->{state}{$type} = [$data => $self->{now}]; |
263 | |
273 | |
264 | if ($type eq "O") { |
274 | if ($type eq "O") { |
265 | my @data = split /\s+/, $data; |
275 | my @data = split /\s+/, $data; |
… | |
… | |
316 | This returns an estimate of the current position based on the last fix and |
326 | This returns an estimate of the current position based on the last fix and |
317 | the time passed since then. Useful for interactive applications where you |
327 | the time passed since then. Useful for interactive applications where you |
318 | want more frequent updates, but not very useful to store, as the next fix |
328 | want more frequent updates, but not very useful to store, as the next fix |
319 | might well be totally off. |
329 | might well be totally off. |
320 | |
330 | |
321 | If the fix is older then C<$max_seconds> (default: C<1.9>) or if no fix is |
331 | If the fix is older then C<$max_seconds> (default: C<1.9> times the update |
322 | available, returns the empty list. |
332 | interval, i.e. usually C<1.9> seconds) or if no fix is available, returns |
|
|
333 | the empty list. |
323 | |
334 | |
324 | =cut |
335 | =cut |
325 | |
336 | |
326 | sub estimate { |
337 | sub estimate { |
327 | my ($self, $max) = @_; |
338 | my ($self, $max) = @_; |
328 | |
339 | |
329 | $max ||= 1.9 unless defined $max; |
340 | $max ||= 1.9 * $self->{interval} unless defined $max; |
330 | |
341 | |
331 | my $geo = $self->{geo_forward} ||= new Geo::Forward; |
342 | my $geo = $self->{geo_forward} ||= new Geo::Forward; |
332 | |
343 | |
333 | my $fix = $self->{fix} or return; |
344 | my $fix = $self->{fix} or return; |
334 | $fix->{mode} >= 2 or return; |
345 | $fix->{mode} >= 2 or return; |
… | |
… | |
345 | # if we likely have zero speed, return the point itself |
356 | # if we likely have zero speed, return the point itself |
346 | ($fix->{lat}, $fix->{lon}) |
357 | ($fix->{lat}, $fix->{lon}) |
347 | } |
358 | } |
348 | } |
359 | } |
349 | |
360 | |
|
|
361 | sub log { |
|
|
362 | my ($self, @arg) = @_; |
|
|
363 | |
|
|
364 | syswrite $self->{logfh}, JSON::encode_json ([AnyEvent->time, @arg]) . "\n" |
|
|
365 | if $self->{logfh}; |
|
|
366 | } |
|
|
367 | |
|
|
368 | sub record_log { |
|
|
369 | my ($self, $path) = @_, |
|
|
370 | |
|
|
371 | require JSON; |
|
|
372 | |
|
|
373 | open $self->{logfh}, ">", $path |
|
|
374 | or Carp::croak "$path: $!"; |
|
|
375 | |
|
|
376 | $self->log (start => $VERSION); |
|
|
377 | } |
|
|
378 | |
350 | =back |
379 | =back |
351 | |
380 | |
352 | =head1 SEE ALSO |
381 | =head1 SEE ALSO |
353 | |
382 | |
354 | L<AnyEvent>. |
383 | L<AnyEvent>. |