-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrbn.js
158 lines (132 loc) · 3.99 KB
/
rbn.js
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
const reconnect = require('reconnect-net');
const wsManager = require('./ws-manager');
const carrier = require('carrier');
const config = require('./config');
const db = require('./db');
const utils = require('./utils');
const rbnSpotRegex = /^DX de (\S+):\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+dB\s+(\S+)\s+\S+\s+(CQ|DX)\s+(\d+)Z$/;
class RbnReceiver {
start() {
this.restartConnection();
wsManager.on('message', (ws, message) => {
if (message.rbnFilter !== undefined) {
//console.log("Set RBN filter to " + JSON.stringify(message.rbnFilter));
ws.rbnFilter = message.rbnFilter;
this.sendSpotHistory(ws)
}
});
}
restartConnection() {
if (this.re)
this.re.disconnect();
this.resetTimer();
this.re = reconnect((stream) => {
console.log("Connected to RBN");
stream.write(config.rbn.login + "\r\n");
if (config.rbn.server.commands) {
config.rbn.server.commands.forEach(command => {
stream.write(command + "\r\n");
});
}
carrier.carry(stream, (line) => {
this.handleLine(line);
});
});
this.re.on('error', (err) => {
console.error(`RBN connection error: ${err}`);
});
this.re.connect(config.rbn.server);
}
resetTimer() {
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(() => {
console.error("RBN: timeout, reconnecting");
this.restartConnection();
}, config.rbn.timeout);
}
handleLine(line) {
this.resetTimer();
let matches = rbnSpotRegex.exec(line);
if (matches) {
let spot = {
timeStamp: new Date(),
callsign: matches[3],
homeCallsign: this.homeCallsign(matches[3]),
spotter: matches[1].replace("-#", ""),
frequency: parseFloat((matches[2]/1000).toFixed(4)),
mode: matches[4],
snr: parseInt(matches[5]),
speed: parseInt(matches[6])
};
// Check if this is a known SOTA activator
db.getDb().collection('activators').countDocuments({callsign: { $in: utils.makeCallsignVariations(spot.homeCallsign) }}, (error, result) => {
if (result > 0) {
spot.isActivator = true;
}
db.getDb().collection('rbnspots').insertOne(spot, (error, result) => {
// _id has now been added, but not in our preferred format
spot._id = spot._id.toHexString()
wsManager.broadcast({'rbnSpot': spot}, (ws) => {
if (!ws.rbnFilter) {
return false;
}
if (ws.rbnFilter.homeCallsign && ws.rbnFilter.homeCallsign.includes(spot.homeCallsign)) {
return true;
}
if (ws.rbnFilter.isActivator && spot.isActivator) {
return true;
}
return false;
});
});
});
}
}
sendSpotHistory(ws) {
// Send the spot history for the currently defined RBN filter
if (!ws.rbnFilter.homeCallsign && !ws.rbnFilter.isActivator) {
return;
}
let query = {};
if (ws.rbnFilter.homeCallsign) {
query.homeCallsign = ws.rbnFilter.homeCallsign;
}
if (ws.rbnFilter.isActivator) {
query.isActivator = true;
}
let maxAge = parseInt(ws.rbnFilter.maxAge) || 3600000;
query.timeStamp = {$gte: new Date(new Date().getTime() - maxAge)};
db.getDb().collection('rbnspots').find(query).sort({timeStamp: -1}).limit(config.rbn.maxSpotHistory).toArray((err, rbnSpots) => {
if (err) {
console.error(err);
return;
}
rbnSpots.forEach(spot => {
spot._id = spot._id.toHexString();
});
let response = {rbnSpotHistory: rbnSpots};
if (ws.rbnFilter.viewId) {
response.viewId = ws.rbnFilter.viewId;
}
wsManager.unicast(response, ws);
});
}
homeCallsign(callsign) {
let parts = callsign.split('/');
let longestPart = '';
parts.forEach(part => {
if (part.length > longestPart.length) {
longestPart = part;
}
})
// For UK callsigns, normalize them all to 2E/G/M for the sake of comparison
let matches = longestPart.match(/^(2[DEIJMUW]|G[DIJMUW]?|M[DIJMUW]?)(\d[A-Z]{2,3})$/)
if (matches) {
longestPart = matches[1].replace(/^2./, '2E').replace(/^G[DIJMUW]/, 'G').replace(/^M[DIJMUW]/, 'M') + matches[2]
}
return longestPart;
}
}
module.exports = RbnReceiver;