#!/usr/bin/perl # Download tracking log from Open GPS Tracker # Mike Ingle < mike at opengpstracker dot org > 2008/04/25 version 1.0 use Net::POP3; $POP3_PORT = 110; $POP3_TIMEOUT = 60; # Parse the switches $invalid = 0; for($i=0;$i<=$#ARGV;$i++) { $c = $ARGV[$i]; if($c eq '-h') { $i++; $POP3_HOST = $ARGV[$i]; } elsif($c eq '-u') { $i++; $POP3_USERNAME = $ARGV[$i]; } elsif($c eq '-pw') { $i++; $POP3_PASSWORD = $ARGV[$i]; } elsif($c eq '-tf') { $i++; $TEXT_FILE = $ARGV[$i]; } elsif($c eq '-gf') { $i++; $GPX_FILE = $ARGV[$i]; } elsif($c eq '-to') { $i++; $POP3_TIMEOUT = $ARGV[$i]; $POP3_TIMEOUT += 0; } elsif($c eq '-po') { $i++; $POP3_PORT = $ARGV[$i]; $POP3_PORT += 0; } else { $invalid = 1; last; } } if(($invalid == 1) || ($POP3_HOST eq '') || ($POP3_PORT == 0) || ($POP3_TIMEOUT == 0) || ($POP3_USERNAME eq '') || ($POP3_PASSWORD eq '') || (($TEXT_FILE eq '')&&($GPX_FILE eq ''))) { print "Usage: perl download_tracking_log.pl\ -h pop3-host\ -u pop3-username\ -pw pop3-password\ -tf output text file\ -gf output GPX file\ -to output timeout (default 60) seconds\ -po output port (default 110)\ Ex: perl download_tracking_log.pl -h pop.somesite.com -u trk1\@somesite.com -pw passwd1 -gf out.gpx -tf out.txt\n"; exit(1); } # Messages shorter than this value will be read $GET_THRESHOLD = 4096; $ANNOUNCE_INTERVAL = 30; $ANNOUNCE_INTERVAL = 5; if($TEXT_FILE ne '') { log_print("Opening the text output file"); if(!(open(TXT,'>'.$TEXT_FILE))) { log_print("Text file open failed: ".$!); exit(1); } } if($GPX_FILE ne '') { log_print("Opening the GPX output file"); if(!(open(GPX,'>'.$GPX_FILE))) { log_print("GPX file open failed: ".$!); exit(1); } } log_print("Connecting to the POP3 server"); if(($POP3_PORT ne '')&&($POP3_PORT != 110)) { $POP3_HOSTPORT = $POP3_HOST . ':' . $POP3_PORT; } else { $POP3_HOSTPORT = $POP3_HOST; } $pop = Net::POP3->new($POP3_HOSTPORT, Timeout => 60); if($pop eq undef) { log_print("POP3 connection failed, aborted"); exit(1); } log_print("Sending the login"); $login = $pop->login($POP3_USERNAME,$POP3_PASSWORD); if($login eq undef) { log_print("POP3 login failed, aborted"); exit(1); } $login += 0; log_print(sprintf("Account has %u message%s",$login,(($login==1)?"":"s"))); if($login == 0) { $pop->quit(); log_print("No messages to download, aborted"); exit(0); } log_print("Getting the list of messages"); $msglist = $pop->list(); undef(@getmsgs); $num_getmsgs = 0; foreach $key(sort numerically keys %$msglist) { if($msglist->{$key} <= $GET_THRESHOLD) { push(@getmsgs,$key); $num_getmsgs++; } } log_print(sprintf("Account has %u message%s below size threshold", $num_getmsgs,(($num_getmsgs==1)?"":"s"))); $get_count = 0; $get_track_count = 0; $get_last_announce = 0; $get_start_time = time(); $last_date = '0'; undef(@tracking_entries); foreach $msgnum(@getmsgs) { $msg = $pop->get($msgnum); $get_count++; $now = time(); foreach $line(@$msg) { if($line=~/POS [0-9]+ [0-9\.]+ [NS] [0-9]+ [0-9\.]+ [EW] ALT [0-9]+ \S+ SPEED [0-9\.]+ \S+ COURSE [0-9\.]+ AT (\S+) (\S+) UTC SATS [0-9]+/) { $get_track_count++; # print $line; chomp($line); push(@tracking_entries,'0:'.$line); $last_date = $1.' '.$2; } elsif($line=~/GPS TIMED OUT WAITING FOR FIX, LAST GOOD FIX /) { # $get_track_count++; print $line; # chomp($line); # push(@tracking_entries,$last_date.':'.$line); } elsif($line=~/NO DATASTREAM FROM GPS DEVICE, LAST GOOD FIX /) { # $get_track_count++; print $line; # chomp($line); # push(@tracking_entries,$last_date.':'.$line); } elsif($line=~/MC=[0-9]/) { # $get_track_count++; # print $line; } } if(($now - $get_last_announce > $ANNOUNCE_INTERVAL)||($get_count == $num_getmsgs)) { $get_last_announce = $now; $eta = ((1-($get_count/$num_getmsgs))/($get_count/$num_getmsgs))*($now-$get_start_time)/60; log_print(sprintf("Got %u message%s, %u tracking report%s, %u%% done, done in %.1f minutes", $get_count,(($get_count==1)?"":"s"), $get_track_count,(($get_track_count==1)?"":"s"), int($get_count*100/$num_getmsgs),$eta)); } } $pop->quit(); log_print("Sorting ".$get_track_count." tracking reports"); @tracking_entries = sort by_locate_time @tracking_entries; if($TEXT_FILE ne '') { log_print("Writing the text output file"); foreach $_(@tracking_entries) { s/^[^:]*://; print TXT $_,"\n"; } close(TXT); } if($GPX_FILE ne '') { log_print("Writing the GPX output file"); print GPX <<'ENDTEXT'; ENDTEXT foreach $line(@tracking_entries) { if($line=~/POS ([0-9]+) ([0-9\.]+) ([NS]) ([0-9]+) ([0-9\.]+) ([EW]) ALT ([0-9]+) (\S+) SPEED ([0-9\.]+) (\S+) COURSE ([0-9\.]+) AT (\S+) (\S+) UTC SATS ([0-9]+)/) { # lat-int lat-minsec lat-ns lon-int lon-minsec lon-ew alt alt-unit speed speed-unit course date time sats # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $lat = $1 + ($2 / 60); if($4 eq 'S') { $lat = 0 - $lat; } $lon = $4 + ($5 / 60); if($6 eq 'W') { $lon = 0 - $lon; } $ele = $7; if($8 eq 'FT') { $ele *= 0.3048; } $sat = ($14 + 0); $dt = '20'.$12.'T'.$13.'Z'; $dt=~s/\//-/g; print GPX " ${ele}${sat}\n"; } } print GPX <<'ENDTEXT'; ENDTEXT close(GPX); } log_print("Done"); sub log_print { print format_time(time()).' '.$_[0]."\n"; } sub format_time { my(@nowtime)=localtime($_[0]); return(sprintf("%02u:%02u:%02u",$nowtime[2],$nowtime[1],$nowtime[0])); } sub by_locate_time { my($aa,$bb); if($a=~/POS [0-9]+ [0-9\.]+ [NS] [0-9]+ [0-9\.]+ [EW] ALT [0-9]+ \S+ SPEED [0-9\.]+ \S+ COURSE [0-9\.]+ AT (\S+ \S+) UTC SATS [0-9]+/) { $aa = $1; } else { $aa = $a; } if($b=~/POS [0-9]+ [0-9\.]+ [NS] [0-9]+ [0-9\.]+ [EW] ALT [0-9]+ \S+ SPEED [0-9\.]+ \S+ COURSE [0-9\.]+ AT (\S+ \S+) UTC SATS [0-9]+/) { $bb = $1; } else { $bb = $b; } return($aa cmp $bb); } sub numerically { my($aa) = $a + 0; my($bb) = $b + 0; return($aa <=> $bb); } # EOF