Refactored main to split server and odas in separated files
authorCedric Godin <cedric.godin@usherbrooke.ca>
Fri, 26 Jan 2018 16:31:09 +0000 (11:31 -0500)
committerCedric Godin <cedric.godin@usherbrooke.ca>
Fri, 26 Jan 2018 16:31:09 +0000 (11:31 -0500)
main.js
odas.js [new file with mode: 0644]
odas_interface/web_interface.ios [deleted file]
package.json
ressources/js/audio_stream.js
ressources/js/odas_launcher.js [new file with mode: 0644]
ressources/js/tcp_link.js
servers.js [new file with mode: 0644]
views/live_data.html

diff --git a/main.js b/main.js
index e1de87e91b4fc6f4688293f769eda9c480460c98..548b969ac64a2a6e5ff45e20e7d39d725209756f 100644 (file)
--- a/main.js
+++ b/main.js
@@ -2,7 +2,7 @@ const electron = require('electron')
 
 // Module to control application life.
 const app = electron.app
-app.commandLine.appendSwitch('--ignore-gpu-blacklist');
+app.commandLine.appendSwitch('--ignore-gpu-blacklist');   // Allows Web GL on Ubuntu
 
 // Module to create native browser window.
 const BrowserWindow = electron.BrowserWindow
@@ -12,11 +12,11 @@ const url = require('url')
 
 // Keep a global reference of the window object, if you don't, the window will
 // be closed automatically when the JavaScript object is garbage collected.
-let mainWindow
+let odasStudio = {}
 
 function createWindow () {
   // Create the browser window.
-  mainWindow = new BrowserWindow({width: 1200, height: 800,
+  odasStudio.mainWindow = new BrowserWindow({width: 1200, height: 800,
     'web-preferences': {
               'web-security': false,
               "webgl": true
@@ -24,7 +24,7 @@ function createWindow () {
 
 
   // and load the index.html of the app.
-  mainWindow.loadURL(url.format({
+  odasStudio.mainWindow.loadURL(url.format({
     pathname: path.join(__dirname, 'views/live_data.html'),
     protocol: 'file:',
     slashes: true
@@ -34,11 +34,11 @@ function createWindow () {
   //mainWindow.webContents.openDevTools()
 
   // Emitted when the window is closed.
-  mainWindow.on('closed', function () {
+  odasStudio.mainWindow.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.
-    mainWindow = null
+    odasStudio.mainWindow = null
   })
 }
 
@@ -59,7 +59,7 @@ app.on('window-all-closed', function () {
 app.on('activate', function () {
   // On OS X it's common to re-create a window in the app when the
   // dock icon is clicked and there are no other windows open.
-  if (mainWindow === null) {
+  if (odasStudio.mainWindow === null) {
     createWindow()
   }
 })
@@ -67,136 +67,8 @@ app.on('activate', function () {
 // In this file you can include the rest of your app's specific main process
 // code. You can also put them in separate files and require them here.
 
-/*
- * Create TCP server for source tracking
- */
+const sockets = require('./servers.js')
+odasStudio.odas = require('./odas.js')
 
-// Load modules
-const StringDecoder = require('string_decoder').StringDecoder;
-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);
-
-    try {
-      mainWindow.webContents.send('newTracking',str);
-      if(typeof odas_process == 'undefined') {
-        mainWindow.webContents.send('remote-online');
-      }
-    }
-
-    catch(err) {
-      console.log('Window was closed');
-    }
-
-  }
-
-  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);
-  }
-}
-
-
-/*
- * 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);
-
-    try {
-      mainWindow.webContents.send('newPotential',str);
-      if(typeof odas_process == 'undefined') {
-        mainWindow.webContents.send('remote-online', conn.remoteAddress);
-      }
-    }
-
-    catch(err) {
-
-      console.log('Window was closed');
-    }
-
-  }
-
-  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')
-})
+sockets.startTrackingServer(odasStudio)
+sockets.startPotentialServer(odasStudio)
diff --git a/odas.js b/odas.js
new file mode 100644 (file)
index 0000000..010383b
--- /dev/null
+++ b/odas.js
@@ -0,0 +1,29 @@
+/*
+ * ODAS Control
+ */
+
+exports.odas_process
+
+const electron = require('electron')
+const ipcMain = electron.ipcMain
+const child_process = require('child_process')
+
+ipcMain.on('launch-odas', function(event, core, config) {
+
+  console.log('received launch command')
+  console.log(core)
+  console.log(config)
+
+  exports.odas_process = child_process.spawn(core, ['-c', config])
+
+  event.sender.send('launched-odas', true)
+})
+
+
+ipcMain.on('stop-odas', function(event) {
+
+  exports.odas_process.kill('SIGINT')
+  exports.odas_process = undefined
+
+  console.log('received stop command')
+})
diff --git a/odas_interface/web_interface.ios b/odas_interface/web_interface.ios
deleted file mode 100644 (file)
index 8749432..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-# Input
-
-input:
-{
-   
-#   raw: { type = "file"; path = "in.raw"; format = "bin16"; }
-    raw: { type = "soundcard"; card = 1; device = 0; format = "bin16"; }
-
-};
-
-# Output:
-
-output:
-{
-    raw:
-    (
-#        { type = "file"; path = "recorded_raw_16.raw"; format = "bin16"; }
-#        { type = "file"; path = "recorded_raw_32.raw"; format = "bin32"; },
-#        { type = "file"; path = "recorded_raw_24.raw"; format = "bin24"; }
-    );
-
-    mapping:
-    (
-#        { type = "file"; path = "recorded_mapping_16.raw"; format = "bin16"; }
-#        { type = "file"; path = "recorded_mapping_32.raw"; format = "bin32"; }
-    );
-
-    resample:
-    (
-#        { type = "file"; path = "recorded_resample_16.raw"; format = "bin16"; }
-#        { type = "file"; path = "recorded_resample_32.raw"; format = "bin32"; }
-    );
-
-    stft:
-    (
-#        { type = "file"; path = "recorded_stft.bin"; format = "float"; }
-    );
-
-    ssl:
-    (
-        { type = "socket"; ip = "127.0.0.1"; port = 9001; format = "json"; }
-    );
-
-    sst:
-    (
-        { type = "socket"; ip = "127.0.0.1"; port = 9000; format = "json"; }
-    );
-
-    sss:
-    (
-#        { type = "file"; path = "separated_stft.bin"; format = "float"; }
-    );
-
-    sspf:
-    (
-#        { type = "file"; path = "postfiltered_stft.bin"; format = "float"; }
-    );
-
-}
index cc4fcdc83c28c1ffa2431a66fc7451a26aecd580..21f962032b962d083a045158608a84f513d0be1c 100644 (file)
@@ -1,26 +1,22 @@
 {
-  "name": "electron-quick-start",
+  "name": "ODAS_Desktop",
   "version": "1.0.0",
-  "description": "A minimal Electron application",
+  "description": "A desktop GUI for the ODAS library",
   "main": "main.js",
   "scripts": {
     "start": "electron ."
   },
-  "repository": "https://github.com/electron/electron-quick-start",
-  "keywords": [
-    "Electron",
-    "quick",
-    "start",
-    "tutorial",
-    "demo"
-  ],
-  "author": "GitHub",
-  "license": "CC0-1.0",
-  "devDependencies": {
+  "author": "Cedric Godin",
+  "license": "GPL-3.0",
+  "dependencies": {
     "electron": "~1.7.8",
-    "express": "^4.15.2",
-    "express-ws": "^3.0.0",
     "ip": "^1.1.5",
-    "systeminformation": "^3.17.3"
+    "speaker": "^0.4.0",
+    "systeminformation": "^3.17.3",
+    "wav": "^1.0.1"
+  },
+  "repository" :
+  { "type" : "git"
+  , "url" : "https://github.com/introlab/odas_web.git"
   }
 }
index 63c13467daed225aff88c33402d48d6bd5b867a6..bfae0bda0533e11200a8d452b95ee33c3bba3cc4 100644 (file)
@@ -10,48 +10,64 @@ var streamStarted = false;
 
 var watchdog = null;
 
+/*
+ * Audio socket
+ */
+
+
+const processAudio = function(msg) {
+
+     var reader = new FileReader();
+
+    reader.onloadend = function() {
+        document.dispatchEvent(new CustomEvent('audioData', { 'detail': reader.result}));
+    };
+
+    reader.readAsArrayBuffer(msg.data);
+};
+
 document.addEventListener('audioData',function(ev) {
-    
+
     var data = new Int16Array(ev.detail);
     var dataNormalized = new Float32Array(data.length);
-    
+
     for(var i=0; i<data.length; i++) {
         dataNormalized[i] = (data[i]/32768.0);
     }
-    
+
     if(!streamStarted)
         startTime = audioCtx.currentTime + startBuffer;
-    
+
     streamStarted = true;
     scheduleBuffer(dataNormalized);
-    
+
     clearInterval(watchdog);
     watchdog = setInterval(function(){
-        
+
         streamStarted = false;
         console.log('Watchdog');
         clearInterval(watchdog);
-        
+
     }, 10000);
 });
 
 function scheduleBuffer(audio) {
-    
+
     var buffer = audioCtx.createBuffer(channels, audio.length, sampleRate);
     buffer.copyToChannel(audio,0,0);
-    
+
     var source = audioCtx.createBufferSource();
     source.buffer = buffer;
-    
+
     var gain = audioCtx.createGain();
     gain.gain.value = 15;
-    
+
     source.connect(gain);
     gain.connect(audioCtx.destination);
-    
+
     source.start(startTime);
-    
+
     console.log(`Scheduled in ${startTime-audioCtx.currentTime}`);
-    
+
     startTime += buffer.duration;
 }
diff --git a/ressources/js/odas_launcher.js b/ressources/js/odas_launcher.js
new file mode 100644 (file)
index 0000000..790e721
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ * 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'
index 45a8a130a4d1976ded8c3a023d381ab454728490..5e411b07efa965d72be2a86215fbd2f37c6bf6c1 100644 (file)
@@ -191,150 +191,3 @@ document.addEventListener('clearChart', function(e){
 
     },500);
 });
-
-/*
- * Audio socket
- */
-
-
-const processAudio = function(msg) {
-
-     var reader = new FileReader();
-
-    reader.onloadend = function() {
-        document.dispatchEvent(new CustomEvent('audioData', { 'detail': reader.result}));
-    };
-
-    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/servers.js b/servers.js
new file mode 100644 (file)
index 0000000..e1b9edf
--- /dev/null
@@ -0,0 +1,113 @@
+// Load modules
+const StringDecoder = require('string_decoder').StringDecoder;
+var net = require('net');
+
+let trackingServer
+let potentialServer
+
+/*
+ * Create TCP server for source tracking
+ */
+
+ exports.startTrackingServer = (odasStudio) => {
+
+   trackingServer = net.createServer();
+   trackingServer.on('connection', handleConnection);
+
+   trackingServer.listen(9000, function() {
+     console.log('server listening to %j', trackingServer.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);
+
+       try {
+         odasStudio.mainWindow.webContents.send('newTracking',str);
+         if(typeof odasStudio.odas.odas_process == 'undefined') {
+           odasStudio.mainWindow.webContents.send('remote-online');
+         }
+       }
+
+       catch(err) {
+         console.log('Window was closed');
+       }
+
+     }
+
+     function onConnClose() {
+       console.log('connection from %s closed', remoteAddress);
+       odasStudio.mainWindow.webContents.send('remote-offline');
+     }
+
+     function onConnError(err) {
+       console.log('Connection %s error: %s', remoteAddress, err.message);
+     }
+   }
+
+ }
+
+
+/*
+ * Create TCP server for potential sources
+ */
+
+ exports.startPotentialServer = (odasStudio) => {
+
+   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);
+
+       try {
+         odasStudio.mainWindow.webContents.send('newPotential',str);
+         if(typeof odasStudio.odas.odas_process == 'undefined') {
+           odasStudio.mainWindow.webContents.send('remote-online', conn.remoteAddress);
+         }
+       }
+
+       catch(err) {
+
+         console.log('Window was closed');
+       }
+
+     }
+
+     function onConnClose() {
+       console.log('connection from %s closed', remoteAddress);
+       odasStudio.mainWindow.webContents.send('remote-offline');
+     }
+
+     function onConnError(err) {
+       console.log('Connection %s error: %s', remoteAddress, err.message);
+     }
+   }
+ }
index b759be6df62c0c81b8c6d92322095e97e1aa7223..e9bea13b0ec837dbafacd942628bc8b2d7f23789 100644 (file)
         <script src="./../ressources/js/source_sphere.js"></script>
 
         <script src="./../ressources/js/audio_stream.js"></script>
+        <script src="./../ressources/js/odas_launcher.js"></script>
 
         <script src="./../ressources/js/legal.js"></script>
     </body>