make the RESPOTTING work again
[spider.git] / perl / RBN.pm
1 #
2 # The RBN connection system
3 #
4 # Copyright (c) 2020 Dirk Koopman G1TLH
5 #
6
7 use warnings;
8 use strict;
9
10 package RBN;
11
12 use 5.10.1;
13
14 use lib qw {.};
15
16 use DXDebug;
17 use DXUtil;
18 use DXLog;
19 use DXUser;
20 use DXChannel;
21 use Math::Round qw(nearest nearest_floor);
22 use Date::Parse;
23 use Time::HiRes qw(gettimeofday);
24 use Spot;
25 use DXJSON;
26 use IO::File;
27
28 use constant {
29                           ROrigin => 0,
30                           RQrg => 1,
31                           RCall => 2,
32                           RMode => 3,
33                           RStrength => 4,
34                           RTime => 5,
35                           RUtz => 6,
36                           Respot => 7,
37                           RQra => 8,
38                           RSpotData => 9,
39                          };
40
41 use constant {
42                           SQrg => 0,
43                           SCall => 1,
44                           STime => 2,
45                           SComment => 3,
46                           SOrigin => 4,
47                           SZone => 11,
48                          };
49 use constant {
50                           OQual => 0,
51                           OAvediff => 1,
52                           OSpare => 2,
53                           ODiff => 3,
54                          };
55 use constant {
56                           CTime => 0,
57                           CQual => 1,
58                           CData => 2,
59                          };
60
61 use constant {
62                           DScore => 0,
63                           DGood => 1,
64                           DBad => 2,
65                           DLastin => 3,
66                           DEviants => 4,
67                          };
68
69
70 our $DATA_VERSION = 1;
71
72 our @ISA = qw(DXChannel);
73
74 our $startup_delay = 5*60;              # don't send anything out until this timer has expired
75                                 # this is to allow the feed to "warm up" with duplicates
76                                 # so that the "big rush" doesn't happen.
77
78 our $minspottime = 30*60;               # the time between respots of a callsign - if a call is
79                                 # still being spotted (on the same freq) and it has been
80                                 # spotted before, it's spotted again after this time
81                                 # until the next minspottime has passed.
82
83 our $beacontime = 5*60;                 # same as minspottime, but for beacons (and shorter)
84
85 our $dwelltime = 10;                    # the amount of time to wait for duplicates before issuing
86                                 # a spot to the user (no doubt waiting with bated breath).
87
88 our $filterdef = $Spot::filterdef; # we use the same filter as the Spot system. Can't think why :-).
89
90 my $spots;                                              # the GLOBAL spot cache
91
92 my %runtime;                                    # how long each channel has been running
93
94 our $cachefn = localdata('rbn_cache');
95 our $cache_valid = 4*60;                # The cache file is considered valid if it is not more than this old
96
97 our $maxqrgdiff = 10;                   # the maximum
98 our $minqual = 2;                               # the minimum quality we will accept for output
99
100 my $json;
101 my $noinrush = 0;                               # override the inrushpreventor if set
102 our $maxdeviants = 5;                   # the number of deviant QRGs to record for skimmer records
103
104 sub init
105 {
106         $json = DXJSON->new;
107         $json->canonical(0);
108         if (check_cache()) {
109                 $noinrush = 1;
110         } else {
111                 $spots = {VERSION=>$DATA_VERSION};
112         }
113         if (defined $DB::VERSION) {
114                 $noinrush = 1;
115                 $json->indent(1);
116         }
117         
118 }
119
120 sub new 
121 {
122         my $self = DXChannel::alloc(@_);
123
124         # routing, this must go out here to prevent race condx
125         my $pkg = shift;
126         my $call = shift;
127
128         $self->{last} = 0;
129         $self->{noraw} = 0;
130         $self->{nospot} = 0;
131         $self->{nouser} = {};
132         $self->{norbn} = 0;
133         $self->{noraw10} = 0;
134         $self->{nospot10} = 0;
135         $self->{nouser10} = {};
136         $self->{norbn10} = 0;
137         $self->{nospothour} = 0;
138         $self->{nouserhour} = {};
139         $self->{norbnhour} = 0;
140         $self->{norawhour} = 0;
141         $self->{sort} = 'N';
142         $self->{lasttime} = $main::systime;
143         $self->{minspottime} = $minspottime;
144         $self->{beacontime} = $beacontime;
145         $self->{showstats} = 0;
146         $self->{pingint} = 0;
147         $self->{nopings} = 0;
148         $self->{queue} = {};
149
150         return $self;
151 }
152
153 sub start
154
155         my ($self, $line, $sort) = @_;
156         my $user = $self->{user};
157         my $call = $self->{call};
158         my $name = $user->{name};
159                 
160         # log it
161         my $host = $self->{conn}->peerhost;
162         $host ||= "unknown";
163         $self->{hostname} = $host;
164
165         $self->{name} = $name ? $name : $call;
166         $self->state('prompt');         # a bit of room for further expansion, passwords etc
167         $self->{lang} = $user->lang || $main::lang || 'en';
168         if ($line =~ /host=/) {
169                 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
170                 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
171                 unless ($h) {
172                         ($h) = $line =~ /host=([\da..fA..F:]+)/;
173                         $line =~ s/\s*host=[\da..fA..F:]+// if $h;
174                 }
175                 if ($h) {
176                         $h =~ s/^::ffff://;
177                         $self->{hostname} = $h;
178                 }
179         }
180         $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
181         $self->{consort} = $line;       # save the connection type
182
183         LogDbg('DXCommand', "$call connected from $self->{hostname}");
184
185         # set some necessary flags on the user if they are connecting
186         $self->{registered} = 1;
187         # sort out privilege reduction
188         $self->{priv} = 0;
189
190         # get the filters
191         my $nossid = $call;
192         $nossid =~ s/-\d+$//;
193
194         $self->{inrbnfilter} = Filter::read_in('rbn', $call, 1) 
195                 || Filter::read_in('rbn', 'node_default', 1);
196         
197         # clean up qra locators
198         my $qra = $user->qra;
199         $qra = undef if ($qra && !DXBearing::is_qra($qra));
200         unless ($qra) {
201                 my $lat = $user->lat;
202                 my $long = $user->long;
203                 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);  
204         }
205
206         # if we have been running and stopped for a while 
207         # if the cache is warm enough don't operate the inrush preventor
208         $self->{inrushpreventor} = exists $runtime{$call} && $runtime{$call} > $startup_delay || $noinrush ?  0 : $main::systime + $startup_delay;
209         dbg("RBN: noinrush: $noinrush, setting inrushpreventor on $self->{call} to $self->{inrushpreventor}");
210 }
211
212 my @queue;                                              # the queue of spots ready to send
213
214 sub normal
215 {
216         my $self = shift;
217         my $line = shift;
218         my @ans;
219         my $dbgrbn = isdbg('rbn');
220         
221         # remove leading and trailing spaces
222         chomp $line;
223         $line =~ s/^\s*//;
224         $line =~ s/\s*$//;
225
226         # add base RBN
227
228         my $now = $main::systime;
229
230         # parse line
231         dbg "RBN:RAW,$line" if isdbg('rbnraw');
232         return unless $line=~/^DX\s+de/;
233
234         my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
235
236         # fix up FT8 spots from 7001
237         $t = $u, $u = '' if !$t && is_ztime($u);
238         $t = $sort, $sort = '' if !$t && is_ztime($sort);
239         my $qra = $spd, $spd = '' if is_qra($spd);
240         $u = $qra if $qra;
241
242         # is this anything like a callsign?
243         unless (is_callsign($call)) {
244                 dbg("RBN: ERROR $call from $origin on $qrg is invalid, dumped");
245                 return;
246         }
247
248         $origin =~ s/\-(?:\d{1,2}\-)?\#$//; # get rid of all the crap we aren't interested in
249
250
251         $sort ||= '';
252         $tx ||= '';
253         $qra ||= '';
254     dbg qq{RBN:input decode or:$origin qr:$qrg ca:$call mo:$mode s:$s m:$m sp:$spd u:$u sort:$sort t:$t tx:$tx qra:$qra} if $dbgrbn;
255
256         ++$self->{noraw};
257         ++$self->{noraw10};
258         ++$self->{norawhour};
259         
260         my $b;
261         
262         if ($t || $tx) {
263
264                 # fix up times for things like 'NXDXF B' etc
265                 if ($tx && is_ztime($t)) {
266                         if (is_ztime($tx)) {
267                                 $b = $t;
268                                 $t = $tx;
269                         } else {
270                                 dbg "RBN:ERR,$line";
271                                 return (0);
272                         }
273                 }
274                 if ($sort && $sort eq 'NCDXF') {
275                         $mode = 'DXF';
276                         $t = $tx;
277                 }
278                 if ($sort && $sort eq 'BEACON') {
279                         $mode = 'BCN';
280                 }
281                 if ($mode =~ /^PSK/) {
282                         $mode = 'PSK';
283                 }
284                 if ($mode eq 'RTTY') {
285                         $mode = 'RTT';
286                 }
287
288                 # The main de-duping key is [call, $frequency], but we probe a bit around that frequency to find a
289                 # range of concurrent frequencies that might be in play. 
290
291                 # The key to this is deducing the true callsign by "majority voting" (the greater the number of spotters
292         # the more effective this is) together with some lexical analsys probably in conjuction with DXSpider
293                 # data sources (for singleton spots) to then generate a "centre" from and to zone (whatever that will mean if it isn't the usual one)
294                 # and some heuristical "Kwalitee" rating given distance from the zone centres of spotter, recipient user
295         # and spotted. A map can be generated once per user and spotter as they are essentially mostly static. 
296                 # The spotted will only get a coarse position unless other info is available. Programs that parse 
297                 # DX bulletins and the online data online databases could be be used and then cached. 
298
299                 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
300                 # ignored.
301
302                 # Clearly this will only work in the 'mojo' branch of DXSpider where it is possible to pass off external
303                 # data requests to ephemeral or semi resident forked processes that do any grunt work and the main
304                 # process to just the standard "message passing" which has been shown to be able to sustain over 5000 
305                 # per second (limited by the test program's output and network speed, rather than DXSpider's handling).
306
307                 my $search = 5;
308                 my $nqrg = nearest(1, $qrg * 10);  # normalised to nearest Khz
309                 my $sp = "$call|$nqrg";           # hopefully the skimmers will be calibrated at least this well!
310
311                 # find it?
312                 my $cand = $spots->{$sp};
313                 unless ($cand) {
314                         my ($i, $new);
315                         for ($i = $nqrg; !$cand && $i <= $nqrg+$search; $i += 1) {
316                                 $new = "$call|$i";
317                                 $cand = $spots->{$new}, last if exists $spots->{$new};
318                         }
319                         if ($cand) {
320                                 my $diff = $i - $nqrg;
321                                 dbg(qq{RBN: QRG Diff using $new (+$diff) for $sp for qrg $qrg}) if (isdbg('rbnqrg') || $dbgrbn);
322                                 $sp = $new;
323                         }
324                 }
325                 unless ($cand) {
326                         my ($i, $new);
327                         for ($i = $nqrg; !$cand && $i >= $nqrg-$search; $i -= 1) {
328                                 $new = "$call|$i";
329                                 $cand = $spots->{$new}, last if exists $spots->{$new};
330                         }
331                         if ($cand) {
332                                 my $diff = $nqrg - $i;
333                                 dbg(qq{RBN: QRG Diff using $new (-$diff) for $sp for qrg $qrg}) if (isdbg('rbnqrg') || $dbgrbn);
334                                 $sp = $new;
335                         }
336                 }
337                 
338                 # if we have one and there is only one slot and that slot's time isn't expired for respot then return
339                 my $respot = 0;
340                 if ($cand && ref $cand) {
341                         if (@$cand <= CData) {
342                                 if ($self->{minspottime} > 0 && $now - $cand->[CTime] < $self->{minspottime}) {
343                                         dbg("RBN: key: '$sp' call: $call qrg: $qrg DUPE \@ ". atime(int $cand->[CTime])) if $dbgrbn;
344                                         return;
345                                 }
346                                 
347                                 dbg("RBN: key: '$sp' RESPOTTING call: $call qrg: $qrg last seen \@ ". atime(int $cand->[CTime])) if $dbgrbn;
348                                 $cand->[CTime] = $now;
349                                 ++$respot;
350                         }
351
352                         # otherwise we have a spot being built up at the moment
353                 } elsif ($cand) {
354                         dbg("RBN: key '$sp' = '$cand' not ref");
355                         return;
356                 } else {
357                         # new spot / frequency
358                         $spots->{$sp} = $cand = [$now, 0];
359                         dbg("RBN: key: '$sp' call: $call qrg: $qrg NEW" . ($respot ? ' RESPOT' : '')) if $dbgrbn;
360                 }
361
362                 # add me to the display queue unless we are waiting for initial in rush to finish
363                 return unless $noinrush || $self->{inrushpreventor} < $main::systime;
364
365                 # build up a new record and store it in the buildup
366                 # deal with the unix time
367                 my ($hh,$mm) = $t =~ /(\d\d)(\d\d)Z$/;
368                 my $utz = $hh*3600 + $mm*60 + $main::systime_daystart; # possible issue with late spot from previous day
369                 $utz -= 86400 if $utz > $now+3600;                                         # too far ahead, drag it back one day
370
371                 # create record and add into the buildup
372                 my $r = [$origin, nearest(.1, $qrg), $call, $mode, $s, $t, $utz, $respot, $u];
373                 my @s =  Spot::prepare($r->[RQrg], $r->[RCall], $r->[RUtz], '', $r->[ROrigin]);
374                 if ($s[5] == 666) {
375                         dbg("RBN: ERROR invalid prefix/callsign $call from $origin-# on $qrg, dumped");
376                         return;
377                 }
378                 
379                 if ($self->{inrbnfilter}) {
380                         my ($want, undef) = $self->{inrbnfilter}->it($s);
381                         return unless $want;    
382                 }
383                 $r->[RSpotData] = \@s;
384
385                 ++$self->{queue}->{$sp};# unless @$cand>= CData; # queue the KEY (not the record)
386
387                 dbg("RBN: key: '$sp' ADD RECORD call: $call qrg: $qrg origin: $origin respot: $respot") if $dbgrbn;
388
389                 push @$cand, $r;
390
391         } else {
392                 dbg "RBN:DATA,$line" if $dbgrbn;
393         }
394 }
395
396 # we should get the spot record minus the time, so just an array of record (arrays)
397 sub send_dx_spot
398 {
399         my $self = shift;
400         my $quality = shift;
401         my $cand = shift;
402
403         ++$self->{norbn};
404         ++$self->{norbn10};
405         ++$self->{norbnhour};
406         
407         # $r = [$origin, $qrg, $call, $mode, $s, $utz, $respot];
408
409         my $mode = $cand->[CData]->[RMode]; # as all the modes will be the same;
410         
411         my @dxchan = DXChannel::get_all();
412
413         foreach my $dxchan (@dxchan) {
414                 next unless $dxchan->is_user;
415                 my $user = $dxchan->{user};
416                 next unless $user &&  $user->wantrbn;
417
418                 # does this user want this sort of spot at all?
419                 my $want = 0;
420                 ++$want if $user->wantbeacon && $mode =~ /^BCN|DXF/;
421                 ++$want if $user->wantcw && $mode =~ /^CW/;
422                 ++$want if $user->wantrtty && $mode =~ /^RTT/;
423                 ++$want if $user->wantpsk && $mode =~ /^PSK|FSK|MSK/;
424                 ++$want if $user->wantft && $mode =~ /^FT/;
425
426                 dbg(sprintf("RBN: spot selection for $dxchan->{call} mode: '$mode' want: $want flags rbn:%d ft:%d bcn:%d cw:%d psk:%d rtty:%d",
427                                         $user->wantrbn,
428                                         $user->wantft,
429                                         $user->wantbeacon,
430                                         $user->wantcw,
431                                         $user->wantpsk,
432                                         $user->wantrtty,
433                                    )) if isdbg('rbnll');
434
435                 # send one spot to one user out of the ones that we have
436                 $self->dx_spot($dxchan, $quality, $cand) if $want;
437         }
438 }
439
440 sub dx_spot
441 {
442         my $self = shift;
443         my $dxchan = shift;
444         my $quality = shift;
445         my $cand = shift;
446         my $call = $dxchan->{call};
447         my $seeme = $dxchan->user->rbnseeme();
448         my $strength = 100;             # because it could if we talk about FTx
449         my $saver;
450         my %zone;
451         my $respot;
452         my $qra;
453
454         ++$self->{nousers}->{$call};
455         ++$self->{nousers10}->{$call};
456         ++$self->{nousershour}->{$call};
457
458         my $filtered;
459         my $rf = $dxchan->{rbnfilter} || $dxchan->{spotsfilter};
460         my $comment;
461         
462         foreach my $r (@$cand) {
463                 # $r = [$origin, $qrg, $call, $mode, $s, $t, $utz, $respot, $qra];
464                 # Spot::prepare($qrg, $call, $utz, $comment, $origin);
465                 next unless $r && ref $r;
466
467                 $qra = $r->[RQra] if !$qra && $r->[RQra] && is_qra($r->[RQra]);
468
469                 $comment = sprintf "%-3s %2ddB $quality", $r->[RMode], $r->[RStrength];
470                 my $s = $r->[RSpotData];                # the prepared spot
471                 $s->[SComment] = $comment;              # apply new generated comment
472
473                 ++$zone{$s->[SZone]};           # save the spotter's zone
474
475                 # if the 'see me' flag is set, then show all the spots without further adornment (see set/rbnseeme for more info)
476                 if ($seeme) {
477                         send_final($dxchan, $s);
478                         next;
479                 }
480
481                 # save the lowest strength one
482                 if ($r->[RStrength] < $strength) {
483                         $strength = $r->[RStrength];
484                         $saver = $s;
485                         dbg("RBN: STRENGTH spot: $s->[SCall] qrg: $s->[SQrg] origin: $s->[SOrigin] dB: $r->[RStrength] < $strength") if isdbg 'rbnll';
486                 }
487
488                 if ($rf) {
489                         my ($want, undef) = $rf->it($s);
490                         dbg("RBN: FILTERING for $call spot: $s->[SCall] qrg: $s->[SQrg] origin: $s->[SOrigin] dB: $r->[RStrength] com: '$s->[SComment]' want: " . ($want ? 'YES':'NO')) if isdbg 'rbnll';
491                         next unless $want;
492                         $filtered = $s;
493                 }
494         }
495
496         if ($rf) {
497                 $saver = $filtered;             # if nothing passed the filter's lips then $saver == $filtered == undef !
498         }
499         
500         if ($saver) {
501                 my $buf;
502                 # create a zone list of spotters
503                 delete $zone{$saver->[SZone]};  # remove this spotter's zone (leaving all the other zones)
504                 my $z = join ',', sort {$a <=> $b} keys %zone;
505
506                 # alter spot data accordingly
507                 $saver->[SComment] .= " Z:$z" if $z;
508                 
509                 send_final($dxchan, $saver);
510                 
511                 ++$self->{nospot};
512                 ++$self->{nospot10};
513                 ++$self->{nospothour};
514                 
515                 if ($qra) {
516                         my $user = DXUser::get_current($saver->[SCall]) || DXUser->new($saver->[SCall]);
517                         unless ($user->qra && is_qra($user->qra)) {
518                                 $user->qra($qra);
519                                 dbg("RBN: update qra on $saver->[SCall] to $qra");
520                                 $user->put;
521                         }
522                 }
523         }
524 }
525
526 sub send_final
527 {
528         my $dxchan = shift;
529         my $saver = shift;
530         my $call = $dxchan->{call};
531         my $buf;
532         
533         dbg("RBN: SENDING to $call spot: $saver->[SCall] qrg: $saver->[SQrg] origin: $saver->[SOrigin] $saver->[SComment]") if isdbg 'rbnll';
534         if ($dxchan->{ve7cc}) {
535                 my $call = $saver->[SOrigin];
536                 $saver->[SOrigin] .= '-#';
537                 $buf = VE7CC::dx_spot($dxchan, @$saver);
538                 $saver->[SOrigin] = $call;
539         } else {
540                 my $call = $saver->[SOrigin];
541                 $saver->[SOrigin] = substr($call, 0, 6);
542                 $saver->[SOrigin] .= '-#';
543                 $buf = $dxchan->format_dx_spot(@$saver);
544                 $saver->[SOrigin] = $call;
545         }
546         $dxchan->local_send('N', $buf);
547 }
548
549 # per second
550 sub process
551 {
552         my $rbnskim = isdbg('rbnskim');
553         
554         foreach my $dxchan (DXChannel::get_all()) {
555                 next unless $dxchan->is_rbn;
556
557                 # At this point we run the queue to see if anything can be sent onwards to the punter
558                 my $now = $main::systime;
559                 my $ta = [gettimeofday];
560                 my $items = 0;
561                 
562                 # now run the waiting queue which just contains KEYS ($call|$qrg)
563                 foreach my $sp (keys %{$dxchan->{queue}}) {
564                         my $cand = $spots->{$sp};
565                         ++$items;
566                         unless ($cand && $cand->[CTime]) {
567                                 dbg "RBN Cand $sp " . ($cand ? 'def' : 'undef') . " [CTime] " . ($cand->[CTime] ? 'def' : 'undef') . " dwell $dwelltime";
568                                 next;
569                         } 
570                         if ($now >= $cand->[CTime] + $dwelltime ) {
571                                 # we have a candidate, create qualitee value(s);
572                                 unless (@$cand > CData) {
573                                         dbg "RBN: QUEUE key '$sp' MISSING RECORDS, IGNORED" . dd($cand) if isdbg 'rbnqueue';
574                                         delete $spots->{$sp}; # don't remember it either - this means that a spot HAS to come in with sufficient spotters to be processed.
575                                         delete $dxchan->{queue}->{$sp};
576                                         next;
577                                 }
578                                 dbg "RBN: QUEUE PROCESSING key: '$sp' $now >= $cand->[CTime]" if isdbg 'rbnqueue'; 
579                                 my $quality = @$cand - CData;
580                                 my $spotters = $quality;
581
582                                 # dump it and remove it from the queue if it is of unadequate quality
583                                 if ($quality < $minqual) {
584                                         if ($rbnskim) {
585                                                 my $r = $cand->[CData];
586                                                 if ($r) {
587                                                         my $s = "RBN:SKIM Ignored (Q:$quality < Q:$minqual) key: '$sp' = $r->[RCall] on $r->[RQrg] by $r->[ROrigin] \@ $r->[RTime] route: $dxchan->{call}";
588                                                         dbg($s);
589                                                 }
590                                         }
591                                         delete $spots->{$sp}; # don't remember it either - this means that a spot HAS to come in with sufficient spotters to be processed.
592                                         delete $dxchan->{queue}->{$sp};
593                                         next;
594                                 }
595
596                                 $quality = 9 if $quality > 9;
597                                 $cand->[CQual] = $quality if $quality > $cand->[CQual];
598
599                                 my $r;
600
601                                 # this scores each candidate according to its skimmer's QRG score (i.e. how often it agrees with its peers)
602                                 # what happens is hash of all QRGs in candidates are incremented by that skimmer's reputation for "accuracy"
603                                 # or, more exactly, past agreement with the consensus. This score can be from -5 -> +5. 
604                                 my %qrg = ();
605                                 my $skimmer;
606                                 my $sk;
607                                 my $band;
608                                 my %seen = ();
609                                 foreach $r (@$cand) {
610                                         next unless ref $r;
611                                         if (exists $seen{$r->[ROrigin]}) {
612                                                 undef $r;
613                                                 next;
614                                         }
615                                         $seen{$r->[ROrigin]} = 1;
616                                         $band ||= int $r->[RQrg] / 1000;
617                                         $sk = "SKIM|$r->[ROrigin]|$band"; # thus only once per set of candidates
618                                         $skimmer = $spots->{$sk};
619                                         unless ($skimmer) {
620                                                 $skimmer = $spots->{$sk} = [1, 0, 0, $now, []]; # this first time, this new skimmer gets the benefit of the doubt on frequency.
621                                                 dbg("RBN:SKIM new slot $sk " . $json->encode($skimmer)) if $rbnskim;
622                                         }
623                                         $qrg{$r->[RQrg]} += ($skimmer->[DScore] || 1);
624                                 }
625                                 
626                                 # determine the most likely qrg and then set it - NOTE (-)ve votes, generated by the skimmer scoring system above, are ignored
627                                 my @deviant;
628                                 my $c = 0;
629                                 my $mv = 0;
630                                 my $qrg = 0;
631                                 while (my ($k, $votes) = each %qrg) {
632                                         if ($votes >= $mv) {
633                                                 $qrg = $k;
634                                                 $mv = $votes;
635                                         }
636                                         ++$c;
637                                 }
638
639                                 # Ignore possible spots with 0 QRG score - as determined by the skimmer scoring system above -  as they are likely to be wrong 
640                                 unless ($qrg > 0) {
641                                         if ($rbnskim) {
642                                                 my $keys;
643                                                 while (my ($k, $v) = (each %qrg)) {
644                                                         $keys .= "$k=>$v, ";
645                                                 }
646                                                 $keys =~ /,\s*$/;
647                                                 my $i = 0;
648                                                 foreach $r (@$cand) {
649                                                         next unless $r && ref $r;
650                                                         dbg "RBN:SKIM cand $i QRG likely wrong from '$sp' = $r->[RCall] on $r->[RQrg] by $r->[ROrigin] \@ $r->[RTime] (qrgs: $keys c: $c) route: $dxchan->{call}, ignored";
651                                                         ++$i;
652                                                 }
653                                         }
654                                         delete $spots->{$sp}; # get rid
655                                         delete $dxchan->{queue}->{$sp};
656                                         next;
657                                 }
658
659                                 # detemine and spit out the deviants. Then adjust the scores according to whether it is a deviant or good
660                                 # NOTE: deviant nodes can become good (or less bad), and good nodes bad (or less good) on each spot that
661                                 # they generate. This is based solely on each skimmer's agreement (or not) with the "consensus" score generated
662                                 # above ($qrg). The resultant score + good + bad is stored per band and will be used the next time a spot
663                                 # appears on this band from each skimmer.
664                                 foreach $r (@$cand) {
665                                         next unless $r && ref $r;
666                                         my $diff = $c > 1 ? nearest(.1, $r->[RQrg] - $qrg) : 0;
667                                         $sk = "SKIM|$r->[ROrigin]|$band";
668                                         $skimmer = $spots->{$sk};
669                                         if ($diff) {
670                                                 ++$skimmer->[DBad] if $skimmer->[DBad] < $maxdeviants;
671                                                 --$skimmer->[DGood] if $skimmer->[DGood] > 0;
672                                                 push @deviant, sprintf("$r->[ROrigin]:%+.1f", $diff);
673                                                 push @{$skimmer->[DEviants]}, $diff;
674                                                 shift @{$skimmer->[DEviants]} while @{$skimmer->[DEviants]} > $maxdeviants;
675                                         } else {
676                                                 ++$skimmer->[DGood] if $skimmer->[DGood] < $maxdeviants;
677                                                 --$skimmer->[DBad] if $skimmer->[DBad] > 0;
678                                                 shift @{$skimmer->[DEviants]};
679                                         }
680                                         $skimmer->[DScore] = $skimmer->[DGood] - $skimmer->[DBad];
681                                         my $lastin = difft($skimmer->[DLastin], $now, 2);
682                                         my $difflist = join(', ', @{$skimmer->[DEviants]});
683                                         $difflist = " ($difflist)" if $difflist;
684                                         dbg("RBN:SKIM key $sp slot $sk $r->[RQrg] - $qrg = $diff Skimmer score: $skimmer->[DGood] - $skimmer->[DBad] = $skimmer->[DScore] lastseen:$lastin ago$difflist") if $rbnskim; 
685                                         $skimmer->[DLastin] = $now;
686                                         $r->[RSpotData]->[SQrg] = $qrg if $qrg && $c > 1; # set all the QRGs to the agreed value
687                                 }
688
689                                 $qrg = (sprintf "%.1f",  $qrg)+0;
690                                 $r = $cand->[CData];
691                                 $r->[RQrg] = $qrg;
692                                 my $squality = "Q:$cand->[CQual]";
693                                 $squality .= '*' if $c > 1; 
694                                 $squality .= '+' if $r->[Respot];
695
696                                 if (isdbg('progress')) {
697                                         my $s = "RBN: SPOT key: '$sp' = $r->[RCall] on $r->[RQrg] by $r->[ROrigin] \@ $r->[RTime] $squality route: $dxchan->{call}";
698                                         my $td = @deviant;
699                                         $s .= " QRGScore $mv Deviants ($td/$spotters): ";
700                                         $s .= join(', ', sort @deviant) if $td;
701                                         dbg($s);
702                                 }
703
704                                 # finally send it out to any waiting public
705                                 send_dx_spot($dxchan, $squality, $cand);
706                                 
707                                 # clear out the data and make this now just "spotted", but no further action required until respot time
708                                 dbg "RBN: QUEUE key '$sp' cleared" if isdbg 'rbn';
709
710                                 delete $dxchan->{queue}->{$sp};
711
712                                 # calculate new sp (which will be 70% likely the same as the old one)
713                                 # we do this to cope with the fact that the first spotter may well be "wrongly calibrated" giving a qrg that disagrees with the majority.
714                                 # and we want to store the key that corresponds to majority opinion. 
715                                 my $nqrg = nearest(1, $qrg * 10);  # normalised to nearest Khz
716                                 my $nsp = "$r->[RCall]|$nqrg";
717                                 if ($sp ne $nsp) {
718                                         dbg("RBN:SKIM CHANGE KEY sp '$sp' -> '$nsp' for storage") if $rbnskim;
719                                         delete $spots->{$sp};
720                                         $spots->{$nsp} = [$now, $cand->[CQual]];
721                                 } else {
722                                         $spots->{$sp} = [$now, $cand->[CQual]];
723                                 }
724                         }
725                         else {
726                                 dbg sprintf("RBN: QUEUE key: '$sp' SEND time not yet reached %.1f secs left", $cand->[CTime] + $dwelltime - $now) if isdbg 'rbnqueue'; 
727                         }
728                 }
729                 if (isdbg('rbntimer')) {
730                         my $diff = _diffus($ta);
731                         dbg "RBN: TIMER process queue for call: $dxchan->{call} $items spots $diff uS";
732                 }
733         }
734 }
735
736 sub per_minute
737 {
738         foreach my $dxchan (DXChannel::get_all()) {
739                 next unless $dxchan->is_rbn;
740                 dbg "RBN:STATS minute $dxchan->{call} raw: $dxchan->{noraw} retrieved spots: $dxchan->{norbn} delivered: $dxchan->{nospot} after filtering to users: " . scalar keys %{$dxchan->{nousers}} if isdbg('rbnstats');
741                 if ($dxchan->{noraw} == 0 && $dxchan->{lasttime} > 60) {
742                         LogDbg('RBN', "RBN: no input from $dxchan->{call}, disconnecting");
743                         $dxchan->disconnect;
744                 }
745                 $dxchan->{noraw} = $dxchan->{norbn} = $dxchan->{nospot} = 0; $dxchan->{nousers} = {};
746                 $runtime{$dxchan->{call}} += 60;
747         }
748
749         # save the spot cache
750         write_cache() unless $main::systime + $startup_delay < $main::systime;;
751 }
752
753 sub per_10_minute
754 {
755         my $count = 0;
756         my $removed = 0;
757         while (my ($k,$cand) = each %{$spots}) {
758                 next if $k eq 'VERSION';
759                 next if $k =~ /^O\|/;
760                 next if $k =~ /^SKIM\|/;
761                 
762                 if ($main::systime - $cand->[CTime] > $minspottime*2) {
763                         delete $spots->{$k};
764                         ++$removed;
765                 }
766                 else {
767                         ++$count;
768                 }
769         }
770         dbg "RBN:STATS spot cache remain: $count removed: $removed"; # if isdbg('rbn');
771         foreach my $dxchan (DXChannel::get_all()) {
772                 next unless $dxchan->is_rbn;
773                 my $nq = keys %{$dxchan->{queue}};
774                 my $pc = $dxchan->{noraw10} ? sprintf("%.1f%%",$dxchan->{norbn10}*100/$dxchan->{noraw10}) : '0.0%';
775                 dbg "RBN:STATS 10-minute $dxchan->{call} queue: $nq raw: $dxchan->{noraw10} retrieved spots: $dxchan->{norbn10} ($pc) delivered: $dxchan->{nospot10} after filtering to  users: " . scalar keys %{$dxchan->{nousers10}};
776                 $dxchan->{noraw10} = $dxchan->{norbn10} = $dxchan->{nospot10} = 0; $dxchan->{nousers10} = {};
777         }
778 }
779
780 sub per_hour
781 {
782         foreach my $dxchan (DXChannel::get_all()) {
783                 next unless $dxchan->is_rbn;
784                 my $nq = keys %{$dxchan->{queue}};
785                 my $pc = $dxchan->{norawhour} ? sprintf("%.1f%%",$dxchan->{norbnhour}*100/$dxchan->{norawhour}) : '0.0%';
786                 dbg "RBN:STATS hour $dxchan->{call} queue: $nq raw: $dxchan->{norawhour} retrieved spots: $dxchan->{norbnhour} ($pc) delivered: $dxchan->{nospothour} after filtering to users: " . scalar keys %{$dxchan->{nousershour}};
787                 $dxchan->{norawhour} = $dxchan->{norbnhour} = $dxchan->{nospothour} = 0; $dxchan->{nousershour} = {};
788         }
789 }
790
791 sub finish
792 {
793         write_cache();
794 }
795
796 sub write_cache
797 {
798         my $ta = [ gettimeofday ];
799         $json->indent(1)->canonical(1) if isdbg 'rbncache';
800         my $s = eval {$json->encode($spots)};
801         if ($s) {
802                 my $fh = IO::File->new(">$cachefn") or confess("writing $cachefn $!");
803                 $fh->print($s);
804                 $fh->close;
805         } else {
806                 dbg("RBN:Write_cache error '$@'");
807                 return;
808         }
809         $json->indent(0)->canonical(0);
810         my $diff = _diffms($ta);
811         my $size = sprintf('%.3fKB', (length($s) / 1000));
812         dbg("RBN:WRITE_CACHE size: $size time to write: $diff mS");
813 }
814
815 sub check_cache
816 {
817         if (-e $cachefn) {
818                 my $mt = (stat($cachefn))[9];
819                 my $t = $main::systime - $mt || 1;
820                 my $p = difft($mt, 2);
821                 if ($t < $cache_valid) {
822                         dbg("RBN:check_cache '$cachefn' spot cache exists, created $p ago and not too old");
823                         my $fh = IO::File->new($cachefn);
824                         my $s;
825                         if ($fh) {
826                                 local $/ = undef;
827                                 $s = <$fh>;
828                                 dbg("RBN:check_cache cache read size " . length $s);
829                                 $fh->close;
830                         } else {
831                                 dbg("RBN:check_cache file read error $!");
832                                 return undef;
833                         }
834                         if ($s) {
835                                 eval {$spots = $json->decode($s)};
836                                 if ($spots && ref $spots) {     
837                                         if (exists $spots->{VERSION} && $spots->{VERSION} == $DATA_VERSION) {
838                                                 # now clean out anything that has spot build ups in progress
839                                                 while (my ($k, $cand) = each %$spots) {
840                                                         next if $k eq 'VERSION';
841                                                         next if $k =~ /^O\|/;
842                                                         next if $k =~ /^SKIM\|/;
843                                                         if (@$cand > CData) {
844                                                                 $spots->{$k} = [$cand->[CTime], $cand->[CQual]];
845                                                         }
846                                                 }
847                                                 dbg("RBN:check_cache spot cache restored");
848                                                 return 1;
849                                         } 
850                                 }
851                                 dbg("RBN::checkcache error decoding $@");
852                         }
853                 } else {
854                         my $d = difft($main::systime-$cache_valid);
855                         dbg("RBN::checkcache '$cachefn' created $p ago is too old (> $d), ignored");
856                 }
857         } else {
858                 dbg("RBN:check_cache '$cachefn' spot cache not present");
859         }
860         
861         return undef;
862 }
863
864 1;