main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {app, BrowserWindow, ipcMain, dialog} from 'electron';
  2. import {exec} from 'child_process';
  3. import path from 'path';
  4. import {fileURLToPath} from 'url';
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = path.dirname(__filename);
  7. function createWindow() {
  8. const win = new BrowserWindow({
  9. width: 800,
  10. height: 600,
  11. webPreferences: {
  12. preload: path.join(__dirname, 'preload.js'), // 确保路径正确
  13. contextIsolation: true,
  14. enableRemoteModule: false,
  15. nodeIntegration: false,
  16. }
  17. });
  18. win.webContents.openDevTools(); // 打开开发者工具进行调试
  19. win.loadURL('http://localhost:5173'); // 假设你的Vite开发服务器在3000端口运行
  20. console.log('Window created and URL loaded');
  21. }
  22. app.whenReady().then(createWindow);
  23. app.on('window-all-closed', () => {
  24. if (process.platform !== 'darwin') {
  25. app.quit();
  26. }
  27. });
  28. app.on('activate', () => {
  29. if (BrowserWindow.getAllWindows().length === 0) {
  30. createWindow();
  31. }
  32. });
  33. // ------------- 进程通信 -------------
  34. ipcMain.on('open-gazebo', (event, arg) => {
  35. console.log('Received open-gazebo event');
  36. exec('gazebo', (error, stdout, stderr) => {
  37. if (error) {
  38. console.error(`exec error: ${error}`);
  39. return;
  40. }
  41. console.log(`stdout: ${stdout}`);
  42. console.error(`stderr: ${stderr}`);
  43. });
  44. });
  45. ipcMain.on('open-rviz', (event, arg) => {
  46. console.log('Received open-rviz event');
  47. exec('rviz', (error, stdout, stderr) => {
  48. if (error) {
  49. console.error(`exec error: ${error}`);
  50. return;
  51. }
  52. console.log(`stdout: ${stdout}`);
  53. console.error(`stderr: ${stderr}`);
  54. });
  55. });
  56. ipcMain.on('close-gazebo', (event, arg) => {
  57. console.log('Received close-gazebo event');
  58. exec('pkill -f gzserver & pkill -f gzclient', (error, stdout, stderr) => {
  59. if (error) {
  60. console.error(`exec error: ${error}`);
  61. return;
  62. }
  63. console.log(`stdout: ${stdout}`);
  64. console.error(`stderr: ${stderr}`);
  65. });
  66. });
  67. ipcMain.on('close-rviz', (event, arg) => {
  68. console.log('Received close-rviz event');
  69. exec('pkill -f rviz', (error, stdout, stderr) => {
  70. if (error) {
  71. console.error(`exec error: ${error}`);
  72. return;
  73. }
  74. console.log(`stdout: ${stdout}`);
  75. console.error(`stderr: ${stderr}`);
  76. });
  77. });
  78. ipcMain.handle('dialog:open', async (event, options = {}) => {
  79. const result = await dialog.showOpenDialog(BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0], options);
  80. return result.canceled ? null : result.filePaths;
  81. });
  82. ipcMain.on('docker-import', (event, sudoPassword, filePath, tag) => {
  83. const command = 'echo "' + sudoPassword + '" | sudo -S docker import ' + filePath + ' pji_nav:' + tag
  84. console.log('导入算法镜像文件:', command);
  85. exec(command, (error, stdout, stderr) => {
  86. if (error) {
  87. console.error(`exec error: ${error}`);
  88. return;
  89. }
  90. console.log(`stdout: ${stdout}`);
  91. console.error(`stderr: ${stderr}`);
  92. });
  93. });