FreeDATA/gui/daemon.js

196 lines
5.3 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')
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');
2021-09-13 16:27:50 +00:00
setTimeout(connectTNC, 2000)
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)
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 */
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);
}
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(){
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
exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, pttprotocol, pttport, serialspeed, pttspeed, data_bits, stop_bits, handshake) {
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-09-13 18:02:14 +00:00
pttprotocol: pttprotocol,
pttport: pttport,
serialspeed: serialspeed,
pttspeed: pttspeed,
data_bits: data_bits,
stop_bits: stop_bits,
handshake: handshake
2021-07-25 14:35:50 +00:00
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)
}
// TEST HAMLIB
exports.testHamlib = function(deviceid, deviceport, serialspeed, pttprotocol, pttport, pttspeed, data_bits, stop_bits, handshake) {
var json_command = JSON.stringify({
type: 'GET',
command: 'TEST_HAMLIB',
parameter: [{
deviceid: deviceid,
deviceport: deviceport,
pttprotocol: pttprotocol,
pttport: pttport,
serialspeed: serialspeed,
pttspeed: pttspeed,
data_bits: data_bits,
stop_bits: stop_bits,
handshake: handshake
}]
})
console.log(json_command)
writeDaemonCommand(json_command)
}