From 5c32a93708e34cee086dcc8bfa3dbe534aadccc2 Mon Sep 17 00:00:00 2001 From: Cedric Godin Date: Tue, 16 Jan 2018 11:10:08 -0500 Subject: [PATCH] Version desktop fonctionnelle avec nouvel ODAS --- main.js | 164 ++++++++++++++++++++++++++++++ package.json | 25 +++-- ressources/js/bootstrap_slider.js | 10 +- ressources/js/interface.js | 56 +++++++--- ressources/js/legal.js | 34 +++++++ ressources/js/source_sphere.js | 154 ++++++++++++++-------------- ressources/js/tcp_link.js | 137 ++++++++++--------------- server.js | 108 ++++++++------------ views/legal.html | 33 ++---- views/live_data.html | 73 ++++++------- 10 files changed, 484 insertions(+), 310 deletions(-) create mode 100644 main.js create mode 100644 ressources/js/legal.js diff --git a/main.js b/main.js new file mode 100644 index 0000000..b21eb98 --- /dev/null +++ b/main.js @@ -0,0 +1,164 @@ +const electron = require('electron') + +// Module to control application life. +const app = electron.app +app.commandLine.appendSwitch('--ignore-gpu-blacklist'); + +// Module to create native browser window. +const BrowserWindow = electron.BrowserWindow + +const path = require('path') +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 + +function createWindow () { + // Create the browser window. + mainWindow = new BrowserWindow({width: 1200, height: 800, + 'web-preferences': { + 'web-security': false, + "webgl": true + }}) + + + // and load the index.html of the app. + mainWindow.loadURL(url.format({ + pathname: path.join(__dirname, 'views/live_data.html'), + protocol: 'file:', + slashes: true + })) + + // Open the DevTools. + mainWindow.webContents.openDevTools() + + // Emitted when the window is closed. + 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 + }) +} + +// This method will be called when Electron has finished +// initialization and is ready to create browser windows. +// Some APIs can only be used after this event occurs. +app.on('ready', createWindow) + +// Quit when all windows are closed. +app.on('window-all-closed', function () { + // On OS X it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (process.platform !== 'darwin') { + app.quit() + } +}) + +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) { + createWindow() + } +}) + +// 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 + */ + +// 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); + } + + catch(err) { + console.log('Window was closed'); + } + + } + + 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); + + try { + mainWindow.webContents.send('newPotential',str); + } + + catch(err) { + + console.log('Window was closed'); + } + + } + + function onConnClose() { + console.log('connection from %s closed', remoteAddress); + } + + function onConnError(err) { + console.log('Connection %s error: %s', remoteAddress, err.message); + } +} diff --git a/package.json b/package.json index f576bd5..102b93b 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,23 @@ { - "name": "odas-web", - "version": "0.2.0", - "description": "Web interface for ODAS platform", - "repository": "MARS on GitHub/introlab", - "license" : "MIT", - "dependencies": { + "name": "electron-quick-start", + "version": "1.0.0", + "description": "A minimal Electron application", + "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": { + "electron": "~1.7.8", "express": "^4.15.2", "express-ws": "^3.0.0", "systeminformation": "^3.17.3" diff --git a/ressources/js/bootstrap_slider.js b/ressources/js/bootstrap_slider.js index 1b242ae..76a98ee 100644 --- a/ressources/js/bootstrap_slider.js +++ b/ressources/js/bootstrap_slider.js @@ -1,8 +1,10 @@ /*! ======================================================= - VERSION 9.8.0 + VERSION 9.8.0 ========================================================= */ "use strict"; +var rangeSlider; + var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /*! ========================================================= @@ -82,7 +84,7 @@ var windowIsDefined = (typeof window === "undefined" ? "undefined" : _typeof(win } // Reference to Slider constructor - var Slider; + var Slider; (function ($) { @@ -1898,7 +1900,7 @@ var windowIsDefined = (typeof window === "undefined" ? "undefined" : _typeof(win } })($); + rangeSlider = new Slider('#range_slider', {}); + return Slider; }); - -var rangeSlider = new Slider('#range_slider', {}); \ No newline at end of file diff --git a/ressources/js/interface.js b/ressources/js/interface.js index 35fe13a..5bf9d75 100644 --- a/ressources/js/interface.js +++ b/ressources/js/interface.js @@ -26,18 +26,18 @@ function energyIsInRange(e) { // Get gradiant index corresponding to energy function scaleEnergy(e) { - + var min = rangeSlider.getValue()[0]; var max = rangeSlider.getValue()[1]; - + // Rescale according to minimal value - + e -= min; max -= min; - + if( e > max) e = max; - + // Round energy between 0 and 10 return Math.round((e/max)*10); } @@ -49,12 +49,12 @@ function scaleEnergy(e) { // Single source data class Source { constructor(index) { - + // Web UI info this.index = index; this.rgbValueString = rgbValueStrings[index]; this.selected = true; - + // Source info this.id = null; this.active = false; @@ -77,15 +77,15 @@ class PotentialSource { // Single data frame class DataFrame { constructor() { - + this.timestamp = null; this.ptimestamp = null; - + this.sources = []; rgbValueStrings.forEach(function (color,i) { this.sources.push(new Source(i)); }.bind(this)); - + this.potentialSources = []; } } @@ -105,7 +105,7 @@ var sourceManager = new Vue({ methods : { showHide: function(e) { document.dispatchEvent(new Event('update-selection')); - } + } } }); @@ -127,10 +127,42 @@ var filterManager = new Vue({ } }); +/* + * System Monitor + */ + // System monitor model var systemMonitor = new Vue({ el: '#system-monitor', data: { system : {cpu:'0 %',mem:0,temp:0} } -}); \ No newline at end of file +}); + +var si = require('systeminformation'); + +function updateSi() { // Gather params + + var sysInfo = {cpu:0,mem:0,temp:0}; + + si.currentLoad(function(data) { + sysInfo.cpu = data.currentload; + + si.mem(function(data) { + sysInfo.mem = (data.active/data.total)*100; + + si.cpuTemperature(function(data) { + sysInfo.temp = data.main; + + 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'; + + }); + }); + }); + +} + +// Schedule update +setInterval(updateSi,500); diff --git a/ressources/js/legal.js b/ressources/js/legal.js new file mode 100644 index 0000000..0f1855d --- /dev/null +++ b/ressources/js/legal.js @@ -0,0 +1,34 @@ +const electron = require('electron') + +const BrowserWindow = electron.remote.BrowserWindow + +const path = require('path') +const url = require('url') + +let legalWindow + +function openLegalWindow () { + // Create the browser window. + legalWindow = new BrowserWindow({width: 800, height: 600, show: false}) + + + // and load the index.html of the app. + legalWindow.loadURL(url.format({ + pathname: path.join(__dirname, 'legal.html'), + protocol: 'file:', + slashes: true + })) + + legalWindow.once('ready-to-show', function () { + + legalWindow.show() + }) + + // Emitted when the window is closed. + legalWindow.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. + legalWindow = null + }) +} diff --git a/ressources/js/source_sphere.js b/ressources/js/source_sphere.js index b3d459d..fa5bd05 100644 --- a/ressources/js/source_sphere.js +++ b/ressources/js/source_sphere.js @@ -17,13 +17,13 @@ animate(); */ function init() { - + // Canvas canvas = document.getElementById("sphere"); - + canvas.style.height = '100%'; canvas.style.width = '100%'; - + // Renderer renderer = new THREE.WebGLRenderer( { antialias: true, canvas: canvas }); renderer.setClearColor( 0xffffff ); @@ -39,112 +39,112 @@ function init() { controls.rotateSpeed = 5; controls.noZoom = true; controls.noPan = true; - + controls.dynamicDampingFactor = 0.2; - + // Scene scene = new THREE.Scene(); // Sphere - + // Top - var sphereGeometry = new THREE.SphereGeometry( 1, 10, 10,0,Math.PI,0,Math.PI); + var sphereGeometry = new THREE.SphereGeometry( 1, 10, 10,0,Math.PI,0,Math.PI); var sphereMaterial = new THREE.MeshBasicMaterial({ color:0x0000ff, transparent: true, opacity:0.25}); - + var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial ); //scene.add(sphere); - + var sphereMaterial = new THREE.MeshBasicMaterial({ color:0x0000ff, transparent: true, opacity:0.25, wireframe:true}); - + var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial ); scene.add(sphere); - + // Bottom - var sphereGeometry = new THREE.SphereGeometry( 1, 10, 10,Math.PI,Math.PI,0,Math.PI); + var sphereGeometry = new THREE.SphereGeometry( 1, 10, 10,Math.PI,Math.PI,0,Math.PI); var sphereMaterial = new THREE.MeshBasicMaterial({ color:0x8d4f1a, transparent: true, opacity:0.5}); - + var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial ); //scene.add(sphere); - + var sphereMaterial = new THREE.MeshBasicMaterial({ color:0x8d4f1a, transparent: true, opacity:0.5, wireframe:true}); - + var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial ); scene.add(sphere); - + // Ground var groundGeometry = new THREE.CircleGeometry(1,10); var groundMaterial = new THREE.MeshBasicMaterial({ color:0x328327, transparent: true, opacity:0.5}); groundMaterial.side = THREE.DoubleSide; - + var ground = new THREE.Mesh(groundGeometry, groundMaterial); scene.add(ground); - + // Cube var cubeGeometry = new THREE.BoxGeometry(0.5,0.5,0.1); var cubeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe:false}); - + var cube = new THREE.Mesh(cubeGeometry,cubeMaterial); - + scene.add(cube); - + // Sources sourceGroup = new THREE.Group(); var sourceGeometry = new THREE.SphereGeometry( 0.08, 10, 10); - + rgbValueStrings.forEach(function(color) { - + var sourceMaterial = new THREE.MeshBasicMaterial( {color: color, wireframe:false} ); var source = new THREE.Mesh( sourceGeometry, sourceMaterial ); source.visible = false; - + sources3D.push(source); sourceGroup.add(source); }); scene.add( sourceGroup ); - + // Potential sources potGroup = new THREE.Group(); scene.add(potGroup); - + /* * Draw sub canvas with axis */ - + // Canvas subCanvas = document.getElementById("axis"); - + // Renderer subRenderer = new THREE.WebGLRenderer( { antialias: true, canvas: subCanvas }); subRenderer.setClearColor(0xffffff); subRenderer.setSize(80,80); - + // Scene subScene = new THREE.Scene(); - + // Camera subCamera = new THREE.PerspectiveCamera(90,1,0.1,5); subCamera.position.z = 3.5; subCamera.up = camera.up; - + // Axis var axis = new THREE.AxisHelper(1.2); scene.add( axis ); var subAxis = new THREE.AxisHelper(1.2); subScene.add( subAxis ); - + // Add axes labels var loader = new THREE.FontLoader(); - loader.load( 'fonts/helvetiker_regular.typeface.json', function ( helFont ) { - + loader.load( './../ressources/fonts/helvetiker_regular.typeface.json', function ( helFont ) { + var textGeo = new THREE.TextGeometry('X', { size: 0.3, height: 0.001, curveSegments: 6, font: helFont, - style: "normal" + style: "normal" }); var textMaterial = new THREE.MeshBasicMaterial({ color: "rgb(255,0,0)" }); @@ -154,13 +154,13 @@ function init() { labelX.position.y = 0.01; labelX.position.z = 0; subScene.add(labelX); - + var textGeo = new THREE.TextGeometry('Y', { size: 0.3, height: 0.001, curveSegments: 6, font: helFont, - style: "normal" + style: "normal" }); var textMaterial = new THREE.MeshBasicMaterial({ color: "rgb(0,255,0)" }); @@ -170,13 +170,13 @@ function init() { labelY.position.y = 1.2; labelY.position.z = 0; subScene.add(labelY); - + var textGeo = new THREE.TextGeometry('Z', { size: 0.3, height: 0.001, curveSegments: 6, font: helFont, - style: "normal" + style: "normal" }); var textMaterial = new THREE.MeshBasicMaterial({ color: "rgb(0,0,255)" }); @@ -186,7 +186,7 @@ function init() { labelZ.position.y = 0.01; labelZ.position.z = 1.2; subScene.add(labelZ); - + } ); window.addEventListener( 'resize', onWindowResize, false ); @@ -196,11 +196,11 @@ function init() { * Resize canvas and renderer when page is resized */ function onWindowResize() { - + camera.aspect = canvas.offsetWidth/canvas.offsetHeight ; camera.updateProjectionMatrix(); renderer.setSize(canvas.offsetWidth,canvas.offsetHeight); - + canvas.style.height = '100%'; canvas.style.width = '100%'; } @@ -210,12 +210,12 @@ function onWindowResize() { */ function animate() { - + requestAnimationFrame( animate ); - + if(controls.enabled) controls.update(); - + try { subCamera.rotation.copy(camera.rotation); subCamera.position.copy(camera.position); @@ -224,21 +224,21 @@ function animate() { labelY.rotation.copy(camera.rotation); labelZ.rotation.copy(camera.rotation); } - + catch(err) { // Scene not loaded } - + finally { render(); } } function render() { - + renderer.render( scene, camera ); subRenderer.render( subScene, subCamera); - + } /* @@ -257,15 +257,15 @@ rgbValueStrings.forEach(function(color) { // Update on new event document.addEventListener('tracking', function(e) { - + // Clear older trail pixels if(sources3DTrail.length > 0) { - + sources3DTrail.forEach(function(src,i) { src.life--; - + if(src.life < 1) { // Dispose of pixel if life is over - + src.obj.material.dispose(); src.obj.geometry.dispose(); src.obj.parent.remove(src.obj); @@ -274,19 +274,19 @@ document.addEventListener('tracking', function(e) { } }) } - + // Update source on sphere currentFrame.sources.forEach(function(source,index) { - + sources3D[index].visible = filterManager.showSources && source.active && source.selected && !(source.x == 0 && source.y == 0 && source.z == 0); - + sources3D[index].position.x = source.x; sources3D[index].position.y = source.y; sources3D[index].position.z = source.z; - + // Add a pixel to the tail if the source is visible if( sources3D[index].visible ) { - + var geo = new THREE.Geometry(); var ps = new THREE.Vector3(source.x,source.y,source.z); geo.vertices.push(ps); @@ -300,10 +300,10 @@ document.addEventListener('tracking', function(e) { // Update visibility when selection is changed document.addEventListener('update-selection',function(e){ - + currentFrame.sources.forEach(function(source,index) { sources3D[index].visible = filterManager.showSources && source.active && source.selected && !(source.x == 0 && source.y == 0 && source.z == 0); - }); + }); }); /* @@ -322,13 +322,13 @@ heatmapColors.forEach(function(color) { // Update on 'potential' event document.addEventListener('potential', function(e) { - + // Remove old trail points if(potSources3D.length > 0) { - + potSources3D.forEach(function(src,i) { src.life--; - + if(src.life < 1) { // Dispose if life is over src.obj.material.dispose(); src.obj.geometry.dispose(); @@ -338,23 +338,23 @@ document.addEventListener('potential', function(e) { } }) } - + // Add potential source to sphere if potential source filter is selected if(currentFrame.potentialSources.length > 0 && filterManager.showPotentials) { - + currentFrame.potentialSources.forEach(function(s) { - + if ( energyIsInRange(s.e) ) { // Add source if source's energy's in range var geo = new THREE.Geometry(); var ps = new THREE.Vector3(s.x,s.y,s.z); geo.vertices.push(ps); - + var sys = new THREE.Points(geo,potSourceMaterial[scaleEnergy(s.e)]); potSources3D.push({obj:sys,life:50}); potGroup.add(sys); } }); - + } }); @@ -363,27 +363,27 @@ document.addEventListener('potential', function(e) { */ document.addEventListener('clearChart',function(e) { - + if(potSources3D.length > 0) { - + potSources3D.forEach(function(src,i) { src.obj.material.dispose(); src.obj.geometry.dispose(); src.obj.parent.remove(src.obj); }); } - + potSources3D = []; - + if(sources3DTrail.length > 0) { - + sources3DTrail.forEach(function(src,i) { src.obj.material.dispose(); src.obj.geometry.dispose(); src.obj.parent.remove(src.obj); }); } - + sources3DTrail = []; }); @@ -393,12 +393,12 @@ document.addEventListener('clearChart',function(e) { */ var viewFront = function() { - + camera.up.set(0,0,1); - + camera.position.x = 0; camera.position.y = -camOffset; camera.position.z = 0; - + controls.reset(); }; diff --git a/ressources/js/tcp_link.js b/ressources/js/tcp_link.js index b312f5c..c280b70 100644 --- a/ressources/js/tcp_link.js +++ b/ressources/js/tcp_link.js @@ -2,46 +2,29 @@ * Web Socket connection to server */ -// Generate websocket server URL -var loc = window.location, new_uri, sys_uri, pot_uri; - -if (loc.protocol === "https:") { - new_uri = "wss:"; -} else { - new_uri = "ws:"; -} - -audio_uri = new_uri + "//" + loc.host + "/audio"; -sys_uri = new_uri + "//" + loc.host + "/system.info"; -pot_uri = new_uri + "//" + loc.host + "/potential"; -new_uri += "//" + loc.host + "/tracking"; + const ipcRenderer = require('electron').ipcRenderer /* * Tracking data socket */ -// Open socket and create parser -var socket = new WebSocket(new_uri); -console.log(new_uri); - // Update current data with received data var indexMap = {}; -socket.onmessage = function(msg) { - - try { - var strData = msg.data; - var data = JSON.parse(strData); +const processTracking = function(event, msg) { + + try { + var data = JSON.parse(msg); } catch(err) { // Can't parse frame console.error(err); - console.log(strData); + console.log(msg); return; } - + if(Math.abs(data.timeStamp - currentFrame.timestamp) > 1) console.warn('Frame skipped ' + data.timeStamp.toString()); @@ -53,21 +36,21 @@ socket.onmessage = function(msg) { indexPool.push(index); }); var hasNewSource = false; - + data.src = data.src.filter(function(s) { return s.id !== 0; }); - + if(data.src) { // If frame contains sources data.src.forEach(function(src) { // Remove still used index from the pool - + if(typeof(indexMap[src.id])!='undefined') { // If source is not new indexPool.splice(indexPool.indexOf(indexMap[src.id]),1); //console.log(indexPool); } }); - + data.src.forEach(function(src) { // Update sources if(typeof(indexMap[src.id])!='undefined') { // Source is already registered @@ -91,75 +74,73 @@ socket.onmessage = function(msg) { currentFrame.sources[newMap[src.id]].active = !(src.x==0 && src.y==0 && src.z==0); }); - + } - + indexMap = newMap; - + indexPool.forEach(function(index) { // Clear unused source slot - + currentFrame.sources[index].id = null; - + currentFrame.sources[index].x = null; currentFrame.sources[index].y = null; currentFrame.sources[index].z = null; - + currentFrame.sources[index].active = false; currentFrame.sources[index].selected = true; }); // Trigger update document.dispatchEvent(new Event('tracking')); - + if(hasNewSource) document.dispatchEvent(new Event('update-selection')); }; +ipcRenderer.on('newTracking', processTracking) + /* * Potential sources socket */ -var potentialSocket = new WebSocket(pot_uri); -console.log(pot_uri); - // Update current data with received potential sources -potentialSocket.onmessage = function(msg) { - - try { - var strData = msg.data; - var data = JSON.parse(strData); +const processPotential = function(event, msg) { + + try { + var data = JSON.parse(msg); } catch(err) { // Can't parse frame console.error(err); - console.log(strData); + console.log(msg); return; } - + if(Math.abs(data.timeStamp - currentFrame.ptimestamp) > 1) console.warn('Frame skipped ' + data.timeStamp.toString()); currentFrame.ptimestamp = data.timeStamp; currentFrame.potentialSources = []; - + if(data.src) { // If frame contains sources - + data.src.forEach(function(source) { - + var newSource = new PotentialSource(); - + newSource.e = source.E; newSource.x = source.x; newSource.y = source.y; newSource.z = source.z; - + currentFrame.potentialSources.push(newSource); - + }); - + } // Trigger update @@ -167,76 +148,62 @@ potentialSocket.onmessage = function(msg) { }; +ipcRenderer.on('newPotential', processPotential) + /* * Frame reset when no data is received */ document.addEventListener('clearChart', function(e){ - + currentFrame.timestamp = 0; currentFrame.ptimestamp = 0; - + currentFrame.sources.forEach(function(source){ source.id = null; - + source.x = null; source.y = null; source.z = null; - + source.active = false; source.selected = true; }); - + currentFrame.potentialSources = []; - + document.dispatchEvent(new Event('tracking')); document.dispatchEvent(new Event('potential')); document.dispatchEvent(new Event('update-selection')); - + sourceManager.showPotentials = true; document.dispatchEvent(new Event('potential-visibility')); - + viewFront(); - + var req = setInterval(function() { - + document.dispatchEvent(new Event('request-chart')); - + hasPotential = false; clearInterval(req); console.log('UI cleaned'); - + },500); }); -/* - * System info socket - */ - -var systemSocket = new WebSocket(sys_uri); - -systemSocket.onmessage = function(msg) { - - var data = JSON.parse(msg.data); - - systemMonitor.system.cpu = data.cpu.toPrecision(3).toString() + ' %'; - systemMonitor.system.mem = data.mem.toPrecision(2).toString() + ' %'; - systemMonitor.system.temp = data.temp.toPrecision(3).toString() + ' °C'; -}; - /* * Audio socket */ -var audioSocket = new WebSocket(audio_uri); -audioSocket.onmessage = function(msg) { - +const processAudio = function(msg) { + var reader = new FileReader(); - + reader.onloadend = function() { document.dispatchEvent(new CustomEvent('audioData', { 'detail': reader.result})); }; - reader.readAsArrayBuffer(msg.data); -}; \ No newline at end of file + reader.readAsArrayBuffer(msg.data); +}; diff --git a/server.js b/server.js index dff563c..e2f5d49 100644 --- a/server.js +++ b/server.js @@ -34,23 +34,23 @@ app.get('/legal', function(req, res) { // 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); @@ -60,23 +60,23 @@ app.ws('/tracking',function(ws, req) { // 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); @@ -86,23 +86,23 @@ app.ws('/potential',function(ws, req) { // 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); @@ -112,24 +112,24 @@ app.ws('/system.info',function(ws, req) { // 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); @@ -154,14 +154,14 @@ console.log('Listening...'); // Load modules var net = require('net'); -var server = net.createServer(); +var server = net.createServer(); server.on('connection', handleConnection); -server.listen(9000, function() { +server.listen(9000, function() { console.log('server listening to %j', server.address()); }); -function handleConnection(conn) { +function handleConnection(conn) { var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; console.log('new client connection from %s', remoteAddress); @@ -170,12 +170,12 @@ function handleConnection(conn) { conn.on('error', onConnError); function onConnData(d) { - + var decoder = new StringDecoder(); - + // Decode received string var str = decoder.write(d); - + eventEmitter.emit('newTracking',str); } @@ -194,14 +194,14 @@ function handleConnection(conn) { * Create TCP server for potential sources */ -var potentialServer = net.createServer(); +var potentialServer = net.createServer(); potentialServer.on('connection', handlePotConnection); -potentialServer.listen(9001, function() { +potentialServer.listen(9001, function() { console.log('server listening to %j', potentialServer.address()); }); -function handlePotConnection(conn) { +function handlePotConnection(conn) { var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; console.log('new client connection from %s', remoteAddress); @@ -210,12 +210,12 @@ function handlePotConnection(conn) { conn.on('error', onConnError); function onConnData(d) { - + var decoder = new StringDecoder(); - + // Decode received string var str = decoder.write(d); - + eventEmitter.emit('newPotential',str); } @@ -235,43 +235,21 @@ function handlePotConnection(conn) { */ // Load modules -var si = require('systeminformation'); - -function updateSi() { // Gather params - - var sysInfo = {cpu:0,mem:0,temp:0}; - - si.currentLoad(function(data) { - sysInfo.cpu = data.currentload; - - si.mem(function(data) { - sysInfo.mem = (data.active/data.total)*100; - - si.cpuTemperature(function(data) { - sysInfo.temp = data.main; - eventEmitter.emit('sysInfo',sysInfo); - }); - }); - }); - -} -// Schedule update -setInterval(updateSi,500); /* * Audio stream server */ -var audioServer = net.createServer(); +var audioServer = net.createServer(); audioServer.on('connection', handleAudioConnection); -audioServer.listen(10001, function() { +audioServer.listen(10001, function() { console.log('server listening to %j', audioServer.address()); }); -function handleAudioConnection(conn) { - +function handleAudioConnection(conn) { + var remoteAddress = conn.remoteAddress + ':' + conn.remotePort; console.log('new client connection from %s', remoteAddress); @@ -280,8 +258,8 @@ function handleAudioConnection(conn) { conn.on('error', onConnError); function onConnData(d) { - - console.log(`Received audio on ${new Date().getTime()}`); + + console.log(`Received audio on ${new Date().getTime()}`); eventEmitter.emit('newAudio',d); } @@ -297,16 +275,16 @@ function handleAudioConnection(conn) { 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/legal.html b/views/legal.html index 29f2803..561dd1d 100644 --- a/views/legal.html +++ b/views/legal.html @@ -5,30 +5,16 @@ Legal - - + + @@ -39,7 +25,7 @@

The MIT License (MIT)

- Copyright 2017 IntRoLab
+ Copyright IntRoLab

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -62,17 +48,14 @@ - +

- - - - \ No newline at end of file + diff --git a/views/live_data.html b/views/live_data.html index db69ed7..10bc3a3 100644 --- a/views/live_data.html +++ b/views/live_data.html @@ -4,18 +4,18 @@ - + Live Data - - - - - + + + + +