cvpilot-tool/apps/desktop/electron/ipcMainHandlers.ts
2024-09-02 11:18:49 +08:00

68 lines
2.0 KiB
TypeScript

import { dialog, ipcMain } from "electron";
import os from "os";
import {
findDcmFiles,
processFilesInBatches,
structureMetadata,
} from "./core/dicom";
import { EVENT_PARSE_DICOM } from "./ipcEvent";
import PythonManager from "./core/PythonManager";
/**
* 渲染进程和主进程的事件调度
*/
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);
});
event.reply("scan-progress-done", structDicom);
});
};
export default registerIpcMainHandlers;