-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.ts
72 lines (62 loc) · 1.9 KB
/
controller.ts
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
import { STEPS_PER_DEGREE } from './config/constants';
import { spawn } from 'child_process';
import { WebApp } from './classes/web-app';
import { MotorController } from './classes/motor-controller';
let currentScanNumber = 0;
let currentOptions = {};
let scanningInProgress = false;
const webApp = new WebApp();
const motorController = new MotorController();
webApp.getMessagesObservable().subscribe((data) => {
if (data.message === 'startScan') {
startScan(data.options);
}
if (data.message === 'stopScan') {
stopScan();
}
});
function startScan(options) {
console.log('starting scan with options', options);
scanningInProgress = true;
currentOptions = options;
webApp.notifyScanStart();
runScans(options).then(() => {
stopScan();
});
}
function stopScan() {
scanningInProgress = false;
currentScanNumber = 0;
webApp.notifyScanStop();
}
function executeScan(options) {
return new Promise(resolve => {
const scanJob = spawn("/Users/karliscaune/scanline_build/scanline", ["-flatbed", ...options]);
scanJob.on('close', () => {
console.log('done scanning!');
resolve('done scanning');
})
});
}
function turnPlate(degrees, direction) {
console.log('turning plate with degrees', degrees, 'direction', direction);
return new Promise(resolve => {
motorController.updateDirection(direction).then(() => {
motorController.sendSteps(degrees.toString()).then(() => resolve('done'));
});
});
}
async function runScans(options) {
for (let i = 1; i < options.numberOfScans; i++) {
const scanningOptions = [
`-resolution ${options.scanResolution}`,
`-${options.fileType}`,
`-name ${options.fileName}_${String(i).padStart(4, '0')}`,
`${options.dirName}`,
];
console.log(scanningOptions);
await executeScan(scanningOptions);
webApp.notifyScanFinish(i);
await turnPlate(options.scanDegrees, options.direction);
}
}