2019-07-11 17:11:23 -04:00
|
|
|
const WebSocket = require("ws");
|
|
|
|
const net = require("net");
|
|
|
|
const fs = require("fs");
|
2018-07-10 01:15:26 -04:00
|
|
|
|
|
|
|
const client = new net.Socket();
|
2019-07-11 17:11:23 -04:00
|
|
|
const wss = new WebSocket.Server({ host: "127.0.0.1", port: 4321 });
|
2018-07-10 01:15:26 -04:00
|
|
|
|
|
|
|
wss.broadcast = function(data) {
|
|
|
|
wss.clients.forEach(client => {
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-07-11 17:11:23 -04:00
|
|
|
client.connect(14580, "rotate.aprs2.net", () =>
|
|
|
|
client.write("user KC1GDW pass -1 filter r/43.90/-72.15/75\r\n")
|
|
|
|
);
|
2018-07-10 01:15:26 -04:00
|
|
|
|
2019-07-11 17:55:15 -04:00
|
|
|
function datestamp(date) {
|
|
|
|
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
|
|
|
|
}
|
|
|
|
|
2019-07-11 17:11:23 -04:00
|
|
|
client.on("data", function(data) {
|
|
|
|
let str = data.toString("utf8").replace(/^\s+|\s+$/g, "");
|
2018-07-10 01:15:26 -04:00
|
|
|
console.log(str);
|
|
|
|
|
|
|
|
// strip whitespace, then handle multiple APRS packets per TCP packet
|
|
|
|
str.split("\r\n").forEach(packet => {
|
2019-07-11 17:11:23 -04:00
|
|
|
if (!packet.startsWith("#")) {
|
|
|
|
// ignore comments
|
2018-07-10 01:15:26 -04:00
|
|
|
let date = new Date();
|
2019-07-11 17:11:23 -04:00
|
|
|
fs.appendFile(
|
2019-07-11 18:08:43 -04:00
|
|
|
`log/log${datestamp(date)}.json`,
|
2019-07-11 17:55:15 -04:00
|
|
|
JSON.stringify([date, packet]) + "\n",
|
2019-07-11 17:11:23 -04:00
|
|
|
err => {
|
|
|
|
if (err) throw err;
|
|
|
|
}
|
|
|
|
);
|
2019-07-11 17:55:15 -04:00
|
|
|
wss.broadcast(JSON.stringify([date, packet]));
|
2018-07-10 01:15:26 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-07-18 12:27:41 -04:00
|
|
|
|
2019-07-11 17:55:15 -04:00
|
|
|
wss.on("connection", ws => {
|
|
|
|
let date = new Date();
|
2019-07-11 18:08:43 -04:00
|
|
|
fs.readFileSync(`log/log${datestamp(date)}.json`)
|
2019-07-11 17:55:15 -04:00
|
|
|
.toString()
|
|
|
|
.split("\n")
|
|
|
|
.forEach(line => ws.send(line));
|
|
|
|
});
|