import { dialog, ipcMain } from "electron"; import os from "os"; import { findDcmFiles, keyProp, processFilesInBatches, StructuredMetadata, structureMetadata, } from "./core/dicom"; import { EVENT_PARSE_DICOM } from "./ipcEvent"; import PythonManager from "./core/PythonManager"; import { db } from "./core/db"; /** * 渲染进程和主进程的事件调度 */ const registerIpcMainHandlers = ( mainWindow: Electron.BrowserWindow | null, pythonManager: PythonManager ) => { if (!mainWindow) return; ipcMain.removeAllListeners(); /** * 等待渲染完成再显示窗口 */ ipcMain.on("ipc-loaded", () => mainWindow.show()); /** * 解析dicoM */ ipcMain.on(EVENT_PARSE_DICOM, async (event, file: string) => { const dirDialog = await dialog.showOpenDialog(mainWindow, { properties: ["openDirectory"], }); if (dirDialog.filePaths.length > 0) { const filePaths = await findDcmFiles(dirDialog.filePaths[0]); const batchSize = os.cpus().length * 1 || 10; console.time("分批处理"); const unraw = await processFilesInBatches(filePaths, batchSize); console.timeEnd("分批处理"); const result = structureMetadata(unraw); event.reply(EVENT_PARSE_DICOM + ":RES", result); } }); ipcMain.on("python-service", (event, data) => { const { running } = data; running ? pythonManager.startFlask() : pythonManager.stopFlask(); }); ipcMain.on("import-dicom-dialog-visible", async (event) => { const result = await dialog.showOpenDialog({ properties: ["openDirectory"], }); if (result.canceled) return null; const filePaths = result.filePaths[0]; event.reply("scan-start"); 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 }); await db.write(); } event.reply("scan-progress-done", structDicom); }); ipcMain.on("db:series:select", async (event, data) => { await db.read(); const seriesList = db.data.series; event.reply("db:series:select:response", seriesList); }); }; export default registerIpcMainHandlers;