2021-07-17 07:03:40 +00:00
|
|
|
var net = require('net');
|
2021-09-04 18:23:58 +00:00
|
|
|
const path = require('path')
|
|
|
|
const {
|
|
|
|
ipcRenderer
|
|
|
|
} = require('electron')
|
2021-07-23 15:40:44 +00:00
|
|
|
|
2021-09-04 18:23:58 +00:00
|
|
|
// 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-04 18:23:58 +00:00
|
|
|
var configFolder = path.join(appDataFolder, "codec2-FreeDATA");
|
|
|
|
var configPath = path.join(configFolder, 'config.json')
|
|
|
|
const config = require(configPath);
|
2021-07-17 07:03:40 +00:00
|
|
|
|
|
|
|
var daemon = new net.Socket();
|
|
|
|
var msg = ''; // Current message, per connection.
|
|
|
|
|
|
|
|
setTimeout(connectDAEMON, 500)
|
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
function connectDAEMON() {
|
2021-07-17 07:03:40 +00:00
|
|
|
|
|
|
|
console.log('connecting to DAEMON...')
|
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 19:37:56 +00:00
|
|
|
|
2021-09-04 18:23:58 +00:00
|
|
|
if (config.tnclocation == 'localhost') {
|
|
|
|
daemon.connect(3001, '127.0.0.1')
|
2021-09-04 14:33:17 +00:00
|
|
|
} else {
|
|
|
|
daemon.connect(config.daemon_port, config.daemon_host)
|
|
|
|
|
|
|
|
}
|
2021-09-04 18:23:58 +00:00
|
|
|
|
2021-07-17 07:03:40 +00:00
|
|
|
//client.setTimeout(5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon.on('connect', function(data) {
|
2021-07-24 07:06:22 +00:00
|
|
|
console.log('DAEMON connection established')
|
2021-07-17 07:03:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
daemon.on('error', function(data) {
|
2021-07-24 07:06:22 +00:00
|
|
|
console.log('DAEMON connection error');
|
2021-07-17 07:03:40 +00:00
|
|
|
setTimeout(connectDAEMON, 2000)
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
client.on('close', function(data) {
|
|
|
|
console.log(' TNC connection closed');
|
|
|
|
setTimeout(connectTNC, 2000)
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
daemon.on('end', function(data) {
|
2021-07-24 07:06:22 +00:00
|
|
|
console.log('DAEMON connection ended');
|
|
|
|
setTimeout(connectDAEMON, 2000)
|
2021-07-17 07:03:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//exports.writeCommand = function(command){
|
2021-07-24 07:06:22 +00:00
|
|
|
writeDaemonCommand = function(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 (daemon.readyState == 'open') {
|
|
|
|
//uiMain.setDAEMONconnection('open')
|
|
|
|
daemon.write(command + '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (daemon.readyState == 'closed') {
|
|
|
|
//uiMain.setDAEMONconnection('closed')
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
2021-07-24 07:06:22 +00:00
|
|
|
|
|
|
|
if (daemon.readyState == 'opening') {
|
|
|
|
//uiMain.setDAEMONconnection('opening')
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let Data = {
|
2021-07-24 07:06:22 +00:00
|
|
|
daemon_connection: daemon.readyState,
|
|
|
|
};
|
|
|
|
ipcRenderer.send('request-update-daemon-connection', Data);
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
// "https://stackoverflow.com/questions/9070700/nodejs-net-createserver-large-amount-of-data-coming-in"
|
2021-07-17 07:03:40 +00:00
|
|
|
|
|
|
|
daemon.on('data', function(data) {
|
2021-07-24 07:06:22 +00:00
|
|
|
|
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'] == 'DAEMON_STATE') {
|
|
|
|
let Data = {
|
|
|
|
input_devices: data['INPUT_DEVICES'],
|
|
|
|
output_devices: data['OUTPUT_DEVICES'],
|
2021-09-04 20:27:01 +00:00
|
|
|
serial_devices: data['SERIAL_DEVICES'],
|
2021-07-25 14:18:29 +00:00
|
|
|
tnc_running_state: data['DAEMON_STATE'][0]['STATUS'],
|
|
|
|
ram_usage: data['RAM'],
|
|
|
|
cpu_usage: data['CPU'],
|
|
|
|
version: data['VERSION'],
|
2021-07-24 07:06:22 +00:00
|
|
|
};
|
|
|
|
ipcRenderer.send('request-update-daemon-state', 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;
|
|
|
|
}
|
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
exports.getDaemonState = function() {
|
|
|
|
//function getDaemonState(){
|
2021-09-04 14:33:17 +00:00
|
|
|
command = '{"type" : "GET", "command" : "DAEMON_STATE"}'
|
2021-07-24 07:06:22 +00:00
|
|
|
writeDaemonCommand(command)
|
2021-07-17 07:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// START TNC
|
|
|
|
// ` `== multi line string
|
|
|
|
|
2021-07-25 14:35:50 +00:00
|
|
|
exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, ptt, serialspeed) {
|
2021-07-24 07:06:22 +00:00
|
|
|
var json_command = JSON.stringify({
|
|
|
|
type: 'SET',
|
|
|
|
command: 'STARTTNC',
|
|
|
|
parameter: [{
|
|
|
|
rx_audio: rx_audio,
|
|
|
|
tx_audio: tx_audio,
|
|
|
|
deviceid: deviceid,
|
|
|
|
deviceport: deviceport,
|
2021-07-25 14:35:50 +00:00
|
|
|
ptt: ptt,
|
|
|
|
serialspeed: serialspeed
|
|
|
|
|
2021-09-04 18:23:58 +00:00
|
|
|
}]
|
2021-07-24 07:06:22 +00:00
|
|
|
})
|
2021-07-17 07:03:40 +00:00
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
//console.log(json_command)
|
|
|
|
writeDaemonCommand(json_command)
|
2021-07-17 07:03:40 +00:00
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
}
|
2021-07-17 07:03:40 +00:00
|
|
|
|
2021-07-24 07:06:22 +00:00
|
|
|
// STOP TNC
|
|
|
|
exports.stopTNC = function() {
|
|
|
|
command = '{"type" : "SET", "command": "STOPTNC" , "parameter": "---" }'
|
|
|
|
writeDaemonCommand(command)
|
|
|
|
}
|