| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- import {app, BrowserWindow, ipcMain, dialog} from 'electron';
- import {exec, spawn} from 'child_process';
- import path from 'path';
- import {fileURLToPath} from 'url';
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- function createWindow() {
- const win = new BrowserWindow({
- width: 800,
- height: 600,
- webPreferences: {
- preload: path.join(__dirname, 'preload.js'), // 确保路径正确
- contextIsolation: true,
- enableRemoteModule: false,
- nodeIntegration: false,
- }
- });
- win.webContents.openDevTools(); // 打开开发者工具进行调试
- win.loadURL('http://localhost:5173'); // 开发环境
- // win.loadURL('http://36.110.106.156:81'); // 生产环境
- console.log('Window created and URL loaded');
- }
- app.whenReady().then(createWindow);
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow();
- }
- });
- // ------------- 进程通信 -------------
- ipcMain.on('open-gazebo', (event, arg) => {
- console.log('Received open-gazebo event');
- exec('gazebo', (error, stdout, stderr) => {
- if (error) {
- console.error(`exec error: ${error}`);
- return;
- }
- console.log(`stdout: ${stdout}`);
- console.error(`stderr: ${stderr}`);
- });
- });
- ipcMain.on('open-rviz', (event, arg) => {
- console.log('Received open-rviz event');
- exec('rviz', (error, stdout, stderr) => {
- if (error) {
- console.error(`exec error: ${error}`);
- return;
- }
- console.log(`stdout: ${stdout}`);
- console.error(`stderr: ${stderr}`);
- });
- });
- ipcMain.on('close-gazebo', (event, arg) => {
- console.log('Received close-gazebo event');
- exec('pkill -f gzserver & pkill -f gzclient', (error, stdout, stderr) => {
- if (error) {
- console.error(`exec error: ${error}`);
- return;
- }
- console.log(`stdout: ${stdout}`);
- console.error(`stderr: ${stderr}`);
- });
- });
- ipcMain.on('close-rviz', (event, arg) => {
- console.log('Received close-rviz event');
- exec('pkill -f rviz', (error, stdout, stderr) => {
- if (error) {
- console.error(`exec error: ${error}`);
- return;
- }
- console.log(`stdout: ${stdout}`);
- console.error(`stderr: ${stderr}`);
- });
- });
- ipcMain.handle('dialog:open', async (event, options = {}) => {
- const result = await dialog.showOpenDialog(BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0], options);
- return result.canceled ? null : result.filePaths;
- });
- ipcMain.on('docker-import', (event, sudoPassword, filePath, tag) => {
- const command = 'echo "' + sudoPassword + '" | sudo -S docker import ' + filePath + ' pji_nav:' + tag
- console.log('导入算法镜像文件:', command);
- exec(command, (error, stdout, stderr) => {
- if (error) {
- console.error(`exec error: ${error}`);
- return;
- }
- console.log(`stdout: ${stdout}`);
- console.error(`stderr: ${stderr}`);
- });
- });
- // ipcMain.on('generate-world', (event, rosbag_path) => {
- //
- // const serviceScript = 'nonhup bash /home/cicv/work/pji_desktop/run_map2gazabo.sh >/dev/null 2>&1 & '
- // const child1 = exec(serviceScript, (error, stdout, stderr) => {
- // if (error) {
- // console.error(`exec error: ${error}`);
- // return;
- // }
- // console.log(`stdout: ${stdout}`);
- // console.error(`stderr: ${stderr}`);
- //
- // // 启动第二个脚本
- // const script2 = exec('/home/cicv/work/pji_desktop/play_rosbag.sh /home/cicv/work/pji_desktop/map_bag/map_anqing.bag', (error, stdout, stderr) => {
- // if (error) {
- // console.error(`执行第二个脚本时出错: ${error}`);
- // return;
- // }
- // console.log(`第二个脚本的输出: ${stdout}`);
- // });
- // });
- // });
- ipcMain.on('generate-world', (event, rosbag_path) => {
- // 使用 spawn 启动脚本
- const serviceProcess = spawn('bash', ["/home/cicv/work/pji_desktop/run_map2gazebo.sh"], { detached: true });
- // 设置为后台进程
- serviceProcess.unref();
- // 监听输出
- serviceProcess.stdout.on('data', (data) => {
- console.log(`第一个脚本的输出: ${data}`);
- // 根据第一个脚本的输出判断其是否准备好
- if (data.toString().includes('Service map2gazebo started')) {
- startSecondScript();
- }
- });
- // 监听错误
- serviceProcess.stderr.on('data', (data) => {
- console.error(`执行第一个脚本时出错: ${data}`);
- });
- // 监听关闭
- serviceProcess.on('close', (data) => {
- console.log(`第二个脚本已关闭: ${data}`);
- });
- function startSecondScript() {
- // 启动第二个脚本
- const script2 = exec('bash /home/cicv/work/pji_desktop/play_rosbag.sh /home/cicv/work/pji_desktop/map_bag/map.bag', (error, stdout, stderr) => {
- if (error) {
- console.error(`执行第二个脚本时出错: ${error}`);
- return;
- }
- console.log(`第二个脚本的输出: ${stdout}`);
- });
- script2.on('exit', (code) => {
- console.log(`第二个脚本已退出,退出码: ${code}`);
- });
- }
- });
|