main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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'); // 开发环境
  20. win.loadURL('http://36.110.106.156:81'); // 生产环境
  21. console.log('Window created and URL loaded');
  22. }
  23. app.whenReady().then(createWindow);
  24. app.on('window-all-closed', () => {
  25. if (process.platform !== 'darwin') {
  26. app.quit();
  27. }
  28. });
  29. app.on('activate', () => {
  30. if (BrowserWindow.getAllWindows().length === 0) {
  31. createWindow();
  32. }
  33. });
  34. // ------------- 进程通信 -------------
  35. ipcMain.on('open-gazebo', (event, arg) => {
  36. console.log('Received open-gazebo event');
  37. exec('gazebo', (error, stdout, stderr) => {
  38. if (error) {
  39. console.error(`exec error: ${error}`);
  40. return;
  41. }
  42. console.log(`stdout: ${stdout}`);
  43. console.error(`stderr: ${stderr}`);
  44. });
  45. });
  46. ipcMain.on('open-rviz', (event, arg) => {
  47. console.log('Received open-rviz event');
  48. exec('rviz', (error, stdout, stderr) => {
  49. if (error) {
  50. console.error(`exec error: ${error}`);
  51. return;
  52. }
  53. console.log(`stdout: ${stdout}`);
  54. console.error(`stderr: ${stderr}`);
  55. });
  56. });
  57. ipcMain.on('close-gazebo', (event, arg) => {
  58. console.log('Received close-gazebo event');
  59. exec('pkill -f gzserver & pkill -f gzclient', (error, stdout, stderr) => {
  60. if (error) {
  61. console.error(`exec error: ${error}`);
  62. return;
  63. }
  64. console.log(`stdout: ${stdout}`);
  65. console.error(`stderr: ${stderr}`);
  66. });
  67. });
  68. ipcMain.on('close-rviz', (event, arg) => {
  69. console.log('Received close-rviz event');
  70. exec('pkill -f rviz', (error, stdout, stderr) => {
  71. if (error) {
  72. console.error(`exec error: ${error}`);
  73. return;
  74. }
  75. console.log(`stdout: ${stdout}`);
  76. console.error(`stderr: ${stderr}`);
  77. });
  78. });
  79. ipcMain.handle('dialog:open', async (event, options = {}) => {
  80. const result = await dialog.showOpenDialog(BrowserWindow.getFocusedWindow() || BrowserWindow.getAllWindows()[0], options);
  81. return result.canceled ? null : result.filePaths;
  82. });
  83. ipcMain.on('docker-import', (event, sudoPassword, filePath, tag) => {
  84. const command = 'echo "' + sudoPassword + '" | sudo -S docker import ' + filePath + ' pji_nav:' + tag
  85. console.log('导入算法镜像文件:', command);
  86. exec(command, (error, stdout, stderr) => {
  87. if (error) {
  88. console.error(`exec error: ${error}`);
  89. return;
  90. }
  91. console.log(`stdout: ${stdout}`);
  92. console.error(`stderr: ${stderr}`);
  93. });
  94. });
  95. ipcMain.on('generate-world', (event, rosbag_path) => {
  96. // const command = 'sh /home/cicv/work/pji_desktop/run_map2gazabo.sh'
  97. //
  98. // exec(command, (error, stdout, stderr) => {
  99. // if (error) {
  100. // console.error(`exec error: ${error}`);
  101. // return;
  102. // }
  103. // console.log(`stdout: ${stdout}`);
  104. // console.error(`stderr: ${stderr}`);
  105. // });
  106. // 在 Electron 应用启动后执行本地脚本
  107. const serviceScript = 'sh /home/cicv/work/pji_desktop/run_map2gazabo.sh';
  108. // 使用 spawn 启动脚本
  109. const serviceProcess = spawn(serviceScript, [], { detached: true });
  110. // 设置为后台进程
  111. serviceProcess.unref();
  112. // 监听输出
  113. serviceProcess.stdout.on('data', (data) => {
  114. console.log(`Service output: ${data}`);
  115. });
  116. serviceProcess.stderr.on('data', (data) => {
  117. console.error(`Service error: ${data}`);
  118. });
  119. });