Skip to content

Commit 2779bbd

Browse files
committed
Finish up migration from remote module
This is the final step before migrating to the next Electron release.
1 parent 402e685 commit 2779bbd

File tree

4 files changed

+148
-150
lines changed

4 files changed

+148
-150
lines changed

background_tasks/linker.html

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,96 @@
22
const { PythonShell } = require('python-shell');
33
const { ipcRenderer } = require('electron');
44
const loadBalancer = require('electron-load-balancer');
5-
const { getScriptPath } = require('../utils/bundling');
65
const { oscilloscopeXYProcessor, oscilloscopeVoltageProcessor, LAProcessor } = require('../utils/preProcessor.js');
76
const log = require('electron-log');
87

9-
const bridgePath = getScriptPath('bridge.py');
10-
const pyshell = new PythonShell(bridgePath);
11-
log.info('Python spawned:', pyshell && pyshell.command);
8+
ipcRenderer.invoke('GET_SCRIPT_PATH', 'bridge.py').then((bridgePath) => {
9+
const pyshell = new PythonShell(bridgePath);
10+
log.info('Python spawned:', pyshell && pyshell.command);
1211

13-
loadBalancer.job(
14-
ipcRenderer,
15-
'linker',
16-
() => {
17-
loadBalancer.onReceiveData(ipcRenderer, 'linker', args => {
18-
log.info({ args });
19-
pyshell.send(JSON.stringify(args));
20-
});
12+
loadBalancer.job(
13+
ipcRenderer,
14+
'linker',
15+
() => {
16+
loadBalancer.onReceiveData(ipcRenderer, 'linker', args => {
17+
log.info({ args });
18+
pyshell.send(JSON.stringify(args));
19+
});
2120

22-
pyshell.on('stderr', function(stderr) {
23-
log.error(stderr);
24-
});
21+
pyshell.on('stderr', function(stderr) {
22+
log.error(stderr);
23+
});
2524

26-
pyshell.on('message', function(results) {
27-
log.info({ results });
28-
try {
29-
const parsedJSON = JSON.parse(results);
30-
switch (parsedJSON['type']) {
31-
case 'START_OSC':
32-
if (parsedJSON.isFFT) {
33-
parsedOutput = oscilloscopeVoltageProcessor(parsedJSON);
34-
parsedJSON['fitType'] !== null &&
35-
ipcRenderer.send('OSC_FIT_DATA', {
36-
fitOutput1Sine: parsedJSON['fitOutput1Sine'],
37-
fitOutput2Sine: parsedJSON['fitOutput2Sine'],
38-
fitOutput1Square: parsedJSON['fitOutput1Square'],
39-
fitOutput2Square: parsedJSON['fitOutput2Square'],
40-
});
41-
ipcRenderer.send('OSC_FFT_DATA', { data: parsedOutput });
42-
}
43-
else if( parsedJSON.isXYPlot ){
44-
parsedOutput = oscilloscopeXYProcessor(parsedJSON);
45-
ipcRenderer.send('OSC_XY_PLOT_DATA', { data: parsedOutput });
46-
}
47-
else {
48-
parsedOutput = oscilloscopeVoltageProcessor(parsedJSON);
49-
ipcRenderer.send('OSC_VOLTAGE_DATA', { data: parsedOutput });
50-
}
51-
break;
52-
case 'START_LA':
53-
parsedOutput = LAProcessor(parsedJSON)
54-
ipcRenderer.send('LA_DATA', {
55-
data: parsedOutput
56-
});
57-
break;
58-
case 'SENSORS_SCAN':
59-
ipcRenderer.send('SENSORS_SCAN', { data: parsedJSON.data });
60-
break;
61-
case 'START_MUL_MET':
62-
ipcRenderer.send('MUL_MET_DATA', { data: parsedJSON.data, prefix: parsedJSON.prefix });
63-
break;
64-
case 'GET_CONFIG_PWR_SRC':
65-
ipcRenderer.send('PWR_SRC_CONFIG', parsedJSON);
66-
break;
67-
case 'GET_CONFIG_OSC':
68-
ipcRenderer.send('OSC_CONFIG', parsedJSON);
69-
break;
70-
case 'GET_CONFIG_LA':
71-
ipcRenderer.send('LA_CONFIG', parsedJSON);
72-
break;
73-
case 'GET_CONFIG_MUL_MET':
74-
ipcRenderer.send('MUL_MET_CONFIG', parsedJSON);
75-
break;
76-
case 'GET_CONFIG_WAV_GEN':
77-
ipcRenderer.send('WAV_GEN_CONFIG', parsedJSON);
78-
break;
79-
case 'DEVICE_CONNECTION_STATUS':
80-
ipcRenderer.send('CONNECTION_STATUS', parsedJSON);
81-
break;
82-
case 'DATA_WRITING_STATUS':
83-
ipcRenderer.send('DATA_WRITING_STATUS', parsedJSON);
84-
break;
85-
default:
86-
break;
87-
}
88-
} catch (error) {
89-
log.error(error);
90-
}
91-
});
92-
},
93-
() => {
94-
pyshell.terminate();
95-
},
96-
);
25+
pyshell.on('message', function(results) {
26+
log.info({ results });
27+
try {
28+
const parsedJSON = JSON.parse(results);
29+
switch (parsedJSON['type']) {
30+
case 'START_OSC':
31+
if (parsedJSON.isFFT) {
32+
parsedOutput = oscilloscopeVoltageProcessor(parsedJSON);
33+
parsedJSON['fitType'] !== null &&
34+
ipcRenderer.send('OSC_FIT_DATA', {
35+
fitOutput1Sine: parsedJSON['fitOutput1Sine'],
36+
fitOutput2Sine: parsedJSON['fitOutput2Sine'],
37+
fitOutput1Square: parsedJSON['fitOutput1Square'],
38+
fitOutput2Square: parsedJSON['fitOutput2Square'],
39+
});
40+
ipcRenderer.send('OSC_FFT_DATA', { data: parsedOutput });
41+
}
42+
else if( parsedJSON.isXYPlot ){
43+
parsedOutput = oscilloscopeXYProcessor(parsedJSON);
44+
ipcRenderer.send('OSC_XY_PLOT_DATA', { data: parsedOutput });
45+
}
46+
else {
47+
parsedOutput = oscilloscopeVoltageProcessor(parsedJSON);
48+
ipcRenderer.send('OSC_VOLTAGE_DATA', { data: parsedOutput });
49+
}
50+
break;
51+
case 'START_LA':
52+
parsedOutput = LAProcessor(parsedJSON)
53+
ipcRenderer.send('LA_DATA', {
54+
data: parsedOutput
55+
});
56+
break;
57+
case 'SENSORS_SCAN':
58+
ipcRenderer.send('SENSORS_SCAN', { data: parsedJSON.data });
59+
break;
60+
case 'START_MUL_MET':
61+
ipcRenderer.send('MUL_MET_DATA', { data: parsedJSON.data, prefix: parsedJSON.prefix });
62+
break;
63+
case 'GET_CONFIG_PWR_SRC':
64+
ipcRenderer.send('PWR_SRC_CONFIG', parsedJSON);
65+
break;
66+
case 'GET_CONFIG_OSC':
67+
ipcRenderer.send('OSC_CONFIG', parsedJSON);
68+
break;
69+
case 'GET_CONFIG_LA':
70+
ipcRenderer.send('LA_CONFIG', parsedJSON);
71+
break;
72+
case 'GET_CONFIG_MUL_MET':
73+
ipcRenderer.send('MUL_MET_CONFIG', parsedJSON);
74+
break;
75+
case 'GET_CONFIG_WAV_GEN':
76+
ipcRenderer.send('WAV_GEN_CONFIG', parsedJSON);
77+
break;
78+
case 'DEVICE_CONNECTION_STATUS':
79+
ipcRenderer.send('CONNECTION_STATUS', parsedJSON);
80+
break;
81+
case 'DATA_WRITING_STATUS':
82+
ipcRenderer.send('DATA_WRITING_STATUS', parsedJSON);
83+
break;
84+
default:
85+
break;
86+
}
87+
} catch (error) {
88+
log.error(error);
89+
}
90+
});
91+
},
92+
() => {
93+
pyshell.terminate();
94+
},
95+
);
96+
});
9797
</script>

background_tasks/playback.html

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,50 @@
22
const { PythonShell } = require('python-shell');
33
const { ipcRenderer } = require('electron');
44
const loadBalancer = require('electron-load-balancer');
5-
const { getScriptPath } = require('../utils/bundling');
65
const { LAProcessor } = require('../utils/preProcessor.js');
76
const log = require('electron-log');
87

98
// For distribution, we bundle everything into an asar archive.
10-
const bridgePath = getScriptPath('bridge.py');
11-
const pyshell = new PythonShell(bridgePath);
12-
log.info('Python spawned:', pyshell && pyshell.command);
9+
ipcRenderer.invoke('GET_SCRIPT_PATH', 'bridge.py').then((bridgePath) => {
10+
const pyshell = new PythonShell(bridgePath);
11+
log.info('Python spawned:', pyshell && pyshell.command);
1312

14-
loadBalancer.job(
15-
ipcRenderer,
16-
'playback',
17-
() => {
18-
loadBalancer.onReceiveData(ipcRenderer, 'playback', args => {
19-
pyshell.send(JSON.stringify(args));
20-
});
13+
loadBalancer.job(
14+
ipcRenderer,
15+
'playback',
16+
() => {
17+
loadBalancer.onReceiveData(ipcRenderer, 'playback', args => {
18+
pyshell.send(JSON.stringify(args));
19+
});
2120

22-
pyshell.on('stderr', function(stderr) {
23-
log.error(stderr);
24-
});
21+
pyshell.on('stderr', function(stderr) {
22+
log.error(stderr);
23+
});
2524

26-
pyshell.on('message', function(results) {
27-
log.info(results);
28-
try {
29-
const parsedJSON = JSON.parse(results);
30-
switch (parsedJSON['type']) {
31-
case 'FETCH_ROB_ARM':
32-
ipcRenderer.send('FETCH_ROB_ARM');
33-
break;
34-
case 'FETCH_LA':
35-
ipcRenderer.send('FETCH_LA');
36-
break;
37-
case 'DATA_WRITING_STATUS':
38-
ipcRenderer.send('DATA_WRITING_STATUS', parsedJSON);
39-
break;
40-
}
41-
}
42-
catch (error) {
43-
log.error(error);
44-
}
45-
});
46-
},
47-
() => {
48-
pyshell.terminate();
49-
},
50-
);
25+
pyshell.on('message', function(results) {
26+
log.info(results);
27+
try {
28+
const parsedJSON = JSON.parse(results);
29+
switch (parsedJSON['type']) {
30+
case 'FETCH_ROB_ARM':
31+
ipcRenderer.send('FETCH_ROB_ARM');
32+
break;
33+
case 'FETCH_LA':
34+
ipcRenderer.send('FETCH_LA');
35+
break;
36+
case 'DATA_WRITING_STATUS':
37+
ipcRenderer.send('DATA_WRITING_STATUS', parsedJSON);
38+
break;
39+
}
40+
}
41+
catch (error) {
42+
log.error(error);
43+
}
44+
});
45+
},
46+
() => {
47+
pyshell.terminate();
48+
},
49+
);
50+
});
5151
</script>

public/electron.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ const { app } = electron;
99
const { BrowserWindow, dialog } = electron;
1010
const nativeImage = electron.nativeImage;
1111

12+
const scriptDir = 'scripts';
13+
14+
/**
15+
* Get the path to the requested Python script, specified by its name.
16+
* In dev mode, the path is just relative to the files in `background_tasks`.
17+
* For distribution, we bundle everything into an asar archive except for the
18+
* Python scripts. Otherwise, the Python interpreter cannot locate them.
19+
*/
20+
const getScriptPath = scriptName => {
21+
const appPath = app.getAppPath();
22+
if (process.env.DEV) {
23+
return path.join(appPath, scriptDir, scriptName);
24+
}
25+
return path
26+
.join(appPath, scriptDir, scriptName)
27+
.replace('app.asar', 'app.asar.unpacked');
28+
};
29+
1230
if (process.env.DEV) {
1331
const {
1432
default: installExtension,
@@ -82,7 +100,7 @@ loadBalancer.register(
82100
playback: '/background_tasks/playback.html',
83101
},
84102
// Set to true to unhide the window, useful for IPC debugging
85-
{ debug: false },
103+
{ debug: true },
86104
);
87105

88106
ipcMain.on('OSC_VOLTAGE_DATA', (event, args) => {
@@ -151,6 +169,10 @@ ipcMain.on('SENSORS_SCAN', (event, args) => {
151169
mainWindow.webContents.send('SENSORS_SCAN', args);
152170
});
153171

172+
ipcMain.handle('GET_SCRIPT_PATH', (event, args) => {
173+
return getScriptPath(args);
174+
});
175+
154176
ipcMain.handle('OPEN_IMPORT_WINDOW', async (event, dataPath) => {
155177
return dialog
156178
.showOpenDialog(null, {

utils/bundling.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)