AnyEvent-GPSD
view release on metacpan or search on metacpan
my $fix = $self->{fix} or return;
$fix->{mode} >= 2 or return;
my $diff = AnyEvent->time - $fix->{time};
$diff <= $max or return;
if ($fix->{speed} >= $self->{min_speed}) {
my ($lat, $lon) = $geo->forward ($fix->{lat}, $fix->{lon}, $fix->{bearing}, $fix->{speed} * $diff);
($lat, $lon)
} else {
# if we likely have zero speed, return the point itself
($fix->{lat}, $fix->{lon})
}
}
sub log {
my ($self, @arg) = @_;
syswrite $self->{logfh}, JSON::encode_json ([AnyEvent->time, @arg]) . "\n"
if $self->{logfh};
}
=item $gps->record_log ($path)
If C<$path> is defined, then that file will be created or truncated and a
log of all (raw) packets received will be written to it. This log file can
later be replayed by calling C<< $gps->replay_log ($path) >>.
If C<$path> is undefined then the log will be closed.
=cut
sub record_log {
my ($self, $path) = @_;
if (defined $path) {
$self->record_log;
require JSON;
open $self->{logfh}, ">:perlio", $path
or Carp::croak "$path: $!";
$self->log (start => $VERSION, 0, 0, { interval => $self->{interval} });
} elsif ($self->{logfh}) {
$self->log ("stop");
delete $self->{logfh};
}
}
=item $gps->replay_log ($path, %options)
Replays a log file written using C<record_log> (or stops replaying when
C<$path> is undefined). While the log file replays, real GPS events will
be ignored. This comes in handy when testing.
Please note that replaying a log will change configuration options that
will not be restored, so it's best not to reuse a gpsd object after a
replay.
The C<AnyEvent::GPSD> distribution comes with an example log
(F<eg/example.aegps>) that you can replay for testing or enjoyment
purposes.
The options include:
=over 4
=item compress => 1
If set to a true value (default: false), then passages without fix will be
replayed much faster than passages with fix. The same happens for passages
without much movement.
=item stretch => $factor
Multiplies all times by the given factor. Values < 1 make the log replay
faster, values > 1 slower. Note that the frequency of fixes will not be
increased, o stretch factors > 1 do not work well.
A stretch factor of zero is not allowed, but if you want to replay a log
instantly you may speicfy a very low value (e.g. 1e-10).
=back
=cut
sub replay_log {
my ($self, $path, %option) = @_;
if (defined $path) {
$self->replay_log;
require JSON;
open my $fh, "<:perlio", $path
or Carp::croak "$path: $!";
$self->{stretch} = $option{stretch} || 1;
$self->{compress} = $option{compress};
$self->{imterval} /= $self->{stretch};
Scalar::Util::weaken $self;
$self->{replay_cb} = sub {
my $line = <$fh>;
if (2 > length $line) {
$self->replay_log;
} else {
my ($time, $type, @data) = @{ JSON::decode_json ($line) };
$time *= $self->{stretch};
if ($type eq "start") {
my ($module_version, $major_version, $minor_version, $args) = @data;
( run in 2.754 seconds using v1.01-cache-2.11-cpan-437f7b0c052 )