FreeDATA/gui/sock.js

858 lines
24 KiB
JavaScript
Raw Normal View History

2023-02-11 21:49:07 +00:00
var net = require("net");
const path = require("path");
const { ipcRenderer } = require("electron");
const FD = require("./freedata");
2023-02-11 21:49:07 +00:00
const log = require("electron-log");
const socketLog = log.scope("tnc");
//const utf8 = require("utf8");
2023-02-11 21:49:07 +00:00
2021-09-04 18:23:58 +00:00
// https://stackoverflow.com/a/26227660
2023-02-11 21:49:07 +00:00
var appDataFolder =
process.env.APPDATA ||
(process.platform == "darwin"
? process.env.HOME + "/Library/Application Support"
: process.env.HOME + "/.config");
2021-09-13 16:27:50 +00:00
var configFolder = path.join(appDataFolder, "FreeDATA");
2023-02-11 21:49:07 +00:00
var configPath = path.join(configFolder, "config.json");
2021-09-04 18:23:58 +00:00
const config = require(configPath);
2021-07-17 07:03:40 +00:00
var client = new net.Socket();
2023-02-11 21:49:07 +00:00
var socketchunk = ""; // Current message, per connection.
2021-07-17 07:03:40 +00:00
// split character
2023-02-11 21:49:07 +00:00
const split_char = "\0;\1;";
// globals for getting new data only if available so we are saving bandwidth
2023-02-11 21:49:07 +00:00
var rxBufferLengthTnc = 0;
var rxBufferLengthGui = 0;
//var rxMsgBufferLengthTnc = 0;
//var rxMsgBufferLengthGui = 0;
2021-08-23 14:24:49 +00:00
// global to keep track of TNC connection error emissions
2023-02-11 21:49:07 +00:00
var tncShowConnectStateError = 1;
2022-11-25 11:04:15 +00:00
// global for storing ip information
var tnc_port = config.tnc_port;
var tnc_host = config.tnc_host;
2021-08-23 14:24:49 +00:00
// network connection Timeout
2023-02-11 21:49:07 +00:00
setTimeout(connectTNC, 2000);
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
function connectTNC() {
2023-02-11 21:49:07 +00:00
//exports.connectTNC = function(){
//socketLog.info('connecting to TNC...')
2021-07-24 07:06:22 +00:00
2023-02-11 21:49:07 +00:00
//clear message buffer after reconnecting or initial connection
socketchunk = "";
2021-09-04 18:23:58 +00:00
2023-02-11 21:49:07 +00:00
if (config.tnclocation == "localhost") {
client.connect(3000, "127.0.0.1");
} else {
client.connect(tnc_port, tnc_host);
}
2021-07-17 07:03:40 +00:00
}
2023-02-11 21:49:07 +00:00
client.on("connect", function (data) {
socketLog.info("TNC connection established");
let Data = {
busy_state: "-",
arq_state: "-",
//channel_state: "-",
frequency: "-",
mode: "-",
bandwidth: "-",
dbfs_level: 0,
};
ipcRenderer.send("request-update-tnc-state", Data);
// also update tnc connection state
ipcRenderer.send("request-update-tnc-connection", {
tnc_connection: client.readyState,
});
tncShowConnectStateError = 1;
});
client.on("error", function (data) {
if (tncShowConnectStateError == 1) {
socketLog.error("TNC connection error");
tncShowConnectStateError = 0;
}
setTimeout(connectTNC, 500);
client.destroy();
let Data = {
tnc_connection: client.readyState,
busy_state: "-",
arq_state: "-",
//channel_state: "-",
frequency: "-",
mode: "-",
bandwidth: "-",
dbfs_level: 0,
};
ipcRenderer.send("request-update-tnc-state", Data);
ipcRenderer.send("request-update-tnc-connection", {
tnc_connection: client.readyState,
});
2021-07-17 07:03:40 +00:00
});
/*
client.on('close', function(data) {
socketLog.info(' TNC connection closed');
2021-09-13 16:27:50 +00:00
setTimeout(connectTNC, 2000)
2021-07-17 07:03:40 +00:00
});
*/
2023-02-11 21:49:07 +00:00
client.on("end", function (data) {
socketLog.info("TNC connection ended");
ipcRenderer.send("request-update-tnc-connection", {
tnc_connection: client.readyState,
});
client.destroy();
2023-02-11 21:49:07 +00:00
setTimeout(connectTNC, 500);
2021-07-17 07:03:40 +00:00
});
2023-02-11 21:49:07 +00:00
writeTncCommand = function (command) {
//socketLog.info(command)
// we use the writingCommand function to update our TCPIP state because we are calling this function a lot
// if socket opened, we are able to run commands
2023-02-11 21:49:07 +00:00
if (client.readyState == "open") {
client.write(command + "\n");
}
2022-02-12 15:34:47 +00:00
2023-02-11 21:49:07 +00:00
if (client.readyState == "closed") {
socketLog.info("CLOSED!");
}
2021-07-17 07:03:40 +00:00
2023-02-11 21:49:07 +00:00
if (client.readyState == "opening") {
socketLog.info("connecting to TNC...");
}
};
2021-07-17 07:03:40 +00:00
2023-02-11 21:49:07 +00:00
client.on("data", function (socketdata) {
ipcRenderer.send("request-update-tnc-connection", {
tnc_connection: client.readyState,
});
2021-07-17 07:03:40 +00:00
2023-02-11 21:49:07 +00:00
/*
inspired by:
2021-07-24 07:06:22 +00:00
stackoverflow.com questions 9070700 nodejs-net-createserver-large-amount-of-data-coming-in
*/
2023-02-11 21:49:07 +00:00
socketdata = socketdata.toString("utf8"); // convert data to string
socketchunk += socketdata; // append data to buffer so we can stick long data together
2023-02-11 21:49:07 +00:00
// check if we received begin and end of json data
if (socketchunk.startsWith('{"') && socketchunk.endsWith('"}\n')) {
var data = "";
2023-02-11 21:49:07 +00:00
// split data into chunks if we received multiple commands
socketchunk = socketchunk.split("\n");
//don't think this is needed anymore
//data = JSON.parse(socketchunk[0])
2023-02-11 21:49:07 +00:00
// search for empty entries in socketchunk and remove them
for (i = 0; i < socketchunk.length; i++) {
if (socketchunk[i] === "") {
socketchunk.splice(i, 1);
}
}
2023-02-11 21:49:07 +00:00
//iterate through socketchunks array to execute multiple commands in row
for (i = 0; i < socketchunk.length; i++) {
//check if data is not empty
if (socketchunk[i].length > 0) {
//try to parse JSON
try {
data = JSON.parse(socketchunk[i]);
} catch (e) {
socketLog.info("Throwing away data!!!!\n" + e); // "SyntaxError
//socketLog.info(e); // "SyntaxError
2023-02-11 21:49:07 +00:00
socketLog.info(socketchunk[i]);
socketchunk = "";
//If we're here, I don't think we want to process any data that may be in data variable
continue;
}
2023-02-11 21:49:07 +00:00
}
if (data["command"] == "tnc_state") {
//socketLog.info(data)
// set length of RX Buffer to global variable
rxBufferLengthTnc = data["rx_buffer_length"];
//rxMsgBufferLengthTnc = data["rx_msg_buffer_length"];
2023-02-11 21:49:07 +00:00
let Data = {
mycallsign: data["mycallsign"],
mygrid: data["mygrid"],
ptt_state: data["ptt_state"],
busy_state: data["tnc_state"],
arq_state: data["arq_state"],
arq_session: data["arq_session"],
//channel_state: data['CHANNEL_STATE'],
frequency: data["frequency"],
speed_level: data["speed_level"],
mode: data["mode"],
bandwidth: data["bandwidth"],
dbfs_level: data["audio_dbfs"],
fft: data["fft"],
channel_busy: data["channel_busy"],
scatter: data["scatter"],
info: data["info"],
rx_buffer_length: data["rx_buffer_length"],
rx_msg_buffer_length: data["rx_msg_buffer_length"],
tx_n_max_retries: data["tx_n_max_retries"],
arq_tx_n_frames_per_burst: data["arq_tx_n_frames_per_burst"],
arq_tx_n_bursts: data["arq_tx_n_bursts"],
arq_tx_n_current_arq_frame: data["arq_tx_n_current_arq_frame"],
arq_tx_n_total_arq_frames: data["arq_tx_n_total_arq_frames"],
arq_rx_frame_n_bursts: data["arq_rx_frame_n_bursts"],
arq_rx_n_current_arq_frame: data["arq_rx_n_current_arq_frame"],
arq_n_arq_frames_per_data_frame:
data["arq_n_arq_frames_per_data_frame"],
arq_bytes_per_minute: data["arq_bytes_per_minute"],
arq_seconds_until_finish: data["arq_seconds_until_finish"],
arq_compression_factor: data["arq_compression_factor"],
total_bytes: data["total_bytes"],
arq_transmission_percent: data["arq_transmission_percent"],
stations: data["stations"],
beacon_state: data["beacon_state"],
hamlib_status: data["hamlib_status"],
listen: data["listen"],
audio_recording: data["audio_recording"],
speed_list: data["speed_list"],
2023-02-12 16:14:20 +00:00
strength: data["strength"],
2023-02-15 22:19:07 +00:00
is_codec2_traffic: data["is_codec2_traffic"],
2023-02-11 21:49:07 +00:00
//speed_table: [{"bpm" : 5200, "snr": -3, "timestamp":1673555399},{"bpm" : 2315, "snr": 12, "timestamp":1673555500}],
};
ipcRenderer.send("request-update-tnc-state", Data);
//continue to next for loop iteration, nothing else needs to be done here
continue;
}
// ----------- catch tnc messages START -----------
if (data["freedata"] == "tnc-message") {
switch (data["fec"]) {
case "is_writing":
2023-02-13 00:45:43 +00:00
// RX'd FECiswriting
ipcRenderer.send("request-show-fec-toast-iswriting", {
2023-02-13 00:13:55 +00:00
data: [data],
});
break;
}
switch (data["cq"]) {
case "transmitting":
// CQ TRANSMITTING
ipcRenderer.send("request-show-cq-toast-transmitting", {
data: [data],
});
break;
case "received":
// CQ RECEIVED
ipcRenderer.send("request-show-cq-toast-received", {
data: [data],
});
break;
}
switch (data["qrv"]) {
case "transmitting":
// QRV TRANSMITTING
ipcRenderer.send("request-show-qrv-toast-transmitting", {
data: [data],
});
break;
case "received":
// QRV RECEIVED
ipcRenderer.send("request-show-qrv-toast-received", {
data: [data],
});
break;
}
switch (data["beacon"]) {
case "transmitting":
// BEACON TRANSMITTING
ipcRenderer.send("request-show-beacon-toast-transmitting", {
data: [data],
});
break;
case "received":
// BEACON RECEIVED
ipcRenderer.send("request-show-beacon-toast-received", {
data: [data],
});
ipcRenderer.send("request-new-msg-received", { data: [data] });
break;
}
switch (data["ping"]) {
case "transmitting":
// PING TRANSMITTING
ipcRenderer.send("request-show-ping-toast-transmitting", {
data: [data],
});
break;
case "received":
// PING RECEIVED
ipcRenderer.send("request-show-ping-toast-received", {
data: [data],
});
2023-02-13 00:13:55 +00:00
ipcRenderer.send("request-new-msg-received", { data: [data] });
break;
case "acknowledge":
2023-02-13 00:13:55 +00:00
// PING ACKNOWLEDGE
ipcRenderer.send("request-show-ping-toast-received-ack", {
data: [data],
});
ipcRenderer.send("request-new-msg-received", { data: [data] });
break;
}
2022-06-15 08:51:19 +00:00
// ARQ SESSION && freedata == tnc-message
2023-02-11 21:49:07 +00:00
if (data["arq"] == "session") {
2023-02-13 00:13:55 +00:00
switch (data["status"]) {
case "connecting":
// ARQ Open
ipcRenderer.send("request-show-arq-toast-session-connecting", {
data: [data],
});
break;
2023-02-13 00:13:55 +00:00
case "connected":
// ARQ Opening
ipcRenderer.send("request-show-arq-toast-session-connected", {
data: [data],
});
break;
2023-02-13 00:13:55 +00:00
case "waiting":
// ARQ Opening
ipcRenderer.send("request-show-arq-toast-session-waiting", {
data: [data],
});
break;
2023-02-13 00:13:55 +00:00
case "close":
// ARQ Closing
ipcRenderer.send("request-show-arq-toast-session-close", {
data: [data],
});
break;
case "failed":
// ARQ Failed
ipcRenderer.send("request-show-arq-toast-session-failed", {
data: [data],
});
break;
2023-02-11 21:49:07 +00:00
}
}
// ARQ TRANSMISSION && freedata == tnc-message
2023-02-11 21:49:07 +00:00
if (data["arq"] == "transmission") {
switch (data["status"]) {
case "opened":
// ARQ Open
ipcRenderer.send("request-show-arq-toast-datachannel-opened", {
2023-02-11 21:49:07 +00:00
data: [data],
});
break;
2023-02-13 00:13:55 +00:00
case "opening":
// ARQ Opening IRS/ISS
if (data["irs"] == "False") {
ipcRenderer.send("request-show-arq-toast-datachannel-opening", {
data: [data],
2023-02-13 00:13:55 +00:00
});
2023-02-19 18:50:45 +00:00
ipcRenderer.send("request-update-transmission-status", {
data: [data],
});
2023-02-13 00:13:55 +00:00
} else {
ipcRenderer.send(
"request-show-arq-toast-datachannel-received-opener",
{ data: [data] }
2023-02-13 00:13:55 +00:00
);
2023-02-19 18:50:45 +00:00
ipcRenderer.send("request-update-reception-status", {
data: [data],
});
2023-02-13 00:13:55 +00:00
}
2023-02-19 18:50:45 +00:00
break;
2023-02-13 00:13:55 +00:00
case "waiting":
// ARQ waiting
ipcRenderer.send("request-show-arq-toast-datachannel-waiting", {
data: [data],
});
break;
2023-02-13 00:13:55 +00:00
case "receiving":
// ARQ RX
ipcRenderer.send("request-update-reception-status", {
data: [data],
});
break;
2023-02-13 00:13:55 +00:00
case "failed":
// ARQ TX Failed
if (data["reason"] == "protocol version missmatch") {
ipcRenderer.send(
"request-show-arq-toast-transmission-failed-ver",
{ data: [data] }
);
} else {
ipcRenderer.send("request-show-arq-toast-transmission-failed", {
data: [data],
});
}
2023-02-19 18:50:45 +00:00
switch (data["irs"]) {
case "True":
ipcRenderer.send("request-update-reception-status", {
data: [data],
});
break;
default:
ipcRenderer.send("request-update-transmission-status", {
2023-02-20 17:55:21 +00:00
data: [data],
});
2023-02-19 18:50:45 +00:00
break;
2023-02-20 17:55:21 +00:00
}
break;
2023-02-13 00:13:55 +00:00
case "received":
// ARQ Received
ipcRenderer.send("request-show-arq-toast-transmission-received", {
data: [data],
});
2023-02-19 18:50:45 +00:00
ipcRenderer.send("request-update-reception-status", {
2023-02-13 00:13:55 +00:00
data: [data],
});
dataArray = [];
messageArray = [];
socketLog.info(data);
// we need to encode here to do a deep check for checking if file or message
//var encoded_data = atob(data['data'])
var encoded_data = FD.atob_FD(data["data"]);
2023-02-13 00:13:55 +00:00
var splitted_data = encoded_data.split(split_char);
if (splitted_data[0] == "f") {
dataArray.push(data);
}
if (splitted_data[0] == "m") {
messageArray.push(data);
console.log(data);
}
rxBufferLengthGui = dataArray.length;
let Files = {
data: dataArray,
};
ipcRenderer.send("request-update-rx-buffer", Files);
ipcRenderer.send("request-new-msg-received", Files);
//rxMsgBufferLengthGui = messageArray.length;
let Messages = {
data: messageArray,
};
ipcRenderer.send("request-new-msg-received", Messages);
break;
case "transmitting":
// ARQ transmitting
ipcRenderer.send(
"request-show-arq-toast-transmission-transmitting",
{ data: [data] }
);
ipcRenderer.send("request-update-transmission-status", {
data: [data],
});
break;
case "transmitted":
// ARQ transmitted
ipcRenderer.send(
"request-show-arq-toast-transmission-transmitted",
{ data: [data] }
);
ipcRenderer.send("request-update-transmission-status", {
data: [data],
});
break;
2023-02-11 21:49:07 +00:00
}
}
}
// ----------- catch tnc info messages END -----------
// if we manually checking for the rx buffer we are getting an array of multiple data
if (data["command"] == "rx_buffer") {
socketLog.info(data);
// iterate through buffer list and sort it to file or message array
dataArray = [];
messageArray = [];
for (i = 0; i < data["data-array"].length; i++) {
try {
// we need to encode here to do a deep check for checking if file or message
//var encoded_data = atob(data['data-array'][i]['data'])
var encoded_data = FD.atob_FD(data["data-array"][i]["data"]);
2023-02-11 21:49:07 +00:00
var splitted_data = encoded_data.split(split_char);
if (splitted_data[0] == "f") {
dataArray.push(data["data-array"][i]);
}
2023-02-11 21:49:07 +00:00
if (splitted_data[0] == "m") {
messageArray.push(data["data-array"][i]);
}
} catch (e) {
socketLog.info(e);
}
}
2023-02-11 21:49:07 +00:00
rxBufferLengthGui = dataArray.length;
let Files = {
data: dataArray,
};
ipcRenderer.send("request-update-rx-buffer", Files);
//rxMsgBufferLengthGui = messageArray.length;
2023-02-11 21:49:07 +00:00
let Messages = {
data: messageArray,
};
//ipcRenderer.send('request-update-rx-msg-buffer', Messages);
ipcRenderer.send("request-new-msg-received", Messages);
}
2021-07-17 07:03:40 +00:00
}
2023-02-11 21:49:07 +00:00
//finally delete message buffer
socketchunk = "";
}
2021-07-17 07:03:40 +00:00
});
function hexToBytes(hex) {
2023-02-11 21:49:07 +00:00
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
2021-07-17 07:03:40 +00:00
}
//Get TNC State
2023-02-11 21:49:07 +00:00
exports.getTncState = function () {
command = '{"type" : "get", "command" : "tnc_state"}';
writeTncCommand(command);
};
2021-07-23 15:40:44 +00:00
//Get DATA State
2023-02-11 21:49:07 +00:00
exports.getDataState = function () {
command = '{"type" : "get", "command" : "data_state"}';
//writeTncCommand(command)
};
2021-07-23 15:40:44 +00:00
// Send Ping
2023-02-11 21:49:07 +00:00
exports.sendPing = function (dxcallsign) {
command =
'{"type" : "ping", "command" : "ping", "dxcallsign" : "' +
dxcallsign +
'"}';
writeTncCommand(command);
};
// Send CQ
2023-02-11 21:49:07 +00:00
exports.sendCQ = function () {
command = '{"type" : "broadcast", "command" : "cqcqcq"}';
writeTncCommand(command);
};
2021-07-26 16:09:04 +00:00
2022-03-31 19:13:30 +00:00
// Set AUDIO Level
2023-02-11 21:49:07 +00:00
exports.setTxAudioLevel = function (value) {
command =
'{"type" : "set", "command" : "tx_audio_level", "value" : "' + value + '"}';
writeTncCommand(command);
};
2022-03-31 19:13:30 +00:00
2021-07-26 16:09:04 +00:00
// Send File
2023-02-11 21:49:07 +00:00
exports.sendFile = function (
dxcallsign,
mode,
frames,
filename,
filetype,
data,
checksum
) {
socketLog.info(data);
socketLog.info(filetype);
socketLog.info(filename);
var datatype = "f";
data =
datatype +
split_char +
filename +
split_char +
filetype +
split_char +
checksum +
split_char +
data;
socketLog.info(data);
//socketLog.info(btoa(data))
//Btoa / atob will not work with charsets > 8 bits (i.e. the emojis); should probably move away from using it
//TODO: Will need to update anyother occurences and throughly test
//data = btoa(data)
data = FD.btoa_FD(data);
2023-02-11 21:49:07 +00:00
command =
'{"type" : "arq", "command" : "send_raw", "parameter" : [{"dxcallsign" : "' +
dxcallsign +
'", "mode" : "' +
mode +
'", "n_frames" : "' +
frames +
'", "data" : "' +
data +
'"}]}';
writeTncCommand(command);
};
2021-07-26 16:09:04 +00:00
// Send Message
2023-02-11 21:49:07 +00:00
exports.sendMessage = function (
dxcallsign,
mode,
frames,
data,
checksum,
uuid,
command
) {
data = FD.btoa_FD(
2023-02-11 21:49:07 +00:00
"m" +
split_char +
command +
split_char +
checksum +
split_char +
uuid +
split_char +
data
);
command =
'{"type" : "arq", "command" : "send_raw", "uuid" : "' +
uuid +
'", "parameter" : [{"dxcallsign" : "' +
dxcallsign +
'", "mode" : "' +
mode +
'", "n_frames" : "' +
frames +
'", "data" : "' +
data +
'", "attempts": "15"}]}';
socketLog.info(command);
socketLog.info("-------------------------------------");
writeTncCommand(command);
};
2023-02-26 06:02:53 +00:00
// Send Request message
//It would be then „m + split + request + split + request-type“
2023-02-26 06:03:32 +00:00
function sendRequest(dxcallsign, mode, frames, data, command) {
data = FD.btoa_FD("m" + split_char + command + split_char + data);
2023-02-26 06:02:53 +00:00
command =
'{"type" : "arq", "command" : "send_raw", "parameter" : [{"dxcallsign" : "' +
dxcallsign +
'", "mode" : "' +
mode +
'", "n_frames" : "' +
frames +
'", "data" : "' +
data +
'", "attempts": "15"}]}';
socketLog.info(command);
socketLog.info("--------------REQ--------------------");
writeTncCommand(command);
2023-02-26 06:03:32 +00:00
}
2023-02-26 06:02:53 +00:00
// Send Response message
//It would be then „m + split + request + split + request-type“
function sendResponse(dxcallsign, mode, frames, data, command) {
data = FD.btoa_FD("m" + split_char + command + split_char + data);
command =
'{"type" : "arq", "command" : "send_raw", "parameter" : [{"dxcallsign" : "' +
dxcallsign +
'", "mode" : "' +
mode +
'", "n_frames" : "' +
frames +
'", "data" : "' +
data +
'", "attempts": "15"}]}';
socketLog.info(command);
socketLog.info("--------------RES--------------------");
writeTncCommand(command);
}
2023-02-26 06:02:53 +00:00
//Send station info request
2023-02-26 13:08:26 +00:00
exports.sendRequestInfo = function (dxcallsign) {
2023-02-26 06:02:53 +00:00
//Command 0 = user/station information
//Command 1 = shared folder list
2023-02-26 13:08:26 +00:00
sendRequest(dxcallsign, 255, 1, "0", "req");
2023-02-26 06:02:53 +00:00
};
//Send shared folder file list request
exports.sendRequestSharedFolderList = function (dxcallsign) {
//Command 0 = user/station information
//Command 1 = shared folder list
sendRequest(dxcallsign, 255, 1, "1", "req");
};
//Send station info response
exports.sendResponseInfo = function (dxcallsign, userinfo) {
//Command 0 = user/station information
//Command 1 = shared folder list
sendResponse(dxcallsign, 255, 1, userinfo, "res-0");
};
//Send station info response
exports.sendResponseSharedFolderList = function (dxcallsign, sharedFolderList) {
//Command 0 = user/station information
//Command 1 = shared folder list
sendResponse(dxcallsign, 255, 1, sharedFolderList, "res-1");
};
//STOP TRANSMISSION
2023-02-11 21:49:07 +00:00
exports.stopTransmission = function () {
command = '{"type" : "arq", "command": "stop_transmission"}';
writeTncCommand(command);
};
2021-08-15 15:30:50 +00:00
// Get RX BUffer
2023-02-11 21:49:07 +00:00
exports.getRxBuffer = function () {
command = '{"type" : "get", "command" : "rx_buffer"}';
2023-02-11 21:49:07 +00:00
// call command only if new data arrived
if (rxBufferLengthGui != rxBufferLengthTnc) {
writeTncCommand(command);
}
};
// START BEACON
2023-02-11 21:49:07 +00:00
exports.startBeacon = function (interval) {
command =
'{"type" : "broadcast", "command" : "start_beacon", "parameter": "' +
interval +
'"}';
writeTncCommand(command);
};
// STOP BEACON
2023-02-11 21:49:07 +00:00
exports.stopBeacon = function () {
command = '{"type" : "broadcast", "command" : "stop_beacon"}';
writeTncCommand(command);
};
// OPEN ARQ SESSION
2023-02-11 21:49:07 +00:00
exports.connectARQ = function (dxcallsign) {
command =
'{"type" : "arq", "command" : "connect", "dxcallsign": "' +
dxcallsign +
'", "attempts": "15"}';
writeTncCommand(command);
};
// CLOSE ARQ SESSION
2023-02-11 21:49:07 +00:00
exports.disconnectARQ = function () {
command = '{"type" : "arq", "command" : "disconnect"}';
writeTncCommand(command);
};
2022-03-31 19:13:30 +00:00
// SEND TEST FRAME
2023-02-11 21:49:07 +00:00
exports.sendTestFrame = function () {
command = '{"type" : "set", "command" : "send_test_frame"}';
writeTncCommand(command);
};
2022-11-25 11:04:15 +00:00
2023-02-12 16:45:03 +00:00
// SEND FEC
exports.sendFEC = function (mode, payload) {
2023-02-12 16:46:54 +00:00
command =
2023-02-12 17:00:33 +00:00
'{"type" : "fec", "command" : "transmit", "mode" : "' +
2023-02-12 16:46:54 +00:00
mode +
'", "payload" : "' +
payload +
'"}';
2023-02-12 16:45:03 +00:00
writeTncCommand(command);
};
2023-02-12 17:28:10 +00:00
// SEND FEC IS WRITING
exports.sendFecIsWriting = function (mycallsign) {
command =
'{"type" : "fec", "command" : "transmit_is_writing", "mycallsign" : "' +
mycallsign +
'"}';
writeTncCommand(command);
};
2022-12-26 11:27:13 +00:00
// RECORD AUDIO
2023-02-11 21:49:07 +00:00
exports.record_audio = function () {
command = '{"type" : "set", "command" : "record_audio"}';
writeTncCommand(command);
};
2022-11-25 11:04:15 +00:00
// SET FREQUENCY
2023-02-11 21:49:07 +00:00
exports.set_frequency = function (frequency) {
command =
'{"type" : "set", "command" : "frequency", "frequency": ' + frequency + "}";
writeTncCommand(command);
};
2023-01-04 19:12:03 +00:00
// SET MODE
2023-02-11 21:49:07 +00:00
exports.set_mode = function (mode) {
command = '{"type" : "set", "command" : "mode", "mode": "' + mode + '"}';
console.log(command);
writeTncCommand(command);
};
ipcRenderer.on("action-update-tnc-ip", (event, arg) => {
client.destroy();
let Data = {
busy_state: "-",
arq_state: "-",
//channel_state: "-",
frequency: "-",
mode: "-",
bandwidth: "-",
dbfs_level: 0,
};
ipcRenderer.send("request-update-tnc-state", Data);
tnc_port = arg.port;
tnc_host = arg.adress;
connectTNC();
2022-12-27 21:57:54 +00:00
});
// https://stackoverflow.com/a/50579690
// crc32 calculation
//console.log(crc32('abc'));
//console.log(crc32('abc').toString(16).toUpperCase()); // hex
2023-02-11 21:49:07 +00:00
var crc32 = function (r) {
for (var a, o = [], c = 0; c < 256; c++) {
a = c;
for (var f = 0; f < 8; f++) a = 1 & a ? 3988292384 ^ (a >>> 1) : a >>> 1;
o[c] = a;
}
for (var n = -1, t = 0; t < r.length; t++)
n = (n >>> 8) ^ o[255 & (n ^ r.charCodeAt(t))];
return (-1 ^ n) >>> 0;
};