cvpilot-tool/apps/desktop/electron/ipcEvent/common/index.ts

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-10-11 21:17:38 +08:00
import { app, ipcMain, shell } from "electron";
import { mkdir, stat } from "fs/promises";
import { db } from "../../core/db";
import log from "electron-log";
import path from "node:path";
2024-10-11 16:26:38 +08:00
import { getMetadata, getTotalScanLength } from "../../core/pacs";
import { InferStructuralEnum } from "../../core/alg.type";
import { readFileSync, statSync } from "fs";
export const registerCommonHandler = () => {
2024-10-13 22:14:03 +08:00
ipcMain.handle("output:open", async (_event, SeriesInstanceUID) => {
await db.read();
const optPath = db.data.setting.outputPath;
2024-10-13 22:14:03 +08:00
const resolvedPath = path.join(optPath, SeriesInstanceUID);
try {
// 检查路径是否存在
const stats = await stat(resolvedPath);
if (stats.isDirectory()) shell.openPath(resolvedPath);
} catch (error) {
log.error(error);
await mkdir(resolvedPath, { recursive: true });
shell.openPath(resolvedPath);
}
});
2024-10-11 16:26:38 +08:00
ipcMain.handle("measure:excel", async (_event, SeriesInstanceUID) => {
const excelPath = path.join(
app.getPath("userData"),
"output",
SeriesInstanceUID
);
try {
2024-10-11 21:17:38 +08:00
if (statSync(excelPath).isDirectory()) {
shell.openPath(excelPath);
return { code: 1 };
}
2024-10-11 16:26:38 +08:00
} catch (error) {
2024-10-11 21:17:38 +08:00
return { code: 0, msg: "请先进行AI推理" };
2024-10-11 16:26:38 +08:00
}
});
ipcMain.handle("measure:json", async (_event, SeriesInstanceUID) => {
// 读取测量json
const physicalLength = await getTotalScanLength(SeriesInstanceUID);
const module =
physicalLength && physicalLength < 200
? InferStructuralEnum.AORTA
: InferStructuralEnum.PERI;
// 获取metadata
2024-10-11 21:17:38 +08:00
if (module !== InferStructuralEnum.AORTA)
return { code: 0, msg: "仅开放根部测量报告" };
2024-10-11 16:26:38 +08:00
const tags = await getMetadata(SeriesInstanceUID);
const rootPath = path.join(
app.getPath("userData"),
"output",
SeriesInstanceUID
);
const measurementPath = path.join(rootPath, module, "measurement.json");
if (statSync(measurementPath).isFile()) {
const measurement = readFileSync(measurementPath, "utf-8");
return { measurement: JSON.parse(measurement), tags };
} else {
2024-10-11 21:17:38 +08:00
return { code: 0, msg: "请先进行AI推理" };
2024-10-11 16:26:38 +08:00
}
});
};