#!/usr/bin/perl
use strict;
use GPS::gpsd;
use Getopt::Std;
use Report::http;

my $opt={};
getopts('i:', $opt);

my ($host,$port)=split(q{:}, shift());
$host||=q{localhost};
$port||=q{2947};

$opt->{'i'}||=int(2 ** 15 + rand(2 ** 15));
print "Device:", $opt->{'i'}, " - $host:$port\n";

my $gps=GPS::gpsd->new(host=>$host, port=>$port) || die("Error: Cannot connect to the gpsd server");

print join("|", qw{Type Status Lat Lon Alt Speed Heading}), "\n";
my $config={
            time=>5,              #seconds
            distance=>100,        #meters
            track=>20,            #meters
            device=>$opt->{'i'},  #device id
           };

$gps->subscribe(handler=>\&gpsd_handler,
                config=>$config);

sub report {
  my $point=shift();
  my $config=shift();
  print join "|", $config->{'type'},
                  $point->status,
                  $point->lat,
                  $point->lon,
                  $point->alt,
                  $point->speed,
                  $point->heading,
                  "\n";
   use Report::http;
   my $rpt=new Report::http;
   my $return=$rpt->send({device=>$config->{'device'},
                          lat=>$point->lat,
                          lon=>$point->lon,
                          speed=>$point->speed,
                          heading=>$point->heading,
                          dtg=>$point->datetime,  #should use time here
                          type=>$config->{'type'}});
  return $return ? $point : undef();
}

sub gpsd_handler {
  my $p1=shift(); #last true return or undef if first
  my $p2=shift(); #current fix
  my $config=shift();
  unless (defined($p1)) {
    $config->{'type'}="first";
    return report($p2, $config);
  } else {
    if ($gps->time($p1, $p2) > $config->{'time'}) {
      $config->{'type'}="time";
      return report($p2, $config);
    } else {
      if ($gps->distance($p1, $p2) > $config->{'distance'}) {
        $config->{'type'}="distance";
        return report($p2, $config);
      } else {
        if ($gps->distance($gps->track($p1, $gps->time($p1,$p2)), $p2)
              > $config->{'track'}) {
          $config->{'type'}="track";
          return report($p2, $config);
        } else {
          return undef();
        }
      }
    }
  }
}
