ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-GPSD/GPSD.pm
(Generate patch)

Comparing AnyEvent-GPSD/GPSD.pm (file contents):
Revision 1.2 by root, Wed Jul 2 05:17:37 2008 UTC vs.
Revision 1.4 by root, Fri Jul 18 01:31:12 2008 UTC

88 88
89C<snr> contains the signal strength in decibals (28+ is usually the 89C<snr> contains the signal strength in decibals (28+ is usually the
90minimum value for a good fix). 90minimum value for a good fix).
91 91
92C<fix> contains either C<1> to indicate that this satellite was used for 92C<fix> contains either C<1> to indicate that this satellite was used for
93the last position fix, C<0> otherwise. EGNOS/WAAS etc. satellites will 93the last position fix, C<0> otherwise. EGNOS/WAAS etc. satellites will
94always show as C<0>, even if their correction info was used. 94always show as C<0>, even if their correction info was used.
95
96The passed hash references are read-only.
95 97
96=item on_fix => $cb->({point}) 98=item on_fix => $cb->({point})
97 99
98Called regularly (usually about once/second), even when there is no 100Called regularly (usually about once/second), even when there is no
99connection to the GPSD (so is useful to update your idea of the current 101connection to the GPSD (so is useful to update your idea of the current
151 153
152sub retry { 154sub 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
249sub feed { 254sub 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;
316This returns an estimate of the current position based on the last fix and 326This returns an estimate of the current position based on the last fix and
317the time passed since then. Useful for interactive applications where you 327the time passed since then. Useful for interactive applications where you
318want more frequent updates, but not very useful to store, as the next fix 328want more frequent updates, but not very useful to store, as the next fix
319might well be totally off. 329might well be totally off.
320 330
321If the fix is older then C<$max_seconds> (default: C<1.9>) or if no fix is 331If the fix is older then C<$max_seconds> (default: C<1.9> times the update
322available, returns the empty list. 332interval, i.e. usually C<1.9> seconds) or if no fix is available, returns
333the empty list.
323 334
324=cut 335=cut
325 336
326sub estimate { 337sub 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
361sub log {
362 my ($self, @arg) = @_;
363
364 syswrite $self->{logfh}, JSON::encode_json ([AnyEvent->time, @arg]) . "\n"
365 if $self->{logfh};
366}
367
368sub 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
354L<AnyEvent>. 383L<AnyEvent>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines