3 # An RBN deduping filter
5 # Copyright (c) 2017 Dirk Koopman G1TLH
10 use IO::Socket::IP -register;
11 use Math::Round qw(nearest);
13 my $host = 'telnet.reversebeacon.net';
15 my $mycall = shift or die "usage:rbn.pl <callsign> [debug] [stats] [cw] [rtty] [psk] [beacon] [<time between repeat spots in minutes>]\n";
17 my $minspottime = 30*60; # minimum length of time between successive spots
18 my $showstats; # show RBN and Spot stats
32 ++$dbg if $arg =~ /^deb/i;
33 ++$showstats if $arg =~ /^stat/i;
34 $minspottime = $arg * 60 if $arg =~ /^\d+$/;
35 if (!$override && $arg =~ /^cw|rtty|psk|beacon$/i) {
37 $wantcw = $wantrtty = $wantpsk = $wantbeacon = 0;
39 ++$wantcw if $arg =~ /^cw$/i;
40 ++$wantpsk if $arg =~ /^psk$/i;
41 ++$wantrtty if $arg =~ /^rtty$/i;
42 ++$wantbeacon if $arg =~ /^beacon$/i;
45 for ($attempts = 1; $attempts <= 5; ++$attempts) {
46 say "admin,connecting to $host $port.. (attempt $attempts) " if $dbg;
47 $sock = IO::Socket::IP->new(
55 die "admin,Cannot connect to $host:$port after 5 attempts $!" unless $sock;
56 say "admin,connected" if $dbg;
57 print $sock "$mycall\r\n";
58 say "admin,call sent" if $dbg;
72 # periodic clearing out of the two caches
73 if ($tim % 60 == 0 && $last != $tim) {
77 while (my ($k,$v) = each %d) {
85 say "admin,rbn cache: $removed removed $count remain" if $dbg;
86 $count = $removed = 0;
87 while (my ($k,$v) = each %spot) {
88 if ($tim-$v > $minspottime*2) {
95 say "admin,spot cache: $removed removed $count remain" if $dbg;
97 say join(',', "STAT", $noraw, $norbn, $nospot) if $showstats;
98 $noraw = $norbn = $nospot = 0;
104 my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t) = split /[:\s]+/;
107 # We have an RBN data line, dedupe it very simply on time, ignore QRG completely.
108 # This works because the skimmers are NTP controlled (or should be) and will receive
109 # the spot at the same time (velocity factor of the atmosphere taken into account :-)
117 $qrg = sprintf('%.1f', nearest(.1, $qrg)); # to nearest 100Hz (to catch the odd multiple decpl QRG [eg '7002.07']).
118 say join(',', "RBN", $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t) if $dbg;
120 # Determine whether to "SPOT" it based on whether we have not seen it before (near this QRG) or,
121 # if we have, has it been a "while" since the last time we spotted it? If it has been spotted
122 # before then "RESPOT" it.
123 my $nqrg = nearest(1, $qrg); # normalised to nearest Khz
124 my $sp = "$call|$nqrg"; # hopefully the skimmers will be calibrated at least this well!
127 if (!$ts || $tim - $ts >= $minspottime) {
128 if ($wantbeacon && $sort =~ /^BEA/) {
131 next if !$wantcw && $mode =~ /^CW/;
132 next if !$wantrtty && $mode =~ /^RTTY/;
133 next if !$wantpsk && $mode =~ /^PSK/;
137 my $tag = $ts ? "RESPOT" : "SPOT";
138 say join(',', $tag, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
142 say "data,$_" if $dbg;