cvpilot-tool/apps/desktop/electron/ipcMainHandlers.ts

102 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-08-29 16:59:25 +08:00
import { dialog, ipcMain } from "electron";
import os from "os";
import {
findDcmFiles,
2024-09-02 14:18:06 +08:00
keyProp,
2024-08-29 16:59:25 +08:00
processFilesInBatches,
2024-09-02 14:18:06 +08:00
StructuredMetadata,
2024-08-29 16:59:25 +08:00
structureMetadata,
} from "./core/dicom";
import { EVENT_PARSE_DICOM } from "./ipcEvent";
import PythonManager from "./core/PythonManager";
2024-09-02 14:18:06 +08:00
import { db } from "./core/db";
2024-08-29 16:59:25 +08:00
/**
*
*/
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();
});
2024-08-30 16:58:44 +08:00
2024-09-03 14:10:36 +08:00
/**
* dialog
*/
2024-09-02 11:18:49 +08:00
ipcMain.on("import-dicom-dialog-visible", async (event) => {
2024-08-30 16:58:44 +08:00
const result = await dialog.showOpenDialog({
properties: ["openDirectory"],
});
if (result.canceled) return null;
const filePaths = result.filePaths[0];
2024-09-02 11:18:49 +08:00
event.reply("scan-start");
2024-09-03 14:10:36 +08:00
const scanTimeStart = Date.now();
2024-09-02 11:18:49 +08:00
const dcmPaths = await findDcmFiles(filePaths);
const batchSize = os.cpus().length * 1 || 10;
const items = await processFilesInBatches(dcmPaths, batchSize);
const structDicom = await structureMetadata(items, (progress) => {
2024-09-02 11:18:49 +08:00
event.reply("scan-progress", progress);
});
2024-09-02 14:18:06 +08:00
// 存数据库
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 })
2024-09-03 14:10:36 +08:00
: db.data.series.push({
...item,
createTime: changeTime,
updateTime: changeTime,
});
2024-09-02 14:18:06 +08:00
await db.write();
}
2024-09-03 14:10:36 +08:00
event.reply("scan-progress-done", {
structDicom,
scanDuration: Date.now() - scanTimeStart,
});
2024-08-30 16:58:44 +08:00
});
2024-09-02 14:18:06 +08:00
2024-09-03 14:10:36 +08:00
/**
* api
*/
2024-09-02 14:18:06 +08:00
ipcMain.on("db:series:select", async (event, data) => {
await db.read();
const seriesList = db.data.series;
event.reply("db:series:select:response", seriesList);
});
2024-08-29 16:59:25 +08:00
};
export default registerIpcMainHandlers;