cvpilot-tool/apps/desktop/electron/ipcEvent/common/index.ts
2024-10-11 16:26:38 +08:00

63 lines
2.0 KiB
TypeScript

import { app, BrowserWindow, 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";
import { getMetadata, getTotalScanLength } from "../../core/pacs";
import { InferStructuralEnum } from "../../core/alg.type";
import { readFileSync, statSync } from "fs";
export const registerCommonHandler = () => {
ipcMain.handle("output:open", async () => {
await db.read();
const optPath = db.data.setting.outputPath;
const resolvedPath = path.resolve(optPath);
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);
}
});
ipcMain.handle("measure:excel", async (_event, SeriesInstanceUID) => {
const excelPath = path.join(
app.getPath("userData"),
"output",
SeriesInstanceUID
);
try {
const stats = await stat(excelPath);
if (stats.isDirectory()) shell.openPath(excelPath);
} catch (error) {
console.log(error);
}
});
ipcMain.handle("measure:json", async (_event, SeriesInstanceUID) => {
// 读取测量json
const physicalLength = await getTotalScanLength(SeriesInstanceUID);
const module =
physicalLength && physicalLength < 200
? InferStructuralEnum.AORTA
: InferStructuralEnum.PERI;
// 获取metadata
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 {
return null;
}
});
};