Added local ODAS launch
authorCedric Godin <cedric.godin@usherbrooke.ca>
Thu, 18 Jan 2018 21:05:03 +0000 (16:05 -0500)
committerCedric Godin <cedric.godin@usherbrooke.ca>
Thu, 18 Jan 2018 21:05:03 +0000 (16:05 -0500)
main.js
package.json
ressources/js/interface.js
ressources/js/tcp_link.js
server.js [deleted file]
views/live_data.html

diff --git a/main.js b/main.js
index b21eb9894374261c4525373e9ea3a34aefd11a0b..e1de87e91b4fc6f4688293f769eda9c480460c98 100644 (file)
--- 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')
+})
index 102b93b5011275caf053ae252d45f441bf0394ab..cc4fcdc83c28c1ffa2431a66fc7451a26aecd580 100644 (file)
@@ -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"
   }
 }
index 5bf9d75dc94201b0411763f856cb38beeace1c58..09a2da5566e93a99e3373560dea7a67662f296b1 100644 (file)
@@ -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();
 
             });
         });
index c280b708d2a4e11208173999f6eab5b373e92c71..45a8a130a4d1976ded8c3a023d381ab454728490 100644 (file)
@@ -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 (file)
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);
-
-}
index 10bc3a308fbaffc184086e8e8f46a6a433f48a4c..b759be6df62c0c81b8c6d92322095e97e1aa7223 100644 (file)
@@ -47,7 +47,7 @@
                         <div class="well" id="system-monitor">
                             <form>
                                 <div class="form-group">
-                                    <h4>System Monitor</h4>
+                                    <h4>Local System Monitor</h4>
                                 </div>
                                 <div class="form-group">
                                     <div class="input-group">
                                         <span class="input-group-addon">Memory Usage</span><span class="form-control">{{system.mem}}</span>
                                     </div>
                                 </div>
+                                <div class="form-group">
+                                    <div class="input-group">
+                                        <span class="input-group-addon">IP</span><span class="form-control">{{system.ip}}</span>
+                                    </div>
+                                </div>
+                              </form>
+                            </div>
+                            <div class="well" id="odas-local">
+                              <form>
+                                <div class="form-group">
+                                    <h4>ODAS Control</h4>
+                                </div>
+                                <div class="form-group">
+                                  <label>ODAS Core</label>
+                                  <input type="text" class="form-control" id="odas-core-path" placeholder="Not specified"/>
+                                </div>
+                                <div class="form-group">
+                                  <label>ODAS Config</label>
+                                  <input type="text" class="form-control" id="odas-config-path" placeholder="Not specified"/>
+                                </div>
                             </form>
+                            <button class="btn btn-primary" id="odas-btn">Launch ODAS</button>
+                        </div>
+                        <div class="well" id="odas-remote">
+                          <h4>ODAS Control</h4>
+                          <p>ODAS Remote is connected</p>
                         </div>
                     </div>
                     <div class="col-md-7 layout">