-
Notifications
You must be signed in to change notification settings - Fork 13
/
renderer.js
62 lines (55 loc) · 2.15 KB
/
renderer.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
const fs = window.nodeRequire("fs")
const iconv = window.nodeRequire('iconv-lite')
const path = window.nodeRequire('path')
const exec = window.nodeRequire('child_process').exec
// 任何你期望执行的cmd命令,ls都可以
let cmdStr = 'gps_sdr_sim.exe'
// 执行cmd命令的目录,如果使用cd xx && 上面的命令,这种将会无法正常退出子进程
let cmdPath = path.join(process.cwd(),'/resources/console/win')
let binPath = path.join(process.cwd(),'/resources/console/win/gpssim.bin')
if (process.env.NODE_ENV === 'development') {
cmdPath = path.join(process.cwd(),'/console/win')
binPath = path.join(
process.cwd(),'/console/win/gpssim.bin')
console.log(cmdPath)
}
console.log(cmdPath)
// 子进程名称
let workerProcess
let workerProcessSymbol = false
function runExec(lat, lng, height, time) {
if (workerProcessSymbol === true) {
console.log("Repeat build")
return
}
workerProcessSymbol = true
// 执行命令行,如果命令不需要路径,或就是项目根目录,则不需要cwd参数:
cmdStr = cmdStr + " -e brdc -l " + lat + "," + lng + "," + height + " -d " + time + " -b 8"
console.log(cmdStr)
select("graph").style.display = "block"
workerProcess = exec(cmdStr, {cwd: cmdPath, encoding: 'buffer'})
// 不受child_process默认的缓冲区大小的使用方法,没参数也要写上{}:workerProcess = exec(cmdStr, {})
// 打印正常的后台可执行程序输出
workerProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
// 打印错误的后台可执行程序输出
workerProcess.stderr.on('data', function (data) {
console.log('stderr: ' + iconv.decode(data, 'cp936'));
let num = parseFloat(iconv.decode(data, 'cp936').match(/([^run =]+)$/)[0]);
processUpdate(num / time * 100);
});
// 退出之后的输出
workerProcess.on('close', function (code) {
console.log('out code:' + code);
if(code == 0) {
select("bar").innerHTML = "完成"
fs.rename(binPath, "./gpssim.bin", function(err){
if(err){
throw err;
}
})
}
workerProcessSymbol = false;
})
}