From 15dd098ecbbf641edcdc6bf7bae0b1ca89557f62 Mon Sep 17 00:00:00 2001 From: Cedric Godin Date: Thu, 18 Jan 2018 16:05:03 -0500 Subject: [PATCH] Added local ODAS launch --- main.js | 40 ++++- package.json | 1 + ressources/js/interface.js | 4 +- ressources/js/tcp_link.js | 131 +++++++++++++++++ server.js | 290 ------------------------------------- views/live_data.html | 27 +++- 6 files changed, 200 insertions(+), 293 deletions(-) delete mode 100644 server.js diff --git a/main.js b/main.js index b21eb98..e1de87e 100644 --- a/main.js +++ b/main.js @@ -31,7 +31,7 @@ function createWindow () { })) // Open the DevTools. - mainWindow.webContents.openDevTools() + //mainWindow.webContents.openDevTools() // Emitted when the window is closed. mainWindow.on('closed', function () { @@ -99,6 +99,9 @@ function handleConnection(conn) { try { mainWindow.webContents.send('newTracking',str); + if(typeof odas_process == 'undefined') { + mainWindow.webContents.send('remote-online'); + } } catch(err) { @@ -109,6 +112,7 @@ function handleConnection(conn) { function onConnClose() { console.log('connection from %s closed', remoteAddress); + mainWindow.webContents.send('remote-offline'); } function onConnError(err) { @@ -145,6 +149,9 @@ function handlePotConnection(conn) { try { mainWindow.webContents.send('newPotential',str); + if(typeof odas_process == 'undefined') { + mainWindow.webContents.send('remote-online', conn.remoteAddress); + } } catch(err) { @@ -156,9 +163,40 @@ function handlePotConnection(conn) { function onConnClose() { console.log('connection from %s closed', remoteAddress); + mainWindow.webContents.send('remote-offline'); } function onConnError(err) { console.log('Connection %s error: %s', remoteAddress, err.message); } } + + +/* + * ODAS Control + */ + +const ipcMain = electron.ipcMain +const child_process = require('child_process') + +let odas_process + +ipcMain.on('launch-odas', function(event, core, config) { + + console.log('received launch command') + console.log(core) + console.log(config) + + odas_process = child_process.spawn(core, ['-c', config]) + + event.sender.send('launched-odas', true) +}) + + +ipcMain.on('stop-odas', function(event) { + + odas_process.kill('SIGINT') + odas_process = undefined + + console.log('received stop command') +}) diff --git a/package.json b/package.json index 102b93b..cc4fcdc 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "electron": "~1.7.8", "express": "^4.15.2", "express-ws": "^3.0.0", + "ip": "^1.1.5", "systeminformation": "^3.17.3" } } diff --git a/ressources/js/interface.js b/ressources/js/interface.js index 5bf9d75..09a2da5 100644 --- a/ressources/js/interface.js +++ b/ressources/js/interface.js @@ -135,11 +135,12 @@ var filterManager = new Vue({ var systemMonitor = new Vue({ el: '#system-monitor', data: { - system : {cpu:'0 %',mem:0,temp:0} + system : {cpu:'0 %',mem:0,temp:0, ip:'0.0.0.0'} } }); var si = require('systeminformation'); +var ip = require('ip'); function updateSi() { // Gather params @@ -157,6 +158,7 @@ function updateSi() { // Gather params systemMonitor.system.cpu = sysInfo.cpu.toPrecision(3).toString() + ' %'; systemMonitor.system.mem = sysInfo.mem.toPrecision(2).toString() + ' %'; systemMonitor.system.temp = sysInfo.temp.toPrecision(3).toString() + ' °C'; + systemMonitor.system.ip = ip.address(); }); }); diff --git a/ressources/js/tcp_link.js b/ressources/js/tcp_link.js index c280b70..45a8a13 100644 --- a/ressources/js/tcp_link.js +++ b/ressources/js/tcp_link.js @@ -207,3 +207,134 @@ const processAudio = function(msg) { reader.readAsArrayBuffer(msg.data); }; + + +/* + * ODAS Launcher + */ + +const dialog = require('electron').remote.dialog + +const core_path_field = document.getElementById('odas-core-path') +const config_path_field = document.getElementById('odas-config-path') +const launch_btn = document.getElementById('odas-btn') + +const odas_local_well = document.getElementById('odas-local') +const odas_remote_well = document.getElementById('odas-remote') + +let odas_core_path +let odas_config_path + +if(localStorage.odas_core_path) { + odas_core_path = localStorage.odas_core_path + core_path_field.value = odas_core_path +} + +if(localStorage.odas_config_path) { + odas_config_path = localStorage.odas_config_path + config_path_field.value = odas_config_path +} + + +core_path_field.addEventListener('click', function() { + + dialog.showOpenDialog( + { properties: ['openFile'], title: 'Browse ODAS Core executable'}, + function (files) { + + if(files) { + let path = files[0] + odas_core_path = path + core_path_field.value = odas_core_path + localStorage.odas_core_path = odas_core_path + } + }) +}) + + +config_path_field.addEventListener('click', function() { + + dialog.showOpenDialog( + { properties: ['openFile'], title: 'Browse ODAS config file'}, + function (files) { + + if(files) { + let path = files[0] + odas_config_path = path + config_path_field.value = odas_config_path + localStorage.odas_config_path = odas_config_path + } + }) +}) + + +const launchOdas = function() { + + if(typeof odas_core_path == 'undefined') { + + dialog.showMessageBox({ + type:'info', + buttons:['OK'], + title:'Error', + message:'Core path not set.', + detail:'Please set ODAS Core path before launching ODAS.' + }) + + return + } + + + if(typeof odas_config_path == 'undefined') { + + dialog.showMessageBox({ + type:'info', + buttons:['OK'], + title:'Error', + message:'Config file path not set.', + detail:'Please set ODAS configuration file path before launching ODAS.' + }) + + return + } + + ipcRenderer.send('launch-odas', odas_core_path, odas_config_path) + launch_btn.removeEventListener('click', launchOdas) + launch_btn.innerHTML = 'Launching...' +} + + +const stopOdas = function() { + + ipcRenderer.send('stop-odas') + + launch_btn.innerHTML = 'Launch ODAS' + launch_btn.removeEventListener('click', stopOdas) + launch_btn.addEventListener('click', launchOdas) + + setTimeout(function() {document.dispatchEvent(new Event('clearChart'));}, 1000) +} + +launch_btn.addEventListener('click', launchOdas) + + +ipcRenderer.on('launched-odas', function(sender, ok) { + + if(ok) { + launch_btn.innerHTML = 'Stop ODAS' + launch_btn.addEventListener('click', stopOdas) + } +}) + +ipcRenderer.on('remote-online', function(ip) { + odas_remote_well.style.display = 'block' + odas_local_well.style.display = 'none' + +}) + +ipcRenderer.on('remote-offline', function() { + odas_remote_well.style.display = 'none' + odas_local_well.style.display = 'block' + setTimeout(function() {document.dispatchEvent(new Event('clearChart'));}, 1000) +}) + +odas_remote_well.style.display = 'none' diff --git a/server.js b/server.js deleted file mode 100644 index e2f5d49..0000000 --- a/server.js +++ /dev/null @@ -1,290 +0,0 @@ -/* - * Create web server - */ - -// Load modules -var express = require('express'); -var path = require('path'); -var events = require('events'); -var StringDecoder = require('string_decoder').StringDecoder; - -// Prep app and event manager -var app = express(); -var expressWs = require('express-ws')(app); - -var eventEmitter = new events.EventEmitter(); - -// Deliver static ressources -app.use(express.static('ressources')); - -// Main page route -app.get('/', function(req, res) { - res.sendFile(path.join(__dirname+'/views/live_data.html')); -}); - -// Config page route -app.get('/config', function(req, res) { - res.sendFile(path.join(__dirname+'/views/configure.html')); -}); - -// Legal page route -app.get('/legal', function(req, res) { - res.sendFile(path.join(__dirname+'/views/legal.html')); -}); - -// Websocket to stream tracking -app.ws('/tracking',function(ws, req) { - - // Sends new data to client - var sendData = function(data) { - - try { - ws.send(data); - } - - catch(e) { - console.warn('Socket closed'); - } - }; - - // Register client to event - eventEmitter.on('newTracking',sendData); - console.log('registered tracking'); - - // Remove listener when connection closes - ws.on('close', function() { - eventEmitter.removeListener('newTracking',sendData); - }); - -}); - -// Websocket to stream potential sources -app.ws('/potential',function(ws, req) { - - // Sends new data to client - var sendData = function(data) { - - try { - ws.send(data); - } - - catch(e) { - console.warn('Socket closed'); - } - }; - - // Register client to event - eventEmitter.on('newPotential',sendData); - console.log('registered potential'); - - // Remove listener when connection closes - ws.on('close', function() { - eventEmitter.removeListener('newPotential',sendData); - }); - -}); - -// Websocket to stream system information -app.ws('/system.info',function(ws, req) { - - // Sends new data to client - var sendData = function(data) { - - try { - ws.send(JSON.stringify(data)); - } - - catch(e) { - console.warn('Socket closed'); - } - }; - - // Register client to event - eventEmitter.on('sysInfo',sendData); - console.log('registered sysInfo'); - - // Remove listener when connection closes - ws.on('close', function() { - eventEmitter.removeListener('sysInfo',sendData); - }); - -}); - -// Websocket to stream audio -app.ws('/audio',function(ws, req) { - - // Sends new data to client - var sendData = function(data) { - - try { - ws.send(data); - console.log(`Sent audio on ${new Date().getTime()}`); - } - - catch(e) { - console.warn('Socket closed'); - } - }; - - // Register client to event - eventEmitter.on('newAudio',sendData); - console.log('registered audio'); - - // Remove listener when connection closes - ws.on('close', function() { - eventEmitter.removeListener('newAudio',sendData); - }); - -}); - -// Redirect to main page -app.use(function(req, res, next){ - res.sendFile(path.join(__dirname+'/live_data.html')); -}); - -// Starts web server -app.listen(8080); -console.log('Listening...'); - - -/* - * Create TCP server for source tracking - */ - -// Load modules -var net = require('net'); - -var server = net.createServer(); -server.on('connection', handleConnection); - -server.listen(9000, function() { - console.log('server listening to %j', server.address()); -}); - -function handleConnection(conn) { - var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; - console.log('new client connection from %s', remoteAddress); - - conn.on('data', onConnData); - conn.once('close', onConnClose); - conn.on('error', onConnError); - - function onConnData(d) { - - var decoder = new StringDecoder(); - - // Decode received string - var str = decoder.write(d); - - eventEmitter.emit('newTracking',str); - - } - - function onConnClose() { - console.log('connection from %s closed', remoteAddress); - } - - function onConnError(err) { - console.log('Connection %s error: %s', remoteAddress, err.message); - } -} - - -/* - * Create TCP server for potential sources - */ - -var potentialServer = net.createServer(); -potentialServer.on('connection', handlePotConnection); - -potentialServer.listen(9001, function() { - console.log('server listening to %j', potentialServer.address()); -}); - -function handlePotConnection(conn) { - var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; - console.log('new client connection from %s', remoteAddress); - - conn.on('data', onConnData); - conn.once('close', onConnClose); - conn.on('error', onConnError); - - function onConnData(d) { - - var decoder = new StringDecoder(); - - // Decode received string - var str = decoder.write(d); - - eventEmitter.emit('newPotential',str); - - } - - function onConnClose() { - console.log('connection from %s closed', remoteAddress); - } - - function onConnError(err) { - console.log('Connection %s error: %s', remoteAddress, err.message); - } -} - - -/* - * System monitor - */ - -// Load modules - - -/* - * Audio stream server - */ - -var audioServer = net.createServer(); -audioServer.on('connection', handleAudioConnection); - -audioServer.listen(10001, function() { - console.log('server listening to %j', audioServer.address()); -}); - -function handleAudioConnection(conn) { - - var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; - console.log('new client connection from %s', remoteAddress); - - conn.on('data', onConnData); - conn.once('close', onConnClose); - conn.on('error', onConnError); - - function onConnData(d) { - - console.log(`Received audio on ${new Date().getTime()}`); - eventEmitter.emit('newAudio',d); - } - - function onConnClose() { - console.log('connection from %s closed', remoteAddress); - } - - function onConnError(err) { - console.log('Connection %s error: %s', remoteAddress, err.message); - } -} - -var dummyPorts = []; - -for(n = 2; n<=16; n++) { - - var dummy = net.createServer(); - - dummy.on('connection', function(conn) { - // Dummy - }); - - dummy.listen(10000+n); - console.log(`Dummy listen on ${10000+n}`); - - dummyPorts.push(dummy); - -} diff --git a/views/live_data.html b/views/live_data.html index 10bc3a3..b759be6 100644 --- a/views/live_data.html +++ b/views/live_data.html @@ -47,7 +47,7 @@
-

System Monitor

+

Local System Monitor

@@ -64,7 +64,32 @@ Memory Usage{{system.mem}}
+
+
+ IP{{system.ip}} +
+
+
+
+
+
+
+

ODAS Control

+
+
+ + +
+
+ + +
+ +
+
+

ODAS Control

+

ODAS Remote is connected

-- 2.34.1