FreeDATA/gui/sock.js

369 lines
12 KiB
JavaScript
Raw Normal View History

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 socketchunk = ''; // Current message, per connection.
2021-07-17 07:03:40 +00:00
// split character
const split_char = '\0;'
// 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
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
socketchunk = '';
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-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-24 07:06:22 +00:00
// setTimeout( function() { exports.connectTNC(tnc_host, tnc_port); }, 2000 );
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-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(socketdata) {
2021-07-17 07:03:40 +00:00
2021-09-13 16:27:50 +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
*/
2021-09-04 18:23:58 +00:00
socketdata = socketdata.toString('utf8'); // convert data to string
socketchunk += socketdata// append data to buffer so we can stick long data together
// check if we received begin and end of json data
if (socketchunk.startsWith('{"') && socketchunk.endsWith('"}\n')) {
var data = ''
// split data into chunks if we received multiple commands
socketchunk = socketchunk.split("\n");
data = JSON.parse(socketchunk[0])
// search for empty entries in socketchunk and remove them
for (i = 0; i < socketchunk.length; i++) {
if (socketchunk[i] === ''){
socketchunk.splice(i, 1);
}
}
//iterate through socketchunks array to execute multiple commands in row
for (i = 0; i < socketchunk.length; i++) {
2021-09-04 18:23:58 +00:00
//check if data is not empty
if(socketchunk[i].length > 0){
//try to parse JSON
try {
data = JSON.parse(socketchunk[i])
} catch (e) {
console.log(e); // "SyntaxError
console.log(socketchunk[i])
socketchunk = ''
}
}
2021-08-16 17:39:20 +00:00
if (data['command'] == 'tnc_state') {
//console.log(data)
// set length of RX Buffer to global variable
rxBufferLengthTnc = data['rx_buffer_length']
rxMsgBufferLengthTnc = data['rx_msg_buffer_length']
let Data = {
ptt_state: data['ptt_state'],
busy_state: data['tnc_state'],
arq_state: data['arq_state'],
//channel_state: data['CHANNEL_STATE'],
frequency: data['frequency'],
mode: data['mode'],
bandwith: data['bandwith'],
rms_level: (data['audio_rms'] / 1000) * 100,
fft: data['fft'],
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_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'],
};
ipcRenderer.send('request-update-tnc-state', Data);
}
/* A TEST WITH STREAMING DATA .... */
if (data['arq'] == 'received') {
dataArray = []
messageArray = []
if(data['data'][0]['dt'] == 'f'){
dataArray.push(data)
}
if(data['data'][0]['dt'] == 'm'){
messageArray.push(data)
}
rxBufferLengthGui = dataArray.length
let Files = {
data: dataArray,
};
ipcRenderer.send('request-update-rx-buffer', Files);
rxMsgBufferLengthGui = messageArray.length
let Messages = {
data: messageArray,
};
ipcRenderer.send('request-update-rx-msg-buffer', Messages);
}
if (data['command'] == 'rx_buffer') {
// iterate through buffer list and sort it to file or message array
dataArray = []
messageArray = []
for (i = 0; i < data['data-array'].length; i++) {
try{
var encoded_data = atob(data['data-array'][i]['data'])
var splitted_data = encoded_data.split(split_char)
if(splitted_data[0] == 'f'){
dataArray.push(data['data-array'][i])
//dataArray.push(splitted_data)
}
if(splitted_data[0] == 'm'){
messageArray.push(data['data-array'][i])
//messageArray.push(splitted_data)
}
} catch (e) {
console.log(e)
}
}
console.log(dataArray)
rxBufferLengthGui = dataArray.length
let Files = {
data: dataArray,
};
ipcRenderer.send('request-update-rx-buffer', Files);
rxMsgBufferLengthGui = messageArray.length
let Messages = {
data: messageArray,
};
ipcRenderer.send('request-update-rx-msg-buffer', Messages);
}
}
//finally delete message buffer
socketchunk = '';
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;
}
//Get TNC State
2021-07-24 07:06:22 +00:00
exports.getTncState = function() {
command = '{"type" : "get", "command" : "tnc_state"}';
2021-07-24 07:06:22 +00:00
writeTncCommand(command)
2021-07-17 07:03:40 +00:00
}
2021-07-23 15:40:44 +00:00
//Get DATA State
2021-07-24 07:06:22 +00:00
exports.getDataState = function() {
command = '{"type" : "get", "command" : "data_state"}';
2021-07-25 16:19:51 +00:00
//writeTncCommand(command)
2021-07-23 15:40:44 +00:00
}
// Send Ping
2021-07-24 07:06:22 +00:00
exports.sendPing = function(dxcallsign) {
command = '{"type" : "ping", "command" : "ping", "dxcallsign" : "' + dxcallsign + '"}'
2021-07-24 07:06:22 +00:00
writeTncCommand(command)
}
// Send CQ
2021-07-24 07:06:22 +00:00
exports.sendCQ = function() {
command = '{"type" : "broadcast", "command" : "cqcqcq"}'
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) {
console.log(data)
console.log(filetype)
console.log(filename)
var datatype = "f"
//data = data.split('base64,')
//data = data[1]
data = datatype + split_char + filename + split_char + filetype + split_char + checksum + split_char + data
console.log(data)
console.log(btoa(data))
data = btoa(data)
//command = '{"type" : "arq", "command" : "send_file", "parameter" : [{"dxcallsign" : "' + dxcallsign + '", "mode" : "' + mode + '", "n_frames" : "' + frames + '", "filename" : "' + filename + '", "filetype" : "' + filetype + '", "data" : "' + data + '", "checksum" : "' + checksum + '"}]}'
command = '{"type" : "arq", "command" : "send_raw", "parameter" : [{"dxcallsign" : "' + dxcallsign + '", "mode" : "' + mode + '", "n_frames" : "' + frames + '", "data" : "' + data + '"}]}'
2021-07-26 16:09:04 +00:00
writeTncCommand(command)
}
// Send Message
exports.sendMessage = function(dxcallsign, mode, frames, data, checksum) {
command = '{"type" : "arq", "command" : "send_message", "parameter" : [{ "dxcallsign" : "' + dxcallsign + '", "mode" : "' + mode + '", "n_frames" : "' + frames + '", "data" : "' + data + '" , "checksum" : "' + checksum + '"}]}'
2021-11-19 16:30:17 +00:00
console.log(command)
2021-07-26 16:09:04 +00:00
writeTncCommand(command)
2021-08-15 15:30:50 +00:00
}
//STOP TRANSMISSION
exports.stopTransmission = function() {
command = '{"type" : "arq", "command": "stop_transmission"}'
writeTncCommand(command)
}
2021-08-15 15:30:50 +00:00
// Get RX BUffer
exports.getRxBuffer = function() {
command = '{"type" : "get", "command" : "rx_buffer"}'
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
}
// START BEACON
exports.startBeacon = function(interval) {
command = '{"type" : "broadcast", "command" : "start_beacon", "parameter": "' + interval + '"}'
writeTncCommand(command)
}
// STOP BEACON
exports.stopBeacon = function() {
command = '{"type" : "broadcast", "command" : "stop_beacon"}'
writeTncCommand(command)
}