2024-09-20 15:55:17 +08:00
|
|
|
import log from "electron-log";
|
|
|
|
import { spawn } from "node:child_process";
|
|
|
|
import path from "node:path";
|
|
|
|
import { InferReq } from "./alg.type";
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
|
|
export const ALGServerRoot = "http://127.0.0.1:5000/root";
|
|
|
|
|
|
|
|
export const getEntryPath = () => {
|
|
|
|
// 区分操作系统
|
|
|
|
return path.join(process.env.VITE_PUBLIC!, "main.exe");
|
|
|
|
};
|
|
|
|
|
|
|
|
export const startALGServer = () => {
|
|
|
|
const entryPath = getEntryPath();
|
|
|
|
const child_process = spawn(entryPath);
|
|
|
|
child_process.on("message", (data) => console.log(data));
|
|
|
|
child_process.stdout.on("data", (data) => log.info(data.toString()));
|
|
|
|
child_process.stderr.on("data", (data) => log.error(data.toString()));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 执行推理任务
|
|
|
|
*/
|
|
|
|
export const executeInferTask = (
|
|
|
|
task: InferReq,
|
|
|
|
onData: (data: string) => void
|
|
|
|
) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post(ALGServerRoot, task, {
|
|
|
|
responseType: "stream",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
response.data.on("data", (chunk: Buffer) => {
|
|
|
|
const data = chunk.toString();
|
|
|
|
onData(data); // 实时处理数据
|
|
|
|
});
|
|
|
|
|
|
|
|
response.data.on("end", () => {
|
|
|
|
resolve(task); // 数据流结束
|
|
|
|
});
|
|
|
|
|
|
|
|
response.data.on("error", (err: Error) => {
|
2024-09-20 16:48:32 +08:00
|
|
|
console.log("error", err);
|
2024-09-20 15:55:17 +08:00
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
})
|
2024-09-20 16:48:32 +08:00
|
|
|
.catch((error) => {
|
|
|
|
console.log("axios error", error);
|
|
|
|
reject(error);
|
|
|
|
});
|
2024-09-20 15:55:17 +08:00
|
|
|
});
|
|
|
|
};
|