2021-07-17 07:03:40 +00:00
|
|
|
var net = require('net');
|
2021-09-04 18:23:58 +00:00
|
|
|
const path = require('path')
|
2021-07-24 07:06:22 +00:00
|
|
|
const {
|
|
|
|
ipcRenderer
|
2021-09-04 18:23:58 +00:00
|
|
|
} = require('electron')
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/26227660
|
2021-09-04 19:37:56 +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");
|
2021-09-04 18:23:58 +00:00
|
|
|
var configPath = path.join(configFolder, 'config.json')
|
|
|
|
const config = require(configPath);
|
2021-07-17 07:03:40 +00:00
|
|
|
|
|
|
|
var client = new net.Socket();
|
|
|
|
var msg = ''; // Current message, per connection.
|
|
|
|
|
2021-10-17 14:59:15 +00:00
|
|
|
// globals for getting new data only if available so we are saving bandwith
|
2021-08-23 14:24:49 +00:00
|
|
|
var rxBufferLengthTnc = 0
|
|
|
|
var rxBufferLengthGui = 0
|
2021-10-17 14:59:15 +00:00
|
|
|
var rxMsgBufferLengthTnc = 0
|
|
|
|
var rxMsgBufferLengthGui = 0
|
2021-08-23 14:24:49 +00:00
|
|
|
|
|
|
|
// network connection Timeout
|
2021-07-24 07:06:22 +00:00
|
|
|
setTimeout(connectTNC, 3000)
|
2021-07-17 07:03:40 +00:00
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
function connectTNC() {
|
|
|
|
//exports.connectTNC = function(){
|
2021-09-04 14:33:17 +00:00
|
|
|
//console.log('connecting to TNC...')
|
2021-07-24 07:06:22 +00:00
|
|
|
|
2021-07-17 07:03:40 +00:00
|
|
|
//clear message buffer after reconnecting or inital connection
|
|
|
|
msg = '';
|
2021-09-04 18:23:58 +00:00
|
|
|
|
|
|
|
if (config.tnclocation == 'localhost') {
|
|
|
|
client.connect(3000, '127.0.0.1')
|
2021-09-04 14:33:17 +00:00
|
|
|
} else {
|
|
|
|
client.connect(config.tnc_port, config.tnc_host)
|
|
|
|
}
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.on('connect', function(data) {
|
2021-07-24 07:06:22 +00:00
|
|
|
console.log('TNC connection established')
|
2021-07-17 07:03:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.on('error', function(data) {
|
2021-07-24 07:06:22 +00:00
|
|
|
console.log('TNC connection error');
|
|
|
|
|
|
|
|
let Data = {
|
|
|
|
busy_state: "-",
|
|
|
|
arq_state: "-",
|
2021-12-26 08:24:22 +00:00
|
|
|
//channel_state: "-",
|
2021-07-24 07:06:22 +00:00
|
|
|
frequency: "-",
|
|
|
|
mode: "-",
|
|
|
|
bandwith: "-",
|
|
|
|
rms_level: 0
|
2021-07-17 07:03:40 +00:00
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
};
|
|
|
|
ipcRenderer.send('request-update-tnc-state', Data);
|
2021-07-17 07:03:40 +00:00
|
|
|
|
|
|
|
setTimeout(connectTNC, 2000)
|
2021-07-24 07:06:22 +00:00
|
|
|
// setTimeout( function() { exports.connectTNC(tnc_host, tnc_port); }, 2000 );
|
2021-07-19 19:01:38 +00:00
|
|
|
|
2021-07-17 07:03:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
client.on('close', function(data) {
|
|
|
|
console.log(' TNC connection closed');
|
2021-09-13 16:27:50 +00:00
|
|
|
setTimeout(connectTNC, 2000)
|
2021-07-17 07:03:40 +00:00
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
client.on('end', function(data) {
|
2021-07-24 07:06:22 +00:00
|
|
|
console.log('TNC connection ended');
|
2021-09-13 16:27:50 +00:00
|
|
|
//setTimeout(connectTNC, 2000)
|
2021-07-24 07:06:22 +00:00
|
|
|
setTimeout(connectTNC, 0)
|
2021-07-19 19:01:38 +00:00
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
// setTimeout( function() { exports.connectTNC(tnc_host, tnc_port); }, 2000 );
|
2021-07-19 19:01:38 +00:00
|
|
|
|
2021-07-17 07:03:40 +00:00
|
|
|
});
|
|
|
|
|
2021-09-13 16:27:50 +00:00
|
|
|
//exports.writeTncCommand = function(command){
|
2021-07-24 07:06:22 +00:00
|
|
|
writeTncCommand = function(command) {
|
|
|
|
|
2021-09-04 14:33:17 +00:00
|
|
|
//console.log(command)
|
2021-07-17 07:03:40 +00:00
|
|
|
// we use the writingCommand function to update our TCPIP state because we are calling this function a lot
|
2021-07-24 07:06:22 +00:00
|
|
|
// if socket openend, we are able to run commands
|
|
|
|
if (client.readyState == 'open') {
|
|
|
|
//uiMain.setTNCconnection('open')
|
|
|
|
client.write(command + '\n');
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
if (client.readyState == 'closed') {
|
|
|
|
//uiMain.setTNCconnection('closed')
|
2021-09-04 14:33:17 +00:00
|
|
|
//console.log("CLOSED!!!!!")
|
2021-07-24 07:06:22 +00:00
|
|
|
}
|
2021-07-19 19:01:38 +00:00
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
if (client.readyState == 'opening') {
|
|
|
|
//uiMain.setTNCconnection('opening')
|
2021-09-04 14:33:17 +00:00
|
|
|
//console.log("OPENING!!!!!")
|
|
|
|
console.log('connecting to TNC...')
|
2021-07-24 07:06:22 +00:00
|
|
|
}
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.on('data', function(data) {
|
|
|
|
|
2021-09-13 16:27:50 +00:00
|
|
|
/*
|
2021-07-24 07:06:22 +00:00
|
|
|
stackoverflow.com questions 9070700 nodejs-net-createserver-large-amount-of-data-coming-in
|
|
|
|
*/
|
|
|
|
|
2021-07-17 07:03:40 +00:00
|
|
|
data = data.toString('utf8'); // convert data to string
|
|
|
|
msg += data.toString('utf8'); // append data to buffer so we can stick long data together
|
2021-08-07 18:57:36 +00:00
|
|
|
//console.log(data)
|
2021-07-24 07:06:22 +00:00
|
|
|
// check if we reached an EOF, if true, clear buffer and parse JSON data
|
2021-08-23 16:14:24 +00:00
|
|
|
if (data.endsWith('"EOF":"EOF"}')) {
|
2021-07-24 07:06:22 +00:00
|
|
|
//console.log(msg)
|
|
|
|
try {
|
|
|
|
//console.log(msg)
|
|
|
|
data = JSON.parse(msg)
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e); /* "SyntaxError*/
|
|
|
|
}
|
|
|
|
msg = '';
|
|
|
|
/* console.log("EOF detected!") */
|
|
|
|
|
2021-08-23 16:14:24 +00:00
|
|
|
//console.log(data)
|
2021-07-24 07:06:22 +00:00
|
|
|
|
|
|
|
if (data['COMMAND'] == 'TNC_STATE') {
|
2021-09-04 14:33:17 +00:00
|
|
|
//console.log(data)
|
2021-10-17 14:59:15 +00:00
|
|
|
// set length of RX Buffer to global variable
|
2021-08-23 14:24:49 +00:00
|
|
|
rxBufferLengthTnc = data['RX_BUFFER_LENGTH']
|
2021-10-17 14:59:15 +00:00
|
|
|
rxMsgBufferLengthTnc = data['RX_MSG_BUFFER_LENGTH']
|
2021-11-19 17:49:36 +00:00
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
let Data = {
|
2021-08-08 09:43:50 +00:00
|
|
|
toe: Date.now() - data['TIMESTAMP'], // time of execution
|
2021-07-24 07:06:22 +00:00
|
|
|
ptt_state: data['PTT_STATE'],
|
|
|
|
busy_state: data['TNC_STATE'],
|
|
|
|
arq_state: data['ARQ_STATE'],
|
2021-12-26 08:24:22 +00:00
|
|
|
//channel_state: data['CHANNEL_STATE'],
|
2021-07-24 07:06:22 +00:00
|
|
|
frequency: data['FREQUENCY'],
|
|
|
|
mode: data['MODE'],
|
|
|
|
bandwith: data['BANDWITH'],
|
2021-07-25 14:35:50 +00:00
|
|
|
rms_level: (data['AUDIO_RMS'] / 1000) * 100,
|
2021-09-05 09:26:09 +00:00
|
|
|
fft: data['FFT'],
|
2021-08-06 20:10:45 +00:00
|
|
|
scatter: data['SCATTER'],
|
2021-10-07 19:04:23 +00:00
|
|
|
info: data['INFO'],
|
2021-08-07 17:57:52 +00:00
|
|
|
rx_buffer_length: data['RX_BUFFER_LENGTH'],
|
2021-10-17 14:59:15 +00:00
|
|
|
rx_msg_buffer_length: data['RX_MSG_BUFFER_LENGTH'],
|
2021-08-07 17:57:52 +00:00
|
|
|
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'],
|
2021-08-15 15:30:50 +00:00
|
|
|
arq_bytes_per_minute: data['ARQ_BYTES_PER_MINUTE'],
|
|
|
|
total_bytes: data['TOTAL_BYTES'],
|
2021-09-04 14:33:17 +00:00
|
|
|
arq_transmission_percent: data['ARQ_TRANSMISSION_PERCENT'],
|
2021-08-07 17:57:52 +00:00
|
|
|
stations: data['STATIONS'],
|
2021-12-06 19:16:14 +00:00
|
|
|
beacon_state: data['BEACON_STATE'],
|
2021-07-24 07:06:22 +00:00
|
|
|
};
|
2021-09-04 14:33:17 +00:00
|
|
|
//console.log(Data)
|
2021-07-24 07:06:22 +00:00
|
|
|
ipcRenderer.send('request-update-tnc-state', Data);
|
|
|
|
}
|
2021-09-04 18:23:58 +00:00
|
|
|
|
2021-08-16 17:39:20 +00:00
|
|
|
if (data['COMMAND'] == 'RX_BUFFER') {
|
2021-09-04 18:23:58 +00:00
|
|
|
|
2021-08-23 14:24:49 +00:00
|
|
|
rxBufferLengthGui = data['DATA-ARRAY'].length
|
2021-08-16 17:39:20 +00:00
|
|
|
let Data = {
|
2021-09-04 18:23:58 +00:00
|
|
|
data: data['DATA-ARRAY'],
|
2021-08-16 17:39:20 +00:00
|
|
|
};
|
2021-11-19 17:49:36 +00:00
|
|
|
|
2021-08-16 17:39:20 +00:00
|
|
|
ipcRenderer.send('request-update-rx-buffer', Data);
|
|
|
|
}
|
|
|
|
|
2021-10-17 14:59:15 +00:00
|
|
|
if (data['COMMAND'] == 'RX_MSG_BUFFER') {
|
|
|
|
|
|
|
|
rxMsgBufferLengthGui = data['DATA-ARRAY'].length
|
|
|
|
let Data = {
|
|
|
|
data: data['DATA-ARRAY'],
|
|
|
|
};
|
2021-11-19 17:49:36 +00:00
|
|
|
|
2021-10-17 14:59:15 +00:00
|
|
|
ipcRenderer.send('request-update-rx-msg-buffer', Data);
|
|
|
|
}
|
2021-07-24 07:06:22 +00:00
|
|
|
// check if EOF ...
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
2021-07-24 07:06:22 +00:00
|
|
|
|
2021-07-17 07:03:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function hexToBytes(hex) {
|
|
|
|
for (var bytes = [], c = 0; c < hex.length; c += 2)
|
2021-07-24 07:06:22 +00:00
|
|
|
bytes.push(parseInt(hex.substr(c, 2), 16));
|
2021-07-17 07:03:40 +00:00
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2021-09-13 16:27:50 +00:00
|
|
|
//Save myCall
|
2021-07-24 07:06:22 +00:00
|
|
|
exports.saveMyCall = function(callsign) {
|
2021-09-04 18:23:58 +00:00
|
|
|
command = '{"type" : "SET", "command": "MYCALLSIGN" , "parameter": "' + callsign + '", "timestamp" : ' + Date.now() + '}'
|
2021-07-24 07:06:22 +00:00
|
|
|
writeTncCommand(command)
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 19:01:38 +00:00
|
|
|
// Save myGrid
|
2021-07-24 07:06:22 +00:00
|
|
|
exports.saveMyGrid = function(grid) {
|
2021-09-04 18:23:58 +00:00
|
|
|
command = '{"type" : "SET", "command": "MYGRID" , "parameter": "' + grid + '", "timestamp" : ' + Date.now() + '}'
|
2021-07-24 07:06:22 +00:00
|
|
|
writeTncCommand(command)
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 19:01:38 +00:00
|
|
|
//Get TNC State
|
2021-07-24 07:06:22 +00:00
|
|
|
exports.getTncState = function() {
|
2021-09-04 18:23:58 +00:00
|
|
|
command = '{"type" : "GET", "command" : "TNC_STATE", "timestamp" : ' + Date.now() + '}';
|
2021-07-24 07:06:22 +00:00
|
|
|
writeTncCommand(command)
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
2021-07-19 19:01:38 +00:00
|
|
|
|
2021-07-23 15:40:44 +00:00
|
|
|
//Get DATA State
|
2021-07-24 07:06:22 +00:00
|
|
|
exports.getDataState = function() {
|
2021-09-04 18:23:58 +00:00
|
|
|
command = '{"type" : "GET", "command" : "DATA_STATE", "timestamp" : ' + Date.now() + '}';
|
2021-07-25 16:19:51 +00:00
|
|
|
//writeTncCommand(command)
|
2021-07-23 15:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Get Heard Stations
|
2021-10-24 12:44:55 +00:00
|
|
|
//exports.getHeardStations = function() {
|
|
|
|
// command = '{"type" : "GET", "command" : "HEARD_STATIONS", "timestamp" : ' + Date.now() + '}';
|
|
|
|
// writeTncCommand(command)
|
|
|
|
//}
|
2021-07-23 15:40:44 +00:00
|
|
|
|
2021-07-19 19:01:38 +00:00
|
|
|
// Send Ping
|
2021-07-24 07:06:22 +00:00
|
|
|
exports.sendPing = function(dxcallsign) {
|
2021-09-04 18:23:58 +00:00
|
|
|
command = '{"type" : "PING", "command" : "PING", "dxcallsign" : "' + dxcallsign + '", "timestamp" : ' + Date.now() + '}'
|
2021-07-24 07:06:22 +00:00
|
|
|
writeTncCommand(command)
|
2021-07-19 19:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send CQ
|
2021-07-24 07:06:22 +00:00
|
|
|
exports.sendCQ = function() {
|
2021-12-06 19:16:14 +00:00
|
|
|
command = '{"type" : "BROADCAST", "command" : "CQCQCQ", "timestamp" : ' + Date.now() + '}'
|
2021-07-24 07:06:22 +00:00
|
|
|
writeTncCommand(command)
|
2021-07-26 16:09:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send File
|
|
|
|
exports.sendFile = function(dxcallsign, mode, frames, filename, filetype, data, checksum) {
|
2021-09-04 18:23:58 +00:00
|
|
|
command = '{"type" : "ARQ", "command" : "sendFile", "dxcallsign" : "' + dxcallsign + '", "mode" : "' + mode + '", "n_frames" : "' + frames + '", "filename" : "' + filename + '", "filetype" : "' + filetype + '", "data" : "' + data + '", "checksum" : "' + checksum + '", "timestamp" : ' + Date.now() + '}'
|
2021-07-26 16:09:04 +00:00
|
|
|
writeTncCommand(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send Message
|
|
|
|
exports.sendMessage = function(dxcallsign, mode, frames, data, checksum) {
|
2021-11-19 16:30:17 +00:00
|
|
|
command = '{"type" : "ARQ", "command" : "sendMessage", "dxcallsign" : "' + dxcallsign + '", "mode" : "' + mode + '", "n_frames" : "' + frames + '", "data" : "' + data + '" , "checksum" : "' + checksum + '", "timestamp" : ' + Date.now() + '}'
|
|
|
|
console.log(command)
|
2021-07-26 16:09:04 +00:00
|
|
|
writeTncCommand(command)
|
2021-08-15 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 09:29:08 +00:00
|
|
|
|
|
|
|
//STOP TRANSMISSION
|
|
|
|
exports.stopTransmission = function() {
|
|
|
|
command = '{"type" : "ARQ", "command": "stopTransmission", "timestamp" : ' + Date.now() + '}'
|
|
|
|
writeTncCommand(command)
|
|
|
|
}
|
|
|
|
|
2021-08-15 15:30:50 +00:00
|
|
|
// Get RX BUffer
|
|
|
|
exports.getRxBuffer = function() {
|
2021-09-04 18:23:58 +00:00
|
|
|
command = '{"type" : "GET", "command" : "RX_BUFFER", "timestamp" : ' + Date.now() + '}'
|
2021-08-23 14:24:49 +00:00
|
|
|
|
|
|
|
// call command only if new data arrived
|
2021-09-04 18:23:58 +00:00
|
|
|
if (rxBufferLengthGui != rxBufferLengthTnc) {
|
2021-08-23 14:24:49 +00:00
|
|
|
writeTncCommand(command)
|
|
|
|
}
|
2021-09-05 09:26:09 +00:00
|
|
|
}
|
2021-10-17 14:59:15 +00:00
|
|
|
|
|
|
|
// Get RX MSG BUffer
|
2021-10-17 15:22:07 +00:00
|
|
|
exports.getMsgRxBuffer = function() {
|
2021-10-17 14:59:15 +00:00
|
|
|
command = '{"type" : "GET", "command" : "RX_MSG_BUFFER", "timestamp" : ' + Date.now() + '}'
|
|
|
|
|
|
|
|
// call command only if new data arrived
|
|
|
|
if (rxMsgBufferLengthGui != rxMsgBufferLengthTnc) {
|
|
|
|
writeTncCommand(command)
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 17:49:36 +00:00
|
|
|
|
|
|
|
// DELETE RX MSG BUffer
|
|
|
|
exports.delRxMsgBuffer = function() {
|
|
|
|
command = '{"type" : "SET", "command" : "DEL_RX_MSG_BUFFER", "timestamp" : ' + Date.now() + '}'
|
|
|
|
|
|
|
|
// call command only if new data arrived
|
|
|
|
if (rxMsgBufferLengthGui != rxMsgBufferLengthTnc) {
|
|
|
|
writeTncCommand(command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 19:16:14 +00:00
|
|
|
// START BEACON
|
|
|
|
exports.startBeacon = function(interval) {
|
|
|
|
command = '{"type" : "BROADCAST", "command" : "START_BEACON", "parameter": "' + interval + '","timestamp" : ' + Date.now() + '}'
|
|
|
|
writeTncCommand(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
// STOP BEACON
|
|
|
|
exports.stopBeacon = function() {
|
|
|
|
command = '{"type" : "BROADCAST", "command" : "STOP_BEACON", "timestamp" : ' + Date.now() + '}'
|
|
|
|
writeTncCommand(command)
|
|
|
|
}
|
2021-11-19 17:49:36 +00:00
|
|
|
|