import { app, dialog, ipcMain } from "electron"; import os from "os"; import { findDcmFiles, keyProp, processFilesInBatches, structureMetadata, } from "./core/dicom"; import { db } from "./core/db"; import axios from "axios"; import path from "node:path"; /** * 渲染进程和主进程的事件调度 */ const registerIpcMainHandlers = (mainWindow: Electron.BrowserWindow | null) => { if (!mainWindow) return; ipcMain.removeAllListeners(); /** * 等待渲染完成再显示窗口 */ ipcMain.on("ipc-loaded", () => mainWindow.show()); ipcMain.on("ai:task", (_, data) => { const { selectDicoms } = data; const promises = selectDicoms.reduce((promiseChain, dicom) => { return promiseChain.then(() => { const { filePaths, PatientName, SeriesInstanceUID } = dicom; const img_path = path.dirname(filePaths[0]); const save_path = path.join( app.getPath("userData"), "output", PatientName, SeriesInstanceUID ); const params = { img_path, save_path, pu: "GPU", module: "root", turbo: true, }; const startTime = Date.now(); // 记录请求开始时间 console.log( `Request for ${PatientName} started at ${new Date( startTime ).toISOString()}` ); console.log(params); return axios .post("http://127.0.0.1:5000/root", { ...params }) .then((response) => { const endTime = Date.now(); // 记录请求结束时间 const duration = (endTime - startTime) / 1000; // 计算耗时,单位为秒 console.log( `Response for ${PatientName} received at ${new Date( endTime ).toISOString()} (Duration: ${duration} seconds)` ); console.log(response.data); }) .catch((error) => { const endTime = Date.now(); // 即使失败也记录结束时间 const duration = (endTime - startTime) / 1000; console.error( `Error for ${PatientName} at ${new Date( endTime ).toISOString()} (Duration: ${duration} seconds)` ); console.error(error); }); }); }, Promise.resolve()); // 从 resolve() 开始,依次链接每个 Promise const overallStartTime = Date.now(); // 记录总的开始时间 promises.then(() => { const overallEndTime = Date.now(); // 记录总的结束时间 const overallDuration = (overallEndTime - overallStartTime) / 1000; // 总耗时 console.log(`All dicoms processed in ${overallDuration} seconds.`); }); // const img_path = // const { // img_path, // save_path, // pu = "GPU", // module = "root", // turbo = true, // } = data; }); /** * 显示选择文件夹的dialog */ ipcMain.on("import-dicom-dialog-visible", async (event) => { console.log(1111); const result = await dialog.showOpenDialog({ properties: ["openDirectory"], }); if (result.canceled) return null; const filePaths = result.filePaths[0]; event.reply("scan-start"); const scanTimeStart = Date.now(); const dcmPaths = await findDcmFiles(filePaths); const batchSize = os.cpus().length * 1 || 10; const items = await processFilesInBatches(dcmPaths, batchSize); const structDicom = await structureMetadata(items, (progress) => { event.reply("scan-progress", progress); }); // 存数据库 const changeTime = Date.now(); for (const item of structDicom) { const existSeries = db.data.series.find( (i) => i[keyProp] === item[keyProp] ); existSeries ? Object.assign(existSeries, item, { updateTime: changeTime }) : db.data.series.push({ ...item, createTime: changeTime, updateTime: changeTime, }); await db.write(); } event.reply("scan-progress-done", { structDicom, scanDuration: Date.now() - scanTimeStart, }); }); /** * api 获取 列表的中数据 */ ipcMain.on("db:series:select", async (event) => { await db.read(); const seriesList = db.data.series; event.reply("db:series:select:response", seriesList); }); }; export default registerIpcMainHandlers;