FreeDATA/gui/daemon.js

234 lines
6.6 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')
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-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 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')
let Data = {
daemon_connection: daemon.readyState,
};
ipcRenderer.send('request-update-daemon-connection', Data);
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)
let Data = {
daemon_connection: daemon.readyState,
};
ipcRenderer.send('request-update-daemon-connection', Data);
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)
let Data = {
daemon_connection: daemon.readyState,
};
ipcRenderer.send('request-update-daemon-connection', Data);
2021-07-17 07:03:40 +00:00
});
*/
daemon.on('end', function(data) {
2021-07-24 07:06:22 +00:00
console.log('DAEMON connection ended');
setTimeout(connectDAEMON, 2000)
let Data = {
daemon_connection: daemon.readyState,
};
ipcRenderer.send('request-update-daemon-connection', Data);
2021-07-17 07:03:40 +00:00
});
2021-09-13 16:27:50 +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') {
2021-09-13 16:27:50 +00:00
//uiMain.setDAEMONconnection('open')
2021-07-24 07:06:22 +00:00
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 */
if (data.endsWith('}\n')) {
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') {
2021-07-24 07:06:22 +00:00
let Data = {
input_devices: data['input_devices'],
output_devices: data['output_devices'],
python_version: data['python_version'],
hamlib_version: data['hamlib_version'],
serial_devices: data['serial_devices'],
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);
}
if (data['command'] == 'test_hamlib') {
let Data = {
hamlib_result: data['result'],
};
ipcRenderer.send('request-update-hamlib-test', 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-07-24 07:06:22 +00:00
exports.getDaemonState = function() {
//function getDaemonState(){
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
exports.startTNC = function(mycall, mygrid, rx_audio, tx_audio, radiocontrol, devicename, deviceport, pttprotocol, pttport, serialspeed, data_bits, stop_bits, handshake, rigctld_ip, rigctld_port) {
2021-07-24 07:06:22 +00:00
var json_command = JSON.stringify({
type: 'set',
command: 'start_tnc',
2021-07-24 07:06:22 +00:00
parameter: [{
mycall: mycall,
mygrid: mygrid,
2021-07-24 07:06:22 +00:00
rx_audio: rx_audio,
tx_audio: tx_audio,
radiocontrol: radiocontrol,
2021-12-26 08:24:22 +00:00
devicename: devicename,
2021-07-24 07:06:22 +00:00
deviceport: deviceport,
2021-09-13 18:02:14 +00:00
pttprotocol: pttprotocol,
pttport: pttport,
serialspeed: serialspeed,
data_bits: data_bits,
stop_bits: stop_bits,
handshake: handshake,
rigctld_port: rigctld_port,
rigctld_ip: rigctld_ip
2021-09-04 18:23:58 +00:00
}]
2021-07-24 07:06:22 +00:00
})
2021-07-17 07:03:40 +00:00
console.log(json_command)
2021-07-24 07:06:22 +00:00
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": "stop_tnc" , "parameter": "---" }'
2021-07-24 07:06:22 +00:00
writeDaemonCommand(command)
}
// TEST HAMLIB
exports.testHamlib = function(radiocontrol, devicename, deviceport, serialspeed, pttprotocol, pttport, data_bits, stop_bits, handshake, rigctld_ip, rigctld_port) {
var json_command = JSON.stringify({
type: 'get',
command: 'test_hamlib',
parameter: [{
radiocontrol: radiocontrol,
2021-12-26 08:24:22 +00:00
devicename: devicename,
deviceport: deviceport,
pttprotocol: pttprotocol,
pttport: pttport,
serialspeed: serialspeed,
data_bits: data_bits,
stop_bits: stop_bits,
handshake: handshake,
rigctld_port: rigctld_port,
rigctld_ip: rigctld_ip
}]
})
console.log(json_command)
writeDaemonCommand(json_command)
}
//Save myCall
exports.saveMyCall = function(callsign) {
command = '{"type" : "set", "command": "mycallsign" , "parameter": "' + callsign + '"}'
writeDaemonCommand(command)
}
// Save myGrid
exports.saveMyGrid = function(grid) {
command = '{"type" : "set", "command": "mygrid" , "parameter": "' + grid + '"}'
writeDaemonCommand(command)
}