Parcourir la source

refactor: 仿真测试修改为同步逻辑

HeWang il y a 9 mois
Parent
commit
8eff17fe1c
7 fichiers modifiés avec 113 ajouts et 84 suppressions
  1. 25 46
      electron/main.js
  2. 9 2
      electron/preload.js
  3. 5 0
      src/main.ts
  4. 17 0
      src/utils/resetMessage.js
  5. 52 33
      src/views/AboutView.vue
  6. 2 2
      src/views/ReportView.vue
  7. 3 1
      tsconfig.json

+ 25 - 46
electron/main.js

@@ -5,6 +5,7 @@ import {fileURLToPath} from 'url';
 import fs from 'fs';
 import axios from 'axios';
 import {resolve} from "node:dns";
+import {execSync} from "node:child_process";
 
 
 const __filename = fileURLToPath(import.meta.url);
@@ -212,21 +213,33 @@ ipcMain.on('start-container', (event, {zip_file_path, image_name, container_name
     }
 });
 
-ipcMain.on('run-simulation', (event, {obstacle_flag, default_start_flag, default_end_flag, start_point, end_point}) => {
+ipcMain.handle('run-simulation', (event, {obstacle_flag, default_start_flag, default_end_flag, start_point, end_point}) => {
     const command = 'bash /home/cicv/work/pji_desktop/simulation/run_simulation.sh ' + obstacle_flag + ' ' + default_start_flag + ' ' + default_end_flag +
         ' ' + start_point + ' ' + end_point;
     console.log('command:', command);
-    exec(command, (error, stdout, stderr) => {
-        if (error) {
-            console.error(`exec error: ${error}`);
-            event.reply('run-simulation-response', { success: false, message: error.message });
-            return;
-        } else {
-            console.log(`stdout: ${stdout}`);
-            console.error(`stderr: ${stderr}`);
-            event.reply('run-simulation-response', { success: true, message: 'Run simulation successfully' });
-        }
-    });
+
+    // 异步
+    // exec(command, (error, stdout, stderr) => {
+    //     if (error) {
+    //         console.error(`exec error: ${error}`);
+    //         // event.reply('run-simulation-response', { success: false, message: error.message });
+    //         return { success: false, message: error.message }
+    //     } else {
+    //         console.log(`stdout: ${stdout}`);
+    //         console.error(`stderr: ${stderr}`);
+    //         return { success: true, message: 'Run simulation successfully' }
+    //     }
+    // });
+
+    // 同步
+    try {
+        const  output = execSync(command)
+        console.log("output", output.toString());
+        return { success: false, message: error.message }
+    } catch (error) {
+        console.error(`exec error: ${error}`);
+        return { success: true, message: 'Run simulation successfully' }
+    }
 });
 
 ipcMain.handle('download-file', async (event, { url, fileName, savePath, overwriteFlag }) => {
@@ -236,18 +249,6 @@ ipcMain.handle('download-file', async (event, { url, fileName, savePath, overwri
 
         const filePath = path.join(savePath, fileName);
 
-        // fs.access(filePath, fs.constants.F_OK, async (err) => {
-        //     if (err) {
-        //         console.log(`文件不存在: ${filePath}`);
-        //
-        //     } else {
-        //         console.log(`文件已存在: ${filePath}`);
-        //         return new Promise(
-        //             resolve({success: true, filePath})
-        //         )
-        //     }
-        // });
-
         // 查找文件是否存在
         const fileExists = fs.existsSync(filePath)
         if (fileExists && !overwriteFlag) { // 已存在且不覆盖
@@ -276,28 +277,6 @@ ipcMain.handle('download-file', async (event, { url, fileName, savePath, overwri
                 });
             });
         }
-
-        // // 写入文件
-        // const writer = fs.createWriteStream(filePath);
-        //
-        // return new Promise((resolve, reject) => {
-        //     response.data.pipe(writer);
-        //
-        //     let error = null;
-        //     writer.on('error', err => {
-        //         error = err;
-        //         writer.close();
-        //         reject(err);
-        //     });
-        //
-        //     writer.on('close', () => {
-        //         if (!error) {
-        //             resolve({ success: true, filePath }); // 返回成功信息
-        //         } else {
-        //             reject({ success: false, error: error.message }); // 返回错误信息
-        //         }
-        //     });
-        // });
     } catch (error) {
         // console.error('下载文件出错:', error);
         throw error;

+ 9 - 2
electron/preload.js

@@ -34,8 +34,15 @@ contextBridge.exposeInMainWorld('electronAPI', {
         ipcRenderer.send('docker-import', filePath, tag);
     },
     onDockerImportResponse: (callback) => ipcRenderer.on('docker-import-response', callback),
-    runSimulation: (obstacle_flag, default_start_flag, default_end_flag, start_point, end_point) => {
-        ipcRenderer.send('run-simulation', {obstacle_flag, default_start_flag, default_end_flag, start_point, end_point});
+    runSimulation: async (obstacle_flag, default_start_flag, default_end_flag, start_point, end_point) => {
+        // ipcRenderer.send('run-simulation', {obstacle_flag, default_start_flag, default_end_flag, start_point, end_point});
+        try {
+            const result = ipcRenderer.invoke('run-simulation', {obstacle_flag, default_start_flag, default_end_flag, start_point, end_point});
+            return result;
+        } catch (error) {
+            console.error('执行仿真测试出错:', error);
+            throw error;
+        }
     },
     onRunSimulationResponse: (callback) => ipcRenderer.on('run-simulation-response', callback),
     generateWorld: (rosbag_path) => {

+ 5 - 0
src/main.ts

@@ -3,6 +3,9 @@ import ElementPlus from 'element-plus'
 import 'element-plus/dist/index.css'
 import { createApp } from 'vue'
 import { createPinia } from 'pinia'
+import { message } from '@/utils/resetMessage.js'
+
+// const message = require('@/utils/resetMessage.js')
 
 import App from './App.vue' // 挂载主页
 import router from './router'
@@ -13,4 +16,6 @@ app.use(ElementPlus)
 app.use(createPinia())
 app.use(router)
 
+app.config.globalProperties.$msg = message;
+
 app.mount('#app')

+ 17 - 0
src/utils/resetMessage.js

@@ -0,0 +1,17 @@
+
+import { ElMessage } from 'element-plus' //引入message弹出框
+
+let messageDom = null
+const resetMessage = (options) => {
+    if (messageDom) messageDom.close() // 判断弹窗是否已存在,若存在则关闭
+    messageDom = ElMessage(options)
+}
+const typeArr = ['success', 'error', 'warning', 'info']
+typeArr.forEach(type => {
+    resetMessage[type] = options => {
+        if (typeof options === 'string') options = { message: options }
+        options.type = type
+        return resetMessage(options)
+    }
+})
+export const message = resetMessage

+ 52 - 33
src/views/AboutView.vue

@@ -1,5 +1,5 @@
 <template>
-  <el-form :model="form" label-width="auto" style="max-width: 600px">
+  <el-form :model="form" label-width="auto" style="max-width: 700px">
     <!--    <el-form-item label="发布终点     :">-->
     <!--      <el-button type="primary" @click="onSubmit">默认终点</el-button>-->
     <!--      <el-button type="primary" @click="onSubmit">自定义终点</el-button>-->
@@ -20,14 +20,19 @@
       <el-radio-group v-model="form.origin" @change="originChange">
         <el-radio :value="true">默认起点</el-radio>
         <el-radio :value="false">自定义起点</el-radio>
-        <span >自定义起点</span>
       </el-radio-group>
+      <span  v-show="form.origin==false" @click="startDialogVisible=true" style="margin-left: 20px; font-size: 14px; color: gray; cursor: pointer">
+        当前值:X: {{startForm.X}} Y: {{startForm.Y}} Z: {{startForm.Z}} R: {{startForm.R}} P: {{startForm.P}} H: {{startForm.H}}
+      </span>
     </el-form-item>
     <el-form-item label="终点设置     :" v-if="!form.isRandom">
       <el-radio-group v-model="form.destination" @change="destinationChange">
         <el-radio :value="true">默认终点</el-radio>
         <el-radio :value="false">自定义终点</el-radio>
       </el-radio-group>
+      <span  v-show="form.destination==false" @click="endDialogVisible=true" style="margin-left: 20px; font-size: 14px; color: gray; cursor: pointer">
+        当前值:X: {{endForm.X}} Y: {{endForm.Y}} Z: {{endForm.Z}} R: {{endForm.R}} P: {{endForm.P}} H: {{endForm.H}}
+      </span>
     </el-form-item>
     <el-form-item label="加载默认障碍物:">
       <el-radio-group v-model="form.resource">
@@ -49,22 +54,22 @@
         <template #footer>
           <div class="dialog-footer">
             <el-form :model="startForm" label-width="auto">
-              <el-form-item style="margin-bottom: 10px" label="位置 X :">
+              <el-form-item style="margin-bottom: 10px" label="位置 X :" required>
                 <el-input v-model="startForm.X" />
               </el-form-item>
-              <el-form-item style="margin-bottom: 10px" label="位置 Y :">
+              <el-form-item style="margin-bottom: 10px" label="位置 Y :" required>
                 <el-input v-model="startForm.Y" />
               </el-form-item>
               <el-form-item style="margin-bottom: 10px" label="位置 Z :">
-                <el-input v-model="startForm.Z" />
+                <el-input v-model="startForm.Z" disabled/>
               </el-form-item>
               <el-form-item style="margin-bottom: 10px" label="角度 R :">
-                <el-input v-model="startForm.R" />
+                <el-input v-model="startForm.R" disabled/>
               </el-form-item>
               <el-form-item style="margin-bottom: 10px" label="角度 P :">
-                <el-input v-model="startForm.P" />
+                <el-input v-model="startForm.P" disabled/>
               </el-form-item>
-              <el-form-item style="margin-bottom: 10px" label="角度 H :">
+              <el-form-item style="margin-bottom: 10px" label="角度 H :" required>
                 <el-input v-model="startForm.H" />
               </el-form-item>
             </el-form>
@@ -89,13 +94,13 @@
                 <el-input v-model="endForm.Y" />
               </el-form-item>
               <el-form-item style="margin-bottom: 10px" label="位置 Z :">
-                <el-input v-model="endForm.Z" />
+                <el-input v-model="endForm.Z" disabled/>
               </el-form-item>
               <el-form-item style="margin-bottom: 10px" label="角度 R :">
-                <el-input v-model="endForm.R" />
+                <el-input v-model="endForm.R" disabled/>
               </el-form-item>
               <el-form-item style="margin-bottom: 10px" label="角度 P :">
-                <el-input v-model="endForm.P" />
+                <el-input v-model="endForm.P" disabled/>
               </el-form-item>
               <el-form-item style="margin-bottom: 10px" label="角度 H :">
                 <el-input v-model="endForm.H" />
@@ -106,7 +111,8 @@
         </template>
       </el-dialog>
 
-      <el-button style="margin-left: 10px;" type="primary" @click="goToMain">更换设置并执行</el-button>
+<!--      <el-button style="margin-left: 10px;" type="primary" @click="goToMain">更换设置并执行</el-button>-->
+<!--      <el-button style="margin-left: 10px;" type="primary" @click="goToAlgorithmEval">算法评价</el-button>-->
     </el-form-item>
     <el-form-item label="            ">
       <el-button type="primary" @click="goToAlgorithmEval">算法评价</el-button>
@@ -124,10 +130,11 @@ import {reactive} from 'vue'
 import {ref} from 'vue'
 import {ElMessageBox} from 'element-plus'
 import {ElTable, ElLoading, ElMessage} from "element-plus";
-
+import {getCurrentInstance} from "vue";
 
 const startDialogVisible = ref(false)
 const endDialogVisible = ref(false)
+const {proxy} = getCurrentInstance();
 
 const handleClose = (done: () => void) => {
   done()
@@ -152,19 +159,19 @@ const form = reactive({
 const startForm = reactive({
   X: '',
   Y: '',
-  Z: '',
-  R: '',
-  P: '',
-  H: '',
+  Z: '0',
+  R: '0',
+  P: '0',
+  H: '0',
 })
 
 const endForm = reactive({
   X: '',
   Y: '',
-  Z: '',
-  R: '',
-  P: '',
-  H: '',
+  Z: '0',
+  R: '0',
+  P: '0',
+  H: '0',
 })
 
 const originChange = (value: string) => {
@@ -184,7 +191,7 @@ const onSubmit = () => {
 }
 const router = useRouter();
 
-const runSimulation = () => {
+const runSimulation = async () => {
   // 是否加载默认障碍物
   let obstacle_flag = form.resource
   let default_start_flag = form.origin
@@ -194,21 +201,33 @@ const runSimulation = () => {
 
   console.log("end_point=" + end_point)
   // 开启仿真测试
-  window.electronAPI.runSimulation(obstacle_flag, default_start_flag, default_end_flag, start_point, end_point)
+  const result = await window.electronAPI.runSimulation(obstacle_flag, default_start_flag, default_end_flag, start_point, end_point)
   // 监听脚本执行状态
-  window.electronAPI.onRunSimulationResponse( (event, result) => {
-    if (result.success) { // 脚本执行成功
-      console.log('Script execution completed successfully.')
-      ElMessage.success("仿真测试成功!");
-    } else { // 脚本执行过程中发生错误
-      console.error('Script execution failed.');
-      ElMessage.error("仿真测试错误!");
-    }
-  })
+  // window.electronAPI.onRunSimulationResponse( (event, result) => {
+  //   if (result.success) { // 脚本执行成功
+  //     console.log('Script execution completed successfully.')
+  //     // ElMessage.success("仿真测试成功!");
+  //     ElMessage.closeAll()
+  //     proxy.$message.success("仿真测试成功!");
+  //   } else { // 脚本执行过程中发生错误
+  //     console.error('Script execution failed.');
+  //     proxy.$message.error("仿真测试错误!");
+  //   }
+  // })
+  if (result.success) { // 脚本执行成功
+    console.log('Script execution completed successfully.')
+    // ElMessage.success("仿真测试成功!");
+    ElMessage.closeAll()
+    proxy.$message.success("仿真测试成功!");
+  } else { // 脚本执行过程中发生错误
+    console.error('Script execution failed.');
+    proxy.$message.error("仿真测试错误!");
+  }
 }
 
 const goToMain = () => {
-  router.push('/')
+  // router.push('/')
+  router.back()
 }
 
 const goToAlgorithmEval = () => {

+ 2 - 2
src/views/ReportView.vue

@@ -636,8 +636,8 @@ const startSimulation = async (row) => {
     loadingInstance.close()
     // 更新步骤条状态
     simulation_active.value = 1
-    // 延时
-    setTimeout(() => {}, 1500)
+    // // 延时
+    // setTimeout(() => {}, 500)
     // 开启loading
     loadingInstance = ElLoading.service({fullscreen: false, target: '.el-dialog'})
     // 执行脚本 - 数据准备,启动容器

+ 3 - 1
tsconfig.json

@@ -10,5 +10,7 @@
     {
       "path": "./tsconfig.vitest.json"
     }
-  ]
+  ],
+  "noImplicitAny": false,
+  "allowJs": true
 }