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

83 lines
2.2 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,
structureMetadata,
} from "./core/dicom";
2024-09-03 16:51:27 +08:00
// 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,
2024-09-03 16:51:27 +08:00
// pythonManager?: PythonManager
2024-08-29 16:59:25 +08:00
) => {
if (!mainWindow) return;
ipcMain.removeAllListeners();
/**
*
*/
ipcMain.on("ipc-loaded", () => mainWindow.show());
2024-09-03 16:51:27 +08:00
// ipcMain.on("python-service", (_, 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-09-03 16:51:27 +08:00
console.log(1111);
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-03 15:28:30 +08:00
ipcMain.on("db:series:select", async (event) => {
2024-09-02 14:18:06 +08:00
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;