system app data path for config file

This commit is contained in:
DJ2LS 2021-09-04 20:23:58 +02:00 committed by GitHub
parent 385b775350
commit 32a70752fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 249 additions and 336 deletions

View file

@ -1,15 +1,18 @@
var net = require('net'); var net = require('net');
var config = require('./config.json'); const path = require('path')
const {
ipcRenderer
} = require('electron')
// https://stackoverflow.com/a/26227660
var appDataFolder = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME + "/.local/share")
var configFolder = path.join(appDataFolder, "codec2-FreeDATA");
var configPath = path.join(configFolder, 'config.json')
const config = require(configPath);
var daemon = new net.Socket(); var daemon = new net.Socket();
var msg = ''; // Current message, per connection. var msg = ''; // Current message, per connection.
const {
ipcRenderer
} = require('electron');
setTimeout(connectDAEMON, 500) setTimeout(connectDAEMON, 500)
function connectDAEMON() { function connectDAEMON() {
@ -27,7 +30,6 @@ function connectDAEMON() {
} }
//client.setTimeout(5000); //client.setTimeout(5000);
} }
@ -52,11 +54,9 @@ daemon.on('end', function(data) {
setTimeout(connectDAEMON, 2000) setTimeout(connectDAEMON, 2000)
}); });
//exports.writeCommand = function(command){ //exports.writeCommand = function(command){
writeDaemonCommand = function(command) { writeDaemonCommand = function(command) {
// we use the writingCommand function to update our TCPIP state because we are calling this function a lot // we use the writingCommand function to update our TCPIP state because we are calling this function a lot
// if socket openend, we are able to run commands // if socket openend, we are able to run commands
if (daemon.readyState == 'open') { if (daemon.readyState == 'open') {
@ -72,7 +72,6 @@ writeDaemonCommand = function(command) {
//uiMain.setDAEMONconnection('opening') //uiMain.setDAEMONconnection('opening')
} }
let Data = { let Data = {
daemon_connection: daemon.readyState, daemon_connection: daemon.readyState,
}; };
@ -81,14 +80,11 @@ writeDaemonCommand = function(command) {
// "https://stackoverflow.com/questions/9070700/nodejs-net-createserver-large-amount-of-data-coming-in" // "https://stackoverflow.com/questions/9070700/nodejs-net-createserver-large-amount-of-data-coming-in"
daemon.on('data', function(data) { daemon.on('data', function(data) {
data = data.toString('utf8'); /* convert data to string */ data = data.toString('utf8'); /* convert data to string */
msg += data.toString('utf8'); /*append data to buffer so we can stick long data together */ msg += data.toString('utf8'); /*append data to buffer so we can stick long data together */
/* check if we reached an EOF, if true, clear buffer and parse JSON data */ /* check if we reached an EOF, if true, clear buffer and parse JSON data */
if (data.endsWith('}')) { if (data.endsWith('}')) {
/*console.log(msg)*/ /*console.log(msg)*/
@ -117,8 +113,6 @@ daemon.on('data', function(data) {
////// check if EOF ... ////// check if EOF ...
} }
}); });
function hexToBytes(hex) { function hexToBytes(hex) {
@ -133,13 +127,9 @@ exports.getDaemonState = function() {
writeDaemonCommand(command) writeDaemonCommand(command)
} }
// START TNC // START TNC
// ` `== multi line string // ` `== multi line string
exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, ptt, serialspeed) { exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, ptt, serialspeed) {
var json_command = JSON.stringify({ var json_command = JSON.stringify({
type: 'SET', type: 'SET',
@ -155,12 +145,9 @@ exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, ptt, seria
}] }]
}) })
//console.log(json_command) //console.log(json_command)
writeDaemonCommand(json_command) writeDaemonCommand(json_command)
} }
// STOP TNC // STOP TNC

View file

@ -1,16 +1,30 @@
const {app,BrowserWindow,ipcMain} = require('electron') const {
app,
BrowserWindow,
ipcMain
} = require('electron')
const path = require('path') const path = require('path')
const fs = require('fs')
var testpath = path.join(app.getPath ("appData"), "codec2-FreeDATA"); app.setName("codec2-FreeDATA");
console.log(testpath)
var appDataFolder = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME + "/.local/share")
var configFolder = path.join(appDataFolder, "codec2-FreeDATA");
var configPath = path.join(configFolder, 'config.json')
// create folder if not exists
if (!fs.existsSync(configFolder)) {
fs.mkdirSync(configFolder);
}
// create config file if not exists
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, '{}')
}
const configPath = path.join(__dirname, 'config.json');
const config = require(configPath); const config = require(configPath);
const exec = require('child_process').exec; const exec = require('child_process').exec;
console.log(configPath)
let win = null; let win = null;
let data = null; let data = null;
@ -76,7 +90,6 @@ function createWindow() {
}) })
*/ */
// https://stackoverflow.com/questions/44258831/only-hide-the-window-when-closing-it-electron // https://stackoverflow.com/questions/44258831/only-hide-the-window-when-closing-it-electron
/* /*
data.on('close', function(evt) { data.on('close', function(evt) {
@ -86,11 +99,9 @@ function createWindow() {
*/ */
} }
app.whenReady().then(() => { app.whenReady().then(() => {
createWindow() createWindow()
// start daemon // start daemon
// https://stackoverflow.com/a/5775120 // https://stackoverflow.com/a/5775120
daemonProcess = exec('./daemon', function callback(error, stdout, stderr) { daemonProcess = exec('./daemon', function callback(error, stdout, stderr) {
@ -98,8 +109,6 @@ app.whenReady().then(() => {
console.log(error) console.log(error)
}); });
app.on('activate', () => { app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) { if (BrowserWindow.getAllWindows().length === 0) {
createWindow() createWindow()
@ -116,7 +125,6 @@ app.on('window-all-closed', () => {
} }
}) })
// IPC HANDLER // IPC HANDLER
/* /*
ipcMain.on('show-data-window', (event, arg) => { ipcMain.on('show-data-window', (event, arg) => {

View file

@ -1,29 +1,28 @@
const path = require('path') const path = require('path')
const configPath = path.join(__dirname, 'config.json');
const config = require(configPath);
const sock = require('./sock.js');
const daemon = require('./daemon.js');
const { const {
ipcRenderer ipcRenderer
} = require('electron'); } = require('electron')
const sock = require('./sock.js');
const daemon = require('./daemon.js');
const fs = require('fs'); const fs = require('fs');
const {
locatorToLatLng,
distance,
bearingDistance,
latLngToLocator
} = require('qth-locator');
const { locatorToLatLng, distance, bearingDistance, latLngToLocator } = require('qth-locator'); // https://stackoverflow.com/a/26227660
var appDataFolder = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME + "/.local/share")
var configFolder = path.join(appDataFolder, "codec2-FreeDATA");
var configPath = path.join(configFolder, 'config.json')
const config = require(configPath);
// START INTERVALL COMMAND EXECUTION FOR STATES // START INTERVALL COMMAND EXECUTION FOR STATES
setInterval(daemon.getDaemonState, 1000) setInterval(daemon.getDaemonState, 1000)
setInterval(sock.getTncState, 250) setInterval(sock.getTncState, 250)
setInterval(sock.getRxBuffer, 1000) setInterval(sock.getRxBuffer, 1000)
// UPDATE FFT DEMO // UPDATE FFT DEMO
updateFFT = function(fft) { updateFFT = function(fft) {
@ -34,8 +33,6 @@ updateFFT = function(fft) {
} }
setInterval(updateFFT, 250) setInterval(updateFFT, 250)
// WINDOW LISTENER // WINDOW LISTENER
window.addEventListener('DOMContentLoaded', () => { window.addEventListener('DOMContentLoaded', () => {
// LOAD SETTINGS // LOAD SETTINGS
@ -74,14 +71,12 @@ window.addEventListener('DOMContentLoaded', () => {
document.getElementById("remote-tnc-field").style.visibility = 'hidden'; document.getElementById("remote-tnc-field").style.visibility = 'hidden';
} }
// Create spectrum object on canvas with ID "waterfall" // Create spectrum object on canvas with ID "waterfall"
global.spectrum = new Spectrum( global.spectrum = new Spectrum(
"waterfall", { "waterfall", {
spectrumPercent: 20 spectrumPercent: 20
}); });
// SETUP OF SCATTER DIAGRAM // SETUP OF SCATTER DIAGRAM
global.data = { global.data = {
@ -95,7 +90,6 @@ window.addEventListener('DOMContentLoaded', () => {
}], }],
}; };
var ctx = document.getElementById('scatter').getContext('2d'); var ctx = document.getElementById('scatter').getContext('2d');
global.myChart = new Chart(ctx, { global.myChart = new Chart(ctx, {
type: 'scatter', type: 'scatter',
@ -158,8 +152,6 @@ window.addEventListener('DOMContentLoaded', () => {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
}); });
// on change port and host // on change port and host
document.getElementById("tnc_adress").addEventListener("change", () => { document.getElementById("tnc_adress").addEventListener("change", () => {
console.log(document.getElementById("tnc_adress").value) console.log(document.getElementById("tnc_adress").value)
@ -219,14 +211,12 @@ window.addEventListener('DOMContentLoaded', () => {
var serialspeed = document.getElementById("hamlib_serialspeed").value var serialspeed = document.getElementById("hamlib_serialspeed").value
var ptt = document.getElementById("hamlib_ptt").value var ptt = document.getElementById("hamlib_ptt").value
config.deviceid = deviceid config.deviceid = deviceid
config.deviceport = deviceport config.deviceport = deviceport
config.serialspeed = serialspeed config.serialspeed = serialspeed
config.ptt = ptt config.ptt = ptt
fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
daemon.startTNC(rx_audio, tx_audio, deviceid, deviceport, ptt, serialspeed) daemon.startTNC(rx_audio, tx_audio, deviceid, deviceport, ptt, serialspeed)
setTimeout(function() { setTimeout(function() {
sock.saveMyCall(config.mycall); sock.saveMyCall(config.mycall);
@ -250,7 +240,6 @@ window.addEventListener('DOMContentLoaded', () => {
} }
}) })
// START TRANSMISSION // START TRANSMISSION
document.getElementById("startTransmission").addEventListener("click", () => { document.getElementById("startTransmission").addEventListener("click", () => {
@ -260,7 +249,6 @@ window.addEventListener('DOMContentLoaded', () => {
//reader.readAsBinaryString(fileList[0]); //reader.readAsBinaryString(fileList[0]);
reader.readAsDataURL(fileList[0]); reader.readAsDataURL(fileList[0]);
reader.onload = function(e) { reader.onload = function(e) {
// binary data // binary data
@ -287,13 +275,8 @@ window.addEventListener('DOMContentLoaded', () => {
}) })
}) })
ipcRenderer.on('action-update-tnc-state', (event, arg) => { ipcRenderer.on('action-update-tnc-state', (event, arg) => {
// TOE TIME OF EXECUTION --> How many time needs a command to be executed until data arrives // TOE TIME OF EXECUTION --> How many time needs a command to be executed until data arrives
@ -307,7 +290,6 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => {
// DATA STATE // DATA STATE
global.rxBufferLengthTnc = arg.rx_buffer_length global.rxBufferLengthTnc = arg.rx_buffer_length
// SCATTER DIAGRAM PLOTTING // SCATTER DIAGRAM PLOTTING
//global.myChart.destroy(); //global.myChart.destroy();
@ -336,8 +318,6 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => {
global.data = newdata global.data = newdata
var ctx = document.getElementById('scatter').getContext('2d'); var ctx = document.getElementById('scatter').getContext('2d');
global.myChart = new Chart(ctx, { global.myChart = new Chart(ctx, {
type: 'scatter', type: 'scatter',
@ -402,7 +382,6 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => {
document.getElementById("rms_level").setAttribute("aria-valuenow", arg.rms_level) document.getElementById("rms_level").setAttribute("aria-valuenow", arg.rms_level)
document.getElementById("rms_level").setAttribute("style", "width:" + arg.rms_level + "%;") document.getElementById("rms_level").setAttribute("style", "width:" + arg.rms_level + "%;")
// CHANNEL STATE // CHANNEL STATE
if (arg.channel_state == 'RECEIVING_SIGNALLING') { if (arg.channel_state == 'RECEIVING_SIGNALLING') {
document.getElementById("signalling_state").className = "btn btn-success"; document.getElementById("signalling_state").className = "btn btn-success";
@ -452,7 +431,6 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => {
document.getElementById("transmission_progress").setAttribute("aria-valuenow", arg.arq_transmission_percent) document.getElementById("transmission_progress").setAttribute("aria-valuenow", arg.arq_transmission_percent)
document.getElementById("transmission_progress").setAttribute("style", "width:" + arg.arq_transmission_percent + "%;") document.getElementById("transmission_progress").setAttribute("style", "width:" + arg.arq_transmission_percent + "%;")
// UPDATE HEARD STATIONS // UPDATE HEARD STATIONS
var tbl = document.getElementById("heardstations"); var tbl = document.getElementById("heardstations");
document.getElementById("heardstations").innerHTML = '' document.getElementById("heardstations").innerHTML = ''
@ -465,7 +443,6 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => {
for (i = 0; i < heardStationsLength; i++) { for (i = 0; i < heardStationsLength; i++) {
// first we update the PING window // first we update the PING window
console.log(document.getElementById("dxCall").value) console.log(document.getElementById("dxCall").value)
if (arg.stations[i]['DXCALLSIGN'] == document.getElementById("dxCall").value) { if (arg.stations[i]['DXCALLSIGN'] == document.getElementById("dxCall").value) {
@ -483,7 +460,6 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => {
document.getElementById("dataModalPingDB").innerHTML = arg.stations[i]['SNR'] document.getElementById("dataModalPingDB").innerHTML = arg.stations[i]['SNR']
} }
// now we update the heard stations list // now we update the heard stations list
var row = document.createElement("tr"); var row = document.createElement("tr");
//https://stackoverflow.com/q/51421470 //https://stackoverflow.com/q/51421470
@ -546,7 +522,6 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => {
row.classList.add("table-primary"); row.classList.add("table-primary");
} }
var snr = document.createElement("td"); var snr = document.createElement("td");
var snrText = document.createElement('span'); var snrText = document.createElement('span');
snrText.innerText = arg.stations[i]['SNR'] snrText.innerText = arg.stations[i]['SNR']
@ -564,12 +539,8 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => {
}); });
ipcRenderer.on('action-update-daemon-state', (event, arg) => { ipcRenderer.on('action-update-daemon-state', (event, arg) => {
// RAM // RAM
document.getElementById("progressbar_ram").setAttribute("aria-valuenow", arg.ram_usage) document.getElementById("progressbar_ram").setAttribute("aria-valuenow", arg.ram_usage)
document.getElementById("progressbar_ram").setAttribute("style", "width:" + arg.ram_usage + "%;") document.getElementById("progressbar_ram").setAttribute("style", "width:" + arg.ram_usage + "%;")
@ -580,7 +551,6 @@ ipcRenderer.on('action-update-daemon-state', (event, arg) => {
document.getElementById("progressbar_cpu").setAttribute("style", "width:" + arg.cpu_usage + "%;") document.getElementById("progressbar_cpu").setAttribute("style", "width:" + arg.cpu_usage + "%;")
document.getElementById("progressbar_cpu_value").innerHTML = arg.cpu_usage + "%" document.getElementById("progressbar_cpu_value").innerHTML = arg.cpu_usage + "%"
// UPDATE AUDIO INPUT // UPDATE AUDIO INPUT
if (document.getElementById("audio_input_selectbox").length != arg.input_devices.length) { if (document.getElementById("audio_input_selectbox").length != arg.input_devices.length) {
@ -623,7 +593,6 @@ ipcRenderer.on('action-update-daemon-state', (event, arg) => {
document.getElementById("hamlib_serialspeed").disabled = true document.getElementById("hamlib_serialspeed").disabled = true
document.getElementById("startTransmission").disabled = false document.getElementById("startTransmission").disabled = false
} else { } else {
document.getElementById('hamlib_deviceid').disabled = false document.getElementById('hamlib_deviceid').disabled = false
document.getElementById('hamlib_deviceport').disabled = false document.getElementById('hamlib_deviceport').disabled = false
@ -644,7 +613,6 @@ ipcRenderer.on('action-update-daemon-state', (event, arg) => {
}); });
ipcRenderer.on('action-update-daemon-connection', (event, arg) => { ipcRenderer.on('action-update-daemon-connection', (event, arg) => {
if (arg.daemon_connection == 'open') { if (arg.daemon_connection == 'open') {
@ -664,7 +632,6 @@ ipcRenderer.on('action-update-daemon-connection', (event, arg) => {
}); });
ipcRenderer.on('action-update-rx-buffer', (event, arg) => { ipcRenderer.on('action-update-rx-buffer', (event, arg) => {
var data = arg.data["DATA"] var data = arg.data["DATA"]
@ -716,8 +683,6 @@ var data = arg.data["DATA"]
fileNameText.innerText = arg.data[i]['RXDATA'][0]['filename'] fileNameText.innerText = arg.data[i]['RXDATA'][0]['filename']
fileName.appendChild(fileNameText); fileName.appendChild(fileNameText);
row.appendChild(timestamp); row.appendChild(timestamp);
row.appendChild(dxCall); row.appendChild(dxCall);
// row.appendChild(dxGrid); // row.appendChild(dxGrid);
@ -725,11 +690,11 @@ var data = arg.data["DATA"]
tbl.appendChild(row); tbl.appendChild(row);
// Creates rxdata folder if not exists // Creates rxdata folder if not exists
// https://stackoverflow.com/a/13544465 // https://stackoverflow.com/a/13544465
fs.mkdir('rxdata', { recursive: true }, function(err) { fs.mkdir('rxdata', {
recursive: true
}, function(err) {
console.log(err); console.log(err);
}); });
@ -746,7 +711,6 @@ var data = arg.data["DATA"]
}); });
ipcRenderer.on('run-tnc-command', (event, arg) => { ipcRenderer.on('run-tnc-command', (event, arg) => {
if (arg.command == 'saveMyCall') { if (arg.command == 'saveMyCall') {
sock.saveMyCall(arg.callsign) sock.saveMyCall(arg.callsign)

View file

@ -1,8 +1,14 @@
var net = require('net'); var net = require('net');
var config = require('./config.json'); const path = require('path')
const { const {
ipcRenderer ipcRenderer
} = require('electron'); } = require('electron')
// https://stackoverflow.com/a/26227660
var appDataFolder = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Application Support' : process.env.HOME + "/.local/share")
var configFolder = path.join(appDataFolder, "codec2-FreeDATA");
var configPath = path.join(configFolder, 'config.json')
const config = require(configPath);
var client = new net.Socket(); var client = new net.Socket();
var msg = ''; // Current message, per connection. var msg = ''; // Current message, per connection.
@ -11,7 +17,6 @@ var msg = ''; // Current message, per connection.
var rxBufferLengthTnc = 0 var rxBufferLengthTnc = 0
var rxBufferLengthGui = 0 var rxBufferLengthGui = 0
// network connection Timeout // network connection Timeout
setTimeout(connectTNC, 3000) setTimeout(connectTNC, 3000)
@ -36,7 +41,6 @@ client.on('connect', function(data) {
client.on('error', function(data) { client.on('error', function(data) {
console.log('TNC connection error'); console.log('TNC connection error');
let Data = { let Data = {
busy_state: "-", busy_state: "-",
arq_state: "-", arq_state: "-",
@ -70,7 +74,6 @@ client.on('end', function(data) {
}); });
//exports.writeTncCommand = function(command){ //exports.writeTncCommand = function(command){
writeTncCommand = function(command) { writeTncCommand = function(command) {
@ -94,8 +97,6 @@ writeTncCommand = function(command) {
} }
} }
client.on('data', function(data) { client.on('data', function(data) {
/* /*
@ -163,54 +164,9 @@ client.on('data', function(data) {
ipcRenderer.send('request-update-rx-buffer', Data); ipcRenderer.send('request-update-rx-buffer', 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') {
//console.log(data['STATIONS'])
let Data = {
stations: data['STATIONS'],
};
//console.log(Data)
ipcRenderer.send('request-update-heard-stations', Data);
}
*/
/*
if (data['COMMAND'] == 'SCATTER') {
console.log(data['SCATTER'])
let Data = {
stations: data['STATIONS'],
};
//console.log(Data)
//ipcRenderer.send('request-update-heard-stations', Data);
}
*/
// check if EOF ... // check if EOF ...
} }
}); });
function hexToBytes(hex) { function hexToBytes(hex) {
@ -219,7 +175,6 @@ function hexToBytes(hex) {
return bytes; return bytes;
} }
//Save myCall //Save myCall
exports.saveMyCall = function(callsign) { exports.saveMyCall = function(callsign) {
command = '{"type" : "SET", "command": "MYCALLSIGN" , "parameter": "' + callsign + '", "timestamp" : ' + Date.now() + '}' command = '{"type" : "SET", "command": "MYCALLSIGN" , "parameter": "' + callsign + '", "timestamp" : ' + Date.now() + '}'
@ -250,7 +205,6 @@ exports.getHeardStations = function() {
writeTncCommand(command) writeTncCommand(command)
} }
// Send Ping // Send Ping
exports.sendPing = function(dxcallsign) { exports.sendPing = function(dxcallsign) {
command = '{"type" : "PING", "command" : "PING", "dxcallsign" : "' + dxcallsign + '", "timestamp" : ' + Date.now() + '}' command = '{"type" : "PING", "command" : "PING", "dxcallsign" : "' + dxcallsign + '", "timestamp" : ' + Date.now() + '}'