Skip to content
Open
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
20 changes: 15 additions & 5 deletions js/main/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { spawn } from 'node:child_process'
const child_process = {
_processes: [],

start: function (command, args, opts, window) {
start: function (command, args, opts, window) {
var process;
try {
process = spawn(command, args, opts);
} catch (err) {
console.log(err);
console.log(`[child_process] Spawn error: ${err.message}`);
if (!window.isDestroyed()) {
window.webContents.send('onChildProcessError', { message: `Spawn error: ${err.message}` });
}
return -1;
}

this._processes.push(process);

process.stdout.on('data', (data) => {
if (!window.isDestroyed()) {
window.webContents.send('onChildProcessStdout', data.toString());
Expand All @@ -27,8 +30,15 @@ const child_process = {
});

process.on('error', (error) => {
console.log(`[child_process] Error: ${error.message}`);
if (!window.isDestroyed()) {
window.webContents.send('onChildProcessError', { message: error.message });
}
});

process.on('exit', (code, signal) => {
if (!window.isDestroyed()) {
window.webContents.send('onChildProcessError', error);
window.webContents.send('onChildProcessExit', { code, signal });
}
});
},
Expand Down
6 changes: 6 additions & 0 deletions js/main/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ contextBridge.exposeInMainWorld('electronAPI', {
return handler;
},
offChildProcessError: (handler) => ipcRenderer.removeListener('onChildProcessError', handler),
onChildProcessExit: (callback) => {
const handler = (_event, data) => callback(data);
ipcRenderer.on('onChildProcessExit', handler);
return handler;
},
offChildProcessExit: (handler) => ipcRenderer.removeListener('onChildProcessExit', handler),
});
28 changes: 19 additions & 9 deletions js/sitl.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ var SitlSerialPortUtils = {

var SITLProcess = {

spawn : null,
isRunning: false,
_errorHandler: null,
_exitHandler: null,

deleteEepromFile(filename) {
window.electronAPI.rm(`${window.electronAPI.appGetPath('userData')}/${filename}`).then(error => {
Expand Down Expand Up @@ -183,17 +184,26 @@ var SITLProcess = {

spawn: function(path, args) {

var opts = undefined;
if (GUI.operating_system == 'Linux')
opts = { useShell: true };

window.electronAPI.startChildProcess(path, args, opts);
if (this._errorHandler) {
window.electronAPI.offChildProcessError(this._errorHandler);
}
if (this._exitHandler) {
window.electronAPI.offChildProcessExit(this._exitHandler);
}

if (this.processHandle == -1) {
this._errorHandler = window.electronAPI.onChildProcessError((error) => {
console.log(`SITL error: ${error.message || JSON.stringify(error)}`);
GUI.log(`SITL error: ${error.message || JSON.stringify(error)}`);
});
this._exitHandler = window.electronAPI.onChildProcessExit((data) => {
if (data.code !== 0 && data.code !== null) {
console.log(`SITL process exited with code ${data.code}`);
GUI.log(`SITL process exited with code ${data.code}`);
}
this.isRunning = false;
return;
}
});

window.electronAPI.startChildProcess(path, args);
this.isRunning = true;
},

Expand Down