فهرست منبع

feat(deviceStatus): 添加清空日志功能支持重新检测时丢弃历史日志

- 新增 clearLogs 方法清空前端日志列表、未读计数及主进程缓存日志
- 引入 logsRequestId 机制,防止清空后仍接收到的历史日志响应覆盖状态
- 重新检测时先清空上一轮日志,再启动检测流程避免旧日志干扰
- 设备状态弹窗新增"清空日志"按钮,无日志时禁用
- startProgress 支持 refreshLogs 选项控制是否拉取日志
- IPC 通道新增 clearErrorLogs 事件定义
- 主进程 controller 和 socket 工具层实现 clearMcuErrorLogs 清空逻辑
panqiuyao 12 ساعت پیش
والد
کامیت
ae8cc3052e

+ 7 - 0
electron/controller/socket.js

@@ -52,6 +52,13 @@ class SocketController extends Controller {
   }
 
   /**
+   * 清空 MCU 错误日志列表
+   */
+  clearMcuErrorLogs() {
+    return pySocket.clearMcuErrorLogs();
+  }
+
+  /**
    * 获取 MCU 错误日志未读数
    */
   getMcuErrorUnreadCount() {

+ 15 - 0
electron/utils/socket.js

@@ -428,6 +428,21 @@ const pySocket = function () {
     return [...mcuErrorLogs];
   };
 
+  // 清空 MCU 错误日志(重新检测时丢掉上一轮上报)
+  this.clearMcuErrorLogs = function() {
+    mcuErrorLogs.length = 0;
+    mcuErrorUnreadCount = 0;
+    try {
+      const win = CoreWindow.getMainWindow();
+      if (win && !win.isDestroyed()) {
+        win.webContents.send('controller.mcu.errorLogUnread', { count: 0 });
+      }
+    } catch (e) {
+      Log.error(e);
+    }
+    return true;
+  };
+
   // 获取未读数
   this.getMcuErrorUnreadCount = function() {
     return mcuErrorUnreadCount;

+ 6 - 3
frontend/src/components/check/index.vue

@@ -179,17 +179,20 @@ function syncMotorListen() {
   }
 }
 
-function startProgress() {
+function startProgress(options = {}) {
   checkInfoStore.set_isCheckStatus(true);
   checkLoading.value = true;
   checkSuccess.value = false;
   deviceStatusStore.resetRunStatus()
   checkInfoStore.reCheckAction()
-  deviceStatusStore.fetchLogs()
+  if (options.refreshLogs !== false) {
+    deviceStatusStore.fetchLogs()
+  }
 }
 
 function reCheck() {
-  startProgress()
+  deviceStatusStore.clearLogs()
+  startProgress({ refreshLogs: false })
 }
 
 watchEffect(async ()=>{

+ 6 - 0
frontend/src/components/device-status-dialog/index.vue

@@ -87,6 +87,7 @@
     </div>
 
     <template #footer>
+      <el-button :disabled="deviceStatusStore.logs.length === 0" @click="handleClearLogs">清空日志</el-button>
       <el-button @click="handleClose">关闭</el-button>
     </template>
   </el-dialog>
@@ -154,6 +155,11 @@ async function refreshStatus() {
   await deviceStatusStore.fetchRunStatus();
 }
 
+function handleClearLogs() {
+  searchKeyword.value = '';
+  deviceStatusStore.clearLogs();
+}
+
 function handleClose() {
   searchKeyword.value = '';
   deviceStatusStore.closeDialog();

+ 18 - 0
frontend/src/stores/modules/deviceStatus.ts

@@ -55,6 +55,8 @@ const useDeviceStatusStore = defineStore('deviceStatus', () => {
   let isListenerInitialized = false;
   let isRunStatusListening = false;
   let runStatusPollTimer: ReturnType<typeof setInterval> | null = null;
+  // 清空日志后递增,丢弃还在路上的历史日志响应
+  let logsRequestId = 0;
 
   function applyRunStatus(result: any): boolean {
     const dataState = result?.data?.data_state;
@@ -142,14 +144,29 @@ const useDeviceStatusStore = defineStore('deviceStatus', () => {
     });
   }
 
+  // 清空当前消息列表,并清掉主进程里缓存的历史日志
+  function clearLogs() {
+    logsRequestId += 1;
+    logs.value = [];
+    unreadCount.value = 0;
+    errorUnreadCount.value = 0;
+    if (!clientStore.isClient) return;
+    clientStore.ipc.send(icpList.mcu.clearErrorLogs);
+  }
+
   // 获取日志列表
   async function fetchLogs() {
     if (!clientStore.isClient) return [];
 
+    const requestId = logsRequestId;
     return new Promise((resolve) => {
       clientStore.ipc.removeAllListeners(icpList.mcu.getErrorLogs);
       clientStore.ipc.send(icpList.mcu.getErrorLogs);
       clientStore.ipc.once(icpList.mcu.getErrorLogs, (event, result) => {
+        if (requestId !== logsRequestId) {
+          resolve(result);
+          return;
+        }
         if (Array.isArray(result)) {
           logs.value = result;
         }
@@ -242,6 +259,7 @@ const useDeviceStatusStore = defineStore('deviceStatus', () => {
     statusLoading,
     initListener,
     fetchLogs,
+    clearLogs,
     fetchRunStatus,
     resetRunStatus,
     startRunStatusListen,

+ 1 - 0
frontend/src/utils/ipc.ts

@@ -19,6 +19,7 @@ const icpList = {
         errorLog: 'controller.socket.message_print_mcu_error_data',  // MCU 错误日志消息
         errorLogUnread: 'controller.mcu.errorLogUnread',              // 未读状态变更通知
         getErrorLogs: 'controller.mcu.getErrorLogs',                  // 获取日志列表
+        clearErrorLogs: 'controller.socket.clearMcuErrorLogs',        // 清空日志列表
         clearErrorUnread: 'controller.mcu.clearErrorUnread',          // 清除未读状态
         setErrorLogMax: 'controller.mcu.setErrorLogMax',              // 设置最大日志条数
     },