forked from PathLabs/primacy_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
532 lines (446 loc) · 14 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/**
* Desc: Main file for controlling window rendering and backend pipeline communication.
*
* Authors:
* - Chance Nelson <[email protected]>
* - Austin Kelly <[email protected]>
* - Alex Lacy <[email protected]>
*/
const { app, BrowserWindow, Menu } = require('electron');
const child_process = require('child_process');
const ipcMain = require('electron').ipcMain;
const {dialog} = require('electron');
var fs = require('fs');
const tar = require('tar');
// 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 win;
let viz;
// get the PID of our current process, for sandboxing uses
let pid = process.pid;
// create a run prefix directory
let prefix = __dirname + '/pipeline/' + pid.toString();
fs.mkdirSync(prefix);
var current_json = {};
var visited_modules = {
1: {
'visited': true,
'executed': false
},
2: {
'visited': false,
'executed': false
},
3: {
'visited': false,
'executed': false
},
4: {
'visited': false,
'executed': false
}
};
var current_module = 1;
function initial() {
/**
* Desc: Create the initial window, and set the close handler
*/
// Create the browser window.
win = new BrowserWindow({width: 1024, height:768, backgroundColor: '#000'});
// and load the index.html of the app.
win.loadURL('file:///'+ __dirname + '/src/html/module1.html');
// Open the DevTools.
// win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('close', function(e){
// 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.
var choice = require('electron').dialog.showMessageBox(this,
{
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to quit?'
});
if(choice == 1){
e.preventDefault();
}
});
}
function goToModule(module_number) {
/**
* Desc: Load and render a module page
*
* Args:
* module_number (int): module number to load. Also affects which html file is loaded.
*
* Returns:
* - If there is a problem rendering the page, False.
* - Else, True.
*/
let response = 0;
const options = {
type: 'question',
buttons: ['Yes', 'No'],
defaultid: 1,
title: 'Question',
message: 'Are you sure you want to leave?',
detail: 'Progress on current module will be lost.'
};
if(module_number <= current_module) {
response = require('electron').dialog.showMessageBox(null, options,(response) => {
if (response == 0){
win.loadURL('file:///' + __dirname + '/src/html/module' + module_number.toString() + '.html');
}
});
}
if(module_number == 1) {
current_module = module_number;
return true;
}
if(!visited_modules[module_number-1]['executed']){
return false;
}
if((current_module <= module_number) && !visited_modules[module_number-1]['executed']) {
return false;
}
// Load the html
if (response == 0){
win.loadURL('file:///' + __dirname + '/src/html/module' + module_number.toString() + '.html');
}
current_module = module_number;
return true;
}
function execPipeline(cmd, args, callback) {
/**
* Desc: Execute a pipeline command with some arguments.
*
* Args:
* cmd (string): name of the pipeline script to call
* args (JSON): JSON object of what to throw at the command
* callback (function (res)): callback function to get result, see Returns
*
* Returns:
* - If pipeline returns an error string, return the error string
* - Else, return null
*/
args_json = JSON.parse(args);
// Merge current args into current json
for(key in args_json) {
current_json[key] = args_json[key];
}
let response = 0;
const options = {
type: 'question',
buttons: ['Yes', 'No'],
defaultid: 1,
title: 'Question',
message: 'Executing will remove progress on future modules',
detail: 'Progress on future modules will be lost. Save your state in file->save'
};
let most_forward_exec = 1;
while(true) {
if(visited_modules[most_forward_exec]['executed']) {
if(most_forward_exec == 3) break;
most_forward_exec++;
} else {
most_forward_exec--;
break;
}
}
console.log("checking most forward exec");
console.log(most_forward_exec, current_module);
if(most_forward_exec > current_module) {
response = require('electron').dialog.showMessageBox(null, options,(response) => {
if (response != 0){
return;
}
// Upon execution, purge the current args of any information for future
// modules. We won't need them, since we're rerunning a previous segment
// Also, reset the state information in visited_modules to match
console.log("processing cmd:", cmd);
switch(cmd) {
case 'primacy primer-collection':
delete current_json['primer_scores'];
delete current_json['set_optimization'];
console.log('remove arguments:');
console.log(args)
visited_modules = {
1: {
'visited': true,
'executed': false
},
2: {
'visited': false,
'executed': false
},
3: {
'visited': false,
'executed': false
},
4: {
'visited': false,
'executed': false
}
};
case 'primacy primer-score':
delete current_json['set_optimization'];
visited_modules = {
1: {
'visited': true,
'executed': true
},
2: {
'visited': true,
'executed': false
},
3: {
'visited': false,
'executed': false
},
4: {
'visited': false,
'executed': false
}
};
}
// Re-enter execPipeline with new, cleaned state
execPipeline(cmd, args, callback);
});
} else {
fs.writeFileSync(prefix + '/args.json', JSON.stringify(current_json));
cmd = cmd + ' ' + prefix + '/args.json'
if(!cmd.includes('primer-score')) {
cmd += ' ' + prefix;
}
console.log(cmd);
child_process.exec(cmd, (error, stdout, stderr) => {
// Read back in file
data = fs.readFileSync(prefix + '/args.json', 'utf-8');
console.log(data.toString());
current_json = JSON.parse(data.toString());
visited_modules[current_module]['executed'] = true;
callback(null);
});
}
}
function updateArgs(new_args) {
let new_args_json = JSON.parse(new_args);
console.log(new_args_json);
for(key in new_args_json) {
console.log(key)
current_json[key] = new_args_json[key];
}
console.log(current_json)
}
function showViz(viz_num) {
switch(viz_num) {
case 1:
if(!visited_modules[1].executed) return false;
break;
case 2:
if(!visited_modules[2].executed) return false;
break;
}
viz = new BrowserWindow({width: 1024, height:768, backgroundColor: '#000'});
viz.on('closed', function() {
viz = null;
});
viz.loadURL('file:///' + __dirname + '/src/html/module' + viz_num.toString() + '_viz.html');
viz.webContents.once('dom-ready', () => {
viz.webContents.send('NEW', JSON.stringify(current_json));
});
return true;
}
/**
* @brief create a save of the current pipeline state
*
* @param save_state_path file path to the tarball to save
*/
function createSaveState(save_state_path) {
// save the current state to args.json
fs.writeFileSync(prefix + '/args.json', JSON.stringify(current_json));
fs.writeFileSync(prefix + '/state.json', JSON.stringify(visited_modules));
// get a list of all files and directories making up the current state
let state_files = fs.readdirSync(prefix);
tar.c({gzip: true, file: save_state_path, cwd: prefix}, state_files);
}
/**
* @brief load a save state into the current pipeline state
*
* @param save_state_path file path to the saved tarball
*/
function loadSaveState(save_state_path) {
// clear out all files in the current state
let state_files = fs.readdirSync(prefix);
for(let file of state_files) {
fs.unlink(prefix + file.toString(), err => {
if(err) console.log(err);
});
}
// extract the state files
tar.x({file: save_state_path, cwd: prefix, sync: true});
// Load in the args and state json files
current_json = JSON.parse(fs.readFileSync(prefix + '/args.json'));
visited_modules = JSON.parse(fs.readFileSync(prefix + '/state.json'));
// Find the most recently visited module
current_module = 1;
while(visited_modules[current_module['executed']]) {
current_module++;
}
// Jump to the most recent module
goToModule(current_module);
// Send IPC message with the arguments to the current module
win.webContents.once('dom-ready', () => {
win.webContents.send('NEW', JSON.stringify(current_json));
});
}
/* CORE WINDOW EVENTS */
app.on('ready', initial);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS 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', () => {
// On macOS 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 (win === null) {
initial();
}
});
/* IPC EVENTS */
ipcMain.on('LOADVIZ', (event, viz_num) => {
if(showViz(viz_num)) {
console.log('opening viz number:', viz_num);
}
});
// Attempt to load a page
ipcMain.on('LOADMODULE', (event, module_number) => {
if(goToModule(module_number)) {
// Send IPC message with the arguments to the current module
console.log('page load', module_number);
win.webContents.once('dom-ready', () => {
win.webContents.send('NEW', JSON.stringify(current_json));
});
} else {
event.sender.send('LOADMODULE', 'DENIED');
}
});
// Attempt to execute pipeline with args
ipcMain.on('EXECUTE', (event, data) => {
execPipeline(data[0], data[1], (result) => {
event.sender.send('EXECUTE', result);
});
});
// Update the current pipeline args and refresh any open windows
ipcMain.on('UPDATE', (event, args) => {
console.log("Updating args");
updateArgs(args);
console.log('Sending NEW')
win.webContents.send('NEW', JSON.stringify(current_json));
});
// Menu Setup and Customization
const template = [
{
label: 'File',
submenu: [
{label: 'Save', click () {
createSaveState(dialog.showSaveDialog());
}},
{label: 'Load', click () {
loadSaveState(dialog.showOpenDialog()[0]);
}},
]
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'delete' },
{ role: 'selectall' },
{ type: 'separator' },
{ label: 'Restore Defaults', role: 'reload'}
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
{
role: 'window',
submenu: [
{ role: 'minimize' },
{ role: 'close' }
]
},
{
role: 'help',
submenu: [
{
label: 'Documentation',
click () { require('electron').shell.openExternal('https://github.com/PathLabs/primacy_demo/blob/master/README.md') }
},
{
label: 'Website',
click () { require('electron').shell.openExternal('https://www.cefns.nau.edu/capstone/projects/CS/2019/PathLab-S19/index.html') }
}
]
}
];
if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
});
// Edit menu
template[1].submenu.push(
{ type: 'separator' },
{
label: 'Speech',
submenu: [
{ role: 'startspeaking' },
{ role: 'stopspeaking' }
]
}
);
// Window menu
template[3].submenu = [
{ role: 'close' },
{ role: 'minimize' },
{ role: 'zoom' },
{ type: 'separator' },
{ role: 'front' }
];
}
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);