From 32a70752fc6bf391db519918f65815b0c1d77f5a Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Sat, 4 Sep 2021 20:23:58 +0200 Subject: [PATCH] system app data path for config file --- gui/daemon.js | 41 ++--- gui/main.js | 52 +++--- gui/preload-main.js | 392 ++++++++++++++++++++------------------------ gui/sock.js | 100 +++-------- 4 files changed, 249 insertions(+), 336 deletions(-) diff --git a/gui/daemon.js b/gui/daemon.js index deccf1bf..561dd999 100644 --- a/gui/daemon.js +++ b/gui/daemon.js @@ -1,15 +1,18 @@ 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 msg = ''; // Current message, per connection. -const { - ipcRenderer -} = require('electron'); - - setTimeout(connectDAEMON, 500) function connectDAEMON() { @@ -19,15 +22,14 @@ function connectDAEMON() { //clear message buffer after reconnecting or inital connection msg = ''; daemon.connect(config.daemon_port, config.daemon_host) - - if (config.tnclocation == 'localhost'){ - daemon.connect(3001, '127.0.0.1') + + if (config.tnclocation == 'localhost') { + daemon.connect(3001, '127.0.0.1') } else { daemon.connect(config.daemon_port, config.daemon_host) } - - + //client.setTimeout(5000); } @@ -52,11 +54,9 @@ daemon.on('end', function(data) { setTimeout(connectDAEMON, 2000) }); - //exports.writeCommand = function(command){ writeDaemonCommand = function(command) { - // 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 (daemon.readyState == 'open') { @@ -72,7 +72,6 @@ writeDaemonCommand = function(command) { //uiMain.setDAEMONconnection('opening') } - let Data = { 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" - daemon.on('data', function(data) { data = data.toString('utf8'); /* convert data to string */ 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 */ if (data.endsWith('}')) { /*console.log(msg)*/ @@ -117,8 +113,6 @@ daemon.on('data', function(data) { ////// check if EOF ... } - - }); function hexToBytes(hex) { @@ -133,13 +127,9 @@ exports.getDaemonState = function() { writeDaemonCommand(command) } - - - // START TNC // ` `== multi line string - exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, ptt, serialspeed) { var json_command = JSON.stringify({ type: 'SET', @@ -152,15 +142,12 @@ exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, ptt, seria ptt: ptt, serialspeed: serialspeed - }] + }] }) - - //console.log(json_command) writeDaemonCommand(json_command) - } // STOP TNC diff --git a/gui/main.js b/gui/main.js index 8827326b..56a3ba17 100644 --- a/gui/main.js +++ b/gui/main.js @@ -1,16 +1,30 @@ -const {app,BrowserWindow,ipcMain} = require('electron') +const { + app, + BrowserWindow, + ipcMain +} = require('electron') const path = require('path') +const fs = require('fs') -var testpath = path.join(app.getPath ("appData"), "codec2-FreeDATA"); -console.log(testpath) +app.setName("codec2-FreeDATA"); + +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 exec = require('child_process').exec; -console.log(configPath) - - let win = null; let data = null; @@ -32,7 +46,7 @@ function createWindow() { }) // hide menu bar win.setMenuBarVisibility(false) - + //open dev tools win.webContents.openDevTools({ mode: 'undocked', @@ -76,7 +90,6 @@ function createWindow() { }) */ - // https://stackoverflow.com/questions/44258831/only-hide-the-window-when-closing-it-electron /* data.on('close', function(evt) { @@ -86,19 +99,15 @@ function createWindow() { */ } - app.whenReady().then(() => { createWindow() - - + // start daemon // https://stackoverflow.com/a/5775120 daemonProcess = exec('./daemon', function callback(error, stdout, stderr) { - // result - console.log(error) - }); - - + // result + console.log(error) + }); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { @@ -116,9 +125,8 @@ app.on('window-all-closed', () => { } }) - // IPC HANDLER - /* +/* ipcMain.on('show-data-window', (event, arg) => { data.show() }); @@ -147,9 +155,9 @@ ipcMain.on('request-update-daemon-connection', (event, arg) => { }); ipcMain.on('run-tnc-command', (event, arg) => { - win.webContents.send('run-tnc-command', arg); + win.webContents.send('run-tnc-command', arg); }); ipcMain.on('request-update-rx-buffer', (event, arg) => { - win.webContents.send('action-update-rx-buffer', arg); -}); \ No newline at end of file + win.webContents.send('action-update-rx-buffer', arg); +}); \ No newline at end of file diff --git a/gui/preload-main.js b/gui/preload-main.js index d6b50620..5b6d757f 100644 --- a/gui/preload-main.js +++ b/gui/preload-main.js @@ -1,29 +1,28 @@ 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 { ipcRenderer -} = require('electron'); +} = require('electron') +const sock = require('./sock.js'); +const daemon = require('./daemon.js'); 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 setInterval(daemon.getDaemonState, 1000) setInterval(sock.getTncState, 250) setInterval(sock.getRxBuffer, 1000) - - - - // UPDATE FFT DEMO updateFFT = function(fft) { @@ -34,8 +33,6 @@ updateFFT = function(fft) { } setInterval(updateFFT, 250) - - // WINDOW LISTENER window.addEventListener('DOMContentLoaded', () => { // LOAD SETTINGS @@ -63,7 +60,7 @@ window.addEventListener('DOMContentLoaded', () => { document.getElementById("waterfall").style.height = '0px'; } -// remote tnc + // remote tnc if (config.tnclocation == 'remote') { document.getElementById("local-remote-switch1").checked = false document.getElementById("local-remote-switch2").checked = true @@ -71,16 +68,14 @@ window.addEventListener('DOMContentLoaded', () => { } else { document.getElementById("local-remote-switch1").checked = true document.getElementById("local-remote-switch2").checked = false - document.getElementById("remote-tnc-field").style.visibility = 'hidden'; + document.getElementById("remote-tnc-field").style.visibility = 'hidden'; } - - // Create spectrum object on canvas with ID "waterfall" - global.spectrum = new Spectrum( - "waterfall", { - spectrumPercent: 20 - }); - + // Create spectrum object on canvas with ID "waterfall" + global.spectrum = new Spectrum( + "waterfall", { + spectrumPercent: 20 + }); // SETUP OF SCATTER DIAGRAM @@ -95,7 +90,6 @@ window.addEventListener('DOMContentLoaded', () => { }], }; - var ctx = document.getElementById('scatter').getContext('2d'); global.myChart = new Chart(ctx, { type: 'scatter', @@ -148,18 +142,16 @@ window.addEventListener('DOMContentLoaded', () => { document.getElementById("local-remote-switch2").checked = false document.getElementById("remote-tnc-field").style.visibility = 'hidden'; config.tnclocation = 'localhost' - fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); - }); - document.getElementById("local-remote-switch2").addEventListener("click", () => { + fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); + }); + document.getElementById("local-remote-switch2").addEventListener("click", () => { document.getElementById("local-remote-switch1").checked = false document.getElementById("local-remote-switch2").checked = true document.getElementById("remote-tnc-field").style.visibility = 'visible'; - config.tnclocation = 'remote' - fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); - }); - - - + config.tnclocation = 'remote' + fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); + }); + // on change port and host document.getElementById("tnc_adress").addEventListener("change", () => { console.log(document.getElementById("tnc_adress").value) @@ -198,7 +190,7 @@ window.addEventListener('DOMContentLoaded', () => { var dxcallsign = document.getElementById("dxCall").value sock.sendPing(dxcallsign) }); - + // dataModalstartPing button clicked document.getElementById("dataModalSendPing").addEventListener("click", () => { var dxcallsign = document.getElementById("dataModalDxCall").value @@ -219,14 +211,12 @@ window.addEventListener('DOMContentLoaded', () => { var serialspeed = document.getElementById("hamlib_serialspeed").value var ptt = document.getElementById("hamlib_ptt").value - config.deviceid = deviceid config.deviceport = deviceport config.serialspeed = serialspeed config.ptt = ptt fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); - daemon.startTNC(rx_audio, tx_audio, deviceid, deviceport, ptt, serialspeed) setTimeout(function() { sock.saveMyCall(config.mycall); @@ -243,74 +233,66 @@ window.addEventListener('DOMContentLoaded', () => { // openDataModule button clicked document.getElementById("openDataModule").addEventListener("click", () => { - if(document.getElementById("mySidebar").style.width == "40%"){ - document.getElementById("mySidebar").style.width = "0px"; - } else { - document.getElementById("mySidebar").style.width = "40%"; - } - }) - - - // START TRANSMISSION - document.getElementById("startTransmission").addEventListener("click", () => { - - var fileList = document.getElementById("dataModalFile").files; - - var reader = new FileReader(); - //reader.readAsBinaryString(fileList[0]); - reader.readAsDataURL(fileList[0]); - - - reader.onload = function(e) { - // binary data - - var data = e.target.result - console.log(data) - - let Data = { - command: "sendFile", - dxcallsign: document.getElementById("dataModalDxCall").value, - mode: document.getElementById("datamode").value, - frames: document.getElementById("framesperburst").value, - filetype: fileList[0].type, - filename: fileList[0].name, - data: data, - checksum: '123123123', - }; - ipcRenderer.send('run-tnc-command', Data); - - }; - reader.onerror = function(e) { - // error occurred - console.log('Error : ' + e.type); - }; - + if (document.getElementById("mySidebar").style.width == "40%") { + document.getElementById("mySidebar").style.width = "0px"; + } else { + document.getElementById("mySidebar").style.width = "40%"; + } }) - - - + + // START TRANSMISSION + document.getElementById("startTransmission").addEventListener("click", () => { + + var fileList = document.getElementById("dataModalFile").files; + + var reader = new FileReader(); + //reader.readAsBinaryString(fileList[0]); + reader.readAsDataURL(fileList[0]); + + reader.onload = function(e) { + // binary data + + var data = e.target.result + console.log(data) + + let Data = { + command: "sendFile", + dxcallsign: document.getElementById("dataModalDxCall").value, + mode: document.getElementById("datamode").value, + frames: document.getElementById("framesperburst").value, + filetype: fileList[0].type, + filename: fileList[0].name, + data: data, + checksum: '123123123', + }; + ipcRenderer.send('run-tnc-command', Data); + + }; + reader.onerror = function(e) { + // error occurred + console.log('Error : ' + e.type); + }; + + }) + }) - - - ipcRenderer.on('action-update-tnc-state', (event, arg) => { // TOE TIME OF EXECUTION --> How many time needs a command to be executed until data arrives - if (typeof(arg.toe) == 'undefined'){ - var toe = 0 + if (typeof(arg.toe) == 'undefined') { + var toe = 0 } else { - var toe = arg.toe - } + var toe = arg.toe + } document.getElementById("toe").innerHTML = toe + ' ms' - - // DATA STATE - global.rxBufferLengthTnc = arg.rx_buffer_length - + + // DATA STATE + global.rxBufferLengthTnc = arg.rx_buffer_length // SCATTER DIAGRAM PLOTTING //global.myChart.destroy(); - + //console.log(arg.scatter.length) var data = arg.scatter @@ -321,51 +303,49 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => { backgroundColor: 'rgb(255, 99, 132)' }], }; - - if (typeof(arg.scatter) == 'undefined'){ - var scatterSize = 0 + + if (typeof(arg.scatter) == 'undefined') { + var scatterSize = 0 } else { - var scatterSize = arg.scatter.length + var scatterSize = arg.scatter.length } - if (global.data != newdata && scatterSize > 0){ - try { - global.myChart.destroy(); - } catch (e) { - // myChart not yet created - } - - global.data = newdata - - - - var ctx = document.getElementById('scatter').getContext('2d'); - global.myChart = new Chart(ctx, { - type: 'scatter', - data: global.data, - options: { - animation: false, - legend: { - display: false, - tooltips: { - enabled: false, + if (global.data != newdata && scatterSize > 0) { + try { + global.myChart.destroy(); + } catch (e) { + // myChart not yet created + } + + global.data = newdata + + var ctx = document.getElementById('scatter').getContext('2d'); + global.myChart = new Chart(ctx, { + type: 'scatter', + data: global.data, + options: { + animation: false, + legend: { + display: false, + tooltips: { + enabled: false, + }, }, - }, - scales: { - display: false, - grid: { - display: false - }, - x: { - type: 'linear', - position: 'bottom', - display: false - }, - y: { - display: false + scales: { + display: false, + grid: { + display: false + }, + x: { + type: 'linear', + position: 'bottom', + display: false + }, + y: { + display: false + } } - } - }, - }); + }, + }); } // PTT STATE @@ -380,7 +360,7 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => { // BUSY STATE if (arg.busy_state == 'BUSY') { document.getElementById("busy_state").className = "btn btn-danger"; - document.getElementById("startTransmission").disabled = true + document.getElementById("startTransmission").disabled = true } else if (arg.busy_state == 'IDLE') { document.getElementById("busy_state").className = "btn btn-success"; document.getElementById("startTransmission").disabled = false @@ -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("style", "width:" + arg.rms_level + "%;") - // CHANNEL STATE if (arg.channel_state == 'RECEIVING_SIGNALLING') { document.getElementById("signalling_state").className = "btn btn-success"; @@ -433,57 +412,54 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => { // SET BANDWITH document.getElementById("bandwith").innerHTML = arg.bandwith - - // SET BYTES PER MINUTE - if (typeof(arg.arq_bytes_per_minute) == 'undefined'){ - var arq_bytes_per_minute = 0 + + // SET BYTES PER MINUTE + if (typeof(arg.arq_bytes_per_minute) == 'undefined') { + var arq_bytes_per_minute = 0 } else { - var arq_bytes_per_minute = arg.arq_bytes_per_minute - } + var arq_bytes_per_minute = arg.arq_bytes_per_minute + } document.getElementById("bytes_per_min").innerHTML = arq_bytes_per_minute - - // SET TOTAL BYTES - if (typeof(arg.total_bytes) == 'undefined'){ - var total_bytes = 0 + + // SET TOTAL BYTES + if (typeof(arg.total_bytes) == 'undefined') { + var total_bytes = 0 } else { - var total_bytes = arg.total_bytes - } + var total_bytes = arg.total_bytes + } document.getElementById("total_bytes").innerHTML = total_bytes document.getElementById("transmission_progress").setAttribute("aria-valuenow", arg.arq_transmission_percent) document.getElementById("transmission_progress").setAttribute("style", "width:" + arg.arq_transmission_percent + "%;") - - + // UPDATE HEARD STATIONS - var tbl = document.getElementById("heardstations"); + var tbl = document.getElementById("heardstations"); document.getElementById("heardstations").innerHTML = '' - if (typeof(arg.stations) == 'undefined'){ - var heardStationsLength = 0 + if (typeof(arg.stations) == 'undefined') { + var heardStationsLength = 0 } else { - var heardStationsLength = arg.stations.length + var heardStationsLength = arg.stations.length } - - for (i = 0; i < heardStationsLength; i++) { + for (i = 0; i < heardStationsLength; i++) { // first we update the PING window console.log(document.getElementById("dxCall").value) if (arg.stations[i]['DXCALLSIGN'] == document.getElementById("dxCall").value) { var dxGrid = arg.stations[i]['DXGRID'] - var myGrid = document.getElementById("myGrid").value - try { - var dist = parseInt(distance(myGrid, dxGrid)) + ' km'; + var myGrid = document.getElementById("myGrid").value + try { + var dist = parseInt(distance(myGrid, dxGrid)) + ' km'; document.getElementById("pingDistance").innerHTML = dist document.getElementById("dataModalPingDistance").innerHTML = dist - } catch { - document.getElementById("pingDistance").innerHTML = '---' - document.getElementById("dataModalPingDistance").innerHTML = '---' - } + } catch { + document.getElementById("pingDistance").innerHTML = '---' + document.getElementById("dataModalPingDistance").innerHTML = '---' + } document.getElementById("pingDB").innerHTML = arg.stations[i]['SNR'] - document.getElementById("dataModalPingDB").innerHTML = arg.stations[i]['SNR'] + document.getElementById("dataModalPingDB").innerHTML = arg.stations[i]['SNR'] } - // now we update the heard stations list var row = document.createElement("tr"); //https://stackoverflow.com/q/51421470 @@ -513,45 +489,44 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => { var gridDistance = document.createElement("td"); var gridDistanceText = document.createElement('span'); - - try{ + + try { gridDistanceText.innerText = parseInt(distance(document.getElementById("myGrid").value, arg.stations[i]['DXGRID'])) + ' km'; } catch { gridDistanceText.innerText = '---' } - gridDistance.appendChild(gridDistanceText); + gridDistance.appendChild(gridDistanceText); var dataType = document.createElement("td"); var dataTypeText = document.createElement('span'); dataTypeText.innerText = arg.stations[i]['DATATYPE'] dataType.appendChild(dataTypeText); - - if(dataTypeText.innerText == 'CQ CQ CQ'){ + + if (dataTypeText.innerText == 'CQ CQ CQ') { row.classList.add("table-success"); } - - if(dataTypeText.innerText == 'DATA-CHANNEL'){ + + if (dataTypeText.innerText == 'DATA-CHANNEL') { row.classList.add("table-warning"); } - - if(dataTypeText.innerText == 'BEACON'){ + + if (dataTypeText.innerText == 'BEACON') { row.classList.add("table-light"); } - - if(dataTypeText.innerText == 'PING'){ + + if (dataTypeText.innerText == 'PING') { row.classList.add("table-info"); } - - if(dataTypeText.innerText == 'PING-ACK'){ - row.classList.add("table-primary"); - } + if (dataTypeText.innerText == 'PING-ACK') { + row.classList.add("table-primary"); + } var snr = document.createElement("td"); var snrText = document.createElement('span'); snrText.innerText = arg.stations[i]['SNR'] snr.appendChild(snrText); - + row.appendChild(timestamp); row.appendChild(dxCall); row.appendChild(dxGrid); @@ -561,15 +536,11 @@ ipcRenderer.on('action-update-tnc-state', (event, arg) => { tbl.appendChild(row); } - + }); - - - ipcRenderer.on('action-update-daemon-state', (event, arg) => { - // RAM document.getElementById("progressbar_ram").setAttribute("aria-valuenow", 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_value").innerHTML = arg.cpu_usage + "%" - // UPDATE AUDIO INPUT 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("startTransmission").disabled = false - } else { document.getElementById('hamlib_deviceid').disabled = false document.getElementById('hamlib_deviceport').disabled = false @@ -644,17 +613,16 @@ ipcRenderer.on('action-update-daemon-state', (event, arg) => { }); - ipcRenderer.on('action-update-daemon-connection', (event, arg) => { if (arg.daemon_connection == 'open') { document.getElementById("daemon_connection_state").className = "btn btn-success"; - document.getElementById("blurdiv").style.webkitFilter = "blur(0px)"; + document.getElementById("blurdiv").style.webkitFilter = "blur(0px)"; } if (arg.daemon_connection == 'opening') { document.getElementById("daemon_connection_state").className = "btn btn-warning"; - document.getElementById("blurdiv").style.webkitFilter = "blur(10px)"; + document.getElementById("blurdiv").style.webkitFilter = "blur(10px)"; } if (arg.daemon_connection == 'closed') { @@ -664,10 +632,9 @@ ipcRenderer.on('action-update-daemon-connection', (event, arg) => { }); - ipcRenderer.on('action-update-rx-buffer', (event, arg) => { -var data = arg.data["DATA"] + var data = arg.data["DATA"] var tbl = document.getElementById("rx-data"); document.getElementById("rx-data").innerHTML = '' @@ -704,35 +671,33 @@ var data = arg.data["DATA"] dxCallText.innerText = arg.data[i]['DXCALLSIGN'] dxCall.appendChild(dxCallText); -/* - var dxGrid = document.createElement("td"); - var dxGridText = document.createElement('span'); - dxGridText.innerText = arg.data[i]['DXGRID'] - dxGrid.appendChild(dxGridText); -*/ + /* + var dxGrid = document.createElement("td"); + var dxGridText = document.createElement('span'); + dxGridText.innerText = arg.data[i]['DXGRID'] + dxGrid.appendChild(dxGridText); + */ var fileName = document.createElement("td"); var fileNameText = document.createElement('span'); fileNameText.innerText = arg.data[i]['RXDATA'][0]['filename'] fileName.appendChild(fileNameText); - - row.appendChild(timestamp); row.appendChild(dxCall); - // row.appendChild(dxGrid); + // row.appendChild(dxGrid); row.appendChild(fileName); - + tbl.appendChild(row); - - - + // Creates rxdata folder if not exists // https://stackoverflow.com/a/13544465 - fs.mkdir('rxdata', { recursive: true }, function(err) { + fs.mkdir('rxdata', { + recursive: true + }, function(err) { console.log(err); }); - + // write file to rxdata folder var base64String = arg.data[i]['RXDATA'][0]['data'] // remove header from base64 String @@ -741,12 +706,11 @@ var data = arg.data["DATA"] //write data to file require("fs").writeFile('rxdata/' + arg.data[i]['RXDATA'][0]['filename'], base64Data, 'base64', function(err) { console.log(err); - }); + }); } }); - ipcRenderer.on('run-tnc-command', (event, arg) => { if (arg.command == 'saveMyCall') { sock.saveMyCall(arg.callsign) @@ -764,4 +728,4 @@ ipcRenderer.on('run-tnc-command', (event, arg) => { if (arg.command == 'sendMessage') { sock.sendMessage(arg.dxcallsign, arg.mode, arg.frames, arg.data, arg.checksum) } -}); +}); \ No newline at end of file diff --git a/gui/sock.js b/gui/sock.js index 5cff3717..7c2f8475 100644 --- a/gui/sock.js +++ b/gui/sock.js @@ -1,8 +1,14 @@ var net = require('net'); -var config = require('./config.json'); +const path = require('path') const { 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 msg = ''; // Current message, per connection. @@ -11,7 +17,6 @@ var msg = ''; // Current message, per connection. var rxBufferLengthTnc = 0 var rxBufferLengthGui = 0 - // network connection Timeout setTimeout(connectTNC, 3000) @@ -21,9 +26,9 @@ function connectTNC() { //clear message buffer after reconnecting or inital connection msg = ''; - - if (config.tnclocation == 'localhost'){ - client.connect(3000, '127.0.0.1') + + if (config.tnclocation == 'localhost') { + client.connect(3000, '127.0.0.1') } else { client.connect(config.tnc_port, config.tnc_host) } @@ -36,7 +41,6 @@ client.on('connect', function(data) { client.on('error', function(data) { console.log('TNC connection error'); - let Data = { busy_state: "-", arq_state: "-", @@ -70,7 +74,6 @@ client.on('end', function(data) { }); - //exports.writeTncCommand = function(command){ writeTncCommand = function(command) { @@ -94,8 +97,6 @@ writeTncCommand = function(command) { } } - - client.on('data', function(data) { /* @@ -122,7 +123,7 @@ client.on('data', function(data) { if (data['COMMAND'] == 'TNC_STATE') { //console.log(data) rxBufferLengthTnc = data['RX_BUFFER_LENGTH'] - + let Data = { toe: Date.now() - data['TIMESTAMP'], // time of execution ptt_state: data['PTT_STATE'], @@ -151,66 +152,21 @@ client.on('data', function(data) { //console.log(Data) ipcRenderer.send('request-update-tnc-state', Data); } - + if (data['COMMAND'] == 'RX_BUFFER') { - + rxBufferLengthGui = data['DATA-ARRAY'].length //console.log(rxBufferLengthGui) let Data = { - data : data['DATA-ARRAY'], + data: data['DATA-ARRAY'], }; //console.log(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 ... } - - }); function hexToBytes(hex) { @@ -219,68 +175,66 @@ function hexToBytes(hex) { return bytes; } - //Save myCall exports.saveMyCall = function(callsign) { - command = '{"type" : "SET", "command": "MYCALLSIGN" , "parameter": "' + callsign + '", "timestamp" : '+Date.now()+'}' + command = '{"type" : "SET", "command": "MYCALLSIGN" , "parameter": "' + callsign + '", "timestamp" : ' + Date.now() + '}' writeTncCommand(command) } // Save myGrid exports.saveMyGrid = function(grid) { - command = '{"type" : "SET", "command": "MYGRID" , "parameter": "' + grid + '", "timestamp" : '+Date.now()+'}' + command = '{"type" : "SET", "command": "MYGRID" , "parameter": "' + grid + '", "timestamp" : ' + Date.now() + '}' writeTncCommand(command) } //Get TNC State exports.getTncState = function() { - command = '{"type" : "GET", "command" : "TNC_STATE", "timestamp" : '+Date.now()+'}'; + command = '{"type" : "GET", "command" : "TNC_STATE", "timestamp" : ' + Date.now() + '}'; writeTncCommand(command) } //Get DATA State exports.getDataState = function() { - command = '{"type" : "GET", "command" : "DATA_STATE", "timestamp" : '+Date.now()+'}'; + command = '{"type" : "GET", "command" : "DATA_STATE", "timestamp" : ' + Date.now() + '}'; //writeTncCommand(command) } //Get Heard Stations exports.getHeardStations = function() { - command = '{"type" : "GET", "command" : "HEARD_STATIONS", "timestamp" : '+Date.now()+'}'; + command = '{"type" : "GET", "command" : "HEARD_STATIONS", "timestamp" : ' + Date.now() + '}'; writeTncCommand(command) } - // Send Ping exports.sendPing = function(dxcallsign) { - command = '{"type" : "PING", "command" : "PING", "dxcallsign" : "' + dxcallsign + '", "timestamp" : '+Date.now()+'}' + command = '{"type" : "PING", "command" : "PING", "dxcallsign" : "' + dxcallsign + '", "timestamp" : ' + Date.now() + '}' writeTncCommand(command) } // Send CQ exports.sendCQ = function() { - command = '{"type" : "CQ", "command" : "CQCQCQ", "timestamp" : '+Date.now()+'}' + command = '{"type" : "CQ", "command" : "CQCQCQ", "timestamp" : ' + Date.now() + '}' writeTncCommand(command) } // Send File exports.sendFile = function(dxcallsign, mode, frames, filename, filetype, data, checksum) { - command = '{"type" : "ARQ", "command" : "sendFile", "dxcallsign" : "'+dxcallsign+'", "mode" : "'+mode+'", "n_frames" : "'+frames+'", "filename" : "'+filename+'", "filetype" : "'+filetype+'", "data" : "'+data+'", "checksum" : "'+checksum+'", "timestamp" : '+Date.now()+'}' + command = '{"type" : "ARQ", "command" : "sendFile", "dxcallsign" : "' + dxcallsign + '", "mode" : "' + mode + '", "n_frames" : "' + frames + '", "filename" : "' + filename + '", "filetype" : "' + filetype + '", "data" : "' + data + '", "checksum" : "' + checksum + '", "timestamp" : ' + Date.now() + '}' writeTncCommand(command) } // Send Message exports.sendMessage = function(dxcallsign, mode, frames, data, checksum) { - command = '{"type" : "ARQ", "command" : "sendMessage", "dxcallsign" : " '+dxcallsign+' ", "mode" : " '+mode+' ", "n_frames" : " '+frames+' ", "data" : '+data+' , "checksum" : " '+checksum+' ", "timestamp" : '+Date.now()+'}' + command = '{"type" : "ARQ", "command" : "sendMessage", "dxcallsign" : " ' + dxcallsign + ' ", "mode" : " ' + mode + ' ", "n_frames" : " ' + frames + ' ", "data" : ' + data + ' , "checksum" : " ' + checksum + ' ", "timestamp" : ' + Date.now() + '}' writeTncCommand(command) } // Get RX BUffer exports.getRxBuffer = function() { - command = '{"type" : "GET", "command" : "RX_BUFFER", "timestamp" : '+Date.now()+'}' + command = '{"type" : "GET", "command" : "RX_BUFFER", "timestamp" : ' + Date.now() + '}' // call command only if new data arrived - if(rxBufferLengthGui != rxBufferLengthTnc){ + if (rxBufferLengthGui != rxBufferLengthTnc) { writeTncCommand(command) } -} +} \ No newline at end of file