FreeDATA/gui/daemon.js

161 lines
3.8 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-17 07:03:40 +00:00
var daemon = new net.Socket();
var msg = ''; // Current message, per connection.
2021-07-24 07:06:22 +00:00
const {
ipcRenderer
} = require('electron');
2021-07-17 07:03:40 +00:00
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-07-23 15:40:44 +00:00
daemon.connect(config.daemon_port, config.daemon_host)
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
}
2021-07-24 07:06:22 +00:00
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-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(){
command = '{"type" : "GET", "command": "DAEMON_STATE"}'
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-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
//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)
}