From da535d66360780bd788077000a3c2cd6e5b7ec08 Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Sat, 24 Jul 2021 09:06:22 +0200 Subject: [PATCH] code cleanup --- gui/daemon.js | 200 ++++++++----------- gui/main.js | 203 ++++++-------------- gui/preload-data.js | 139 +++++++------- gui/preload-main.js | 458 +++++++++++++++++--------------------------- gui/sock.js | 287 ++++++++++++--------------- 5 files changed, 505 insertions(+), 782 deletions(-) diff --git a/gui/daemon.js b/gui/daemon.js index b63e9f54..42a9c05d 100644 --- a/gui/daemon.js +++ b/gui/daemon.js @@ -5,33 +5,29 @@ var config = require('./config.json'); var daemon = new net.Socket(); var msg = ''; // Current message, per connection. -const { ipcRenderer } = require('electron'); +const { + ipcRenderer +} = require('electron'); setTimeout(connectDAEMON, 500) -function connectDAEMON(){ +function connectDAEMON() { console.log('connecting to DAEMON...') - + //clear message buffer after reconnecting or inital connection msg = ''; - - //daemon_host = document.getElementById("daemon_adress").value - //daemon_port = document.getElementById("daemon_port").value - //daemon_host = '192.168.178.163' - //daemon_port = 3001 - daemon.connect(config.daemon_port, config.daemon_host) //client.setTimeout(5000); } daemon.on('connect', function(data) { - console.log('DAEMON connection established') + console.log('DAEMON connection established') }) daemon.on('error', function(data) { - console.log('DAEMON connection error'); + console.log('DAEMON connection error'); setTimeout(connectDAEMON, 2000) }); @@ -43,154 +39,120 @@ client.on('close', function(data) { */ daemon.on('end', function(data) { - console.log('DAEMON connection ended'); - setTimeout(connectDAEMON, 2000) + console.log('DAEMON connection ended'); + setTimeout(connectDAEMON, 2000) }); //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 - // if socket openend, we are able to run commands - if(daemon.readyState == 'open'){ - //uiMain.setDAEMONconnection('open') - console.log(command) - daemon.write(command + '\n'); - } - - if(daemon.readyState == 'closed'){ - //uiMain.setDAEMONconnection('closed') + // if socket openend, we are able to run commands + if (daemon.readyState == 'open') { + //uiMain.setDAEMONconnection('open') + console.log(command) + daemon.write(command + '\n'); } - if(daemon.readyState == 'opening'){ - //uiMain.setDAEMONconnection('opening') - } - - + if (daemon.readyState == 'closed') { + //uiMain.setDAEMONconnection('closed') + } + + if (daemon.readyState == 'opening') { + //uiMain.setDAEMONconnection('opening') + } + + let Data = { - daemon_connection: daemon.readyState, - }; - ipcRenderer.send('request-update-daemon-connection', Data); + daemon_connection: daemon.readyState, + }; + ipcRenderer.send('request-update-daemon-connection', Data); } +// "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) { - + 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 */ + + /* check if we reached an EOF, if true, clear buffer and parse JSON data */ if (data.endsWith('}')) { - /*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'], - tnc_running_state: data['DAEMON_STATE'][0]['STATUS'] + /*console.log(msg)*/ + try { + /*console.log(msg)*/ + data = JSON.parse(msg) + } catch (e) { + console.log(e); /* "SyntaxError */ + } + msg = ''; + /*console.log("EOF detected!")*/ - }; - ipcRenderer.send('request-update-daemon-state', Data); - - - //input_devices = data['INPUT_DEVICES'] - //uiMain.updateAudioInput(input_devices) + if (data['COMMAND'] == 'DAEMON_STATE') { + let Data = { + input_devices: data['INPUT_DEVICES'], + output_devices: data['OUTPUT_DEVICES'], + tnc_running_state: data['DAEMON_STATE'][0]['STATUS'] - //output_devices = data['OUTPUT_DEVICES'] - //uiMain.updateAudioOutput(output_devices) - - //daemon_state = data['DAEMON_STATE'][0]['STATUS'] - //uiMain.updateTncRunningState(daemon_state) - } - - + }; + ipcRenderer.send('request-update-daemon-state', Data); - - -////// check if EOF ... + } + + ////// check if EOF ... } - - - + + + }); function hexToBytes(hex) { for (var bytes = [], c = 0; c < hex.length; c += 2) - bytes.push(parseInt(hex.substr(c, 2), 16)); + bytes.push(parseInt(hex.substr(c, 2), 16)); return bytes; } - - - - - - exports.getDaemonState = function(){ -//function getDaemonState(){ - command = '{"type" : "GET", "command": "DAEMON_STATE"}' - writeDaemonCommand(command) +exports.getDaemonState = function() { + //function getDaemonState(){ + command = '{"type" : "GET", "command": "DAEMON_STATE"}' + writeDaemonCommand(command) } - - - - - - - - // START TNC // ` `== multi line string -exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, ptt){ - var json_command = JSON.stringify({ - type: 'SET', - command: 'STARTTNC', - parameter: [{ - rx_audio: rx_audio, - tx_audio: tx_audio, - deviceid: deviceid, - deviceport: deviceport, - ptt: ptt +exports.startTNC = function(rx_audio, tx_audio, deviceid, deviceport, ptt) { + var json_command = JSON.stringify({ + type: 'SET', + command: 'STARTTNC', + parameter: [{ + rx_audio: rx_audio, + tx_audio: tx_audio, + deviceid: deviceid, + deviceport: deviceport, + ptt: ptt }] - }) - - - - //console.log(json_command) - writeDaemonCommand(json_command) - - + }) + + + + //console.log(json_command) + writeDaemonCommand(json_command) + + } // STOP TNC -exports.stopTNC = function(){ - command = '{"type" : "SET", "command": "STOPTNC" , "parameter": "---" }' - writeDaemonCommand(command) -} - - - - - - +exports.stopTNC = function() { + command = '{"type" : "SET", "command": "STOPTNC" , "parameter": "---" }' + writeDaemonCommand(command) +} \ No newline at end of file diff --git a/gui/main.js b/gui/main.js index f86fd45e..e4bb797c 100644 --- a/gui/main.js +++ b/gui/main.js @@ -1,67 +1,49 @@ -const { app, BrowserWindow, ipcMain } = require('electron') +const {app,BrowserWindow,ipcMain} = require('electron') const path = require('path') var config = require('./config.json'); - - - let win = null; let data = null; - - - - - -function createWindow () { - - - win = new BrowserWindow({ - width: 1220, - height: 900, - webPreferences: { - //preload: path.join(__dirname, 'preload-main.js'), - preload: require.resolve('./preload-main.js'), - nodeIntegration: true, - contextIsolation: false, - enableRemoteModule: false, //https://stackoverflow.com/questions/53390798/opening-new-window-electron/53393655 https://github.com/electron/remote - } - }) - //open dev tools - win.webContents.openDevTools({ - mode : 'undocked', - activate: true, -}) - win.loadFile('src/index.html') - - - - data = new BrowserWindow({ - height: 900, - width: 600, - parent: win, - webPreferences: { - preload: require.resolve('./preload-data.js'), - nodeIntegration: true, - - } - }) +function createWindow() { + win = new BrowserWindow({ + width: 1220, + height: 920, + webPreferences: { + //preload: path.join(__dirname, 'preload-main.js'), + preload: require.resolve('./preload-main.js'), + nodeIntegration: true, + contextIsolation: false, + enableRemoteModule: false, //https://stackoverflow.com/questions/53390798/opening-new-window-electron/53393655 https://github.com/electron/remote + } + }) //open dev tools - data.webContents.openDevTools({ - mode : 'undocked', - activate: true, -}) - data.loadFile('src/data-module.html') - data.hide() - - - - + win.webContents.openDevTools({ + mode: 'undocked', + activate: true, + }) + win.loadFile('src/index.html') + data = new BrowserWindow({ + height: 900, + width: 600, + parent: win, + webPreferences: { + preload: require.resolve('./preload-data.js'), + nodeIntegration: true, + } + }) + //open dev tools + data.webContents.openDevTools({ + mode: 'undocked', + activate: true, + }) + data.loadFile('src/data-module.html') + data.hide() // Emitted when the window is closed. - win.on('closed', function () { + win.on('closed', function() { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. @@ -69,71 +51,46 @@ function createWindow () { data = null; }) -/* - data.on('closed', function () { - // Dereference the window object, usually you would store windows - // in an array if your app supports multi windows, this is the time - // when you should delete the corresponding element. - }) -*/ - - -// https://stackoverflow.com/questions/44258831/only-hide-the-window-when-closing-it-electron -data.on('close', function (evt) { - evt.preventDefault(); - data.hide() -}); - - - - + /* + data.on('closed', function () { + // Dereference the window object, usually you would store windows + // in an array if your app supports multi windows, this is the time + // when you should delete the corresponding element. + }) + */ + // https://stackoverflow.com/questions/44258831/only-hide-the-window-when-closing-it-electron + data.on('close', function(evt) { + evt.preventDefault(); + data.hide() + }); } - - - - - - - - app.whenReady().then(() => { - createWindow() + createWindow() - app.on('activate', () => { - if (BrowserWindow.getAllWindows().length === 0) { - createWindow() - } - }) + app.on('activate', () => { + if (BrowserWindow.getAllWindows().length === 0) { + createWindow() + } + }) }) app.on('window-all-closed', () => { - if (process.platform !== 'darwin') { - app.quit() - } + if (process.platform !== 'darwin') { + app.quit() + } }) - - - - - - - - - - // IPC HANDLER ipcMain.on('show-data-window', (event, arg) => { data.show() }); - ipcMain.on('request-update-tnc-state', (event, arg) => { win.webContents.send('action-update-tnc-state', arg); data.webContents.send('action-update-tnc-state', arg); @@ -157,50 +114,6 @@ ipcMain.on('request-update-daemon-connection', (event, arg) => { }); ipcMain.on('run-tnc-command', (event, arg) => { - win.webContents.send('run-tnc-command', arg); - -/* - if (arg.command == 'saveMyCall'){ - sock.saveMyCall(arg.callsign) - } - if (arg.command == 'saveMyGrid'){ - sock.saveMyGrid(arg.grid) - } - if (arg.command == 'ping'){ - sock.sendPing(arg.dxcallsign) - } -*/ + win.webContents.send('run-tnc-command', arg); }); - - -/* -ipcMain.on('run-daemon-command', (event, arg) => { - win.webContents.send('run-daemon-command', arg); -*/ -/* - if (arg.command == 'startTNC'){ - daemon.startTNC(arg.rx_audio, arg.tx_audio, arg.deviceid, arg.deviceport, arg.ptt) - - } - if (arg.command == 'stopTNC'){ - daemon.stopTNC() - - } -}); - -*/ - - -//setInterval(sock.getTncState, 500) -//setInterval(daemon.getDaemonState, 500) -/* -setInterval(function(){ - sock.getTncState(); - }, 1000); - */ -/* -setInterval(function(){ - daemon.getDaemonState(); - }, 1000); - */ - \ No newline at end of file + \ No newline at end of file diff --git a/gui/preload-data.js b/gui/preload-data.js index 55a30b67..b4a7e501 100644 --- a/gui/preload-data.js +++ b/gui/preload-data.js @@ -1,38 +1,31 @@ - -const { ipcRenderer } = require('electron'); +const { + ipcRenderer +} = require('electron'); var config = require('./config.json'); - //const sock = require('./sock.js') -//const globals = require('./globals.js') - - - window.addEventListener('DOMContentLoaded', () => { - const replaceText = (selector, text) => { - const element = document.getElementById(selector) - if (element) element.innerText = text - } + const replaceText = (selector, text) => { + const element = document.getElementById(selector) + if (element) element.innerText = text + } - // sendPing button clicked - document.getElementById("sendPing").addEventListener("click", () => { + // sendPing button clicked + document.getElementById("sendPing").addEventListener("click", () => { dxcallsign = document.getElementById("dxCall").value //sock.sendPing(dxcallsign) - - let Data = { + + let Data = { command: "ping", dxcallsign: document.getElementById("dxCall").value }; ipcRenderer.send('run-tnc-command', Data); - }) + }) - document.getElementById("startTransmission").addEventListener("click", () => { - alert("HALLO ") - }) + document.getElementById("startTransmission").addEventListener("click", () => { + alert("HALLO ") + }) - - - }) @@ -40,58 +33,58 @@ window.addEventListener('DOMContentLoaded', () => { ipcRenderer.on('action-update-tnc-state', (event, arg) => { -// PTT STATE - if(arg.ptt_state == 'True'){ - document.getElementById("ptt_state").className = "btn btn-danger"; - } else if(arg.ptt_state == 'False'){ - document.getElementById("ptt_state").className = "btn btn-success"; - } else { - document.getElementById("ptt_state").className = "btn btn-secondary" - } + // PTT STATE + if (arg.ptt_state == 'True') { + document.getElementById("ptt_state").className = "btn btn-danger"; + } else if (arg.ptt_state == 'False') { + document.getElementById("ptt_state").className = "btn btn-success"; + } else { + document.getElementById("ptt_state").className = "btn btn-secondary" + } -// BUSY STATE - if(arg.busy_state == 'BUSY'){ - document.getElementById("busy_state").className = "btn btn-danger"; - } else if(arg.busy_state == 'IDLE'){ - document.getElementById("busy_state").className = "btn btn-success"; - } else { - document.getElementById("busy_state").className = "btn btn-secondary" - } - -// ARQ STATE - if(arg.arq_state == 'DATA'){ - document.getElementById("arq_state").className = "btn btn-warning"; - } else if(arg.arq_state == 'IDLE'){ - document.getElementById("arq_state").className = "btn btn-secondary"; - } else { - document.getElementById("arq_state").className = "btn btn-secondary" - } - -// CHANNEL STATE - if(arg.channel_state == 'RECEIVING_SIGNALLING'){ - document.getElementById("signalling_state").className = "btn btn-success"; - document.getElementById("data_state").className = "btn btn-secondary"; - - } else if(arg.channel_state == 'SENDING_SIGNALLING'){ - document.getElementById("signalling_state").className = "btn btn-danger"; - document.getElementById("data_state").className = "btn btn-secondary"; + // BUSY STATE + if (arg.busy_state == 'BUSY') { + document.getElementById("busy_state").className = "btn btn-danger"; + } else if (arg.busy_state == 'IDLE') { + document.getElementById("busy_state").className = "btn btn-success"; + } else { + document.getElementById("busy_state").className = "btn btn-secondary" + } - } else if(arg.channel_state == 'RECEIVING_DATA'){ - document.getElementById("signalling_state").className = "btn btn-secondary"; - document.getElementById("data_state").className = "btn btn-success"; - - } else if(arg.channel_state == 'SENDING_DATA'){ - document.getElementById("signalling_state").className = "btn btn-secondary"; - document.getElementById("data_state").className = "btn btn-danger"; - } else { - document.getElementById("signalling_state").className = "btn btn-secondary" - document.getElementById("busy_state").className = "btn btn-secondary" + // ARQ STATE + if (arg.arq_state == 'DATA') { + document.getElementById("arq_state").className = "btn btn-warning"; + } else if (arg.arq_state == 'IDLE') { + document.getElementById("arq_state").className = "btn btn-secondary"; + } else { + document.getElementById("arq_state").className = "btn btn-secondary" + } - } - - }); - - - ipcRenderer.on('action-update-data-state', (event, arg) => { + // CHANNEL STATE + if (arg.channel_state == 'RECEIVING_SIGNALLING') { + document.getElementById("signalling_state").className = "btn btn-success"; + document.getElementById("data_state").className = "btn btn-secondary"; - }); \ No newline at end of file + } else if (arg.channel_state == 'SENDING_SIGNALLING') { + document.getElementById("signalling_state").className = "btn btn-danger"; + document.getElementById("data_state").className = "btn btn-secondary"; + + } else if (arg.channel_state == 'RECEIVING_DATA') { + document.getElementById("signalling_state").className = "btn btn-secondary"; + document.getElementById("data_state").className = "btn btn-success"; + + } else if (arg.channel_state == 'SENDING_DATA') { + document.getElementById("signalling_state").className = "btn btn-secondary"; + document.getElementById("data_state").className = "btn btn-danger"; + } else { + document.getElementById("signalling_state").className = "btn btn-secondary" + document.getElementById("busy_state").className = "btn btn-secondary" + + } + +}); + + +ipcRenderer.on('action-update-data-state', (event, arg) => { + +}); \ No newline at end of file diff --git a/gui/preload-main.js b/gui/preload-main.js index 54057120..69945c92 100644 --- a/gui/preload-main.js +++ b/gui/preload-main.js @@ -2,284 +2,197 @@ const sock = require('./sock.js') const daemon = require('./daemon.js') const configPath = './config.json' const config = require(configPath); -const { ipcRenderer } = require('electron'); +const { + ipcRenderer +} = require('electron'); const fs = require('fs'); - - - - - - - - - // START INTERVALL COMMAND EXECUTION FOR STATES -setInterval(daemon.getDaemonState, 1000) -setInterval(sock.getTncState, 250) -setInterval(sock.getDataState, 500) -setInterval(sock.getHeardStations, 500) - +setInterval(daemon.getDaemonState, 1000) +setInterval(sock.getTncState, 250) +setInterval(sock.getDataState, 500) +setInterval(sock.getHeardStations, 500) // UPDATE FFT DEMO - updateFFT = function(fft){ - var fft = Array.from({length: 2048}, () => Math.floor(Math.random() * 10)); - spectrum.addData(fft); +updateFFT = function(fft) { + var fft = Array.from({ + length: 2048 + }, () => Math.floor(Math.random() * 10)); + spectrum.addData(fft); } -setInterval(updateFFT, 250) - - - - - - - - - - +setInterval(updateFFT, 250) +// WINDOW LISTENER window.addEventListener('DOMContentLoaded', () => { -// LOAD SETTINGS -document.getElementById("tnc_adress").value = config.tnc_host -document.getElementById("tnc_port").value = config.tnc_port -document.getElementById("myCall").value = config.mycall -document.getElementById("myGrid").value = config.mygrid + // LOAD SETTINGS + document.getElementById("tnc_adress").value = config.tnc_host + document.getElementById("tnc_port").value = config.tnc_port + document.getElementById("myCall").value = config.mycall + document.getElementById("myGrid").value = config.mygrid - - - -// Create spectrum object on canvas with ID "waterfall" + // Create spectrum object on canvas with ID "waterfall" global.spectrum = new Spectrum( "waterfall", { spectrumPercent: 20 - }); + }); -// on change port and host + // on change port and host - document.getElementById("tnc_adress").addEventListener("change", () => { - console.log(document.getElementById("tnc_adress").value) - config.tnc_host = document.getElementById("tnc_adress").value - config.daemon_host = document.getElementById("tnc_adress").value - fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); + document.getElementById("tnc_adress").addEventListener("change", () => { + console.log(document.getElementById("tnc_adress").value) + config.tnc_host = document.getElementById("tnc_adress").value + config.daemon_host = document.getElementById("tnc_adress").value + fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); }); - document.getElementById("tnc_port").addEventListener("change", () => { - config.tnc_port = document.getElementById("tnc_port").value - config.daemon_port = document.getElementById("tnc_port").value + 1 - fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); + document.getElementById("tnc_port").addEventListener("change", () => { + config.tnc_port = document.getElementById("tnc_port").value + config.daemon_port = document.getElementById("tnc_port").value + 1 + fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); -}); + }); - - // tnc_host = document.getElementById("tnc_adress").value - //tnc_port = document.getElementById("tnc_port").value - - - - // saveMyCall button clicked - document.getElementById("saveMyCall").addEventListener("click", () => { + // saveMyCall button clicked + document.getElementById("saveMyCall").addEventListener("click", () => { callsign = document.getElementById("myCall").value config.mycall = callsign fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); - sock.saveMyCall(callsign) - /* - let Data = { - command: "saveMyCall", - callsign: document.getElementById("myCall").value - }; - ipcRenderer.send('run-tnc-command', Data); - - */ - - - }); + }); - // saveMyGrid button clicked - document.getElementById("saveMyGrid").addEventListener("click", () => { + // saveMyGrid button clicked + document.getElementById("saveMyGrid").addEventListener("click", () => { grid = document.getElementById("myGrid").value config.mygrid = grid fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); - sock.saveMyGrid(grid) - /* - let Data = { - command: "saveMyGrid", - grid: document.getElementById("myGrid").value - }; - ipcRenderer.send('run-tnc-command', Data); -*/ + }); - + // startPing button clicked - document.getElementById("sendPing").addEventListener("click", () => { + document.getElementById("sendPing").addEventListener("click", () => { dxcallsign = document.getElementById("dxCall").value sock.sendPing(dxcallsign) - /* - let Data = { - command: "saveMyCall", - callsign: document.getElementById("myCall").value - }; - ipcRenderer.send('run-tnc-command', Data); - - */ - - - }); - - // sendCQ button clicked - document.getElementById("sendCQ").addEventListener("click", () => { - sock.sendCQ() + }); - - - }); - - - - - + // sendCQ button clicked + document.getElementById("sendCQ").addEventListener("click", () => { + sock.sendCQ() + }); - // startTNC button clicked - document.getElementById("startTNC").addEventListener("click", () => { + // startTNC button clicked + document.getElementById("startTNC").addEventListener("click", () => { var rx_audio = document.getElementById("audio_input_selectbox").value - var tx_audio = document.getElementById("audio_output_selectbox").value + var tx_audio = document.getElementById("audio_output_selectbox").value var deviceid = document.getElementById("hamlib_deviceid").value var deviceport = document.getElementById("hamlib_deviceport").value var ptt = document.getElementById("hamlib_ptt").value daemon.startTNC(rx_audio, tx_audio, deviceid, deviceport, ptt) - setTimeout( function() { sock.saveMyCall(config.mycall); }, 5000 ); - setTimeout( function() { sock.saveMyGrid(config.mygrid); }, 5000 ); + setTimeout(function() { + sock.saveMyCall(config.mycall); + }, 5000); + setTimeout(function() { + sock.saveMyGrid(config.mygrid); + }, 5000); + }) - /* - let Data = { - command: "startTNC", - rx_audio : document.getElementById("audio_input_selectbox").value, - tx_audio : document.getElementById("audio_output_selectbox").value, - deviceid : document.getElementById("hamlib_deviceid").value, - deviceport : document.getElementById("hamlib_deviceport").value, - ptt : document.getElementById("hamlib_ptt").value, - }; - ipcRenderer.send('run-daemon-command', Data); - */ - - - }) - - // stopTNC button clicked - document.getElementById("stopTNC").addEventListener("click", () => { + // stopTNC button clicked + document.getElementById("stopTNC").addEventListener("click", () => { daemon.stopTNC() - /* let Data = { - command: "stopTNC", + }) + + // openDataModule button clicked + document.getElementById("openDataModule").addEventListener("click", () => { + //data.show() + let Data = { + message: "Hello World !" }; - ipcRenderer.send('run-daemon-command', Data); - */ - }) - - // openDataModule button clicked - document.getElementById("openDataModule").addEventListener("click", () => { - //data.show() - let Data = { - message: "Hello World !" - }; - ipcRenderer.send('show-data-window', Data); - }) - - - - - - - + ipcRenderer.send('show-data-window', Data); + }) }) - - - - - ipcRenderer.on('action-update-tnc-state', (event, arg) => { -// PTT STATE - if(arg.ptt_state == 'True'){ - document.getElementById("ptt_state").className = "btn btn-danger"; - console.log("PTT TRUE!!!") + // PTT STATE + if (arg.ptt_state == 'True') { + document.getElementById("ptt_state").className = "btn btn-danger"; + console.log("PTT TRUE!!!") - } else if(arg.ptt_state == 'False'){ - document.getElementById("ptt_state").className = "btn btn-success"; - } else { - document.getElementById("ptt_state").className = "btn btn-secondary" - } + } else if (arg.ptt_state == 'False') { + document.getElementById("ptt_state").className = "btn btn-success"; + } else { + document.getElementById("ptt_state").className = "btn btn-secondary" + } -// BUSY STATE - if(arg.busy_state == 'BUSY'){ - document.getElementById("busy_state").className = "btn btn-danger"; - } else if(arg.busy_state == 'IDLE'){ - document.getElementById("busy_state").className = "btn btn-success"; - } else { - document.getElementById("busy_state").className = "btn btn-secondary" - } - -// ARQ STATE - if(arg.arq_state == 'DATA'){ - document.getElementById("arq_state").className = "btn btn-warning"; - } else if(arg.arq_state == 'IDLE'){ - document.getElementById("arq_state").className = "btn btn-secondary"; - } else { - document.getElementById("arq_state").className = "btn btn-secondary" - } - -// RMS - 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"; - document.getElementById("data_state").className = "btn btn-secondary"; - - } else if(arg.channel_state == 'SENDING_SIGNALLING'){ - document.getElementById("signalling_state").className = "btn btn-danger"; - document.getElementById("data_state").className = "btn btn-secondary"; + // BUSY STATE + if (arg.busy_state == 'BUSY') { + document.getElementById("busy_state").className = "btn btn-danger"; + } else if (arg.busy_state == 'IDLE') { + document.getElementById("busy_state").className = "btn btn-success"; + } else { + document.getElementById("busy_state").className = "btn btn-secondary" + } - } else if(arg.channel_state == 'RECEIVING_DATA'){ - document.getElementById("signalling_state").className = "btn btn-secondary"; - document.getElementById("data_state").className = "btn btn-success"; - - } else if(arg.channel_state == 'SENDING_DATA'){ - document.getElementById("signalling_state").className = "btn btn-secondary"; - document.getElementById("data_state").className = "btn btn-danger"; - } else { - document.getElementById("signalling_state").className = "btn btn-secondary" - document.getElementById("busy_state").className = "btn btn-secondary" + // ARQ STATE + if (arg.arq_state == 'DATA') { + document.getElementById("arq_state").className = "btn btn-warning"; + } else if (arg.arq_state == 'IDLE') { + document.getElementById("arq_state").className = "btn btn-secondary"; + } else { + document.getElementById("arq_state").className = "btn btn-secondary" + } - } - -// SET FREQUENCY - document.getElementById("frequency").value = arg.frequency + // RMS + document.getElementById("rms_level").setAttribute("aria-valuenow", arg.rms_level) + document.getElementById("rms_level").setAttribute("style", "width:" + arg.rms_level + "%;") -// SET MODE - document.getElementById("mode").value = arg.mode + // CHANNEL STATE + if (arg.channel_state == 'RECEIVING_SIGNALLING') { + document.getElementById("signalling_state").className = "btn btn-success"; + document.getElementById("data_state").className = "btn btn-secondary"; -// SET BANDWITH - document.getElementById("bandwith").value = arg.bandwith - }); + } else if (arg.channel_state == 'SENDING_SIGNALLING') { + document.getElementById("signalling_state").className = "btn btn-danger"; + document.getElementById("data_state").className = "btn btn-secondary"; + + } else if (arg.channel_state == 'RECEIVING_DATA') { + document.getElementById("signalling_state").className = "btn btn-secondary"; + document.getElementById("data_state").className = "btn btn-success"; + + } else if (arg.channel_state == 'SENDING_DATA') { + document.getElementById("signalling_state").className = "btn btn-secondary"; + document.getElementById("data_state").className = "btn btn-danger"; + } else { + document.getElementById("signalling_state").className = "btn btn-secondary" + document.getElementById("busy_state").className = "btn btn-secondary" + + } + + // SET FREQUENCY + document.getElementById("frequency").value = arg.frequency + + // SET MODE + document.getElementById("mode").value = arg.mode + + // SET BANDWITH + document.getElementById("bandwith").value = arg.bandwith +}); ipcRenderer.on('action-update-daemon-state', (event, arg) => { -// 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) { document.getElementById("audio_input_selectbox").innerHTML = "" - for (i = 0; i < arg.input_devices.length; i++) { + for (i = 0; i < arg.input_devices.length; i++) { var option = document.createElement("option"); option.text = arg.input_devices[i]['NAME']; option.value = arg.input_devices[i]['ID']; @@ -289,94 +202,71 @@ ipcRenderer.on('action-update-daemon-state', (event, arg) => { } // UPDATE AUDIO OUTPUT - if(document.getElementById("audio_output_selectbox").length != arg.output_devices.length){ + if (document.getElementById("audio_output_selectbox").length != arg.output_devices.length) { document.getElementById("audio_output_selectbox").innerHTML = "" - for (i = 0; i < arg.output_devices.length; i++) { + for (i = 0; i < arg.output_devices.length; i++) { var option = document.createElement("option"); option.text = arg.output_devices[i]['NAME']; option.value = arg.output_devices[i]['ID']; document.getElementById("audio_output_selectbox").add(option); } } - -// TNC RUNNING STATE -document.getElementById("tnc_running_state").innerHTML = arg.tnc_running_state; - if(arg.tnc_running_state == "running"){ - document.getElementById('hamlib_deviceid').disabled = true - document.getElementById('hamlib_deviceport').disabled = true - document.getElementById('hamlib_ptt').disabled = true - document.getElementById('audio_input_selectbox').disabled = true - document.getElementById('audio_output_selectbox').disabled = true - document.getElementById('stopTNC').disabled = false - document.getElementById('startTNC').disabled = true - document.getElementById('myCall').disabled = false - document.getElementById('saveMyCall').disabled = false - document.getElementById('myGrid').disabled = false - document.getElementById('saveMyGrid').disabled = false - + + // TNC RUNNING STATE + document.getElementById("tnc_running_state").innerHTML = arg.tnc_running_state; + if (arg.tnc_running_state == "running") { + document.getElementById('hamlib_deviceid').disabled = true + document.getElementById('hamlib_deviceport').disabled = true + document.getElementById('hamlib_ptt').disabled = true + document.getElementById('audio_input_selectbox').disabled = true + document.getElementById('audio_output_selectbox').disabled = true + document.getElementById('stopTNC').disabled = false + document.getElementById('startTNC').disabled = true + document.getElementById('myCall').disabled = false + document.getElementById('saveMyCall').disabled = false + document.getElementById('myGrid').disabled = false + document.getElementById('saveMyGrid').disabled = false + } else { - document.getElementById('hamlib_deviceid').disabled = false - document.getElementById('hamlib_deviceport').disabled = false - document.getElementById('hamlib_ptt').disabled = false - document.getElementById('audio_input_selectbox').disabled = false - document.getElementById('audio_output_selectbox').disabled = false - document.getElementById('stopTNC').disabled = true - document.getElementById('startTNC').disabled = false - document.getElementById('myCall').disabled = true - document.getElementById('saveMyCall').disabled = true - document.getElementById('myGrid').disabled = true - document.getElementById('saveMyGrid').disabled = true + document.getElementById('hamlib_deviceid').disabled = false + document.getElementById('hamlib_deviceport').disabled = false + document.getElementById('hamlib_ptt').disabled = false + document.getElementById('audio_input_selectbox').disabled = false + document.getElementById('audio_output_selectbox').disabled = false + document.getElementById('stopTNC').disabled = true + document.getElementById('startTNC').disabled = false + document.getElementById('myCall').disabled = true + document.getElementById('saveMyCall').disabled = true + document.getElementById('myGrid').disabled = true + document.getElementById('saveMyGrid').disabled = true } - - }); +}); ipcRenderer.on('action-update-daemon-connection', (event, arg) => { + if (arg.daemon_connection == 'open') { + document.getElementById("daemon_connection_state").className = "btn btn-success"; + } + if (arg.daemon_connection == 'opening') { + document.getElementById("daemon_connection_state").className = "btn btn-warning"; + } + if (arg.daemon_connection == 'closed') { + document.getElementById("daemon_connection_state").className = "btn btn-danger"; + } - if(arg.daemon_connection == 'open'){ - document.getElementById("daemon_connection_state").className = "btn btn-success"; - } - if(arg.daemon_connection == 'opening'){ - document.getElementById("daemon_connection_state").className = "btn btn-warning"; - } - if(arg.daemon_connection == 'closed'){ - document.getElementById("daemon_connection_state").className = "btn btn-danger"; - } +}); - }); - - - - - - ipcRenderer.on('run-tnc-command', (event, arg) => { - if (arg.command == 'saveMyCall'){ + +ipcRenderer.on('run-tnc-command', (event, arg) => { + if (arg.command == 'saveMyCall') { sock.saveMyCall(arg.callsign) } - if (arg.command == 'saveMyGrid'){ + if (arg.command == 'saveMyGrid') { sock.saveMyGrid(arg.grid) } - if (arg.command == 'ping'){ - sock.sendPing(arg.dxcallsign) - } - }); - - -/* - ipcRenderer.on('run-daemon-command', (event, arg) => { - if (arg.command == 'startTNC'){ - daemon.startTNC(arg.rx_audio, arg.tx_audio, arg.deviceid, arg.deviceport, arg.ptt) - + if (arg.command == 'ping') { + sock.sendPing(arg.dxcallsign) } - if (arg.command == 'stopTNC'){ - daemon.stopTNC() - - } - }); - -*/ - - - \ No newline at end of file +}); \ No newline at end of file diff --git a/gui/sock.js b/gui/sock.js index 487d35ab..e0cd9212 100644 --- a/gui/sock.js +++ b/gui/sock.js @@ -1,55 +1,45 @@ var net = require('net'); var config = require('./config.json'); +const { + ipcRenderer +} = require('electron'); -const { ipcRenderer } = require('electron'); - -//var client = new net.Socket(); var client = new net.Socket(); var msg = ''; // Current message, per connection. - setTimeout(connectTNC, 3000) - - - - -function connectTNC(){ -//exports.connectTNC = function(){ +setTimeout(connectTNC, 3000) +function connectTNC() { + //exports.connectTNC = function(){ console.log('connecting to TNC...') - + //clear message buffer after reconnecting or inital connection msg = ''; - - console.log(exports) - - //tnc_host = '192.168.178.163' - //tnc_port = 3000 - client.connect(config.tnc_port, config.tnc_host) - //client.setTimeout(5000); + client.connect(config.tnc_port, config.tnc_host) } client.on('connect', function(data) { - console.log('TNC connection established') + console.log('TNC connection established') }) client.on('error', function(data) { - console.log('TNC connection error'); + console.log('TNC connection error'); - let Data = { - busy_state: "-", - arq_state: "-", - channel_state: "-", - frequency : "-", - mode: "-", - bandwith: "-", - rms_level : 0 + let Data = { + busy_state: "-", + arq_state: "-", + channel_state: "-", + frequency: "-", + mode: "-", + bandwith: "-", + rms_level: 0 + + }; + ipcRenderer.send('request-update-tnc-state', Data); - }; - ipcRenderer.send('request-update-tnc-state', Data); - setTimeout(connectTNC, 2000) - // setTimeout( function() { exports.connectTNC(tnc_host, tnc_port); }, 2000 ); + // setTimeout( function() { exports.connectTNC(tnc_host, tnc_port); }, 2000 ); }); @@ -61,184 +51,159 @@ client.on('close', function(data) { */ client.on('end', function(data) { - console.log('TNC connection ended'); + console.log('TNC connection ended'); //setTimeout(connectTNC, 2000) - setTimeout(connectTNC, 0) + setTimeout(connectTNC, 0) - // setTimeout( function() { exports.connectTNC(tnc_host, tnc_port); }, 2000 ); + // setTimeout( function() { exports.connectTNC(tnc_host, tnc_port); }, 2000 ); }); //exports.writeTncCommand = function(command){ - writeTncCommand = function(command){ - +writeTncCommand = function(command) { + console.log(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(client.readyState == 'open'){ - //uiMain.setTNCconnection('open') - client.write(command + '\n'); - } - - if(client.readyState == 'closed'){ - //uiMain.setTNCconnection('closed') - console.log("CLOSED!!!!!") + // if socket openend, we are able to run commands + if (client.readyState == 'open') { + //uiMain.setTNCconnection('open') + client.write(command + '\n'); } - if(client.readyState == 'opening'){ - //uiMain.setTNCconnection('opening') - console.log("OPENING!!!!!") + if (client.readyState == 'closed') { + //uiMain.setTNCconnection('closed') + console.log("CLOSED!!!!!") + } - } + if (client.readyState == 'opening') { + //uiMain.setTNCconnection('opening') + console.log("OPENING!!!!!") + } } - + client.on('data', function(data) { -/* -stackoverflow.com questions 9070700 nodejs-net-createserver-large-amount-of-data-coming-in -*/ - + /* + stackoverflow.com questions 9070700 nodejs-net-createserver-large-amount-of-data-coming-in + */ + data = data.toString('utf8'); // convert data to string msg += data.toString('utf8'); // append data to buffer so we can stick long data together - -/* - if (msg.charCodeAt(msg.length - 1) == 0) { - client.emit('message', msg.substring(0, msg.length - 1)); - msg = ''; - console.log("END OF FILE") - } - */ - - /* - if (msg.startsWith('{"COMMAND":')) { - msg = ''; - msg += data.toString('utf8'); - console.log("BOF detected!") - } - */ - - // check if we reached an EOF, if true, clear buffer and parse JSON data - if (data.endsWith('}')) { - //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'] == 'TNC_STATE'){ - let Data = { - ptt_state: data['PTT_STATE'], - busy_state: data['TNC_STATE'], - arq_state: data['ARQ_STATE'], - channel_state: data['CHANNEL_STATE'], - frequency : data['FREQUENCY'], - mode: data['MODE'], - bandwith: data['BANDWITH'], - rms_level : (data['AUDIO_RMS']/1000)*100 - }; - console.log(Data) - ipcRenderer.send('request-update-tnc-state', 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'){ - let Data = { - dxcallsign: data['DXCALLSIGN'], - dxgrid: data['DXGRID'], - timestamp: data['TIMESTAMP'], - datatype: data['DATATYPE'], - }; - console.log(Data) - ipcRenderer.send('request-update-heard-stations', Data); - } - - - - // check if EOF ... + // check if we reached an EOF, if true, clear buffer and parse JSON data + if (data.endsWith('}')) { + //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'] == 'TNC_STATE') { + let Data = { + ptt_state: data['PTT_STATE'], + busy_state: data['TNC_STATE'], + arq_state: data['ARQ_STATE'], + channel_state: data['CHANNEL_STATE'], + frequency: data['FREQUENCY'], + mode: data['MODE'], + bandwith: data['BANDWITH'], + rms_level: (data['AUDIO_RMS'] / 1000) * 100 + }; + console.log(Data) + ipcRenderer.send('request-update-tnc-state', 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') { + let Data = { + dxcallsign: data['DXCALLSIGN'], + dxgrid: data['DXGRID'], + timestamp: data['TIMESTAMP'], + datatype: data['DATATYPE'], + }; + console.log(Data) + ipcRenderer.send('request-update-heard-stations', Data); + } + + + // check if EOF ... } - - - + + + }); function hexToBytes(hex) { for (var bytes = [], c = 0; c < hex.length; c += 2) - bytes.push(parseInt(hex.substr(c, 2), 16)); + bytes.push(parseInt(hex.substr(c, 2), 16)); return bytes; } - - - - //Save myCall - exports.saveMyCall = function(callsign){ - command = '{"type" : "SET", "command": "MYCALLSIGN" , "parameter": "'+ callsign +'" }' - writeTncCommand(command) +exports.saveMyCall = function(callsign) { + command = '{"type" : "SET", "command": "MYCALLSIGN" , "parameter": "' + callsign + '" }' + writeTncCommand(command) } // Save myGrid - exports.saveMyGrid = function(grid){ - command = '{"type" : "SET", "command": "MYGRID" , "parameter": "'+ grid +'" }' - writeTncCommand(command) +exports.saveMyGrid = function(grid) { + command = '{"type" : "SET", "command": "MYGRID" , "parameter": "' + grid + '" }' + writeTncCommand(command) } //Get TNC State - exports.getTncState = function(){ - command = '{"type" : "GET", "command": "TNC_STATE"}'; - writeTncCommand(command) +exports.getTncState = function() { + command = '{"type" : "GET", "command": "TNC_STATE"}'; + writeTncCommand(command) } //Get DATA State - exports.getDataState = function(){ - command = '{"type" : "GET", "command": "DATA_STATE"}'; - writeTncCommand(command) +exports.getDataState = function() { + command = '{"type" : "GET", "command": "DATA_STATE"}'; + writeTncCommand(command) } //Get Heard Stations - exports.getHeardStations = function(){ - command = '{"type" : "GET", "command": "HEARD_STATIONS"}'; - writeTncCommand(command) +exports.getHeardStations = function() { + command = '{"type" : "GET", "command": "HEARD_STATIONS"}'; + writeTncCommand(command) } // Send Ping - exports.sendPing = function(dxcallsign){ - command = '{"type" : "PING", "command" : "PING", "dxcallsign" : "' + dxcallsign + '"}' - writeTncCommand(command) +exports.sendPing = function(dxcallsign) { + command = '{"type" : "PING", "command" : "PING", "dxcallsign" : "' + dxcallsign + '"}' + writeTncCommand(command) } // Send CQ - exports.sendCQ = function(){ - command = '{"type" : "CQ", "command" : "CQCQCQ"}' - writeTncCommand(command) +exports.sendCQ = function() { + command = '{"type" : "CQ", "command" : "CQCQCQ"}' + writeTncCommand(command) } \ No newline at end of file