From 616f6b06d2216b7cffe274bba09d867dcbd439f1 Mon Sep 17 00:00:00 2001 From: Clovis Durand Date: Fri, 10 Apr 2020 17:30:36 +0200 Subject: [PATCH] [#1] Can now launch the C++ back-end from the electron app Signed-off-by: Clovis Durand --- app/back/backEndLauncher.js | 57 ++++++++++++++++++++++++++++++++----- app/back/ipcBack.js | 3 ++ app/config.json | 3 +- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/app/back/backEndLauncher.js b/app/back/backEndLauncher.js index 3f6e3e3..95c9091 100644 --- a/app/back/backEndLauncher.js +++ b/app/back/backEndLauncher.js @@ -1,15 +1,58 @@ const child = require('child_process'); -const config = require('../config.json') +const fs = require('fs'); /* Set the back-end's path */ -const backEnd = config.backEnd; -const backEndPort = config.backEndPort; -const edsFile = config.edsFile; + +let backEnd; function launchBackEnd() { + let backEnd; + let backEndLibDir; + let backEndPort; + let edsFile; + + lConfig = JSON.parse(fs.readFileSync('./app/config.json', 'utf8')); + + /* Set the configuration variables */ + backEndExe = lConfig.backEndExe; + backEndPort = lConfig.backEndPort; + backEndLibDir = lConfig.backEndLibDir; + edsFile = lConfig.odFileName; + + + console.log('backEndExe : ' + backEndExe + ' (' + typeof(backEndExe) + ')'); + console.log('backEndLibDir : ' + backEndLibDir + ' (' + typeof(backEndLibDir) + ')'); + console.log('backEndPort : ' + backEndPort + ' (' + typeof(backEndPort) + ')'); + console.log('edsFile : ' + edsFile + ' (' + typeof(edsFile) + ')'); + + var lEnv = Object.create(process.env); + lEnv.LD_LIBRARY_PATH = backEndLibDir; + var lParameters = [backEndPort, edsFile]; - child.execFile(backEnd, lParameters, function(err, data) { - console.log(err); - console.log(data.toString()); + console.log('[INFO ] Launching back-end :' + backEndExe + ' ' + backEndPort + ' ' + edsFile); + backEnd = child.spawn(backEndExe, lParameters, { env: lEnv }, function(error, stdout, stderr) { + if(error) { + console.log(error); + return; + } + + console.log(stdout); + console.log(stderr); + }); + + backEnd.stdout.on('data', function(data) { + console.log('[B-END] stdout: ' + data.toString('utf-8')); + }); + + backEnd.stderr.on('data', function(data) { + console.log('[B-END] stderr: ' + data.toString('utf8')); }); + + backEnd.on('exit', function(code) { + console.log('[B-END] child process exited with code ' + code.toString('utf-8')); + }); + + return; } + +module.exports.launchBackEnd = launchBackEnd; diff --git a/app/back/ipcBack.js b/app/back/ipcBack.js index 09c5abe..e87dde6 100644 --- a/app/back/ipcBack.js +++ b/app/back/ipcBack.js @@ -1,7 +1,10 @@ const {ipcMain} = require('electron') +const backEndLauncher = require('./backEndLauncher') ipcMain.on('test-button-signal', () => { console.log('[DEBUG] IPC signal received by main process'); + console.log(' Launching back-end'); + backEndLauncher.launchBackEnd(); }); ipcMain.on('methodCall', async (event, { method, callId, args }) => { diff --git a/app/config.json b/app/config.json index b1a2772..e2cfe03 100644 --- a/app/config.json +++ b/app/config.json @@ -1,6 +1,7 @@ { "backEndExe": "/Users/clovel/repository/CAN-CANOpen/OSCO-OD-Gen/generator/dest/bin/OSCO-OD-Gen-util", - "odFileName": "/Users/clovel/repository/CAN-CANOpen/OSCO-OD-Gen/generator/example/od/IO.eds", + "backEndLibDir": "/Users/clovel/repository/CAN-CANOpen/OSCO-OD-Gen/generator/dest/lib", + "odFileName": "/Users/clovel/repository/CAN-CANOpen/OSCO-OD-Gen/generator/examples/od/IO.eds", "backEndPort": 3000, "backEndAddr": "http://localhost" } \ No newline at end of file