-
Notifications
You must be signed in to change notification settings - Fork 0
/
dxc.pl
273 lines (216 loc) · 7.38 KB
/
dxc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/perl
# For Redpitaya & Pavel Demin FT8 code image @ http://pavel-demin.github.io/red-pitaya-notes/sdr-transceiver-ft8
# Gather decodes from FT8 log file /dev/shm/decode-ft8.log file of format
# 133915 1 0 1 17 0.0 17.0 37.4 3 0.12 10137466 CQ K1RA FM18
# handles msgs: CQ CALL1 GRID, CALL1 CALL2 GRID, CALL1 CALL2 RPT, CALL1 CALL2 RR73, etc.
# creates DXCluster like spots available via telnet port 7373
# caches calls up to 5 minutes before respotting (see $MINTIME)
# v0.7.1 - 2018/04/12 - K1RA
# Start by using following command line
# ./dxc.pl YOURCALL YOURGRID
# ./dxc.pl WX1YZ AB12DE
use strict;
use warnings;
use IO::Socket;
# minimum number of minutes to cache calls before resending
my $MINTIME = 5;
# check for YOUR CALL SIGN
if( ! defined( $ARGV[0]) || ( ! ( $ARGV[0] =~ /\w\d+\w/)) ) {
die "Enter a valid call sign\n";
}
my $mycall = uc( $ARGV[0]);
# check for YOUR GRID SQUARE (6 digit)
if( ! defined( $ARGV[1]) || ( ! ( $ARGV[1] =~ /\w\w\d\d\w\w/)) ) {
die "Enter a valid 6 digit grid\n";
}
my $mygrid = uc( $ARGV[1]);
# DXCluster spot line header
my $prompt = "DX de ".$mycall."-#:";
# holds one single log file line
my $line;
# FT8 fields from FT8 decoder log file
my $gmt;
my $x;
my $snr;
my $dt;
my $freq;
my @rest;
my $ft8msg;
my $call;
my $grid;
my $cqde;
# decode current and last times
my $time;
my $ltime;
# hash of deduplicated calls per band
my %db;
# call + base key for %db hash array
my $cb;
# minute counter to buffer decode lines
my $min = 0;
# lookup table to determine base FT8 frequency used to calculate Hz offset
my %basefrq = (
"184" => 1840000,
"357" => 3573000,
"535" => 5357000,
"707" => 7074000,
"1013" => 10136000,
"1407" => 14074000,
"1810" => 18100000,
"2107" => 21074000,
"2491" => 24915000,
"2807" => 28074000,
"5031" => 50313000
);
# used for calculating signal in Hz from base band FT8 frequency
my $base;
my $hz;
# flag to send new spot
my $send;
# fork and sockets
my $pid;
my $main_sock;
my $new_sock;
$| = 1;
$SIG{CHLD} = sub {wait ()};
# listen for telnet connects on port 7373
$main_sock = new IO::Socket::INET ( LocalPort => 7373,
Listen => 5,
Proto => 'tcp',
ReuseAddr => 1,
);
die "Socket could not be created. Reason: $!\n" unless ($main_sock);
while(1) {
# Loop waiting for new inbound telnet connections
while( $new_sock = $main_sock->accept() ) {
print "New connection - ";
print $new_sock->peerhost() . "\n";
$pid = fork();
die "Cannot fork: $!" unless defined( $pid);
if ($pid == 0) {
# This is the child process
print $new_sock "DX de K1RA-# FT8 Skimmer >\n\r";
# setup tail to watch FT8 decoder log file and pipe for reading
# 193245 1 0 1 0 0.0 0.0 29.0 -2 0.31 14076009 K1HTV K1RA FM18
open( LOG, "< /dev/shm/decode-ft8.log");
# jump to end of file
seek LOG, 0, 2;
# Client loop forever
while(1) {
# read in lines from FT8 decoder log file
READ:
while( $line = <LOG>) {
# check to see if this line says Decoding (end of minute for FT8 decoder)
if( $line =~ /^Decoding/) {
# yes - check if its time to expire calls not seen in $MINTIME window
if( $min++ > $MINTIME) {
# yes - loop thru cache on call+baseband keys
foreach $cb ( keys %db) {
# extract last time call was seen
( $ltime) = split( "," , $db{ $cb});
# check if last time seen > $MINTIME
if( time() > $ltime + ( $MINTIME * 60) ) {
# yes - purge record
delete $db{ $cb};
}
}
# reset 60 minute timer
$min = 0;
}
} # end of a FT8 log decoder minute capture
# check if this is a valid FT8 decode line beginning with 6 digit time stamp
if( ! ( $line =~ /^\d{6}\s/) ) {
# no - go to read next line from decoder log
next READ;
}
# looks like a valid line split into variable fields
# print $line;
($gmt, $x, $x, $x, $x, $x, $x, $x, $snr, $dt, $freq, @rest)= split( " ", $line);
# extract HHMM
$gmt =~ /^(\d\d\d\d)\d\d/;
$gmt = $1;
# get UNIX time since epoch
$time = time();
# determine base frequency for this FT8 band decode
$base = int( $freq / 10000);
# make freq an integer
$freq += 0;
# make the FT8 message by appending remainder of line into one variable, space delimited
$ft8msg = join( " ", @rest);
# Here are all the various FT8 message scenarios we will recognize, extract senders CALL & GRID
# CQ CALL LLnn
if( $ft8msg =~ /^CQ\s([\w\d\/]{3,})\s(\w\w\d\d)/) {
$call = $1;
$grid = $2;
$cqde = "CQ";
# CQ [NA,DX,xx] CALL LLnn
} elsif ( $ft8msg =~ /^CQ\s\w{2}\s([\w\d\/]{3,})\s(\w\w\d\d)/) {
$call = $1;
$grid = $2;
$cqde = "CQ";
# CALL1 CALL2 [R][-+]nn
} elsif ( $ft8msg =~ /^[\w\d\/]{3,}\s([\w\d\/]{3,})\sR*[\-+][0-9]{2}/) {
$call = $1;
$grid = "";
$cqde = "DE";
# CALL1 CALL2 RRR
} elsif ( $ft8msg =~ /^[\w\d\/]{3,}\s([\w\d\/]{3,})\sRRR/) {
$call = $1;
$grid = "";
$cqde = "DE";
# CALL1 CALL2 RR73 or 73
} elsif ( $ft8msg =~ /^[\w\d\/]{3,}\s([\w\d\/]{3,})\sR*73/) {
$call = $1;
$grid = "";
$cqde = "DE";
# CALL1 CALL2 GRID
} elsif ( $ft8msg =~ /^[\w\d\/]{3,}\s([\w\d\/]{3,})\s(\w\w\d\d)/) {
$call = $1;
$grid = $2;
$cqde = "DE";
} else {
# we didn't match any message scenario so skip this line
next READ;
}
# does the call have at least one number in it
if( ! ( $call =~ /\d/) ) {
# no - maybe be TNX, NAME, QSL, so skip this line
next READ;
}
# check cache if we have NOT seen this call on this band yet
if( ! defined( $db{ $call.$base}) ) {
# yes - set flag to send it to client(s)
$send = 1;
# save to hash array using a key of call+baseband
$db{ $call.$base} = $time.",".$call.",".$grid.",".$freq.",".$snr;
} else {
# no - we have seen call before, so get last time call was sent to client
( $ltime) = split( ",", $db{ $call.$base});
# test if current time is > first time seen + MINTIME since we last sent to client
if( time() >= $ltime + ( $MINTIME* 60) ) {
# yes - set flag to send it to client(s)
$send = 1;
# resave to hash array with new time
$db{ $call.$base} = $time.",".$call.",".$grid.",".$freq.",".$snr;
} else {
# no - don't resend or touch time
$send = 0;
}
} # end cache check
# make sure call has at least one number in it
if ( $call =~ /\d/ && $send ) {
$hz = $freq - $basefrq{ $base};
if( !defined( $base) ) { print "$call $base\n"; }
# send client a spot
# DX de K1RA-#: 14074.8 5Q0X FT8 -3 dB CQ 1234 Hz JO54 1737z
printf $new_sock "%-15s %8.1f %-12s FT8 %3s dB %s %4s Hz %4s %6sZ\n\r",$prompt,$basefrq{ $base}/1000,$call,$snr,$cqde,$hz,$grid,$gmt;
}
} # end of reading LOG
sleep 1;
# reset EOF flag
seek LOG, 0, 1;
die "Socket is closed" unless $new_sock->connected;
} # loop client forever
} # else its the parent process, which goes back to accept()
} # main wait for socket loop forever
}