cvpilot-tool/apps/desktop/electron/ipcMainHandlers.ts
2024-09-03 15:28:30 +08:00

82 lines
2.2 KiB
TypeScript

import { dialog, ipcMain } from "electron";
import os from "os";
import {
findDcmFiles,
keyProp,
processFilesInBatches,
structureMetadata,
} from "./core/dicom";
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());
ipcMain.on("python-service", (_, data) => {
const { running } = data;
running ? pythonManager.startFlask() : pythonManager.stopFlask();
});
/**
* 显示选择文件夹的dialog
*/
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 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;