64 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-04-30 00:17:09 +08:00
import http from '@core/http';
2025-05-11 18:46:59 +08:00
import type { RobotDetail, RobotGroup, RobotInfo } from './type';
2025-04-30 00:17:09 +08:00
const enum API {
= '/robot/getAll',
2025-05-05 01:06:09 +08:00
= '/robot/register',
= '/robot/seizeByIds',
2025-05-11 18:46:59 +08:00
= 'robot/syncByGroupId',
2025-04-30 00:17:09 +08:00
}
2025-05-05 01:06:09 +08:00
export async function getAllRobots(): Promise<Array<RobotInfo>> {
2025-04-30 00:17:09 +08:00
type D = RobotInfo[];
2025-05-05 23:21:31 +08:00
try {
const data = await http.post<D>(API.);
return data ?? [];
} catch (error) {
console.debug(error);
return [];
}
2025-04-30 00:17:09 +08:00
}
2025-05-05 01:06:09 +08:00
export async function registerRobot(robot: Omit<RobotDetail, 'id'>): Promise<RobotInfo | null> {
type B = Omit<RobotDetail, 'id'>;
type D = RobotInfo;
2025-05-05 23:21:31 +08:00
try {
const body = robot;
const data = await http.post<D, B>(API., body);
return data ?? null;
} catch (error) {
console.debug(error);
return null;
}
2025-05-05 01:06:09 +08:00
}
2025-05-05 23:21:31 +08:00
export async function seizeRobotByIds(ids: Array<RobotInfo['id']>): Promise<Array<RobotInfo['id']>> {
if (!ids.length) return [];
2025-05-05 01:06:09 +08:00
type B = { ids: string[] };
2025-05-05 23:21:31 +08:00
type D = string[];
2025-05-05 01:06:09 +08:00
try {
const body = { ids };
2025-05-05 23:21:31 +08:00
const data = await http.post<D, B>(API., body);
return data ?? [];
2025-05-05 01:06:09 +08:00
} catch (error) {
console.debug(error);
2025-05-05 23:21:31 +08:00
return [];
2025-05-05 01:06:09 +08:00
}
}
2025-05-11 18:46:59 +08:00
export async function syncGroupRobotsById(id: RobotGroup['id'], sid: RobotGroup['sid']): Promise<boolean> {
if (!id || !sid) return false;
type B = { id: string; sid: string };
type D = void;
try {
const body = { id, sid };
await http.post<D, B>(API., body);
return true;
} catch (error) {
console.debug(error);
return false;
}
}