| | |
| | | import axios from 'axios' |
| | | import axios from 'axios' |
| | | import type { |
| | | InstanceListItem, |
| | | InstanceState, |
| | | InstanceConfig |
| | | InstanceConfig, |
| | | ProtocolTemplate, |
| | | RobotClientStartRequest, |
| | | RobotClientSendRequest, |
| | | RobotClientStatusResponse |
| | | } from '../types' |
| | | |
| | | const api = axios.create({ |
| | |
| | | return response.data |
| | | } |
| | | |
| | | // 获取指定实例状态 |
| | | // 获取实例状态 |
| | | export async function getInstance(id: string): Promise<InstanceState | null> { |
| | | try { |
| | | const response = await api.get<InstanceState>('/SimulatorInstances/GetInstance', { |
| | |
| | | // 创建实例 |
| | | export async function createInstance(config: InstanceConfig): Promise<InstanceState | null> { |
| | | try { |
| | | const response = await api.post<InstanceState>('/SimulatorInstances/Create', config ) |
| | | const response = await api.post<InstanceState>('/SimulatorInstances/Create', config) |
| | | return response.data |
| | | } catch (error) { |
| | | console.error('创建实例失败:', error) |
| | |
| | | } |
| | | } |
| | | |
| | | export async function getProtocolTemplates(): Promise<ProtocolTemplate[]> { |
| | | const response = await api.get<ProtocolTemplate[]>('/ProtocolTemplates') |
| | | return response.data |
| | | } |
| | | |
| | | export async function getProtocolTemplate(id: string): Promise<ProtocolTemplate | null> { |
| | | try { |
| | | const response = await api.get<ProtocolTemplate>(`/ProtocolTemplates/${id}`) |
| | | return response.data |
| | | } catch (error) { |
| | | if (axios.isAxiosError(error) && error.response?.status === 404) { |
| | | return null |
| | | } |
| | | throw error |
| | | } |
| | | } |
| | | |
| | | export async function createProtocolTemplate(template: ProtocolTemplate): Promise<ProtocolTemplate> { |
| | | const response = await api.post<ProtocolTemplate>('/ProtocolTemplates', template) |
| | | return response.data |
| | | } |
| | | |
| | | export async function updateProtocolTemplate(id: string, template: ProtocolTemplate): Promise<ProtocolTemplate> { |
| | | const response = await api.put<ProtocolTemplate>(`/ProtocolTemplates/${id}`, template) |
| | | return response.data |
| | | } |
| | | |
| | | export async function deleteProtocolTemplate(id: string): Promise<void> { |
| | | await api.delete(`/ProtocolTemplates/${id}`) |
| | | } |
| | | |
| | | export async function readMemory(id: string): Promise<Record<string, string>> { |
| | | const response = await api.get<Record<string, string>>('/Memory/ReadMemory', { |
| | | params: { id } |
| | | }) |
| | | return response.data |
| | | } |
| | | |
| | | export async function writeMemory(id: string, data: Record<string, string>): Promise<boolean> { |
| | | try { |
| | | await api.post('/Memory/WriteMemory', data, { |
| | | params: { id } |
| | | }) |
| | | return true |
| | | } catch (error) { |
| | | console.error('写入内存失败:', error) |
| | | return false |
| | | } |
| | | } |
| | | |
| | | // 获取机械手服务端运行状态(包含多实例和接收消息日志) |
| | | export async function getRobotClientStatus(): Promise<RobotClientStatusResponse> { |
| | | const response = await api.get<RobotClientStatusResponse>('/RobotClients/status') |
| | | return response.data |
| | | } |
| | | |
| | | // 启动一个机械手服务端实例 |
| | | export async function startRobotClients(request: RobotClientStartRequest): Promise<RobotClientStatusResponse> { |
| | | const response = await api.post<RobotClientStatusResponse>('/RobotClients/start', request) |
| | | return response.data |
| | | } |
| | | |
| | | // 停止机械手服务端,serverId 为空时停止全部 |
| | | export async function stopRobotClients(serverId?: string): Promise<void> { |
| | | await api.post('/RobotClients/stop', null, { |
| | | params: { serverId } |
| | | }) |
| | | } |
| | | |
| | | // 发送机械手消息(按服务端实例广播或单发) |
| | | export async function sendRobotClientMessage(request: RobotClientSendRequest): Promise<void> { |
| | | await api.post('/RobotClients/send', request) |
| | | } |
| | | |
| | | // 清空指定服务端实例的接收消息日志 |
| | | export async function clearRobotClientReceivedMessages(serverId: string): Promise<void> { |
| | | await api.post('/RobotClients/clear-received', null, { |
| | | params: { serverId } |
| | | }) |
| | | } |
| | | |
| | | export default api |