2 # Obtain the latest keps from the Amsat site and
5 # This will clear out the old keps and rewrite the $root/local/Keps.pm
6 # file to retain the data.
8 # The main state machine code comes more or less straight out of convkeps.pl
9 # This command is really to avoid the (even more) messy business of parsing emails
11 # Copyright (c) 2013 Dirk Koopman, G1TLH
14 # convert (+/-)00000-0 to (+/-).00000e-0
17 my ($sign, $frac, $esign, $exp) = unpack "aa5aa", shift;
18 $esign = '+' if $esign eq ' ';
19 my $n = $sign . "." . $frac . 'e' . $esign . $exp;
28 if ($conn->{kepsin}) {
29 my $fn = "$main::root/local/Keps.pm";
32 my @lines = split /[\r\n]+/, $conn->{kepsin};
54 dbg("keps: $state $_") if isdbg('keps');
56 if ($state == 0 && /^Decode/i) {
58 } elsif ($state == 1) {
60 next if m{^To\s+all}i;
62 if (/^([- \w]+)(?:\s+\[[-+\w]\])?$/) {
64 dbg("keps: $state processing $n") if isdbg('keps');
68 $ref = $keps{$name} = {};
71 } elsif ($state == 2) {
73 my ($id, $number, $epoch, $decay, $mm2, $bstar, $elset) = unpack "xxa5xxa5xxxa15xa10xa8xa8xxxa4x", $_;
74 dbg("keps: $state processing line 1 for $name") if isdbg('keps');
76 $ref->{number} = $number - 0;
77 $ref->{epoch} = $epoch - 0;
78 $ref->{mm1} = $decay - 0;
79 $ref->{mm2} = genenum($mm2);
80 $ref->{bstar} = genenum($bstar);
81 $ref->{elset} = $elset - 0;
82 #print "$id $number $epoch $decay $mm2 $bstar $elset\n";
83 #print "mm2: $ref->{mm2} bstar: $ref->{bstar}\n";
87 #print "out of order on line $line\n";
88 dbg("keps: $state invalid or out of order line 1 for $name") if isdbg('keps');
90 delete $keps{$name} if defined $name;
93 } elsif ($state == 3) {
95 my ($id, $incl, $raan, $ecc, $peri, $man, $mmo, $orbit) = unpack "xxa5xa8xa8xa7xa8xa8xa11a5x", $_;
96 dbg("keps: $state processing line 2 for $name") if isdbg('keps');
97 $ref->{meananomaly} = $man - 0;
98 $ref->{meanmotion} = $mmo - 0;
99 $ref->{inclination} = $incl - 0;
100 $ref->{eccentricity} = ".$ecc" - 0;
101 $ref->{argperigee} = $peri - 0;
102 $ref->{raan} = $raan - 0;
103 $ref->{orbit} = $orbit - 0;
106 #print "out of order on line $line\n";
107 dbg("keps: $state invalid or out of order line 2 for $name") if isdbg('keps');
115 dbg("keps: $count recs, creating $fn") if isdbg('keps');
116 my $dd = new Data::Dumper([\%keps], [qw(*keps)]);
119 open(OUT, ">$fn") or die "$fn $!";
120 print OUT "#\n# this file is automatically produced by the get/keps command\n#\n";
121 print OUT "# Last update: ", scalar gmtime, "\n#\n";
122 print OUT "\npackage Sun;\n\n";
123 print OUT $dd->Dumpxs;
126 dbg("keps: running load/keps") if isdbg('keps');
127 dbg("keps: clearing out old keps") if isdbg('keps');
129 $dxchan->send($dxchan->run_cmd("load/keps"));
139 $conn->{kepsin} .= "$msg\n";
141 # dbg("keps in: $msg") if isdbg('keps');
146 my ($self, $line) = @_;
147 my $call = $self->call;
151 return (1, $self->msg('e24')) unless $Internet::allow;
152 return (1, $self->msg('e5')) if $self->priv < 8;
153 my $target = $Internet::keps_url || 'www.amsat.org';
154 my $path = $Internet::keps_path || '/amsat/ftp/keps/current/nasa.all';
157 dbg("keps: contacting $target:$port") if isdbg('keps');
159 Log('call', "$call: show/keps $line");
160 my $conn = AsyncMsg->post($self, $target, $port, $path,
162 on_disc => \&on_disc);
165 push @out, $self->msg('m21', "show/keps");
167 push @out, $self->msg('e18', 'get/keps error');