FreeDATA/gui/sock.js

207 lines
5.6 KiB
JavaScript
Raw Normal View History

2021-07-17 07:03:40 +00:00
var net = require('net');
2021-07-23 15:40:44 +00:00
var config = require('./config.json');
2021-07-24 07:06:22 +00:00
const {
ipcRenderer
} = require('electron');
2021-07-17 07:03:40 +00:00
var client = new net.Socket();
var msg = ''; // Current message, per connection.
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-07-17 07:03:40 +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-07-24 07:06:22 +00:00
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');
2021-07-17 07:03:40 +00:00
2021-07-24 07:06:22 +00:00
let Data = {
busy_state: "-",
arq_state: "-",
channel_state: "-",
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');
setTimeout(connectTNC, 2000)
});
*/
client.on('end', function(data) {
2021-07-24 07:06:22 +00:00
console.log('TNC connection ended');
//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
});
//exports.writeTncCommand = function(command){
2021-07-24 07:06:22 +00:00
writeTncCommand = function(command) {
2021-07-17 07:03:40 +00:00
console.log(command)
// 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')
console.log("CLOSED!!!!!")
}
2021-07-24 07:06:22 +00:00
if (client.readyState == 'opening') {
//uiMain.setTNCconnection('opening')
console.log("OPENING!!!!!")
}
2021-07-17 07:03:40 +00:00
}
2021-07-24 07:06:22 +00:00
2021-07-17 07:03:40 +00:00
client.on('data', function(data) {
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-07-24 07:06:22 +00:00
// check if we reached an EOF, if true, clear buffer and parse JSON data
2021-07-17 07:03:40 +00:00
if (data.endsWith('}')) {
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!") */
if (data['COMMAND'] == 'TNC_STATE') {
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'],
2021-07-25 14:35:50 +00:00
rms_level: (data['AUDIO_RMS'] / 1000) * 100,
2021-07-24 07:06:22 +00:00
};
console.log(Data)
ipcRenderer.send('request-update-tnc-state', Data);
}
if (data['COMMAND'] == 'DATA_STATE') {
let Data = {
rx_buffer_length: data['RX_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'],
};
console.log(Data)
ipcRenderer.send('request-update-data-state', Data);
}
if (data['COMMAND'] == 'HEARD_STATIONS') {
2021-07-25 16:19:51 +00:00
//console.log(data['STATIONS'])
2021-07-24 07:06:22 +00:00
let Data = {
2021-07-25 16:19:51 +00:00
stations: data['STATIONS'],
2021-07-24 07:06:22 +00:00
};
2021-07-25 16:19:51 +00:00
//console.log(Data)
2021-07-24 07:06:22 +00:00
ipcRenderer.send('request-update-heard-stations', Data);
}
// 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;
}
//Save myCall
2021-07-24 07:06:22 +00:00
exports.saveMyCall = function(callsign) {
command = '{"type" : "SET", "command": "MYCALLSIGN" , "parameter": "' + callsign + '" }'
writeTncCommand(command)
2021-07-17 07:03:40 +00:00
}
// Save myGrid
2021-07-24 07:06:22 +00:00
exports.saveMyGrid = function(grid) {
command = '{"type" : "SET", "command": "MYGRID" , "parameter": "' + grid + '" }'
writeTncCommand(command)
2021-07-17 07:03:40 +00:00
}
//Get TNC State
2021-07-24 07:06:22 +00:00
exports.getTncState = function() {
2021-07-25 14:35:50 +00:00
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() {
2021-07-25 14:35:50 +00:00
command = '{"type" : "GET", "command" : "DATA_STATE"}';
2021-07-25 16:19:51 +00:00
//writeTncCommand(command)
2021-07-23 15:40:44 +00:00
}
//Get Heard Stations
2021-07-24 07:06:22 +00:00
exports.getHeardStations = function() {
2021-07-25 14:35:50 +00:00
command = '{"type" : "GET", "command" : "HEARD_STATIONS"}';
2021-07-24 07:06:22 +00:00
writeTncCommand(command)
2021-07-23 15:40:44 +00:00
}
2021-07-25 14:35:50 +00:00
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 + '"}'
writeTncCommand(command)
}
// Send CQ
2021-07-24 07:06:22 +00:00
exports.sendCQ = function() {
command = '{"type" : "CQ", "command" : "CQCQCQ"}'
writeTncCommand(command)
}