Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run iCub telemetry server and openMCT visualizer in one line command: Iteration 2/2 #32

Merged
merged 5 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions iCubTelemVizServer/iCubTelemVizServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ http.listen(3000, function(){
// Create and start the OpenMCT server
var OpenMctServerHandler = require('./openMctServerHandler');
var openMctServerHandler = new OpenMctServerHandler(console.log);
var ret = openMctServerHandler.setNvmVersion('v14.17.0');
var ret = openMctServerHandler.start();
console.log(ret.status);
console.log(ret.message);
// openMctServerHandler.start();
84 changes: 46 additions & 38 deletions iCubTelemVizServer/openMctServerHandler.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,71 @@
"use strict";

// Import utilities for working with file and directory paths.
const path = require('path');

function OpenMctServerHandler(outputCallback) {
// Create a child process spawn for later setting the NVM version and running the server
this.childProcess = require('child_process');
this.processHandle = null;
this.outputCallback = outputCallback;
}

OpenMctServerHandler.prototype.setNvmVersion = function (nvmVersion) {
if (typeof nvmVersion != 'string') {
this.outputCallback('NVM version must be a string.');
}

// Switch the NVM version
let ret = this.childProcess.spawnSync('nvm',['use',nvmVersion],{cwd:[process.cwd()+'/../openmctStaticServer'],timeout:3000});
if (ret.signal) {
return {status: ret.status, message: ['exited with signal '+ret.signal]};
}
if (ret.error != undefined) {
return {status: ret.status, message: ['error: '+ret.error.message]};
}
if (ret.stderr.length > 0) {
return {satus: ret.status, message: ['stderr: '+ret.stderr]};
}
this.nvmVersion = nvmVersion;
return {status: ret.status, message: ['stdout: '+ret.stdout]};
}

OpenMctServerHandler.prototype.start = function () {
// Check if the server is already running
if (this.isOn()) {
return {status: 'WARNING', msg: 'OpenMCT server already running.'};
}
else {
const embeddedThis = this;
// Start the process
let npmStart = this.childProcess.spawn('npm', ['start']);
// Set the output callbacks
npmStart.stdout.on('data', function (data) {this.outputCallback('stdout: ' + data);});
npmStart.stderr.on('data', function (data) {this.outputCallback('stderr: ' + data);});
npmStart.on('error', function (error) {this.outputCallback('error: ' + error.message);});
npmStart.on('close', function (code) {
embeddedThis.processHandle = null;
this.outputCallback('close: ' + code);
});

this.processHandle = npmStart;
return {status: 'OK', msg: 'Process started.'};
return {status: 'WARNING', message: 'OpenMCT server already running.'};
}

// Inside the callbacks, for 'this' to be the 'OpenMctServerHandler' object instead of the
// callback caller, we need to back it up.
const embeddedThis = this;

// Start the process
const execPath = path.join(process.cwd(), '..', 'openmctStaticServer');
let npmStart = this.childProcess.spawn('npm', ['start'], {shell: 'bash', cwd: execPath});

// Set the output callbacks
npmStart.stdout.on('data', function (data) {
embeddedThis.outputCallback('[OPEN-MCT STATIC SERVER] stdout: ' + data);
});
npmStart.stderr.on('data', function (data) {
embeddedThis.outputCallback('[OPEN-MCT STATIC SERVER] stderr: ' + data);
});
npmStart.on('error', function (error) {
embeddedThis.outputCallback('[OPEN-MCT STATIC SERVER] error: ' + error.message);
});
npmStart.on('close', function (code) {
embeddedThis.processHandle = null;
embeddedThis.outputCallback('[OPEN-MCT STATIC SERVER] close: ' + code);
});

this.processHandle = npmStart;
return {status: 'OK', message: 'Opem-MCT static server process started.'};
}

OpenMctServerHandler.prototype.stop = function () {
this.processHandle.kill();
return {status: 'DELAYED_REPLY', msg: 'Process stopping...'};
return {status: 'PROMISE', message: 'Opem-MCT static server process stopping...'};
}

OpenMctServerHandler.prototype.isOn = function () {
return (this.processHandle != null);
}

function spawnSynchInOpenMCTserverPath(childProcess, shellCommand, cmdArgs) {
// Run a shell command from a child process in 'openmctStaticServer' folder working path.
let ret = childProcess.spawnSync(shellCommand, cmdArgs, {shell: 'bash', cwd: process.cwd(), timeout: 3000});
if (ret.signal) {
return {success: false, cmdRet: {status: ret.status, message: ['exited with signal ' + ret.signal]}};
} else if (ret.error !== undefined) {
return {success: false, cmdRet: {status: ret.status, message: ['error: ' + ret.error.message]}};
} else if (ret.stderr.length > 0) {
return {success: false, cmdRet: {status: ret.status, message: ['stderr: ' + ret.stderr]}};
} else {
return {success: true, cmdRet: {status: ret.status, message: ['stdout: ' + ret.stdout]}};
}
}

module.exports = function (outputCallback) {
return new OpenMctServerHandler(outputCallback);
}
2 changes: 1 addition & 1 deletion iCubTelemVizServer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"main": "iCubTelemVisServer.js",
"scripts": {
"start": "node $NODE_DEBUG_OPTION iCubTelemVizServer.js"
"start": ". ${NVM_DIR}/nvm.sh; nvm use v4.2.2; node ${NODE_DEBUG_OPTION} iCubTelemVizServer.js"
},
"homepage": "https://github.com/dic-iit/yarp-openmct.git#README"
}
2 changes: 1 addition & 1 deletion openmctStaticServer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"main": "server.js",
"scripts": {
"start": "node server.js"
"start": ". ${NVM_DIR}/nvm.sh; nvm use v14.17.0; node server.js"
},
"homepage": "https://github.com/dic-iit/yarp-openmct.git#README"
}