RobotStatusCard.vue
7.6 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<script lang="ts" setup>
/**
* 机器人状态卡片组件
* @author zzy
*/
import { computed, ref } from 'vue'
import { useToast } from 'vuestic-ui'
import type { RobotStatus } from '../../../../services/robotStatusWs'
import robotsApi from '../../../../services/robots'
const props = defineProps<{
robot: RobotStatus
}>()
const { init: notify } = useToast()
const isLoading = ref(false)
// 状态文本
const statusText = computed(() => {
switch (props.robot.status) {
case 1:
return '空闲'
case 2:
return '忙碌'
case 3:
return '异常'
default:
return '未知'
}
})
// 状态颜色
const statusColor = computed(() => {
switch (props.robot.status) {
case 1:
return 'success'
case 2:
return 'warning'
case 3:
return 'danger'
default:
return 'secondary'
}
})
// 在线状态
const onlineText = computed(() => {
switch (props.robot.online) {
case 1:
return '在线'
case 2:
return '离线'
case 3:
return '连接中断'
default:
return '未知'
}
})
// 电量颜色
const batteryColor = computed(() => {
const level = props.robot.batteryLevel ?? 0
if (level <= 20) return 'danger'
if (level <= 50) return 'warning'
return 'success'
})
// 格式化坐标
const formatCoord = (val: number | null) => (val != null ? val.toFixed(2) : '-')
// 解析错误信息
const errorList = computed(() => {
return props.robot.errors ?? []
})
// 根据错误级别获取颜色
const getErrorColor = (err: any) => {
const level = (err.errorType || err.ErrorType || '').toUpperCase()
return level === 'ERROR' ? 'danger' : 'warning'
}
/**
* 复位机器人
* @author zzy
*/
const handleReset = async () => {
if (isLoading.value) return
isLoading.value = true
try {
console.log(props.robot)
const res = await robotsApi.reset(props.robot.robotId)
if (res?.Success || res?.success) {
notify({ message: '复位成功', color: 'success' })
} else {
notify({ message: res?.Message || res?.message || '复位失败', color: 'danger' })
}
} catch (err: any) {
notify({ message: err?.message || '复位失败', color: 'danger' })
} finally {
isLoading.value = false
}
}
/**
* 取消任务
* @author zzy
*/
const handleCancelTask = async () => {
if (isLoading.value) return
isLoading.value = true
try {
const res = await robotsApi.cancelTask(props.robot.robotId)
if (res?.Success || res?.success) {
notify({ message: '取消任务成功', color: 'success' })
} else {
notify({ message: res?.Message || res?.message || '取消任务失败', color: 'danger' })
}
} catch (err: any) {
notify({ message: err?.message || '取消任务失败', color: 'danger' })
} finally {
isLoading.value = false
}
}
</script>
<template>
<VaCard class="robot-card" :class="{ offline: robot.online !== 1 }">
<VaCardContent class="robot-card-content">
<!-- 头部:名称和状态 -->
<div class="robot-header">
<span class="robot-name">{{ robot.robotName }}-{{ robot.robotCode }}</span>
<VaBadge :color="statusColor" :text="statusText" />
</div>
<!-- 在线状态 -->
<div class="robot-row">
<VaIcon :name="robot.online === 1 ? 'wifi' : 'wifi_off'" :color="robot.online === 1 ? 'success' : 'danger'" size="small" />
<span class="robot-label">{{ onlineText }}</span>
</div>
<!-- 电量 -->
<div class="robot-row">
<template v-if="robot.charging">
<div class="battery-icon-stack">
<VaIcon name="battery_std" color="success" size="small" />
<VaIcon name="bolt" color="warning" size="small" class="bolt-overlay" />
</div>
<div class="battery-charging">
<div class="battery-fill"></div>
</div>
</template>
<template v-else>
<VaIcon name="battery_std" color="success" size="small" />
<VaProgressBar :model-value="robot.batteryLevel ?? 0" :color="batteryColor" size="small" class="battery-bar" />
</template>
<span class="battery-text">{{ robot.batteryLevel ?? 0 }}%</span>
</div>
<!-- 坐标 -->
<div class="robot-row coords">
<span>X: {{ formatCoord(robot.x) }}</span>
<span>Y: {{ formatCoord(robot.y) }}</span>
</div>
<!-- 行驶状态 -->
<div class="robot-row" v-if="robot.driving">
<VaIcon name="directions_car" color="primary" size="small" />
<span class="robot-label">行驶中</span>
</div>
<!-- 错误信息 -->
<div class="robot-errors" v-if="errorList.length > 0">
<div v-for="(err, idx) in errorList.slice(0, 2)" :key="idx" class="error-item" :class="getErrorColor(err)">
<VaIcon name="error" :color="getErrorColor(err)" size="small" />
<span>{{ err.errorDescription || '未知错误' }}</span>
</div>
</div>
<!-- 操作按钮 -->
<div class="robot-actions">
<VaButton size="small" color="warning" :loading="isLoading" @click="handleReset" title="复位" class="action-btn">
<VaIcon name="restart_alt" size="small" />
复位
</VaButton>
<VaButton size="small" color="secondary" :loading="isLoading" @click="handleCancelTask" title="取消任务" class="action-btn">
<VaIcon name="cancel" size="small" />
取消任务
</VaButton>
</div>
</VaCardContent>
</VaCard>
</template>
<style scoped>
.robot-card {
margin-bottom: 10px;
transition: all 0.2s;
border: 1px solid var(--va-background-border);
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
}
.robot-card:hover {
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.12);
}
.robot-card.offline {
opacity: 0.6;
background: var(--va-background-secondary);
}
.robot-card-content {
padding: 12px !important;
}
.robot-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.robot-name {
font-weight: 600;
font-size: 14px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 120px;
}
.robot-row {
display: flex;
align-items: center;
gap: 6px;
margin-bottom: 6px;
font-size: 12px;
}
.robot-row.coords {
justify-content: space-between;
color: var(--va-text-secondary);
}
.robot-label {
color: var(--va-text-secondary);
}
.battery-bar {
flex: 1;
max-width: 80px;
}
.battery-text {
min-width: 32px;
text-align: right;
font-size: 11px;
}
.robot-errors {
margin-top: 6px;
padding-top: 6px;
border-top: 1px solid var(--va-background-border);
}
.error-item {
display: flex;
align-items: flex-start;
gap: 4px;
font-size: 11px;
margin-bottom: 2px;
}
.error-item.danger {
color: var(--va-danger);
}
.error-item.warning {
color: var(--va-warning);
}
.error-item span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.robot-actions {
display: flex;
gap: 6px;
margin-top: 8px;
padding-top: 8px;
border-top: 1px solid var(--va-background-border);
}
.action-btn {
flex: 1;
}
.battery-icon-stack {
position: relative;
display: inline-flex;
}
.bolt-overlay {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale(0.7);
}
.battery-charging {
flex: 1;
max-width: 80px;
height: 4px;
background: #e0e0e0;
border-radius: 2px;
overflow: hidden;
position: relative;
}
.battery-fill {
height: 100%;
background: linear-gradient(90deg, #4caf50, #8bc34a);
border-radius: 2px;
animation: battery-charge 1.5s ease-in-out infinite;
}
@keyframes battery-charge {
0% { width: 0%; }
100% { width: 100%; }
}
</style>