123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import {app, BrowserWindow, ipcMain, dialog} from 'electron';
- import {exec} 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 command = 'sh /home/cicv/work/pji_desktop/run_map2gazabo.sh'
- //
- // exec(command, (error, stdout, stderr) => {
- // if (error) {
- // console.error(`exec error: ${error}`);
- // return;
- // }
- // console.log(`stdout: ${stdout}`);
- // console.error(`stderr: ${stderr}`);
- // });
- // 在 Electron 应用启动后执行本地脚本
- const serviceScript = 'sh /home/cicv/work/pji_desktop/run_map2gazabo.sh';
- // 使用 spawn 启动脚本
- const serviceProcess = spawn(serviceScript, [], { detached: true });
- // 设置为后台进程
- serviceProcess.unref();
- // 监听输出
- serviceProcess.stdout.on('data', (data) => {
- console.log(`Service output: ${data}`);
- });
- serviceProcess.stderr.on('data', (data) => {
- console.error(`Service error: ${data}`);
- });
- });
|