wanshenmean
2026-03-19 c493779a8504fe1eb548c865ff268a7f7436ec01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import axios from 'axios'
import type {
  InstanceListItem,
  InstanceState,
  InstanceConfig,
  ProtocolTemplate,
  RobotClientStartRequest,
  RobotClientSendRequest,
  RobotClientStatusResponse
} from '../types'
 
const api = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
  headers: {
    'Content-Type': 'application/json'
  }
})
 
// 获取所有实例
export async function getAllInstances(): Promise<InstanceListItem[]> {
  const response = await api.get<InstanceListItem[]>('/SimulatorInstances/GetAll')
  return response.data
}
 
// 获取实例状态
export async function getInstance(id: string): Promise<InstanceState | null> {
  try {
    const response = await api.get<InstanceState>('/SimulatorInstances/GetInstance', {
      params: { id }
    })
    return response.data
  } catch (error) {
    if (axios.isAxiosError(error) && error.response?.status === 404) {
      return null
    }
    throw error
  }
}
 
// 获取实例配置
export async function getInstanceConfig(id: string): Promise<InstanceConfig | null> {
  try {
    const response = await api.get<InstanceConfig>('/SimulatorInstances/GetInstanceConfig', {
      params: { id }
    })
    return response.data
  } catch (error) {
    if (axios.isAxiosError(error) && error.response?.status === 404) {
      return null
    }
    throw error
  }
}
 
// 创建实例
export async function createInstance(config: InstanceConfig): Promise<InstanceState | null> {
  try {
    const response = await api.post<InstanceState>('/SimulatorInstances/Create', config)
    return response.data
  } catch (error) {
    console.error('创建实例失败:', error)
    return null
  }
}
 
// 更新实例
export async function updateInstance(id: string, config: InstanceConfig): Promise<InstanceState | null> {
  try {
    const response = await api.put<InstanceState>('/SimulatorInstances/Update', config, {
      params: { id }
    })
    return response.data
  } catch (error) {
    console.error('更新实例失败:', error)
    return null
  }
}
 
// 删除实例
export async function deleteInstance(id: string, deleteConfig: boolean = true): Promise<boolean> {
  try {
    await api.delete('/SimulatorInstances/Delete', {
      params: { id, deleteConfig }
    })
    return true
  } catch (error) {
    console.error('删除实例失败:', error)
    return false
  }
}
 
// 启动实例
export async function startInstance(id: string): Promise<InstanceState | null> {
  try {
    const response = await api.post<InstanceState>('/SimulatorInstances/start', null, {
      params: { id }
    })
    return response.data
  } catch (error) {
    console.error('启动实例失败:', error)
    return null
  }
}
 
// 停止实例
export async function stopInstance(id: string): Promise<InstanceState | null> {
  try {
    const response = await api.post<InstanceState>('/SimulatorInstances/stop', null, {
      params: { id }
    })
    return response.data
  } catch (error) {
    console.error('停止实例失败:', error)
    return null
  }
}
 
// 重启实例
export async function restartInstance(id: string): Promise<InstanceState | null> {
  try {
    const response = await api.post<InstanceState>('/SimulatorInstances/restart', null, {
      params: { id }
    })
    return response.data
  } catch (error) {
    console.error('重启实例失败:', error)
    return null
  }
}
 
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